/// <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)); }
/// <summary> /// Writes the address space declaration file. /// </summary> private void WriteTemplate_BinarySchema(string fileName) { StreamWriter writer = new StreamWriter(String.Format(@"{0}\{1}.bsd", OutputDirectory, fileName), false); try { Template template = new Template(writer, TemplatePath + "File.xml", Assembly.GetExecutingAssembly()); template.Replacements.Add("_BuildDate_", Utils.Format("{0:yyyy-MM-dd}", DateTime.UtcNow)); template.Replacements.Add("_Version_", Utils.Format("{0}.{1}", Utils.GetAssemblySoftwareVersion(), Utils.GetAssemblyBuildNumber())); template.Replacements.Add("_DictionaryUri_", TargetNamespace); StringBuilder buffer = new StringBuilder(); buffer.AppendFormat("xmlns=\"{0}\"", NamespaceUris[0]); if (!m_exportAll) { for (int ii = 1; ii < NamespaceUris.Count; ii++) { buffer.Append(template.NewLine); buffer.Append(" "); buffer.AppendFormat("xmlns:s{0}=\"{1}\"", ii - 1, NamespaceUris[ii]); } } template.Replacements.Add("xmlns:s0=\"ListOfNamespaces\"", buffer.ToString()); List <string> imports = new List <string>(); imports.Add(Namespaces.OpcUaBuiltInTypes); if (!m_exportAll) { for (int ii = 1; ii < NamespaceUris.Count; ii++) { imports.Add(NamespaceUris[ii]); } } AddTemplate( template, "<!-- Imports -->", null, imports, new LoadTemplateEventHandler(LoadTemplate_Imports), null); AddTemplate( template, "<!-- ListOfTypes -->", TemplatePath + "OpaqueType.xml", GetListOfTypes(m_exportAll), new LoadTemplateEventHandler(LoadTemplate_DataType), new WriteTemplateEventHandler(WriteTemplate_DataType)); template.WriteTemplate(null); } finally { 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)); }