internal XSMinExclusiveFacet(XmlSchemaMinExclusiveFacet minExclusiveFace) { _facet = minExclusiveFace; _value = ValueFactory.Create(_facet.Value); if (_facet.Annotation is XmlSchemaAnnotation annotation) { _annotation = XMLSchemaSerializer.CreateXSAnnotation(annotation); _annotation.BindToContainer(RootContainer, this); } }
private List <AttributeRule> GetRules(XmlSchemaSimpleTypeRestriction restriction) { List <AttributeRule> rules = new List <AttributeRule>(); List <string> enumValues = null; foreach (XmlSchemaFacet facet in restriction.Facets) { XmlSchemaEnumerationFacet enumFacet = facet as XmlSchemaEnumerationFacet; if (enumFacet != null) { if (enumValues == null) { enumValues = new List <string>(); } enumValues.Add(enumFacet.Value); continue; } XmlSchemaMinExclusiveFacet minExclusiveFacet = facet as XmlSchemaMinExclusiveFacet; if (minExclusiveFacet != null) { double minExclusive; if (Double.TryParse(minExclusiveFacet.Value, out minExclusive)) { rules.Add(new NumericMinRule(minExclusive, false)); } continue; } XmlSchemaMinInclusiveFacet minInclusiveFacet = facet as XmlSchemaMinInclusiveFacet; if (minInclusiveFacet != null) { double minInclusive; if (Double.TryParse(minInclusiveFacet.Value, out minInclusive)) { rules.Add(new NumericMinRule(minInclusive, true)); } continue; } XmlSchemaMaxExclusiveFacet maxExclusiveFacet = facet as XmlSchemaMaxExclusiveFacet; if (maxExclusiveFacet != null) { double maxExclusive; if (Double.TryParse(maxExclusiveFacet.Value, out maxExclusive)) { rules.Add(new NumericMaxRule(maxExclusive, false)); } continue; } XmlSchemaMaxInclusiveFacet maxInclusiveFacet = facet as XmlSchemaMaxInclusiveFacet; if (maxInclusiveFacet != null) { double maxInclusive; if (Double.TryParse(maxInclusiveFacet.Value, out maxInclusive)) { rules.Add(new NumericMaxRule(maxInclusive, true)); } continue; } } if (enumValues != null && enumValues.Count > 0) { rules.Add(new StringEnumRule(enumValues.ToArray())); } return(rules); }
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 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); }
public static IEnumerable <XmlSchemaFacet> createXmlFacets(IEnumerable <ICctsFacet> facets) { var xmlFacets = new List <XmlSchemaFacet>(); foreach (var facet in facets) { XmlSchemaFacet xmlFacet = null; switch (facet.name) { case "fractionDigit": xmlFacet = new XmlSchemaFractionDigitsFacet(); break; case "length": xmlFacet = new XmlSchemaLengthFacet(); break; case "maxExclusive": xmlFacet = new XmlSchemaMaxExclusiveFacet(); break; case "maxInclusive": xmlFacet = new XmlSchemaMaxInclusiveFacet(); break; case "maxLength": xmlFacet = new XmlSchemaMaxLengthFacet(); break; case "minExclusive": xmlFacet = new XmlSchemaMinExclusiveFacet(); break; case "minInclusive": xmlFacet = new XmlSchemaMinInclusiveFacet(); break; case "minLength": xmlFacet = new XmlSchemaMinLengthFacet(); break; case "pattern": xmlFacet = new XmlSchemaPatternFacet(); break; case "totalDigits": xmlFacet = new XmlSchemaTotalDigitsFacet(); break; case "whiteSpace": xmlFacet = new XmlSchemaWhiteSpaceFacet(); break; case "enumeration": foreach (var enumValue in facet.content.Split('|')) { var enumerationFacet = new XmlSchemaEnumerationFacet(); enumerationFacet.Value = enumValue; xmlFacets.Add(enumerationFacet); } break; } if (xmlFacet != null) { xmlFacet.Value = facet.content; xmlFacets.Add(xmlFacet); } } return(xmlFacets); }
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); }
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 == 1) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } double?maxValue = GetterExtensions.Maximum(jSchema); double?maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); Regex regex = new Regex("^[9]+$"); if ((minValue != null && maxValue != null) && Math.Abs((double)minValue).Equals(maxValue) && regex.IsMatch(maxValue.ToString())) { XmlSchemaTotalDigitsFacet facet = new XmlSchemaTotalDigitsFacet { Value = FormatDouble(maxValue.ToString().Length), }; content.Facets.Add(facet); } else { 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); } } 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); }
protected override void Visit(XmlSchemaMinExclusiveFacet facet) { AddLeaf(SimpleTypeStructureNodeType.FacetMinExclusive, facet); }
protected virtual void Visit(XmlSchemaMinExclusiveFacet facet) { }
public XsdSimpleRestrictionType(RelaxngDatatype primitive, RelaxngParamList 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); }