private XmlSchemaSimpleType ExtractSimpleType(string name, JsonSchema jSchema) { XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType { Name = name, }; AddAnnotations(simpleType, jSchema); string reference = GetterExtensions.Ref(jSchema); if (reference != null) { XmlQualifiedName baseTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(reference)); XmlSchemaSimpleTypeRestriction simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction { BaseTypeName = baseTypeName, }; XmlSchemaSimpleTypeRestriction stringFacets = ExtractStringFacets(jSchema); if (stringFacets.Facets.Count > 0) { foreach (XmlSchemaObject facet in stringFacets.Facets) { simpleTypeRestriction.Facets.Add(facet); } } XmlSchemaSimpleTypeRestriction numberFacets = ExtractNumberAndIntegerFacets(jSchema, new TypeKeyword(JsonSchemaType.Number)); if (numberFacets.Facets.Count > 0) { foreach (XmlSchemaObject facet in numberFacets.Facets) { simpleTypeRestriction.Facets.Add(facet); } } simpleType.Content = simpleTypeRestriction; return(simpleType); } TypeKeyword type = jSchema.Get <TypeKeyword>(); if (type == null) { // assume string type if type is missing XmlSchemaSimpleTypeRestriction noTypeSimpleType = ExtractStringFacets(jSchema); noTypeSimpleType.BaseTypeName = null; simpleType.Content = noTypeSimpleType; return(simpleType); } if (type.Value == JsonSchemaType.String) { simpleType.Content = ExtractStringFacets(jSchema); } else if (type.Value == JsonSchemaType.Number || type.Value == JsonSchemaType.Integer) { simpleType.Content = ExtractNumberAndIntegerFacets(jSchema, type); } else if (type.Value == JsonSchemaType.Boolean) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("boolean", XML_SCHEMA_NS), }; simpleType.Content = content; } else if (type.Value == JsonSchemaType.Array) { string xlist = jSchema.OtherData.TryGetString("@xsdType"); if (xlist.Equals("XmlList")) { XmlSchemaSimpleTypeList theList = new XmlSchemaSimpleTypeList(); List <JsonSchema> items = GetterExtensions.Items(jSchema); string typeRef = GetterExtensions.Ref(items[0]); theList.ItemTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(typeRef)); simpleType.Content = theList; } } return(simpleType); }
public XmlSchemaElement GetSchema() { XmlSchemaSimpleType timeUnitType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "minutes"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "hours"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "weeks"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "days"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "months"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "years"; restriction.Facets.Add(enumeration); timeUnitType.Content = restriction; XmlSchemaSimpleType languageType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction languageEnum = new XmlSchemaSimpleTypeRestriction(); languageEnum.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "dicom"; languageEnum.Facets.Add(enumeration); languageType.Content = languageEnum; XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaAttribute attrib = new XmlSchemaAttribute(); attrib.Name = "time"; attrib.Use = XmlSchemaUse.Required; attrib.SchemaTypeName = new XmlQualifiedName("double", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "unit"; attrib.Use = XmlSchemaUse.Required; attrib.SchemaType = timeUnitType; type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "refValue"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "expressionLanguage"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaType = languageType; type.Attributes.Add(attrib); XmlSchemaElement element = new XmlSchemaElement(); element.Name = "online-retention"; element.SchemaType = type; return(element); }
static void JsonSimpleTypeHandler(XmlSchemaObject schemaObject, HelpExampleGeneratorContext context) { XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schemaObject; // Enumerations return 0 if (simpleType.Content != null) { if (simpleType.Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)simpleType.Content; foreach (XmlSchemaObject facet in restriction.Facets) { if (facet is XmlSchemaEnumerationFacet) { context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.numberString); context.writer.WriteString("0"); return; } } } else if (simpleType.Content is XmlSchemaSimpleTypeList) { InvokeHandler(simpleType.Content, context); } } string value = GetConstantValue(simpleType.QualifiedName.Name); if (simpleType.QualifiedName.Name == "base64Binary") { char[] base64stream = value.ToCharArray(); context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.arrayString); for (int i = 0; i < base64stream.Length; i++) { context.writer.WriteStartElement(JsonGlobals.itemString, string.Empty); context.writer.WriteAttributeString(string.Empty, JsonGlobals.typeString, string.Empty, JsonGlobals.numberString); context.writer.WriteValue((int)base64stream[i]); context.writer.WriteEndElement(); } } else if (simpleType.QualifiedName.Name == "dateTime") { DateTime dateTime = DateTime.Parse("1999-05-31T11:20:00", CultureInfo.InvariantCulture); context.writer.WriteString(JsonGlobals.DateTimeStartGuardReader); context.writer.WriteValue((dateTime.ToUniversalTime().Ticks - JsonGlobals.unixEpochTicks) / 10000); switch (dateTime.Kind) { case DateTimeKind.Unspecified: case DateTimeKind.Local: TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime.ToLocalTime()); if (ts.Ticks < 0) { context.writer.WriteString("-"); } else { context.writer.WriteString("+"); } int hours = Math.Abs(ts.Hours); context.writer.WriteString((hours < 10) ? "0" + hours : hours.ToString(CultureInfo.InvariantCulture)); int minutes = Math.Abs(ts.Minutes); context.writer.WriteString((minutes < 10) ? "0" + minutes : minutes.ToString(CultureInfo.InvariantCulture)); break; case DateTimeKind.Utc: break; } context.writer.WriteString(JsonGlobals.DateTimeEndGuardReader); } else if (simpleType.QualifiedName.Name == "char") { context.writer.WriteString(XmlConvert.ToString('a')); } else if (!String.IsNullOrEmpty(value)) { if (simpleType.QualifiedName.Name == "integer" || simpleType.QualifiedName.Name == "int" || simpleType.QualifiedName.Name == "long" || simpleType.QualifiedName.Name == "unsignedLong" || simpleType.QualifiedName.Name == "unsignedInt" || simpleType.QualifiedName.Name == "short" || simpleType.QualifiedName.Name == "unsignedShort" || simpleType.QualifiedName.Name == "byte" || simpleType.QualifiedName.Name == "unsignedByte" || simpleType.QualifiedName.Name == "decimal" || simpleType.QualifiedName.Name == "float" || simpleType.QualifiedName.Name == "double" || simpleType.QualifiedName.Name == "negativeInteger" || simpleType.QualifiedName.Name == "nonPositiveInteger" || simpleType.QualifiedName.Name == "positiveInteger" || simpleType.QualifiedName.Name == "nonNegativeInteger") { context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.numberString); } else if (simpleType.QualifiedName.Name == "boolean") { context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.booleanString); } context.writer.WriteString(value); } else { if (!(simpleType.Content is XmlSchemaSimpleTypeList)) { context.writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.nullString); } } }
/// <summary> /// Processes an attribute, generating the required field and property. Potentially generates /// an enum for an attribute restriction. /// </summary> /// <param name="attribute">Attribute element being processed.</param> /// <param name="typeDeclaration">CodeTypeDeclaration to be used when outputting code.</param> /// <param name="outputXmlMethod">Member method for the OutputXml method.</param> private static void ProcessAttribute(XmlSchemaAttribute attribute, CodeTypeDeclaration typeDeclaration, CodeMemberMethod outputXmlMethod) { string attributeName = attribute.Name; string rawAttributeType = attribute.SchemaTypeName.Name; string attributeType = null; EnumDeclaration enumDeclaration = null; if (rawAttributeType == null || rawAttributeType.Length == 0) { XmlSchemaSimpleTypeRestriction simpleTypeRestriction = attribute.SchemaType.Content as XmlSchemaSimpleTypeRestriction; if (simpleTypeRestriction != null) { attributeType = String.Concat(attributeName, "Type"); bool enumRestriction = false; CodeTypeDeclaration enumTypeDeclaration = new CodeTypeDeclaration(attributeType); enumTypeDeclaration.Attributes = MemberAttributes.Public; enumTypeDeclaration.IsEnum = true; enumDeclaration = new EnumDeclaration(attributeType, enumTypeDeclaration); foreach (XmlSchemaFacet facet in simpleTypeRestriction.Facets) { XmlSchemaEnumerationFacet enumFacet = facet as XmlSchemaEnumerationFacet; if (enumFacet != null) { enumRestriction = true; string enumValue = MakeEnumValue(enumFacet.Value); enumDeclaration.AddValue(enumFacet.Value); CodeMemberField memberField = new CodeMemberField(typeof(int), enumValue); enumTypeDeclaration.Members.Add(memberField); string enumItemDocumentation = GetDocumentation(enumFacet.Annotation); if (enumItemDocumentation != null) { GenerateSummaryComment(memberField.Comments, enumItemDocumentation); } } XmlSchemaPatternFacet patternFacet = facet as XmlSchemaPatternFacet; if (patternFacet != null) { attributeType = (string)simpleTypeNamesToClrTypeNames[simpleTypeRestriction.BaseTypeName.Name]; } } if (enumRestriction) { typeDeclaration.Members.Add(enumTypeDeclaration); } else { enumDeclaration = null; } } XmlSchemaSimpleTypeList simpleTypeList = attribute.SchemaType.Content as XmlSchemaSimpleTypeList; if (simpleTypeList != null) { attributeType = simpleTypeList.ItemTypeName.Name; EnumDeclaration declaration = (EnumDeclaration)typeNamesToEnumDeclarations[attributeType]; declaration.Flags = true; } } else { attributeType = (string)simpleTypeNamesToClrTypeNames[rawAttributeType]; } string documentation = GetDocumentation(attribute.Annotation); // TODO: Handle required fields. GenerateFieldAndProperty(attributeName, attributeType, typeDeclaration, outputXmlMethod, enumDeclaration, documentation, false, false); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="OrderQuantityType"> XmlSchemaSimpleType OrderQuantityType = new XmlSchemaSimpleType(); OrderQuantityType.Name = "OrderQuantityType"; // <xs:restriction base="xs:int"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema"); // <xs:minExclusive value="5"/> XmlSchemaMinExclusiveFacet MinExclusive = new XmlSchemaMinExclusiveFacet(); MinExclusive.Value = "5"; restriction.Facets.Add(MinExclusive); OrderQuantityType.Content = restriction; schema.Items.Add(OrderQuantityType); // <xs:element name="item"> XmlSchemaElement element = new XmlSchemaElement(); element.Name = "item"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); // <xs:attribute name="OrderQuantity" type="OrderQuantityType"/> XmlSchemaAttribute OrderQuantityAttribute = new XmlSchemaAttribute(); OrderQuantityAttribute.Name = "OrderQuantity"; OrderQuantityAttribute.SchemaTypeName = new XmlQualifiedName("OrderQuantityType", ""); complexType.Attributes.Add(OrderQuantityAttribute); element.SchemaType = complexType; schema.Items.Add(element); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
/// <summary> /// Creates an XML schema element for reading a value of the given type /// </summary> /// <param name="Name">Name of the field</param> /// <param name="Type">Type of the field</param> /// <returns>New schema element representing the field</returns> static XmlSchemaElement CreateSchemaFieldElement(string Name, Type Type) { XmlSchemaElement Element = new XmlSchemaElement(); Element.Name = Name; Element.MinOccurs = 0; Element.MaxOccurs = 1; if (Type == typeof(string)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; } else if (Type == typeof(bool)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean).QualifiedName; } else if (Type == typeof(int)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int).QualifiedName; } else if (Type == typeof(float)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Float).QualifiedName; } else if (Type == typeof(double)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Double).QualifiedName; } else if (Type.IsEnum) { XmlSchemaSimpleTypeRestriction Restriction = new XmlSchemaSimpleTypeRestriction(); Restriction.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; foreach (string EnumName in Enum.GetNames(Type)) { XmlSchemaEnumerationFacet Facet = new XmlSchemaEnumerationFacet(); Facet.Value = EnumName; Restriction.Facets.Add(Facet); } XmlSchemaSimpleType EnumType = new XmlSchemaSimpleType(); EnumType.Content = Restriction; Element.SchemaType = EnumType; } else if (Type == typeof(string[])) { XmlSchemaElement ItemElement = new XmlSchemaElement(); ItemElement.Name = "Item"; ItemElement.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; ItemElement.MinOccurs = 0; ItemElement.MaxOccursString = "unbounded"; XmlSchemaSequence Sequence = new XmlSchemaSequence(); Sequence.Items.Add(ItemElement); XmlSchemaComplexType ArrayType = new XmlSchemaComplexType(); ArrayType.Particle = Sequence; Element.SchemaType = ArrayType; } else { throw new Exception("Unsupported field type for XmlConfigFile attribute"); } return(Element); }
static XmlQualifiedName AddAttributeTypeToXmlSchema(SchemaInfo schemaInfo, UxmlAttributeDescription description, IUxmlFactory factory, FactoryProcessingHelper processingData) { if (description.name == null) { return(null); } string attrTypeName = factory.uxmlQualifiedName + "_" + description.name + "_" + k_TypeSuffix; string attrTypeNameInBaseElement = factory.substituteForTypeQualifiedName + "_" + description.name + "_" + k_TypeSuffix; FactoryProcessingHelper.AttributeRecord attrRecord; if (processingData.attributeTypeNames.TryGetValue(attrTypeNameInBaseElement, out attrRecord)) { // If restriction != baseElement.restriction, we need to declare a new type. // Note: we do not support attributes having a less restrictive restriction than its base type. if ((description.restriction == null && attrRecord.desc.restriction == null) || (description.restriction != null && description.restriction.Equals(attrRecord.desc.restriction))) { // Register attrTypeName -> attrRecord for potential future derived elements. processingData.attributeTypeNames.Add(attrTypeName, attrRecord); return(attrRecord.name); } } XmlQualifiedName xqn; FactoryProcessingHelper.AttributeRecord attributeRecord; if (description.restriction == null) { // Type is a built-in type. xqn = new XmlQualifiedName(description.type, description.typeNamespace); attributeRecord = new FactoryProcessingHelper.AttributeRecord { name = xqn, desc = description }; processingData.attributeTypeNames.Add(attrTypeName, attributeRecord); return(xqn); } string attrTypeNameForSchema = factory.uxmlName + "_" + description.name + "_" + k_TypeSuffix; xqn = new XmlQualifiedName(attrTypeNameForSchema, schemaInfo.schema.TargetNamespace); XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); simpleType.Name = attrTypeNameForSchema; UxmlEnumeration enumRestriction = description.restriction as UxmlEnumeration; if (enumRestriction != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); foreach (var v in enumRestriction.values) { XmlSchemaEnumerationFacet enumValue = new XmlSchemaEnumerationFacet(); enumValue.Value = v; restriction.Facets.Add(enumValue); } } else { UxmlValueMatches regexRestriction = description.restriction as UxmlValueMatches; if (regexRestriction != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); XmlSchemaPatternFacet pattern = new XmlSchemaPatternFacet(); pattern.Value = regexRestriction.regex; restriction.Facets.Add(pattern); } else { UxmlValueBounds bounds = description.restriction as UxmlValueBounds; if (bounds != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); XmlSchemaFacet facet; if (bounds.excludeMin) { facet = new XmlSchemaMinExclusiveFacet(); } else { facet = new XmlSchemaMinInclusiveFacet(); } facet.Value = bounds.min; restriction.Facets.Add(facet); if (bounds.excludeMax) { facet = new XmlSchemaMaxExclusiveFacet(); } else { facet = new XmlSchemaMaxInclusiveFacet(); } facet.Value = bounds.max; restriction.Facets.Add(facet); } else { Debug.Log("Unsupported restriction type."); } } } schemaInfo.schema.Items.Add(simpleType); attributeRecord = new FactoryProcessingHelper.AttributeRecord { name = xqn, desc = description }; processingData.attributeTypeNames.Add(attrTypeName, attributeRecord); return(xqn); }
internal void LoadTypeValues(XmlSchemaSimpleType node) { if ((node.Content is XmlSchemaSimpleTypeList) || (node.Content is XmlSchemaSimpleTypeUnion)) { throw ExceptionBuilder.SimpleTypeNotSupported(); } if (node.Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction content = (XmlSchemaSimpleTypeRestriction)node.Content; baseType = content.BaseTypeName.Name; xmlBaseType = content.BaseTypeName; if (baseType == null || baseType.Length == 0) { baseType = content.BaseType.Name; xmlBaseType = null; } foreach (XmlSchemaFacet facet in content.Facets) { if (facet is XmlSchemaLengthFacet) { length = Convert.ToInt32(facet.Value); } if (facet is XmlSchemaMinLengthFacet) { minLength = Convert.ToInt32(facet.Value); } if (facet is XmlSchemaMaxLengthFacet) { maxLength = Convert.ToInt32(facet.Value); } if (facet is XmlSchemaPatternFacet) { pattern = facet.Value; } if (facet is XmlSchemaEnumerationFacet) { enumeration = !enumeration.Equals(string.Empty) ? enumeration + " " + facet.Value : facet.Value; } if (facet is XmlSchemaMinExclusiveFacet) { minExclusive = facet.Value; } if (facet is XmlSchemaMinInclusiveFacet) { minInclusive = facet.Value; } if (facet is XmlSchemaMaxExclusiveFacet) { maxExclusive = facet.Value; } if (facet is XmlSchemaMaxInclusiveFacet) { maxInclusive = facet.Value; } } } }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="SizeType"> XmlSchemaSimpleType SizeType = new XmlSchemaSimpleType(); SizeType.Name = "SizeType"; // <xs:restriction base="xs:string"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:enumeration value="Small"/> XmlSchemaEnumerationFacet enumerationSmall = new XmlSchemaEnumerationFacet(); enumerationSmall.Value = "Small"; restriction.Facets.Add(enumerationSmall); // <xs:enumeration value="Medium"/> XmlSchemaEnumerationFacet enumerationMedium = new XmlSchemaEnumerationFacet(); enumerationMedium.Value = "Medium"; restriction.Facets.Add(enumerationMedium); // <xs:enumeration value="Large"/> XmlSchemaEnumerationFacet enumerationLarge = new XmlSchemaEnumerationFacet(); enumerationLarge.Value = "Large"; restriction.Facets.Add(enumerationLarge); SizeType.Content = restriction; schema.Items.Add(SizeType); // <xs:element name="Item"> XmlSchemaElement elementItem = new XmlSchemaElement(); elementItem.Name = "Item"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); // <xs:attribute name="Size" type="SizeType"/> XmlSchemaAttribute attributeSize = new XmlSchemaAttribute(); attributeSize.Name = "Size"; attributeSize.SchemaTypeName = new XmlQualifiedName("SizeType", ""); complexType.Attributes.Add(attributeSize); elementItem.SchemaType = complexType; schema.Items.Add(elementItem); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
/// <summary> /// Process an IFCXML schema file /// </summary> /// <param name="ifcxmlSchemaFile">the IfcXML schema file info</param> public static bool ProcessIFCSchema(FileInfo ifcxmlSchemaFile) { if (ifcxmlSchemaFile.Name.Equals(loadedSchema) && IfcSchemaEntityTree.EntityDict.Count > 0) { return(false); // The schema file has been processed and loaded before } loadedSchema = Path.GetFileNameWithoutExtension(ifcxmlSchemaFile.Name); IfcSchemaEntityTree.Initialize(loadedSchema); XmlTextReader reader = new XmlTextReader(ifcxmlSchemaFile.FullName); XmlSchema theSchema = XmlSchema.Read(reader, ValidationCallback); foreach (XmlSchemaObject item in theSchema.Items) { if (item is XmlSchemaComplexType) { XmlSchemaComplexType ct = item as XmlSchemaComplexType; string entityName = ct.Name; if (string.Compare(entityName, 0, "Ifc", 0, 3, ignoreCase: true) != 0) { continue; } string parentName = string.Empty; if (ct.ContentModel == null) { continue; } if (ct.ContentModel.Parent == null) { continue; } string predefTypeEnum = null; if (ct.ContentModel.Parent is XmlSchemaComplexType) { XmlSchemaComplexType parent = ct.ContentModel.Parent as XmlSchemaComplexType; XmlSchemaSimpleContentExtension parentSimpleType = parent.ContentModel.Content as XmlSchemaSimpleContentExtension; XmlSchemaComplexContentExtension parentComplexType = parent.ContentModel.Content as XmlSchemaComplexContentExtension; if (parentSimpleType != null) { parentName = parentSimpleType.BaseTypeName.Name; foreach (XmlSchemaAttribute attr in parentSimpleType.Attributes) { if (attr.Name != null && attr.Name.Equals("PredefinedType", StringComparison.InvariantCultureIgnoreCase)) { predefTypeEnum = attr.SchemaTypeName.Name; } } } if (parentComplexType != null) { parentName = parentComplexType.BaseTypeName.Name; foreach (XmlSchemaAttribute attr in parentComplexType.Attributes) { if (attr.Name != null && attr.Name.Equals("PredefinedType", StringComparison.InvariantCultureIgnoreCase)) { predefTypeEnum = attr.SchemaTypeName.Name; break; } } if (string.IsNullOrEmpty(predefTypeEnum) && parentComplexType.Particle != null) { XmlSchemaSequence seq = parentComplexType.Particle as XmlSchemaSequence; if (seq != null) { foreach (XmlSchemaElement elem in seq.Items) { if (elem.Name != null && elem.Name.Equals("PredefinedType", StringComparison.InvariantCultureIgnoreCase)) { predefTypeEnum = elem.SchemaTypeName.Name; break; } } } } } } IfcSchemaEntityTree.Add(entityName, parentName, predefTypeEnum, isAbstract: ct.IsAbstract); } else if (item is XmlSchemaSimpleType) { XmlSchemaSimpleType st = item as XmlSchemaSimpleType; if (st.Name.StartsWith("Ifc", StringComparison.InvariantCultureIgnoreCase) && st.Name.EndsWith("Enum", StringComparison.InvariantCultureIgnoreCase)) { string enumName = st.Name; XmlSchemaSimpleTypeRestriction enums = st.Content as XmlSchemaSimpleTypeRestriction; if (enums != null) { IList <string> enumValueList = new List <string>(); foreach (XmlSchemaEnumerationFacet enumFacet in enums.Facets) { if (IfcSchemaEntityTree.PredefinedTypeEnumDict.ContainsKey(enumName)) { IfcSchemaEntityTree.PredefinedTypeEnumDict[enumName].Add(enumFacet.Value.ToUpper()); } else { enumValueList.Add(enumFacet.Value.ToUpper()); } } if (enumValueList.Count > 0) { IfcSchemaEntityTree.PredefinedTypeEnumDict.Add(enumName, enumValueList); } } } } } return(true); }
protected override XmlSchemaType RamlTypeToSchemaType(RamlType ramlType, ConversionOptions options) { if (ramlType == null) { return(null); } XmlSchemaType xmlSchemaType; // check if it is enum if (ramlType.Enum != null) { xmlSchemaType = new XmlSchemaSimpleType(); xmlSchemaType.Name = ramlType.Name; var restriction = new XmlSchemaSimpleTypeRestriction(); var enumElementTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Enum.ItemsTypeName); if (enumElementTypeName == null) { enumElementTypeName = GetXmlRefDataType(ramlType.Enum.ItemsTypeName, options.XmlNamespace); } if (enumElementTypeName != null) { restriction.BaseTypeName = enumElementTypeName; } foreach (string enumValue in ramlType.Enum.EnumValues) { var enumerationFacet = new XmlSchemaEnumerationFacet(); enumerationFacet.Value = enumValue; restriction.Facets.Add(enumerationFacet); } ((XmlSchemaSimpleType)xmlSchemaType).Content = restriction; return(xmlSchemaType); } xmlSchemaType = new XmlSchemaComplexType(); xmlSchemaType.Name = ramlType.Name; // check if it is array if (ramlType.Array != null) { var arraySequence = new XmlSchemaSequence(); var arrayElement = new XmlSchemaElement(); arrayElement.Name = ramlType.Array.ItemName; arrayElement.MaxOccursString = "unbounded"; var arrayElementTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Array.ItemsTypeName); if (arrayElementTypeName == null) { arrayElementTypeName = GetXmlRefDataType(ramlType.Array.ItemsTypeName, options.XmlNamespace); } if (arrayElementTypeName != null) { arrayElement.SchemaTypeName = arrayElementTypeName; } arraySequence.Items.Add(arrayElement); ((XmlSchemaComplexType)xmlSchemaType).Particle = arraySequence; return(xmlSchemaType); } // check if it has base if (ramlType.Base != null) { // assume simple type for now xmlSchemaType = new XmlSchemaSimpleType(); xmlSchemaType.Name = ramlType.Name; var restriction = new XmlSchemaSimpleTypeRestriction(); var baseTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Base.Name); if (baseTypeName == null) { baseTypeName = GetXmlRefDataType(ramlType.Base.Name, options.XmlNamespace); } if (baseTypeName != null) { restriction.BaseTypeName = baseTypeName; } if (!string.IsNullOrEmpty(ramlType.Base.Pattern)) { var pattern = new XmlSchemaPatternFacet(); pattern.Value = ramlType.Base.Pattern; restriction.Facets.Add(pattern); } ((XmlSchemaSimpleType)xmlSchemaType).Content = restriction; return(xmlSchemaType); } var annotation = GetXmlAnnotattion(ramlType.Description); if (annotation != null && options.GenerateDescriptions) { xmlSchemaType.Annotation = annotation; } var sequence = new XmlSchemaSequence(); ((XmlSchemaComplexType)xmlSchemaType).Particle = sequence; if (ramlType.Properties == null || ramlType.Properties.Count == 0) { return(xmlSchemaType); } foreach (RamlProperty ramlProperty in ramlType.Properties) { var xmlSchemaProperty = new XmlSchemaElement(); xmlSchemaProperty.Name = ramlProperty.Name; var typeName = RamlDataTypeToXmlSchemaDataType(ramlProperty.Type); if (typeName == null) { typeName = GetXmlRefDataType(ramlProperty.Type, options.XmlNamespace); } if (typeName != null) { xmlSchemaProperty.SchemaTypeName = typeName; } var propertyAnnotation = GetXmlAnnotattion(ramlProperty.Description); if (propertyAnnotation != null && options.GenerateDescriptions) { xmlSchemaProperty.Annotation = propertyAnnotation; } sequence.Items.Add(xmlSchemaProperty); if (!ramlProperty.Required) { xmlSchemaProperty.MinOccurs = 0; xmlSchemaProperty.IsNillable = true; } } return(xmlSchemaType); }
public XsdSimpleRestrictionType(RelaxngDatatype primitive, IList <RelaxngParam> parameters) { type = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction r = new XmlSchemaSimpleTypeRestriction(); type.Content = r; string ns = primitive.NamespaceURI; // Remap XML Schema datatypes namespace -> XML Schema namespace. if (ns == "http://www.w3.org/2001/XMLSchema-datatypes") { ns = XSchema.Namespace; } r.BaseTypeName = new XmlQualifiedName(primitive.Name, ns); foreach (RelaxngParam p in parameters) { XmlSchemaFacet f = null; string value = p.Value; switch (p.Name) { case "maxExclusive": f = new XmlSchemaMaxExclusiveFacet(); break; case "maxInclusive": f = new XmlSchemaMaxInclusiveFacet(); break; case "minExclusive": f = new XmlSchemaMinExclusiveFacet(); break; case "minInclusive": f = new XmlSchemaMinInclusiveFacet(); break; case "pattern": f = new XmlSchemaPatternFacet(); // .NET/Mono Regex has a bug that it does not support "IsLatin-1Supplement" // (it somehow breaks at '-'). value = value.Replace("\\p{IsLatin-1Supplement}", "[\\x80-\\xFF]"); break; case "whiteSpace": f = new XmlSchemaWhiteSpaceFacet(); break; case "length": f = new XmlSchemaLengthFacet(); break; case "maxLength": f = new XmlSchemaMaxLengthFacet(); break; case "minLength": f = new XmlSchemaMinLengthFacet(); break; case "fractionDigits": f = new XmlSchemaFractionDigitsFacet(); break; case "totalDigits": f = new XmlSchemaTotalDigitsFacet(); break; default: throw new RelaxngException(String.Format("XML Schema facet {0} is not recognized or not supported.", p.Name)); } f.Value = value; r.Facets.Add(f); } // Now we create XmlSchema to handle simple-type // based validation (since there is no other way, // because of sucky XmlSchemaSimpleType design). schema = new XSchema(); XmlSchemaElement el = new XmlSchemaElement(); el.Name = "root"; el.SchemaType = type; schema.Items.Add(el); schema.Compile(null); }
/// <summary> /// Get the type of content control that should be created by default for this XML node. /// </summary> /// <param name="xmlNode">An XmlNode to convert.</param> /// <returns>A MapingType enumeration value with the type of content control that should be created.</returns> internal static MappingType CheckNodeType(XmlNode xmlNode) { if (xmlNode.SchemaInfo.SchemaElement != null && xmlNode.SchemaInfo.SchemaElement.ElementSchemaType != null) { //is it xsd:dateTime or xsd:base64Binary switch (xmlNode.SchemaInfo.SchemaElement.ElementSchemaType.TypeCode) { case XmlTypeCode.Date: case XmlTypeCode.DateTime: return(MappingType.Date); case XmlTypeCode.Base64Binary: return(MappingType.Picture); default: break; } //is there an enumeration? if (xmlNode.SchemaInfo.SchemaElement.ElementSchemaType is XmlSchemaSimpleType) { if (((XmlSchemaSimpleType)xmlNode.SchemaInfo.SchemaElement.ElementSchemaType).Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction xsstr = (XmlSchemaSimpleTypeRestriction)((XmlSchemaSimpleType)xmlNode.SchemaInfo.SchemaElement.ElementSchemaType).Content; foreach (XmlSchemaFacet xsf in xsstr.Facets) { if (xsf is XmlSchemaEnumerationFacet) { return(MappingType.DropDown); } } } } } else if (xmlNode.SchemaInfo.SchemaAttribute != null && xmlNode.SchemaInfo.SchemaAttribute.AttributeSchemaType != null) { //is it xsd:dateTime or xsd:base64Binary switch (xmlNode.SchemaInfo.SchemaAttribute.AttributeSchemaType.TypeCode) { case XmlTypeCode.Date: case XmlTypeCode.DateTime: return(MappingType.Date); case XmlTypeCode.Base64Binary: return(MappingType.Picture); default: break; } //is there an enumeration? if (xmlNode.SchemaInfo.SchemaAttribute.AttributeSchemaType is XmlSchemaSimpleType) { if (((XmlSchemaSimpleType)xmlNode.SchemaInfo.SchemaAttribute.AttributeSchemaType).Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction xsstr = (XmlSchemaSimpleTypeRestriction)((XmlSchemaSimpleType)xmlNode.SchemaInfo.SchemaAttribute.AttributeSchemaType).Content; foreach (XmlSchemaFacet xsf in xsstr.Facets) { if (xsf is XmlSchemaEnumerationFacet) { return(MappingType.DropDown); } } } } } return(MappingType.Text); }
internal XmlSchemaAttribute CreateXsdAttribute() { XmlSchemaAttribute a = new XmlSchemaAttribute(); SetLineInfo(a); a.Name = Name; a.DefaultValue = resolvedNormalizedDefaultValue; if (OccurenceType != DTDAttributeOccurenceType.Required) { a.Use = XmlSchemaUse.Optional; } XmlQualifiedName qname = XmlQualifiedName.Empty; ArrayList enumeration = null; if (enumeratedNotations != null && enumeratedNotations.Count > 0) { qname = new XmlQualifiedName("NOTATION", XmlSchema.Namespace); enumeration = enumeratedNotations; } else if (enumeratedLiterals != null) { enumeration = enumeratedLiterals; } else { switch (Datatype.TokenizedType) { case XmlTokenizedType.ID: qname = new XmlQualifiedName("ID", XmlSchema.Namespace); break; case XmlTokenizedType.IDREF: qname = new XmlQualifiedName("IDREF", XmlSchema.Namespace); break; case XmlTokenizedType.IDREFS: qname = new XmlQualifiedName("IDREFS", XmlSchema.Namespace); break; case XmlTokenizedType.ENTITY: qname = new XmlQualifiedName("ENTITY", XmlSchema.Namespace); break; case XmlTokenizedType.ENTITIES: qname = new XmlQualifiedName("ENTITIES", XmlSchema.Namespace); break; case XmlTokenizedType.NMTOKEN: qname = new XmlQualifiedName("NMTOKEN", XmlSchema.Namespace); break; case XmlTokenizedType.NMTOKENS: qname = new XmlQualifiedName("NMTOKENS", XmlSchema.Namespace); break; case XmlTokenizedType.NOTATION: qname = new XmlQualifiedName("NOTATION", XmlSchema.Namespace); break; } } if (enumeration != null) { XmlSchemaSimpleType st = new XmlSchemaSimpleType(); SetLineInfo(st); XmlSchemaSimpleTypeRestriction r = new XmlSchemaSimpleTypeRestriction(); SetLineInfo(r); r.BaseTypeName = qname; if (enumeratedNotations != null) { foreach (string name in enumeratedNotations) { XmlSchemaEnumerationFacet f = new XmlSchemaEnumerationFacet(); SetLineInfo(f); r.Facets.Add(f); f.Value = name; } } st.Content = r; } else if (qname != XmlQualifiedName.Empty) { a.SchemaTypeName = qname; } return(a); }
private XmlSchemaSimpleTypeRestriction ExtractStringFacets(JsonSchema jSchema) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("string", XML_SCHEMA_NS), }; EnumKeyword enumKeyword = jSchema.Get <EnumKeyword>(); if (enumKeyword != null) { foreach (JsonValue enumValue in GetterExtensions.Enum(jSchema)) { XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet { Value = enumValue.String, }; content.Facets.Add(enumFacet); } } MinLengthKeyword minLength = jSchema.Get <MinLengthKeyword>(); MaxLengthKeyword maxLength = jSchema.Get <MaxLengthKeyword>(); if (minLength != null && maxLength != null && minLength.Value == maxLength.Value) { // special rule that maps equal min and max lengths to xsd length facet XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(lengthFacet); } else { if (minLength != null) { XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(minLengthFacet); } if (maxLength != null) { XmlSchemaMaxLengthFacet maxLengthFacet = new XmlSchemaMaxLengthFacet { Value = maxLength.Value.ToString(), }; content.Facets.Add(maxLengthFacet); } } PatternKeyword pattern = jSchema.Get <PatternKeyword>(); if (pattern != null) { XmlSchemaPatternFacet patternFacet = new XmlSchemaPatternFacet { Value = pattern.Value.ToString(), }; content.Facets.Add(patternFacet); } FormatKeyword format = jSchema.Get <FormatKeyword>(); if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value.Key)) { content.BaseTypeName = ExtractBaseTypeNameFromFormat(format.Value.Key); } return(content); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="northwestStates"> XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); simpleType.Name = "northwestStates"; schema.Items.Add(simpleType); // <xs:annotation> XmlSchemaAnnotation annNorthwestStates = new XmlSchemaAnnotation(); simpleType.Annotation = annNorthwestStates; // <xs:documentation>States in the Pacific Northwest of US</xs:documentation> XmlSchemaDocumentation docNorthwestStates = new XmlSchemaDocumentation(); annNorthwestStates.Items.Add(docNorthwestStates); docNorthwestStates.Markup = TextToNodeArray("States in the Pacific Northwest of US"); // <xs:restriction base="xs:string"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:enumeration value="WA"> XmlSchemaEnumerationFacet enumerationWA = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationWA); enumerationWA.Value = "WA"; // <xs:annotation> XmlSchemaAnnotation annWA = new XmlSchemaAnnotation(); enumerationWA.Annotation = annWA; // <xs:documentation>Washington</documentation> XmlSchemaDocumentation docWA = new XmlSchemaDocumentation(); annWA.Items.Add(docWA); docWA.Markup = TextToNodeArray("Washington"); // <xs:enumeration value="OR"> XmlSchemaEnumerationFacet enumerationOR = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationOR); enumerationOR.Value = "OR"; // <xs:annotation> XmlSchemaAnnotation annOR = new XmlSchemaAnnotation(); enumerationOR.Annotation = annOR; // <xs:documentation>Oregon</xs:documentation> XmlSchemaDocumentation docOR = new XmlSchemaDocumentation(); annOR.Items.Add(docOR); docOR.Markup = TextToNodeArray("Oregon"); // <xs:enumeration value="ID"> XmlSchemaEnumerationFacet enumerationID = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationID); enumerationID.Value = "ID"; // <xs:annotation> XmlSchemaAnnotation annID = new XmlSchemaAnnotation(); enumerationID.Annotation = annID; // <xs:documentation>Idaho</xs:documentation> XmlSchemaDocumentation docID = new XmlSchemaDocumentation(); annID.Items.Add(docID); docID.Markup = TextToNodeArray("Idaho"); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
private static XmlSchemaSimpleTypeRestriction ExtractNumberAndIntegerFacets(JsonSchema jSchema, TypeKeyword type) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction(); double?minValue = GetterExtensions.Minimum(jSchema); double?minExclusiveValue = GetterExtensions.ExclusiveMinimum(jSchema); if (type.Value == JsonSchemaType.Number) { content.BaseTypeName = new XmlQualifiedName("decimal", XML_SCHEMA_NS); } else if (type.Value == JsonSchemaType.Integer) { if (minValue != null && minValue == 0.0) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } if (minValue != null || minExclusiveValue != null) { if (minValue != null) { XmlSchemaMinInclusiveFacet facet = new XmlSchemaMinInclusiveFacet { Value = FormatDouble((double)minValue), }; content.Facets.Add(facet); } else { XmlSchemaMinExclusiveFacet facet = new XmlSchemaMinExclusiveFacet { Value = FormatDouble((double)minExclusiveValue), }; content.Facets.Add(facet); } } double?maxValue = GetterExtensions.Maximum(jSchema); double?maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); if (maxValue != null || maxExclusiveValue != null) { if (maxValue != null) { XmlSchemaMaxInclusiveFacet maxInclusiveFacet = new XmlSchemaMaxInclusiveFacet { Value = FormatDouble((double)maxValue), }; content.Facets.Add(maxInclusiveFacet); } else { XmlSchemaMaxExclusiveFacet maxExclusiveFacet = new XmlSchemaMaxExclusiveFacet { Value = FormatDouble((double)maxExclusiveValue), }; content.Facets.Add(maxExclusiveFacet); } } return(content); }
static void Main(string[] args) { // Add the customer schema to a new XmlSchemaSet and compile it. // Any schema validation warnings and errors encountered reading or // compiling the schema are handled by the ValidationEventHandler delegate. XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); schemaSet.Add("http://www.tempuri.org", "customer.xsd"); schemaSet.Compile(); // Retrieve the compiled XmlSchema object from the XmlSchemaSet // by iterating over the Schemas property. XmlSchema customerSchema = null; foreach (XmlSchema schema in schemaSet.Schemas()) { customerSchema = schema; } // Create the PhoneNumber element. XmlSchemaElement phoneElement = new XmlSchemaElement(); phoneElement.Name = "PhoneNumber"; // Create the xs:string simple type restriction. XmlSchemaSimpleType phoneType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // Add a pattern facet to the restriction. XmlSchemaPatternFacet phonePattern = new XmlSchemaPatternFacet(); phonePattern.Value = "\\d{3}-\\d{3}-\\d(4)"; restriction.Facets.Add(phonePattern); // Add the restriction to the Content property of the simple type // and the simple type to the SchemaType of the PhoneNumber element. phoneType.Content = restriction; phoneElement.SchemaType = phoneType; // Iterate over each XmlSchemaElement in the Values collection // of the Elements property. foreach (XmlSchemaElement element in customerSchema.Elements.Values) { // If the qualified name of the element is "Customer", // get the complex type of the Customer element // and the sequence particle of the complex type. if (element.QualifiedName.Name.Equals("Customer")) { XmlSchemaComplexType customerType = element.ElementSchemaType as XmlSchemaComplexType; XmlSchemaSequence sequence = customerType.Particle as XmlSchemaSequence; // Add the new PhoneNumber element to the sequence. sequence.Items.Add(phoneElement); } } // Reprocess and compile the modified XmlSchema object and write it to the console. schemaSet.Reprocess(customerSchema); schemaSet.Compile(); customerSchema.Write(Console.Out); }
// recursive decoder private XSDTreeNode DecodeSchema2(XmlSchemaObject obj, XSDTreeNode node) { XSDTreeNode newNode = node; // convert the object to all types and then check what type actually exists XmlSchemaAnnotation annot = obj as XmlSchemaAnnotation; XmlSchemaAttribute attrib = obj as XmlSchemaAttribute; XmlSchemaFacet facet = obj as XmlSchemaFacet; XmlSchemaDocumentation doc = obj as XmlSchemaDocumentation; XmlSchemaAppInfo appInfo = obj as XmlSchemaAppInfo; XmlSchemaElement element = obj as XmlSchemaElement; XmlSchemaSimpleType simpleType = obj as XmlSchemaSimpleType; XmlSchemaComplexType complexType = obj as XmlSchemaComplexType; // if annotation, add a tree node and recurse for documentation and app info if (annot != null) { newNode = new XSDTreeNode("--annotation--", "--annotation--", false); node.Add(newNode); foreach (XmlSchemaObject schemaObject in annot.Items) { DecodeSchema2(schemaObject, newNode); } } // if attribute, add an attribute at tree node else if (attrib != null) { node.AddAttribute(attrib.QualifiedName.Name, attrib.SchemaTypeName.Name); } // if facet, add a tree node else if (facet != null) { newNode = new XSDTreeNode(facet.ToString(), facet.ToString(), false); node.Add(newNode); } // if documentation, add a tree node else if (doc != null) { newNode = new XSDTreeNode("--documentation--", "--documentation--", false); node.Add(newNode); } // if app info, add a tree node else if (appInfo != null) { newNode = new XSDTreeNode("--app info--", "--app info--", false); node.Add(newNode); } // if an element, determine whether the element is a simple type or a complex type else if (element != null) { XmlSchemaSimpleType st = element.SchemaType as XmlSchemaSimpleType; XmlSchemaComplexType ct = element.SchemaType as XmlSchemaComplexType; if (st != null) { // this is a simple type element. Recurse. XSDTreeNode node2 = DecodeSchema2(st, newNode); node2.Name = element.Name; node2.Element = true; node2.ComplexType = false; } else if (ct != null) { // this is a complex type element. Recurse. XSDTreeNode node2 = DecodeSchema2(ct, newNode); newNode.Remove(node2); node2.Name = element.Name; node2.Element = true; node2.ComplexType = true; newNode.Add(node2); } else { // This is a plain ol' fashioned element. newNode = new XSDTreeNode(element.QualifiedName.Name, element.ElementSchemaType?.Name ?? element.SchemaTypeName.Name, false); newNode.Element = true; node.Add(newNode); } } // if a simple type, then add a tree node and recurse facets else if (simpleType != null) { newNode = new XSDTreeNode(simpleType.QualifiedName.Name, simpleType.BaseXmlSchemaType.Name, false); node.Add(newNode); XmlSchemaSimpleTypeRestriction rest = simpleType.Content as XmlSchemaSimpleTypeRestriction; if (rest != null) { foreach (XmlSchemaFacet schemaObject in rest.Facets) { DecodeSchema2(schemaObject, newNode); } } } // if a complex type, add a tree node and recurse its sequence else if (complexType != null) { if (null == complexType.Name) { newNode = new XSDTreeNode("--tmp-name--", complexType.BaseXmlSchemaType.Name, true); } else { newNode = new XSDTreeNode(complexType.Name, complexType.BaseXmlSchemaType.Name, true); } node.Add(newNode); XmlSchemaSequence seq = complexType.Particle as XmlSchemaSequence; if (seq != null) { foreach (XmlSchemaObject schemaObject in seq.Items) { DecodeSchema2(schemaObject, newNode); } } } // now recurse any object collection of the type. foreach (PropertyInfo property in obj.GetType().GetProperties()) { if (property.PropertyType.FullName == "System.Xml.Schema.XmlSchemaObjectCollection") { XmlSchemaObjectCollection childObjectCollection = (XmlSchemaObjectCollection)property.GetValue(obj, null); foreach (XmlSchemaObject schemaObject in childObjectCollection) { DecodeSchema2(schemaObject, newNode); } } } return(newNode); }
public XmlSchemaElement GetSchema() { var type = new XmlSchemaComplexType(); type.Attributes.Add(new XmlSchemaAttribute { Name = "refValue", Use = XmlSchemaUse.Required, SchemaTypeName = new XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema") }); type.Attributes.Add(new XmlSchemaAttribute { Name = "test", Use = XmlSchemaUse.Optional, SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") }); var restriction = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") }; restriction.Facets.Add(new XmlSchemaEnumerationFacet { Value = "years" }); restriction.Facets.Add(new XmlSchemaEnumerationFacet { Value = "weeks" }); restriction.Facets.Add(new XmlSchemaEnumerationFacet { Value = "days" }); var simpleType = new XmlSchemaSimpleType { Content = restriction }; type.Attributes.Add(new XmlSchemaAttribute { Name = "units", Use = XmlSchemaUse.Required, SchemaType = simpleType }); type.Attributes.Add(new XmlSchemaAttribute { Name = "expressionLanguage", Use = XmlSchemaUse.Optional, SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") }); var element = new XmlSchemaElement { Name = "dicom-age-less-than", SchemaType = type }; return(element); }
protected XmlSchemaSimpleType FindOrCreateSimpleType(Type type) { XmlSchemaSimpleType simpleType; string typeId = GenerateSimpleIDFromType(type); simpleType = FindSimpleTypeByID(typeId); if (simpleType != null) { return(simpleType); } simpleType = new XmlSchemaSimpleType(); simpleType.Name = typeId; _nantSimpleTypes.Add(typeId, simpleType); if (type.Equals(typeof(Boolean)) || type.Equals(typeof(bool))) { XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion(); simpleType.Content = union; XmlSchemaSimpleType boolType = new XmlSchemaSimpleType(); union.BaseTypes.Add(boolType); XmlSchemaSimpleType wildcardType = new XmlSchemaSimpleType(); union.BaseTypes.Add(wildcardType); XmlSchemaSimpleTypeRestriction wildcardRestriction = new XmlSchemaSimpleTypeRestriction(); wildcardType.Content = wildcardRestriction; wildcardRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaPatternFacet wildcard = new XmlSchemaPatternFacet(); wildcardRestriction.Facets.Add(wildcard); wildcard.Value = ".*"; XmlSchemaSimpleTypeRestriction boolRestriction = new XmlSchemaSimpleTypeRestriction(); boolRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); boolType.Content = boolRestriction; XmlSchemaEnumerationFacet trueValue = new XmlSchemaEnumerationFacet(); boolRestriction.Facets.Add(trueValue); trueValue.Value = "True"; XmlSchemaEnumerationFacet falseValue = new XmlSchemaEnumerationFacet(); boolRestriction.Facets.Add(falseValue); falseValue.Value = "False"; XmlSchemaSimpleType propertiesType = new XmlSchemaSimpleType(); union.BaseTypes.Add(propertiesType); XmlSchemaSimpleTypeRestriction propertyRestriction = new XmlSchemaSimpleTypeRestriction(); propertiesType.Content = propertyRestriction; propertyRestriction.BaseTypeName = new XmlQualifiedName("CIFactory.Properties", _nantSchema.TargetNamespace); } else if (type.IsSubclassOf(typeof(Enum))) { XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion(); simpleType.Content = union; XmlSchemaSimpleType enumType = new XmlSchemaSimpleType(); union.BaseTypes.Add(enumType); XmlSchemaSimpleType wildcardType = new XmlSchemaSimpleType(); union.BaseTypes.Add(wildcardType); XmlSchemaSimpleTypeRestriction wildcardRestriction = new XmlSchemaSimpleTypeRestriction(); wildcardType.Content = wildcardRestriction; wildcardRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaPatternFacet wildcard = new XmlSchemaPatternFacet(); wildcardRestriction.Facets.Add(wildcard); wildcard.Value = ".*"; XmlSchemaSimpleTypeRestriction enumRestriction = new XmlSchemaSimpleTypeRestriction(); enumType.Content = enumRestriction; enumRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); foreach (String EnumName in System.Enum.GetNames(type)) { XmlSchemaEnumerationFacet enumValue = new XmlSchemaEnumerationFacet(); enumRestriction.Facets.Add(enumValue); enumValue.Value = EnumName; } XmlSchemaSimpleType propertiesType = new XmlSchemaSimpleType(); union.BaseTypes.Add(propertiesType); XmlSchemaSimpleTypeRestriction propertyRestriction = new XmlSchemaSimpleTypeRestriction(); propertiesType.Content = propertyRestriction; propertyRestriction.BaseTypeName = new XmlQualifiedName("CIFactory.Properties", _nantSchema.TargetNamespace); } else if (type.Equals(typeof(CallTask))) { XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion(); simpleType.Content = union; XmlSchemaSimpleType targetType = new XmlSchemaSimpleType(); union.BaseTypes.Add(targetType); XmlSchemaSimpleType wildcardType = new XmlSchemaSimpleType(); union.BaseTypes.Add(wildcardType); XmlSchemaSimpleTypeRestriction wildcardRestriction = new XmlSchemaSimpleTypeRestriction(); wildcardType.Content = wildcardRestriction; wildcardRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaPatternFacet wildcard = new XmlSchemaPatternFacet(); wildcardRestriction.Facets.Add(wildcard); wildcard.Value = ".*"; XmlSchemaSimpleTypeRestriction targetRestriction = new XmlSchemaSimpleTypeRestriction(); targetType.Content = targetRestriction; targetRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); foreach (String TargetName in this.TargetNames) { XmlSchemaEnumerationFacet target = new XmlSchemaEnumerationFacet(); targetRestriction.Facets.Add(target); target.Value = TargetName; } XmlSchemaSimpleType propertiesType = new XmlSchemaSimpleType(); union.BaseTypes.Add(propertiesType); XmlSchemaSimpleTypeRestriction propertyRestriction = new XmlSchemaSimpleTypeRestriction(); propertiesType.Content = propertyRestriction; propertyRestriction.BaseTypeName = new XmlQualifiedName("CIFactory.Properties", _nantSchema.TargetNamespace); } else if (type.Equals(typeof(PropertyTask))) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumeration); enumeration.Value = "${}"; foreach (String PropertyName in this.PropertyNames) { XmlSchemaEnumerationFacet property = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(property); property.Value = String.Format("${{{0}}}", PropertyName); } } else { XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion(); simpleType.Content = union; XmlSchemaSimpleType wildcardType = new XmlSchemaSimpleType(); union.BaseTypes.Add(wildcardType); XmlSchemaSimpleTypeRestriction wildcardRestriction = new XmlSchemaSimpleTypeRestriction(); wildcardType.Content = wildcardRestriction; wildcardRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaPatternFacet wildcard = new XmlSchemaPatternFacet(); wildcardRestriction.Facets.Add(wildcard); wildcard.Value = ".*"; XmlSchemaSimpleType propertiesType = new XmlSchemaSimpleType(); union.BaseTypes.Add(propertiesType); XmlSchemaSimpleTypeRestriction propertyRestriction = new XmlSchemaSimpleTypeRestriction(); propertiesType.Content = propertyRestriction; propertyRestriction.BaseTypeName = new XmlQualifiedName("CIFactory.Properties", _nantSchema.TargetNamespace); } Schema.Items.Add(simpleType); Compile(); return(simpleType); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="RatingType"> XmlSchemaSimpleType RatingType = new XmlSchemaSimpleType(); RatingType.Name = "RatingType"; // <xs:restriction base="xs:number"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema"); // <xs:totalDigits value="2"/> XmlSchemaTotalDigitsFacet totalDigits = new XmlSchemaTotalDigitsFacet(); totalDigits.Value = "2"; restriction.Facets.Add(totalDigits); // <xs:fractionDigits value="1"/> XmlSchemaFractionDigitsFacet fractionDigits = new XmlSchemaFractionDigitsFacet(); fractionDigits.Value = "1"; restriction.Facets.Add(fractionDigits); RatingType.Content = restriction; schema.Items.Add(RatingType); // <xs:element name="movie"> XmlSchemaElement element = new XmlSchemaElement(); element.Name = "movie"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); // <xs:attribute name="rating" type="RatingType"/> XmlSchemaAttribute ratingAttribute = new XmlSchemaAttribute(); ratingAttribute.Name = "rating"; ratingAttribute.SchemaTypeName = new XmlQualifiedName("RatingType", ""); complexType.Attributes.Add(ratingAttribute); element.SchemaType = complexType; schema.Items.Add(element); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
/// <summary> /// Given a Wsdl Message part element returns the type refered to by the message part. /// </summary> /// <param name="message">A Wsdl Message element.</param> /// <returns>The name of the type used by the message element.</returns> internal static string GetMessageTypeName(ServiceDescription svcDesc, Message message) { // If there are no message part(s) the message is void so return null if (message.Parts.Count == 0) { return(null); } // If the message contains a type reference instead of an element reference find the schema type XmlSchemaType type = null; string typeName = null; string elementName = null; if (message.Parts[0].Element == null || message.Parts[0].Element.IsEmpty) { type = FindSchemaType(svcDesc, message.Parts[0].Type.Name, message.Parts[0].Type.Namespace); if (type == null || type.TypeCode == XmlTypeCode.None) { return(message.Parts[0].Type.Name); } typeName = message.Parts[0].Type.Name; } else { XmlSchemaElement element = FindSchemaElement(svcDesc, message.Parts[0].Element.Name, message.Parts[0].Element.Namespace); if (element == null || element.SchemaTypeName == null || element.SchemaTypeName.Name == "") { return(message.Parts[0].Element.Name); } elementName = element.QualifiedName.Name; type = element.ElementSchemaType; typeName = element.SchemaTypeName.Name; } // If this is a complex type return the element type name if (type is XmlSchemaComplexType) { return(elementName == null ? type.Name : elementName); } bool IsEnum = false; if (type.DerivedBy == XmlSchemaDerivationMethod.Restriction) { XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)((XmlSchemaSimpleType)type).Content; foreach (XmlSchemaFacet facet in restriction.Facets) { if (facet is XmlSchemaEnumerationFacet) { IsEnum = true; break; } } } // If this is a simple type with enumeration restrictions, return the type name. // Else return the clr type name if this is a native type or the simple type name if this is not a native type. if (IsEnum == true) { return(typeName); } else { string clrTypeName = CodeGenUtils.GetClrType(type.TypeCode); clrTypeName = clrTypeName != null && clrTypeName.IndexOf("System.") == 0 ? clrTypeName.Substring(7) : clrTypeName; if (clrTypeName == null) { return(typeName); } else if (type.Datatype.ValueType.IsArray) { string typeSuffix = (clrTypeName.Length > 2 && clrTypeName.Substring(clrTypeName.Length - 2) == "[]") ? "" : "[]"; return(clrTypeName + typeSuffix); } else { return(clrTypeName); } } }
public virtual void Check(ConformanceCheckContext ctx, XmlSchemaSimpleTypeRestriction value) { }