Click here to Skip to main content
12,696,092 members (30,157 online)
Click here to Skip to main content

Stats

50.1K views
891 downloads
61 bookmarked
Posted

Static Code Analysis

, 15 Mar 2010 CPOL
A static code analyzer building method call networks + sample applications.
Generator
bin
.noclean
Arebis.CodeAnalysis.Static.dll
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.Common.dll
CGen.exe
Content.cst
Content.cst.gen
COperation.cst
COperation.cst.gen
files
GeneratorMain.cst
GeneratorMain.cst.gen
Index.cst
Index.cst.gen
IndexTop.cst
IndexTop.cst.gen
SOperation.cst
SOperation.cst.gen
UIOperation.cst
UIOperation.cst.gen
Output
SampleApp
LocalTestRun.testrunconfig
ToDoApplication
Infrastructure
Properties
Service References
ToDoContract
Properties
ToDoSample.vsmdi
ToDoService
Entities.edmx
Infrastructure
Properties
ToDoSample.Service.csproj.user
ToDoSampleDb.mdf
ToDoSampleDb_log.ldf
ToDoUnitTests
Properties
Testing
ToDoSample.UnitTests.csproj.user
UnitTests
StaticCodeAnalyzer
Arebis.CodeAnalysis.Static
Arebis.CodeAnalysis.Static.csproj.user
CodeModel.cd
Processors
Rules
Properties
Arebis.Common
Arebis
Collections
Generic
Reflection
Properties
System
Collections
Generic
Net2
Threading
Net2
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml;
using System.IO;
using System.Xml.Xsl;

namespace CodeModelGeneration
{
	public static class DocManager
	{
		private static Dictionary<Assembly, XmlDocument> docStore = new Dictionary<Assembly, XmlDocument>();

		private static XslCompiledTransform transformer = null;
		private static XsltArgumentList transformerArgs = null;

		public static void RegisterTransformer(string xsltfile)
		{
			XslCompiledTransform transformer = new XslCompiledTransform();
			using (XmlTextReader reader = new System.Xml.XmlTextReader(xsltfile))
			{
				reader.ProhibitDtd = false;
				transformer.Load(reader);
			}
			DocManager.transformer = transformer;
			DocManager.transformerArgs = new XsltArgumentList();
		}

		public static string GetDocumentationFor(MethodBase method)
		{
			// Get documentation document:
			XmlDocument doc = GetXmlDocument(method.DeclaringType.Assembly);
			if (doc == null) return null;

			// Find summary:
			string dockey = GetDocumentationKeyFor(method);
			XmlNode summary = doc.SelectSingleNode(String.Format("//doc/members/member[@name='{0}']", dockey));
			if (summary != null)
				return summary.InnerXml;
			else
				return null;
		}

		private static XmlDocument GetXmlDocument(Assembly asm)
		{
			if (!docStore.ContainsKey(asm))
			{
				XmlDocument doc = null;

				string filename = new FileInfo(new Uri(asm.CodeBase, true).AbsolutePath).FullName;
				filename = filename.Replace(new FileInfo(filename).Extension, ".xml");

                if (File.Exists(filename))
				{
					// Load document:
					doc = new XmlDocument();
					doc.Load(filename);

					// Transform if transformer was registered:
					if (DocManager.transformer != null)
					{
						using (Stream stream = new MemoryStream())
						{
							XmlWriter output = new XmlTextWriter(stream, Encoding.UTF8);
							transformer.Transform(doc, transformerArgs, output);
							output.Flush();
							stream.Seek(0, SeekOrigin.Begin);
							doc = new XmlDocument();
							doc.Load(stream);
						}
					}
				}

				docStore[asm] = doc;
			}

			return docStore[asm];
		}

		public static string GetDocumentationKeyFor(Type type)
		{
			StringBuilder sb = new StringBuilder();
			AppendTypeName(sb, type, true);
			return sb.ToString();
		}

		public static string GetDocumentationKeyFor(MethodBase method)
		{
			StringBuilder sb = new StringBuilder();
			AppendMethodName(sb, method, true);
			return sb.ToString();
		}

		private static void AppendTypeName(StringBuilder sb, Type type, bool writePrefix)
		{
			if (writePrefix) sb.Append("T:");

			if (!String.IsNullOrEmpty(type.Namespace))
			{
				sb.Append(type.Namespace);
				sb.Append(".");
			}

			if (type.IsGenericType)
			{
				int paramnumindex = type.Name.IndexOf('`');
				sb.Append(type.Name.Substring(0, paramnumindex));
				sb.Append("{");
				string sep = String.Empty;
				foreach (Type typeParam in type.GetGenericArguments())
				{
					sb.Append(sep);
					AppendTypeName(sb, typeParam, false);
					sep = ",";
				}
				sb.Append("}");
			}
			else
			{
				sb.Append(type.Name);
			}
		}

		private static void AppendMethodName(StringBuilder sb, MethodBase method, bool writePrefix)
		{
			if (writePrefix) sb.Append("M:");

			AppendTypeName(sb, method.DeclaringType, false);
			sb.Append(".");

			sb.Append(method.Name.Replace(".", "#"));
			sb.Append("(");

			string sep = String.Empty;
			foreach(ParameterInfo param in method.GetParameters())
			{
				sb.Append(sep);
				AppendTypeName(sb, param.ParameterType, false);
				sep = ",";
			}
			sb.Append(")");
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect RealDolmen
Belgium Belgium
Rudi is a Software Architect at RealDolmen.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid