Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

Using attributes to document alterations

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
27 May 20033 min read 38.5K   377   11  
Using attributes to solve subjects linked to the daily work of software maintenance.
using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Xml.Serialization;
using System.Globalization;
using Palla.DocGen.Library;

namespace Palla.DocGen.App
{
	/// <summary>
	/// User process for "frmDocGen"
	/// </summary>
	public class UserProcessfrmDocGen
	{
		private DateTime initialDate = DateTime.Now;
		private DateTime finalDate = DateTime.Now;
		private string targetPath = String.Empty;
		private string[] assemblieList = null;

		protected AssemblyInformationCollection ic = new AssemblyInformationCollection();
		
		public event NoticeFaultsEventHandler NoticeFaults = null;
		public event NoticeStartProcessEventHandler NoticeStartProcess = null;
		public event EventHandler NoticeProgress = null;
		public event EventHandler NoticeEndProcess = null;

		#region Constructors

		public UserProcessfrmDocGen()
		{
		}

		#endregion

		#region Virtuals

		protected virtual void OnNoticeFaults(ArrayList messages)
		{
			if(this.NoticeFaults != null)
				this.NoticeFaults(this,new NoticeFaultsEventArgs(messages));
		}

		protected virtual void OnNoticeStartProcess(int limit)
		{
			if(this.NoticeStartProcess != null)
				this.NoticeStartProcess(this,new NoticeStartProcessEventArgs(limit));
		}

		protected virtual void OnNoticeProgress()
		{
			if(this.NoticeProgress != null)
				this.NoticeProgress(this, EventArgs.Empty);
		}

		protected virtual void OnNoticeEndProcess()
		{
			if(this.NoticeEndProcess != null)
				this.NoticeEndProcess(this, EventArgs.Empty);
		}

		protected virtual bool ValidateInstance()
		{
			ArrayList returnErrors = new ArrayList();
			
			if (!Directory.Exists(Path.GetDirectoryName(this.targetPath)))
				returnErrors.Add(String.Format("Path '{0}' no exists.", this.targetPath));

			if (this.initialDate > this.finalDate)
				returnErrors.Add("Selected period is not valid.");
				
			if (this.assemblieList==null || this.assemblieList.Length==0)
				returnErrors.Add("There are not selected assemblies.");	
				
			if (returnErrors.Count>0)
			{
				this.OnNoticeFaults(returnErrors);
				return false;
			}
			else 
				return true;
		}

		protected virtual void LoadAssembliesInformations()
		{
			try
			{
				foreach(string assembly in this.assemblieList)
				{
					Assembly asm = Assembly.LoadFrom(assembly);
					if (asm!=null)
					{
						object[] attributeList = asm.GetCustomAttributes(typeof(UpdateDocumentationAttribute), false);

						foreach(Attribute attrib in attributeList)
						{
							if (attrib!=null)
							{
								DateTime dtAtr = Convert.ToDateTime(((UpdateDocumentationAttribute)attrib).DateChange, CultureInfo.CurrentCulture);

								if (attrib != null &&
									(dtAtr >= this.initialDate && dtAtr <= this.finalDate))
								{
									this.ic.Add(new AssemblyInformation(Path.GetFileName(assembly),
										((UpdateDocumentationAttribute)attrib).Version,
										dtAtr.ToString("d", CultureInfo.CurrentCulture),
										((UpdateDocumentationAttribute)attrib).Priority,
										((UpdateDocumentationAttribute)attrib).ChangesSummary,
										((UpdateDocumentationAttribute)attrib).ChangesSummaryCommonUser)); 
								}
							}
						}
					}

					this.OnNoticeProgress();
				}
			}
			catch
			{
				throw;
			}
		}

		protected virtual void SaveXmlFile()
		{
			try
			{
				XmlSerializer s = new XmlSerializer(typeof(AssemblyInformationCollection));
				TextWriter writer = new StreamWriter(this.XmlFileName);
				s.Serialize(writer, this.ic);
				writer.Close();
			}
			catch
			{
				throw;
			}
		}
	
		protected virtual void DoTransform()
		{
			try
			{
				System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
				xslt.Load(this.XslFileName);

				System.Xml.XPath.XPathDocument xDoc = new System.Xml.XPath.XPathDocument(this.XmlFileName);
				XmlWriter writer = new XmlTextWriter(this.targetPath,System.Text.Encoding.UTF8);
				xslt.Transform(xDoc, null, writer);			
				writer.Close();
			}
			catch
			{
				throw;
			}
		}

		#endregion

		#region Methods

		public virtual bool GenerateIt(string[] assemblieList, string targetPath, DateTime initialDate, DateTime finalDate)
		{
			try
			{
				this.assemblieList = assemblieList;
				this.initialDate = initialDate;
				this.finalDate = finalDate;
				this.targetPath = targetPath;

				if (this.ValidateInstance() &&
					DocGenHelpers.SaveImageFile(targetPath) &&
					DocGenHelpers.SaveXslFile(targetPath))
				{
					//Notify start process
					this.OnNoticeStartProcess(this.assemblieList.Length);
					
					//Loading the informations
					this.LoadAssembliesInformations();

					//Save xml file
					this.SaveXmlFile();

					//Transformation
					this.DoTransform();

					//Notify end process
					this.OnNoticeEndProcess();
					
					return true;
				}
				else
					return false;
			}
			catch
			{
				throw;
			}
		}

		#endregion

		#region Internal Properties

		private string XmlFileName
		{
			get{return Path.GetDirectoryName(this.targetPath) + @"\doc.xml";}
		}

		private string XslFileName
		{
			get{return Path.GetDirectoryName(this.targetPath) + @"\docstyle.xsl";}
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
To develop software is the best work of the world!

Comments and Discussions