/// <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)); }
private void WriteTemplate_ServicesImplementation(string namespacePrefix, string dictionaryName) { // 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}_serviceparser.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 Service 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_serviceparser.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) { ServiceType Service = datatype as ServiceType; if (Service != null) { Template template = new Template(writer, WiresharkTemplatePath + "serviceparserfunction.c", Assembly.GetExecutingAssembly()); template.AddReplacement("_NAME_", Service.Name + "Request"); list.Add(Service.Name + "Request"); AddTemplate( template, "// _FIELDS_", WiresharkTemplatePath + "serviceparserfunction.c", Service.Request, null, new WriteTemplateEventHandler(WriteTemplate_Parser)); template.WriteTemplate(null); template = new Template(writer, WiresharkTemplatePath + "serviceparserfunction.c", Assembly.GetExecutingAssembly()); template.AddReplacement("_NAME_", Service.Name + "Response"); list.Add(Service.Name + "Response"); AddTemplate( template, "// _FIELDS_", WiresharkTemplatePath + "serviceparserfunction.c", Service.Response, 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 registerServiceTypes()\n" + "{\n" + " proto_register_subtree_array(ett, array_length(ett));\n" + "}\n\n"); writer.Close(); } }
/// <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); }
/// <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) { 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)); }