Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / Windows Forms

Traceract

Rate me:
Please Sign up or sign in to vote.
3.69/5 (9 votes)
3 Sep 20059 min read 98.8K   1.6K   37  
A prototype debug tracer with an added dimension.
/*
Copyright (c) 2005, Marc Clifton
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
  of conditions and the following disclaimer. 

* Redistributions in binary form must reproduce the above copyright notice, this 
  list of conditions and the following disclaimer in the documentation and/or other
  materials provided with the distribution. 
 
* Neither the name of MyXaml nor the names of its contributors may be
  used to endorse or promote products derived from this software without specific
  prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;

using Microsoft.CSharp;

using Vts.UnitTest;

using Clifton.Tools.Strings;

namespace Clifton.Tools.Xml.UnitTests
{
	[TestFixture]
	[ProcessTest]
	public class CodeGenUnitTests
	{
		[Test, Sequence(1)]
		public void Namespaces()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MycroXaml Name=\"Test\"\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns:wf=\"System.Windows.Forms, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\">\r\n";
			xml+="    <wf:Form Name=\"AppMainForm\"/>";
			xml+="</MycroXaml>\r\n";

			XmlDocument doc=new XmlDocument();
			doc.LoadXml(xml);
			CodeGen cg=new CodeGen(doc, "Test", "Test", "MyClass");
		}
	}
}


//xml+="  <ObjectGraphTestClass>\r\n";
//xml+="    <Items>\r\n";
//xml+="      <Item Text='a'/>\r\n";
//xml+="      <Item Text='b'/>\r\n";
//xml+="      <Item Text='c'/>\r\n";
//xml+="    </Items>\r\n";
//xml+="  </ObjectGraphTestClass>\r\n";
//



namespace Clifton.Tools.Xml
{
	/// <summary>
	/// Convert an xml document to code.
	/// </summary>
	public class CodeGen
	{
		protected CSharpCodeProvider provider;
		protected ICodeGenerator gen;
		protected CodeTypeDeclaration ctd;
		protected CodeConstructor constructor;

		protected StringBuilder sb;
		protected StringWriter sw;
		protected TextWriter tw;

		protected MycroParser parser;

		public CodeGen(XmlDocument doc, string objName, string ns, string className)
		{
			InitializeCodeDom();
			InitializeWriter();

			CodeCompileUnit ccu=new CodeCompileUnit();
			CodeNamespace cns=new CodeNamespace(ns);
			ccu.Namespaces.Add(cns);

			parser=new MycroParser();
			parser.Load(doc, objName, null);

			Hashtable nsMap=parser.NamespaceMap;
			foreach(DictionaryEntry entry in nsMap)
			{
				string localName=(string)entry.Key;
				string val=(string)entry.Value;
				if ( (localName=="ref") || (localName=="def")	)
				{
					continue;
				}

				cns.Imports.Add(new CodeNamespaceImport(StringHelpers.LeftOf(val, ',')));
			}

			ctd=new CodeTypeDeclaration(className);
			cns.Types.Add(ctd);

			constructor=new CodeConstructor();
			constructor.Attributes=MemberAttributes.Public;
			ctd.Members.Add(constructor);

			parser.InstantiateClass+=new InstantiateClassDlgt(OnInstantiateClass);
			parser.Process();

//			CodeEntryPointMethod Start = new CodeEntryPointMethod();
//			// Create a new method invocation expression.
//			CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( 
//				// Call the System.Console.WriteLine method.
//				new CodeTypeReferenceExpression("System.Console"), "WriteLine", 
//				// Pass a primitive string parameter to the WriteLine method
//				new CodePrimitiveExpression("Hello World!") );
//			// Add the new method code statement.
//			Start.Statements.Add(new CodeExpressionStatement(cs1));
//
//			// Add the code entry point method to the type's members collection
//			ctd.Members.Add( Start );


			CodeGeneratorOptions options=new CodeGeneratorOptions();
			options.BracingStyle="C";
			gen.GenerateCodeFromCompileUnit(ccu, tw, options);
			tw.Close();
			Debug.WriteLine(sb.ToString());

		}

		protected void InitializeCodeDom()
		{
			provider=new CSharpCodeProvider();
			gen=provider.CreateGenerator();
		}

		protected void InitializeWriter()
		{
			sb=new StringBuilder();
			sw=new StringWriter(sb);
			tw=new IndentedTextWriter(sw);
		}

		private void OnInstantiateClass(object sender, ClassEventArgs cea)
		{
			
		}
	}
}

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
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions