using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Xml; using com.itac.oib.traceability.contracts.data; namespace oib5._1.GUI { internal class MessageFactory { private Dictionary typeMap = new Dictionary(); public MessageFactory() { typeMap.Add("TraceabilityData", typeof(TraceabilityData)); typeMap.Add("comp", typeof(ComponentType)); } internal void createMessage(string text) { // read the xml and try to create a message accordingly to the text... XmlReader xmlReader = XmlReader.Create(new StringReader(text)); // read root Tag while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: // The root node is an element. string rootType = xmlReader.Name; Type t = null; if (typeMap.ContainsKey(rootType)) { t = typeMap[rootType]; } if (t != null) { // abbildung von Namen auf Felder, alles in lowercase... Dictionary propertyMap = new Dictionary(); PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { propertyMap.Add(propertyInfo.Name.ToLower(), propertyInfo); } // Read this type... // create an instance Object instance = Activator.CreateInstance(t); populateInstance(instance, t, propertyMap, xmlReader); } break; // case XmlNodeType.EndElement: //Display the end of the element. // Console.Write(""); // break; } } } private void populateInstance(object instance, Type t, Dictionary propertyMap, XmlReader xmlReader) { while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: // The root node is an element. string fieldName = xmlReader.Name.ToLower(); //find the corresponding field if (propertyMap.ContainsKey(fieldName)) { // einfache werte direkt setzen PropertyInfo thisProp = propertyMap[fieldName]; switch (thisProp.PropertyType.Name.ToLower()) { case "string": setStringValue(instance, thisProp, xmlReader); break; case "datetime": setDateTimeValue(instance, thisProp, xmlReader); break; case "Boolean": case "bool": setBoolValue(instance, thisProp, xmlReader); break; case "int": case "int16": case "int32": case "int64": case "uint": case "uint16": case "uint32": case "uint64": setIntValue(instance, thisProp, xmlReader); break; case "double": setDoubleValue(instance, thisProp, xmlReader); break; default: if (thisProp.PropertyType.Namespace.StartsWith("com.itac.")) { // complex type..., arrays, lists, dictionary // if (thisProp.PropertyType.IsArray) { Console.WriteLine("array type " + thisProp.PropertyType.Name); } else if (thisProp.PropertyType.IsGenericType) { Console.WriteLine("generic type " + thisProp.PropertyType.Name); } else { // type direkt... Console.WriteLine("direct type " + thisProp.PropertyType.Name); } } break; } } break; case XmlNodeType.EndElement: //Display the end of the element. break; } } } private void setDoubleValue(object instance, PropertyInfo thisProp, XmlReader xmlReader) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { double simpleValue = double.Parse(xmlReader.Value); thisProp.SetValue(instance, simpleValue); } } private void setBoolValue(object instance, PropertyInfo thisProp, XmlReader xmlReader) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { bool simpleValue = bool.Parse(xmlReader.Value); thisProp.SetValue(instance, simpleValue); } } private void setIntValue(object instance, PropertyInfo thisProp, XmlReader xmlReader) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { int simpleValue = int.Parse(xmlReader.Value); thisProp.SetValue(instance, simpleValue); } } private void setDateTimeValue(object instance, PropertyInfo thisProp, XmlReader xmlReader) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { DateTime simpleValue = DateTime.Parse(xmlReader.Value); thisProp.SetValue(instance, simpleValue); } } private void setStringValue(object instance, PropertyInfo thisProp, XmlReader xmlReader) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { string simpleValue = xmlReader.Value; thisProp.SetValue(instance, simpleValue); } } } }