public void Generate(XmlTextReader reader, TextWriter writer, GenericGenerator generator) { ProtocolData result = new ProtocolData(); Parse(reader, result); generator.Write(result, writer); }
/// <summary> /// Generates the C# code for the given protocol data. /// </summary> /// <param name="data">Protocol data to generate code from.</param> /// <param name="w">The stream to write the code to.</param> /// <remarks>This method is non-reentrant (i.e. not thread safe)</remarks> public override void Write(ProtocolData data, TextWriter w) { mProtocolData = data; mWriter = w; WriteHeader(); WriteEnums(); WriteClasses(); WriteSummary(); WriteFooter(); }
private static void UpdateEnumFields(ProtocolData data, FieldData field) { foreach (string s in data.Enumerations.Keys) { if (field.Description.IndexOf(s) != -1 && field.Type != FieldDataType.FLOAT32) { field.IsEnum = true; field.EnumType = s; return; } } }
private EnumData GetEnumDataForName(ProtocolData data, string name) { EnumData result; if (!data.Enumerations.TryGetValue(name, out result)) { result = new EnumData(); result.Name = name; } return(result); }
private void ProcessInclude(string includeFileName, ProtocolData result) { string basePath = Path.GetDirectoryName(mSourceFileName); string targetFileName = (Path.IsPathRooted(includeFileName)) ? includeFileName : Path.Combine(basePath, includeFileName); using (XmlTextReader reader = new XmlTextReader(targetFileName)) { Parse(reader, result); } }
// __ Impl _______________________________________________________ private void Parse(XmlTextReader reader, ProtocolData result) { ProtocolObject currentObject = null; MessageData currentMsg = null; FieldData currentField = null; EnumData currentEnum = null; EnumEntry currentEntry = null; EnumEntryParameter currentParam = null; int currentEnumValue = 0; while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "include": ProcessInclude(reader.ReadElementContentAsString(), result); break; case "version": result.Version = reader.ReadElementContentAsInt(); break; case "message": if (currentMsg != null) { SortFields(currentMsg); } currentMsg = new MessageData(); currentObject = currentMsg; currentMsg.Name = reader.GetAttribute("name"); currentMsg.Id = GetIntFromString(reader.GetAttribute("id")); result.Messages.Add(currentMsg.Name, currentMsg); break; case "description": currentObject.Description = reader.ReadString(); break; case "field": currentField = new FieldData(); currentField.Name = reader.GetAttribute("name"); currentField.TypeString = reader.GetAttribute("type"); currentField.Type = GetFieldTypeFromString(currentField.TypeString); currentField.NumElements = GetFieldTypeNumElements(currentField.TypeString); currentField.Description = reader.ReadElementContentAsString(); UpdateEnumFields(result, currentField); currentMsg.Fields.Add(currentField); break; case "enum": currentEnum = GetEnumDataForName(result, reader.GetAttribute("name")); currentObject = currentEnum; result.Enumerations[currentEnum.Name] = currentEnum; currentEnumValue = 0; break; case "entry": currentEntry = new EnumEntry(); currentObject = currentEntry; currentEntry.Name = reader.GetAttribute("name"); currentEntry.Value = GetIntFromString(reader.GetAttribute("value"), ++currentEnumValue); currentEnumValue = currentEntry.Value; currentEnum.Entries.Add(currentEntry); break; case "param": currentParam = new EnumEntryParameter(); currentParam.Index = GetIntFromString(reader.GetAttribute("index")); currentParam.Description = reader.ReadElementContentAsString(); currentEntry.Parameters.Add(currentParam); break; } } } }
// __ Impl _______________________________________________________ private void Parse(XmlTextReader reader, ProtocolData result) { ProtocolObject currentObject = null; MessageData currentMsg = null; FieldData currentField = null; EnumData currentEnum = null; EnumEntry currentEntry = null; EnumEntryParameter currentParam = null; int currentEnumValue = 0; while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "include": ProcessInclude(reader.ReadElementContentAsString(), result); break; case "version": result.Version = reader.ReadElementContentAsInt(); break; case "message": if (currentMsg != null) SortFields(currentMsg); currentMsg = new MessageData(); currentObject = currentMsg; currentMsg.Name = reader.GetAttribute("name"); currentMsg.Id = GetIntFromString(reader.GetAttribute("id")); result.Messages.Add(currentMsg.Name, currentMsg); break; case "description": currentObject.Description = reader.ReadString(); break; case "field": currentField = new FieldData(); currentField.Name = reader.GetAttribute("name"); currentField.TypeString = reader.GetAttribute("type"); currentField.Type = GetFieldTypeFromString(currentField.TypeString); currentField.NumElements = GetFieldTypeNumElements(currentField.TypeString); currentField.Description = reader.ReadElementContentAsString(); UpdateEnumFields(result, currentField); currentMsg.Fields.Add(currentField); break; case "enum": currentEnum = GetEnumDataForName(result, reader.GetAttribute("name")); currentObject = currentEnum; result.Enumerations[currentEnum.Name] = currentEnum; currentEnumValue = 0; break; case "entry": currentEntry = new EnumEntry(); currentObject = currentEntry; currentEntry.Name = reader.GetAttribute("name"); currentEntry.Value = GetIntFromString(reader.GetAttribute("value"), ++currentEnumValue); currentEnumValue = currentEntry.Value; currentEnum.Entries.Add(currentEntry); break; case "param": currentParam = new EnumEntryParameter(); currentParam.Index = GetIntFromString(reader.GetAttribute("index")); currentParam.Description = reader.ReadElementContentAsString(); currentEntry.Parameters.Add(currentParam); break; } } } }
private EnumData GetEnumDataForName(ProtocolData data, string name) { EnumData result; if (!data.Enumerations.TryGetValue(name, out result)) { result = new EnumData(); result.Name = name; } return result; }
private void ProcessInclude(string includeFileName, ProtocolData result) { string basePath = Path.GetDirectoryName(mSourceFileName); string targetFileName = (Path.IsPathRooted(includeFileName)) ? includeFileName : Path.Combine(basePath, includeFileName); if (mProcessedIncludes.Contains(targetFileName)) return; mProcessedIncludes.Add(targetFileName); using (XmlTextReader reader = new XmlTextReader(targetFileName)) { Parse(reader, result); } }
public abstract void Write(ProtocolData data, TextWriter w);
// __ Impl _______________________________________________________ private void Parse(XmlTextReader reader, ProtocolData result) { ProtocolObject currentObject = null; MessageData currentMsg = null; FieldData currentField = null; EnumData currentEnum = null; EnumEntry currentEntry = null; EnumEntryParameter currentParam = null; int currentEnumValue = 0; while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "include": ProcessInclude(reader.ReadElementContentAsString(), result); break; case "version": result.Version = reader.ReadElementContentAsInt(); break; case "message": // if (currentMsg != null) SortFields(currentMsg); //<--- if condition deleted currentMsg = new MessageData(); currentObject = currentMsg; currentMsg.Name = reader.GetAttribute("name"); currentMsg.Id = GetIntFromString(reader.GetAttribute("id")); if (currentMsg.Id < 256) // Msg id field is a byte, discard anything beyond 255 { result.Messages.Add(currentMsg.Name, currentMsg); } break; case "description": currentObject.Description = reader.ReadString(); break; case "field": currentField = new FieldData(); currentField.Name = reader.GetAttribute("name"); currentField.TypeString = reader.GetAttribute("type"); currentField.Type = GetFieldTypeFromString(currentField.TypeString); currentField.NumElements = GetFieldTypeNumElements(currentField.TypeString); currentField.Description = reader.ReadElementContentAsString(); UpdateEnumFields(result, currentField); currentMsg.Fields.Add(currentField); break; case "enum": currentEnum = GetEnumDataForName(result, reader.GetAttribute("name")); currentObject = currentEnum; result.Enumerations[currentEnum.Name] = currentEnum; currentEnumValue = 0; break; case "entry": currentEntry = new EnumEntry(); currentObject = currentEntry; currentEntry.Name = reader.GetAttribute("name"); currentEntry.Value = GetIntFromString(reader.GetAttribute("value"), ++currentEnumValue); currentEnumValue = currentEntry.Value; currentEnum.Entries.Add(currentEntry); break; case "param": currentParam = new EnumEntryParameter(); currentParam.Index = GetIntFromString(reader.GetAttribute("index")); currentParam.Description = reader.ReadElementContentAsString(); currentEntry.Parameters.Add(currentParam); break; } } else //<----- Else condition added to control closing of the current node; *** BEGIN **** { if (reader.Name == "message") { if (currentMsg != null) { SortFields(currentMsg); } } } //<----- *** END *** } }