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(); } }
/// <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> /// Imports a datatype. /// </summary> private void ImportDataType(DataType datatype, string targetNamespace) { if (datatype == null) { return; } if (!IsValidName(datatype.Name)) { throw Exception("'{0}' is not a valid datatype name.", datatype.Name); } datatype.QName = new XmlQualifiedName(datatype.Name, targetNamespace); if (m_datatypes.ContainsKey(datatype.QName)) { throw Exception("The datatype name '{0}' already used by another datatype.", datatype.Name); } ComplexType complexType = datatype as ComplexType; if (complexType != null) { if (complexType.Field != null) { foreach (FieldType fieldType in complexType.Field) { if (fieldType.ComplexType != null) { ImportDataType(fieldType.ComplexType, targetNamespace); } } } } ServiceType serviceType = datatype as ServiceType; if (serviceType != null) { if (serviceType.Request != null) { foreach (FieldType fieldType in serviceType.Request) { if (fieldType.ComplexType != null) { ImportDataType(fieldType.ComplexType, targetNamespace); } } } if (serviceType.Response != null) { foreach (FieldType fieldType in serviceType.Response) { if (fieldType.ComplexType != null) { ImportDataType(fieldType.ComplexType, targetNamespace); } } } } m_datatypes.Add(datatype.QName, datatype); }