/*
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)
{
}
}
}