/// <summary> /// Writes an array declaration to the stream. /// </summary> private bool WriteTemplate_Message(Template template, Context context) { ServiceType datatype = context.Target as ServiceType; if (datatype == null) { return(false); } template.AddReplacement("_Namespace_", TargetNamespace); template.AddReplacement("_ServicesNamespace_", ServicesNamespace); template.AddReplacement("_TypesNamespace_", TypesNamespace); template.AddReplacement("_NAME_", datatype.QName.Name); return(template.WriteTemplate(context)); }
private void WriteTemplate_HFIndecesImplementation(string namespacePrefix, string dictionaryName) { List <HFEntry> tmp = new List <HFEntry>(); foreach (HFEntry entry in m_lstFields) { bool bAdd = true; foreach (HFEntry entry2 in tmp) { if (entry.FieldName() == entry2.FieldName()) { bAdd = false; break; } } if (bAdd) { tmp.Add(entry); } } m_lstFields = tmp; string fileName = String.Format(@"{0}\{1}_hfindeces.c", OutputDirectory, namespacePrefix); fileName = fileName.ToLower(); StreamWriter writer = new StreamWriter(fileName, false); Template template = new Template(writer, WiresharkTemplatePath + "hfentries.c", Assembly.GetExecutingAssembly()); template.AddReplacement("_Date_", DateTime.Now.ToString()); AddTemplate( template, "// _INDECES_", WiresharkTemplatePath + "hfentries.c", m_lstFields, null, new WriteTemplateEventHandler(WriteTemplate_Field)); AddTemplate( template, "// _FIELDS_", WiresharkTemplatePath + "hfentries.c", m_lstFields, null, new WriteTemplateEventHandler(WriteTemplate_Field)); template.WriteTemplate(null); writer.Close(); }
/// <summary> /// Writes an array declaration to the stream. /// </summary> private bool WriteTemplate_Array(Template template, Context context) { DataType datatype = context.Target as DataType; if (datatype == null) { return(false); } if (!datatype.AllowArrays) { return(false); } template.WriteLine(String.Empty); template.AddReplacement("_TypeName_", datatype.QName.Name); return(template.WriteTemplate(context)); }
/// <summary> /// Creates a description from the documentation element. /// </summary> protected void CreateDescription(Template template, string token, Documentation documentation) { StringBuilder buffer = new StringBuilder(); if (documentation != null) { if (documentation.Text != null && documentation.Text.Length > 0) { for (int ii = 0; ii < documentation.Text.Length; ii++) { if (buffer.Length > 0) { buffer.Append("\r\n"); } buffer.Append(documentation.Text[ii]); } } } template.AddReplacement(token, buffer.ToString()); }
private void WriteTemplate_HFIndecesHeader(string namespacePrefix, string dictionaryName) { string fileName = String.Format(@"{0}\{1}_hfindeces.h", OutputDirectory, namespacePrefix); fileName = fileName.ToLower(); StreamWriter writer = new StreamWriter(fileName, false); Template template = new Template(writer, WiresharkTemplatePath + "hfentries.h", Assembly.GetExecutingAssembly()); template.AddReplacement("_Date_", DateTime.Now.ToString()); AddTemplate( template, "// _EXTERNINDECES_", WiresharkTemplatePath + "hfentries.h", m_lstFields, null, new WriteTemplateEventHandler(WriteTemplate_Field)); template.WriteTemplate(null); writer.Close(); }
/// <summary> /// Writes the address space declaration file. /// </summary> private void WriteTemplate_Constants(string namespacePrefix, string className, List <DataType> datatypes) { m_className = className; // assign identifiers. foreach (DataType datatype in datatypes) { Constant constant = datatype as Constant; if (constant != null) { if (constant.Name.StartsWith(Severity.Bad.ToString())) { constant.Severity = Severity.Bad; } if (constant.Name.StartsWith(Severity.Good.ToString())) { constant.Severity = Severity.Good; } if (constant.Name.StartsWith(Severity.Uncertain.ToString())) { constant.Severity = Severity.Uncertain; } } } string fileName = null; switch (TargetLanguage) { case Language.DotNet: { fileName = String.Format(@"{0}\{1}.{2}.cs", OutputDirectory, namespacePrefix, className); m_templateSuffix = ".cs"; break; } case Language.AnsiC: { fileName = String.Format(@"{0}\{1}_{2}.h", OutputDirectory, namespacePrefix, className); fileName = fileName.ToLower(); m_templateSuffix = ".h"; break; } case Language.CSV: { fileName = String.Format(@"{0}\{1}.{2}.csv", OutputDirectory, namespacePrefix, className); m_templateSuffix = ".csv"; break; } } StreamWriter writer = new StreamWriter(fileName, false); try { string templatePath = TemplatePath + "Constants.File" + m_templateSuffix; if (className == "Identifiers") { templatePath = TemplatePath + "Constants.DataTypes" + m_templateSuffix; } Template template = new Template(writer, templatePath, Assembly.GetExecutingAssembly()); template.AddReplacement("_Date_", DateTime.Now); template.AddReplacement("_Prefix_", namespacePrefix); template.AddReplacement("_ClassName_", className); template.AddReplacement("_FileName_", namespacePrefix + '_' + className); AddTemplate( template, "// ListOfIdentifiers", TemplatePath + "Constants.Constant" + m_templateSuffix, datatypes, new LoadTemplateEventHandler(LoadTemplate_Constant), new WriteTemplateEventHandler(WriteTemplate_Constant)); AddTemplate( template, "// ListOfEncodings", TemplatePath + "Constants.Constant" + m_templateSuffix, datatypes, new LoadTemplateEventHandler(LoadTemplate_Constant), new WriteTemplateEventHandler(WriteTemplate_Constant)); Context context = new Context(); context.BlankLine = TargetLanguage != Language.CSV; template.WriteTemplate(context); } finally { writer.Close(); } }
/// <summary> /// Writes a constant. /// </summary> private bool WriteTemplate_Constant(Template template, Context context) { DataType datatype = context.Target as DataType; if (datatype == null) { return(false); } Constant constant = context.Target as Constant; if (constant != null) { if (!String.IsNullOrEmpty(constant.Value)) { template.AddReplacement("_IdType_", "string"); template.AddReplacement("_Identifier_", String.Format("\"{0}\"", constant.Value)); template.AddReplacement("_ClassName_", "_" + m_className); } else { if (constant.Severity != Severity.None) { context.BlankLine = TargetLanguage != Language.CSV; uint id = Convert.ToUInt32(constant.Identifier); id <<= 16; switch (constant.Severity) { case Severity.Bad: { id += 0x80000000; break; } case Severity.Uncertain: { id += 0x40000000; break; } } template.AddReplacement("_IdType_", "uint"); template.AddReplacement("_Identifier_", String.Format("0x{0:X8}", id)); template.AddReplacement("_ClassName_", String.Empty); } else { template.AddReplacement("_IdType_", "uint"); template.AddReplacement("_Identifier_", constant.Identifier); template.AddReplacement("_ClassName_", "_" + m_className); } } } string symbolicId = datatype.Name; if (constant != null) { if (constant.Severity != Severity.None) { string name = datatype.Name; int index = name.IndexOf('_'); if (index != -1) { name = name.Substring(index + 1); } symbolicId = String.Format("{0}{1}", constant.Severity, name); } } else { template.AddReplacement("_IdType_", "uint"); template.AddReplacement("_Identifier_", datatype.Identifier); template.AddReplacement("_ClassName_", "Id"); } template.AddReplacement("_SymbolicId_", symbolicId); string description = GetDescription(datatype.Documentation); if (String.IsNullOrEmpty(description)) { description = String.Format("The identifier for the {0} datatype.", symbolicId); } template.AddReplacement("_Description_", description); ComplexType complexType = context.Target as ComplexType; if (complexType != null) { template.AddReplacement("_XmlEncodingId_", complexType.XmlEncodingId); template.AddReplacement("_BinaryEncodingId_", complexType.BinaryEncodingId); } return(template.WriteTemplate(context)); }
/// <summary> /// Writes a datatype to the stream. /// </summary> private bool WriteTemplate_DataType(Template template, Context context) { DataType datatype = context.Target as DataType; if (datatype == null) { return(false); } template.AddReplacement("_TypeName_", datatype.QName.Name); CreateDescription(template, "_Description_", datatype.Documentation); AddTemplate( template, "<!-- ArrayDeclaration -->", TemplatePath + "Array.xml", new DataType[] { datatype }, null, new WriteTemplateEventHandler(WriteTemplate_Array)); ComplexType complexType = datatype as ComplexType; if (complexType != null) { if (!IsNull(complexType.BaseType)) { DataType basetype = Validator.ResolveType(complexType.BaseType); if (basetype == null) { throw new ApplicationException(String.Format("Could not find base type '{0}' for complex type '{1}'.", complexType.BaseType, complexType.QName)); } template.AddReplacement("_BaseType_", GetXmlSchemaTypeName(basetype.QName, -1)); } AddTemplate( template, "<!-- ListOfFields -->", TemplatePath + "Field.xml", complexType.Field, new LoadTemplateEventHandler(LoadTemplate_Field), null); } EnumeratedType enumeratedType = datatype as EnumeratedType; if (enumeratedType != null) { AddTemplate( template, "<!-- ListOfValues -->", TemplatePath + "EnumeratedValue.xml", enumeratedType.Value, new LoadTemplateEventHandler(LoadTemplate_EnumeratedValue), null); } ServiceType serviceType = datatype as ServiceType; if (serviceType != null) { AddTemplate( template, "<!-- ListOfRequestParameters -->", TemplatePath + "Field.xml", serviceType.Request, new LoadTemplateEventHandler(LoadTemplate_Field), null); AddTemplate( template, "<!-- ListOfResponseParameters -->", TemplatePath + "Field.xml", serviceType.Response, new LoadTemplateEventHandler(LoadTemplate_Field), null); } return(template.WriteTemplate(context)); }
/// <summary> /// Writes the /// </summary> private bool WriteTemplate_DataType(Template template, Context context) { DataType datatype = context.Target as DataType; if (datatype == null) { return(false); } template.AddReplacement("_TypeName_", datatype.QName.Name); CreateDescription(template, "_Description_", datatype.Documentation); ComplexType complexType = datatype as ComplexType; if (complexType != null) { List <FieldType> fields = new List <FieldType>(); GetFields(complexType, fields); AddTemplate( template, "<!-- ListOfFields -->", TemplatePath + "Field.xml", fields, new LoadTemplateEventHandler(LoadTemplate_Field), null); } EnumeratedType enumeratedType = datatype as EnumeratedType; if (enumeratedType != null) { uint lengthInBits = 32; bool isOptionSet = false; List <EnumeratedValue> values = new List <EnumeratedValue>(enumeratedType.Value); if (enumeratedType.IsOptionSet) { isOptionSet = true; var baseType = Validator.ResolveType(enumeratedType.BaseType); if (baseType != null) { switch (baseType.Name) { case "SByte": { lengthInBits = 8; break; } case "Byte": { lengthInBits = 8; break; } case "Int16": { lengthInBits = 16; break; } case "UInt16": { lengthInBits = 16; break; } case "Int32": { lengthInBits = 32; break; } case "UInt32": { lengthInBits = 32; break; } case "Int64": { lengthInBits = 64; break; } case "UInt64": { lengthInBits = 64; break; } } } values.Add(new EnumeratedValue() { Name = "None", Value = 0, ValueSpecified = true }); } template.AddReplacement("_LengthInBits_", lengthInBits); template.AddReplacement("_IsOptionSet_", (isOptionSet) ? " IsOptionSet=\"true\"" : ""); AddTemplate( template, "<!-- ListOfValues -->", TemplatePath + "EnumeratedValue.xml", values, new LoadTemplateEventHandler(LoadTemplate_EnumeratedValue), null); } ServiceType serviceType = datatype as ServiceType; if (serviceType != null) { AddTemplate( template, "<!-- ListOfRequestParameters -->", TemplatePath + "Field.xml", serviceType.Request, new LoadTemplateEventHandler(LoadTemplate_Field), null); AddTemplate( template, "<!-- ListOfResponseParameters -->", TemplatePath + "Field.xml", serviceType.Response, new LoadTemplateEventHandler(LoadTemplate_Field), null); } return(template.WriteTemplate(context)); }
private void WriteTemplate_EnumsImplementation(string namespacePrefix, string dictionaryNam) { // get datatypes. List <DataType> datatypes = GetDataTypeList(null, m_exportAll, false); if (datatypes.Count == 0) { return; } // sort types to ensure types are declared in the correct order. List <DataType> sortedTypes = new List <DataType>(); foreach (DataType datatype in datatypes) { AddDataType(datatype, sortedTypes); } datatypes = sortedTypes; string fileName = String.Format(@"{0}\{1}_enumparser.c", OutputDirectory, namespacePrefix); fileName = fileName.ToLower(); StreamWriter writer = new StreamWriter(fileName, false); writer.Write(string.Format(m_sHeader, "OpcUa Enum Type Parser", DateTime.Now)); writer.Write("#ifdef HAVE_CONFIG_H\n" + "# include \"config.h\"\n" + "#endif\n" + "\n" + "#include <gmodule.h>\n" + "#include <epan/packet.h>\n\n" + "#include \"opcua_enumparser.h\"\n\n"); try { foreach (DataType datatype in sortedTypes) { EnumeratedType enumtype = datatype as EnumeratedType; if (enumtype != null) { Template template = new Template(writer, WiresharkTemplatePath + "enumparser.c", Assembly.GetExecutingAssembly()); template.AddReplacement("_NAME_", enumtype.Name); AddTemplate( template, "// _ENTRY_", WiresharkTemplatePath + "enumparser.c", enumtype.Value, null, new WriteTemplateEventHandler(WriteTemplate_Enum)); template.WriteTemplate(null); } } writer.Write("\n/** header field definitions */\n" + "static hf_register_info hf[] =\n" + "{"); int index = 0; int count = 0; // count number of enums foreach (DataType datatype in sortedTypes) { EnumeratedType enumtype = datatype as EnumeratedType; if (enumtype != null) { count++; } } // generate code foreach (DataType datatype in sortedTypes) { EnumeratedType enumtype = datatype as EnumeratedType; if (enumtype != null) { Template template = new Template(writer, WiresharkTemplatePath + "enumregisterinfo.c", Assembly.GetExecutingAssembly()); template.AddReplacement("_NAME_", enumtype.Name); template.WriteTemplate(null); if ((index + 1) < count) { writer.Write(","); } index++; } } writer.Write("\n};\n\n"); writer.Write("/** Register enum types. */\n"); writer.Write("void registerEnumTypes(int proto)\n"); writer.Write("{\n"); writer.Write(" proto_register_field_array(proto, hf, array_length(hf));\n"); writer.Write("}\n"); } finally { writer.Close(); } }
private void WriteTemplate_ComplexTypesImplementation(string namespacePrefix, string dictionaryNam) { // get datatypes. List <DataType> datatypes = GetDataTypeList(null, m_exportAll, false); if (datatypes.Count == 0) { return; } // sort types to ensure types are declared in the correct order. List <DataType> sortedTypes = new List <DataType>(); foreach (DataType datatype in datatypes) { AddDataType(datatype, sortedTypes); } datatypes = sortedTypes; string fileName = String.Format(@"{0}\{1}_complextypeparser.c", OutputDirectory, namespacePrefix); fileName = fileName.ToLower(); List <string> list = new List <string>(); StreamWriter writer = new StreamWriter(fileName, false); writer.Write(string.Format(m_sHeader, "OpcUa Complex Type Parser", DateTime.Now)); writer.Write("#ifdef HAVE_CONFIG_H\n" + "# include \"config.h\"\n" + "#endif\n" + "\n" + "#include <gmodule.h>\n" + "#include <epan/packet.h>\n" + "#include \"opcua_complextypeparser.h\"\n" + "#include \"opcua_enumparser.h\"\n" + "#include \"opcua_simpletypes.h\"\n" + "#include \"opcua_hfindeces.h\"\n\n"); try { foreach (DataType datatype in sortedTypes) { ComplexType complextype = datatype as ComplexType; if (complextype != null) { Template template = new Template(writer, WiresharkTemplatePath + "complexparserfunction.c", Assembly.GetExecutingAssembly()); list.Add(complextype.Name); template.AddReplacement("_NAME_", complextype.Name); if (complextype.BaseType == null) { template.AddReplacement("// _BASE_", ""); } else { template.AddReplacement("// _BASE_", string.Format(" /* parse base class members */ \n parse{0}(subtree, tvb, pOffset, \"[{0}]\");\n /* parse additional members */", complextype.BaseType.Name)); } AddTemplate( template, "// _FIELDS_", WiresharkTemplatePath + "complexparserfunction.c", complextype.Field, null, new WriteTemplateEventHandler(WriteTemplate_Parser)); template.WriteTemplate(null); } } } finally { writer.Write("\n/** Setup protocol subtree array */\n" + "static gint *ett[] =\n{\n"); foreach (string sName in list) { writer.Write(" &ett_opcua_" + sName + ",\n"); } writer.Write("};\n\n"); writer.Write("void registerComplexTypes()\n" + "{\n" + " proto_register_subtree_array(ett, array_length(ett));\n" + "}\n\n"); writer.Close(); } }