3 * Created at ${TIME} ${DATE}
11 using System
.Xml
.XPath
;
12 using System
.Xml
.Serialization
;
13 using System
.Collections
;
15 using System
.CodeDom
.Compiler
;
16 using Microsoft
.CSharp
;
17 using System
.Reflection
;
19 namespace CodeGenerator
22 /// Loads the xml file.
24 public class CodeGenerator
27 /// Namespace name of the will be generated code.
29 public string Namespace
= "LibTpProto.Proto";
32 /// An arraylist of strings which will be included as using statements.
33 /// Eg. string: System
35 public ArrayList UsingStatements
= new ArrayList();
37 private static TypeHandler typeHandler
= new TypeHandler();
39 private string outPutPath
= ".";
41 private string xmlFile
= "./protocol";
43 private XmlTextReader reader
;
45 private TextWriter sourceWriter
;
47 private ArrayList classList
= new ArrayList();
50 /// This will generate the code.
52 /// <param name="xmlFile">Path to the xml file.</param>
53 /// <param name="outputPath">Out put path.</param>
54 public CodeGenerator(string xmlFile
, string outputPath
)
56 // TODO: Add choose which language should be used
58 this.outPutPath
= outputPath
;
59 this.xmlFile
= xmlFile
;
62 if(File
.Exists(xmlFile
))
64 reader
= new XmlTextReader (xmlFile
);
68 throw(new Exception("Could not found xml file."));
71 // TODO: Choose the language of the code gen
75 private void CSharpCodeGen()
77 //Setup code generators for csharp code
78 CSharpCodeProvider provider
= new CSharpCodeProvider();
79 CodeGeneratorOptions options
= new CodeGeneratorOptions();
83 switch (reader
.NodeType
)
85 case XmlNodeType
.Element
: //This node is a element
89 string version
= reader
.GetAttribute("version");
92 outPutPath
+= "/" + version
;
93 Directory
.CreateDirectory(outPutPath
);
96 case 1: //parameterset and packet
97 string name
= reader
.GetAttribute("name");
98 if(name
== "OrderParams")
100 GenerateParaSet(provider
, options
, reader
.ReadSubtree());
102 else if ( name
== "Header")
104 GenerateHeader(provider
, options
, reader
.ReadSubtree());
113 case XmlNodeType
.Text
: //Display the text from each element
115 case XmlNodeType
.EndElement
: //Display the end of a element
122 private void GenerateHeader(CodeDomProvider provider
123 , CodeGeneratorOptions options
126 sourceWriter
= new StreamWriter(outPutPath
+ "/Header.cs");
127 // --- Starts generating code ---
129 //Declare Using Statements
130 UsingStatements
.Add("System");
133 CodeCompileUnit ccu
= new CodeCompileUnit();
135 //Create Class Namespaces
136 CodeNamespace codeNamespace
= new CodeNamespace(Namespace
);
138 //Create Class Using Statements
139 foreach(string s
in UsingStatements
)
141 codeNamespace
.Imports
.Add(new CodeNamespaceImport(s
));
146 switch (reader
.NodeType
)
148 case XmlNodeType
.Element
:
149 if(reader
.Name
== "structure")
151 Console
.WriteLine("Debug: structure will be created.");
152 typeHandler
.AddClass(codeNamespace
, reader
.ReadSubtree(), "Header");
155 case XmlNodeType
.Text
: //Display the text from each element
157 case XmlNodeType
.EndElement
: //Display the end of a element
161 ccu
.Namespaces
.Add(codeNamespace
);
162 provider
.GenerateCodeFromCompileUnit(ccu
, sourceWriter
, options
);
163 sourceWriter
.Close();
167 // TODO: Recode everthing below
168 private void GenerateParaSet(CodeDomProvider provider
169 , CodeGeneratorOptions options
172 sourceWriter
= new StreamWriter(outPutPath
+ "/OrderParams.cs");
173 // --- Starts generating code ---
175 //Declare Using Statements
176 UsingStatements
.Add("System");
179 CodeCompileUnit ccu
= new CodeCompileUnit();
181 //Create Class Namespaces
182 CodeNamespace codeNamespace
= new CodeNamespace(Namespace
);
184 //Create Class Using Statements
185 foreach(string s
in UsingStatements
)
187 codeNamespace
.Imports
.Add(new CodeNamespaceImport(s
));
189 CodeTypeDeclaration ctd
= new CodeTypeDeclaration("OrderParams");
190 string comments
= "";
193 switch (reader
.NodeType
)
195 case XmlNodeType
.Element
:
196 switch (reader
.Depth
)
200 case 1: //parameter and name and description
205 comments
= reader
.Value
;
208 comments
+= reader
.Value
;
212 GenerateParameter(reader
.ReadSubtree(), ctd
, reader
.GetAttribute("name"));
223 case XmlNodeType
.Text
: //Display the text from each element
225 case XmlNodeType
.EndElement
: //Display the end of a element
229 Console
.WriteLine(comments
);
230 ctd
.Comments
.Add(new CodeCommentStatement(comments
));
231 codeNamespace
.Types
.Add(ctd
);
233 ccu
.Namespaces
.Add(codeNamespace
);
234 provider
.GenerateCodeFromCompileUnit(ccu
, sourceWriter
, options
);
235 sourceWriter
.WriteLine();
236 sourceWriter
.Close();
239 private void GenerateParameter(XmlReader reader
, CodeTypeDeclaration ctd
, string name
)
241 CodeTypeDeclaration pclass
= new CodeTypeDeclaration(name
);
244 switch (reader
.NodeType
)
246 case XmlNodeType
.Element
:
247 switch (reader
.Depth
)
261 ctd
.Members
.Add(pclass
);