initial commit
[libtpproto-csharp.git] / codegenerator / CodeGen.cs
blobfd460c616d315c1789844ef0435038b8019b9503
1 /*
2 * License: GPLv3+
3 * Created at ${TIME} ${DATE}
4 *
5 * Author: Marcel Hauf
6 */
8 using System;
9 using System.IO;
10 using System.Xml;
11 using System.Xml.XPath;
12 using System.Xml.Serialization;
13 using System.Collections;
14 using System.CodeDom;
15 using System.CodeDom.Compiler;
16 using Microsoft.CSharp;
17 using System.Reflection;
19 namespace CodeGenerator
21 /// <summary>
22 /// Loads the xml file.
23 /// </summary>
24 public class CodeGenerator
26 /// <summary>
27 /// Namespace name of the will be generated code.
28 /// </summary>
29 public string Namespace = "LibTpProto.Proto";
31 /// <summary>
32 /// An arraylist of strings which will be included as using statements.
33 /// Eg. string: System
34 /// </summary>
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();
49 /// <summary>
50 /// This will generate the code.
51 /// </summary>
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
57 // Setup variables
58 this.outPutPath = outputPath;
59 this.xmlFile = xmlFile;
61 // Setup in/out put
62 if(File.Exists(xmlFile))
64 reader = new XmlTextReader (xmlFile);
66 else
68 throw(new Exception("Could not found xml file."));
71 // TODO: Choose the language of the code gen
72 CSharpCodeGen();
75 private void CSharpCodeGen()
77 //Setup code generators for csharp code
78 CSharpCodeProvider provider = new CSharpCodeProvider();
79 CodeGeneratorOptions options = new CodeGeneratorOptions();
81 while (reader.Read())
83 switch (reader.NodeType)
85 case XmlNodeType.Element: //This node is a element
86 switch (reader.Depth)
88 case 0: //protocol
89 string version = reader.GetAttribute("version");
90 if(version != null)
92 outPutPath += "/" + version;
93 Directory.CreateDirectory(outPutPath);
95 break;
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());
106 break;
107 case 2:
108 break;
109 default:
110 break;
112 break;
113 case XmlNodeType.Text: //Display the text from each element
114 break;
115 case XmlNodeType.EndElement: //Display the end of a element
116 break;
119 reader.Close();
122 private void GenerateHeader(CodeDomProvider provider
123 , CodeGeneratorOptions options
124 , XmlReader reader)
126 sourceWriter = new StreamWriter(outPutPath + "/Header.cs");
127 // --- Starts generating code ---
129 //Declare Using Statements
130 UsingStatements.Add("System");
132 //Setup compile unit
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));
144 while(reader.Read())
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");
154 break;
155 case XmlNodeType.Text: //Display the text from each element
156 break;
157 case XmlNodeType.EndElement: //Display the end of a element
158 break;
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
170 , XmlReader reader)
172 sourceWriter = new StreamWriter(outPutPath + "/OrderParams.cs");
173 // --- Starts generating code ---
175 //Declare Using Statements
176 UsingStatements.Add("System");
178 //Setup compile unit
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 = "";
191 while(reader.Read())
193 switch (reader.NodeType)
195 case XmlNodeType.Element:
196 switch (reader.Depth)
198 case 0:
199 break;
200 case 1: //parameter and name and description
201 switch (reader.Name)
203 //Does not work
204 case "longname":
205 comments = reader.Value;
206 break;
207 case "description":
208 comments += reader.Value;
209 break;
210 // Works
211 case "parameter":
212 GenerateParameter(reader.ReadSubtree(), ctd, reader.GetAttribute("name"));
213 break;
214 default:
215 break;
218 break;
219 default:
220 break;
222 break;
223 case XmlNodeType.Text: //Display the text from each element
224 break;
225 case XmlNodeType.EndElement: //Display the end of a element
226 break;
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);
242 while(reader.Read())
244 switch (reader.NodeType)
246 case XmlNodeType.Element:
247 switch (reader.Depth)
249 case 0:
250 break;
251 case 1:
252 break;
253 default:
254 break;
256 break;
257 default:
258 break;
261 ctd.Members.Add(pclass);