public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="ZipCodeType"> XmlSchemaSimpleType ZipCodeType = new XmlSchemaSimpleType(); ZipCodeType.Name = "ZipCodeType"; // <xs:restriction base="xs:string"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:length value="5"/> XmlSchemaLengthFacet length = new XmlSchemaLengthFacet(); length.Value = "5"; restriction.Facets.Add(length); ZipCodeType.Content = restriction; schema.Items.Add(ZipCodeType); // <xs:element name="Address"> XmlSchemaElement element = new XmlSchemaElement(); element.Name = "Address"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); // <xs:attribute name="ZipCode" type="ZipCodeType"/> XmlSchemaAttribute ZipCodeAttribute = new XmlSchemaAttribute(); ZipCodeAttribute.Name = "ZipCode"; ZipCodeAttribute.SchemaTypeName = new XmlQualifiedName("ZipCodeType", ""); complexType.Attributes.Add(ZipCodeAttribute); 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); }
internal XSLengthFacet(XmlSchemaLengthFacet lengthFacet) { _facet = lengthFacet; if (_facet.Annotation is XmlSchemaAnnotation annotation) { _annotation = XMLSchemaSerializer.CreateXSAnnotation(annotation); _annotation.BindToContainer(RootContainer, this); } }
private void CreateSimpletypeLength(string length, string minLength, string maxLength, bool expected) { passed = true; XmlSchema schema = new XmlSchema(); XmlSchemaSimpleType testType = new XmlSchemaSimpleType(); testType.Name = "TestType"; XmlSchemaSimpleTypeRestriction testTypeRestriction = new XmlSchemaSimpleTypeRestriction(); testTypeRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); if (length != "-") { XmlSchemaLengthFacet _length = new XmlSchemaLengthFacet(); _length.Value = length; testTypeRestriction.Facets.Add(_length); } if (minLength != "-") { XmlSchemaMinLengthFacet _minLength = new XmlSchemaMinLengthFacet(); _minLength.Value = minLength; testTypeRestriction.Facets.Add(_minLength); } if (maxLength != "-") { XmlSchemaMaxLengthFacet _maxLength = new XmlSchemaMaxLengthFacet(); _maxLength.Value = maxLength; testTypeRestriction.Facets.Add(_maxLength); } testType.Content = testTypeRestriction; schema.Items.Add(testType); schema.Compile(new ValidationEventHandler(ValidationCallbackOne)); Assert( (passed ? "Test passed, should have failed" : "Test failed, should have passed") + ": " + length + " " + minLength + " " + maxLength, expected == passed); }
private XmlAttributeType GetAttributeType(XmlSchemaSimpleType simpleType) { XmlAttributeType attributeType; if (!m_attributeTypes.TryGetValue(simpleType.QualifiedName.ToString(), out attributeType)) { bool simpleList = simpleType.Content is XmlSchemaSimpleTypeList; int length = 1; if (simpleList) { length = Int32.MaxValue; // unbounded, until restricted } List <AttributeRule> rules = null; XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction; if (restriction != null) { if (restriction.BaseTypeName != null) { XmlAttributeType baseType; m_attributeTypes.TryGetValue(restriction.BaseTypeName.ToString(), out baseType); if (baseType != null) { length = baseType.Length; } } foreach (XmlSchemaFacet facet in restriction.Facets) { XmlSchemaLengthFacet lengthFacet = facet as XmlSchemaLengthFacet; if (lengthFacet != null) { Int32.TryParse(lengthFacet.Value, out length); continue; } // also handle minLength, maxLength facets in a limited fashion, ie. either one // will specify the array length XmlSchemaMinLengthFacet minLengthFacet = facet as XmlSchemaMinLengthFacet; if (minLengthFacet != null) { int minLength; if (Int32.TryParse(minLengthFacet.Value, out minLength)) { length = Math.Max(length, minLength); } continue; } XmlSchemaMaxLengthFacet maxLengthFacet = facet as XmlSchemaMaxLengthFacet; if (maxLengthFacet != null) { int maxLength; if (Int32.TryParse(maxLengthFacet.Value, out maxLength)) { length = Math.Max(length, maxLength); } continue; } } rules = GetRules(restriction); } string typeName = simpleType.QualifiedName.ToString(); // if xs:IDREF, then the attribute type should be DomNode as this is a reference Type valueType = simpleType.Datatype.ValueType; XmlTypeCode xmlTypeCode = simpleType.Datatype.TypeCode; if (xmlTypeCode == XmlTypeCode.Idref) { if (valueType.IsArray) { valueType = typeof(string[]); } else { valueType = typeof(DomNode); } } // map xs:integer to xs:int (ATGI schema uses xs:integer, which we don't want to map to System.Decimal) if (xmlTypeCode == XmlTypeCode.Integer) { if (valueType.IsArray) { valueType = typeof(Int32[]); } else { valueType = typeof(Int32); } xmlTypeCode = XmlTypeCode.Int; } else if (xmlTypeCode == XmlTypeCode.NonNegativeInteger) { if (valueType.IsArray) { valueType = typeof(UInt32[]); } else { valueType = typeof(UInt32); } xmlTypeCode = XmlTypeCode.UnsignedInt; } // create our extended attribute type attributeType = new XmlAttributeType(typeName, valueType, length, xmlTypeCode); m_annotations.Add(attributeType, GetAnnotation(simpleType)); if (rules != null) { foreach (AttributeRule rule in rules) { attributeType.AddRule(rule); } } if (!string.IsNullOrEmpty(typeName)) { m_attributeTypes.Add(typeName, attributeType); } } return(attributeType); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="LotteryNumber"> XmlSchemaSimpleType LotteryNumberType = new XmlSchemaSimpleType(); LotteryNumberType.Name = "LotteryNumber"; // <xs:restriction base="xs:int"> XmlSchemaSimpleTypeRestriction LotteryNumberRestriction = new XmlSchemaSimpleTypeRestriction(); LotteryNumberRestriction.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema"); // <xs:minInclusive value="1"/> XmlSchemaMinInclusiveFacet minInclusive = new XmlSchemaMinInclusiveFacet(); minInclusive.Value = "1"; LotteryNumberRestriction.Facets.Add(minInclusive); // <xs:maxInclusive value="99"/> XmlSchemaMaxInclusiveFacet maxInclusive = new XmlSchemaMaxInclusiveFacet(); maxInclusive.Value = "99"; LotteryNumberRestriction.Facets.Add(maxInclusive); LotteryNumberType.Content = LotteryNumberRestriction; schema.Items.Add(LotteryNumberType); // <xs:simpleType name="LotteryNumberList"> XmlSchemaSimpleType LotteryNumberListType = new XmlSchemaSimpleType(); LotteryNumberListType.Name = "LotteryNumberList"; // <xs:list itemType="LotteryNumber"/> XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList(); list.ItemTypeName = new XmlQualifiedName("LotteryNumber", ""); LotteryNumberListType.Content = list; schema.Items.Add(LotteryNumberListType); // <xs:simpleType name="LotteryNumbers"> XmlSchemaSimpleType LotteryNumbersType = new XmlSchemaSimpleType(); LotteryNumbersType.Name = "LotteryNumbers"; // <xs:restriction base="LotteryNumberList"> XmlSchemaSimpleTypeRestriction LotteryNumbersRestriction = new XmlSchemaSimpleTypeRestriction(); LotteryNumbersRestriction.BaseTypeName = new XmlQualifiedName("LotteryNumberList", ""); // <xs:length value="5"/> XmlSchemaLengthFacet length = new XmlSchemaLengthFacet(); length.Value = "5"; LotteryNumbersRestriction.Facets.Add(length); LotteryNumbersType.Content = LotteryNumbersRestriction; schema.Items.Add(LotteryNumbersType); // <xs:element name="TodaysLottery" type="LotteryNumbers"> XmlSchemaElement TodaysLottery = new XmlSchemaElement(); TodaysLottery.Name = "TodaysLottery"; TodaysLottery.SchemaTypeName = new XmlQualifiedName("LotteryNumbers", ""); schema.Items.Add(TodaysLottery); 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 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 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); }
protected override void Visit(XmlSchemaLengthFacet facet) { AddLeaf(SimpleTypeStructureNodeType.FacetLength, facet); }
public void Handle(XmlSchemaLengthFacet facet) {
protected virtual void Visit(XmlSchemaLengthFacet 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); }
internal string GenerateSimpleType( XmlSchemaSimpleType type, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) { CodeTypeDeclaration codeClass = CodeDomHelper.CreateClassDeclaration(type); mainNamespace.Types.Add(codeClass); CodeDomHelper.GenerateXmlTypeAttribute(codeClass, type.QualifiedName.Name, type.QualifiedName.Namespace); XmlSchemaSimpleTypeRestriction restriction = type.Content as XmlSchemaSimpleTypeRestriction; Type baseType = XsdToClrPrimitive(restriction.BaseTypeName); CodeMemberField field = CodeDomHelper.AddField(codeClass, "value", baseType); CodeMemberProperty prop = CodeDomHelper.AddPropertyDeclaration(codeClass, field, "Value", baseType); CodeDomHelper.AddTextAttribute(prop.CustomAttributes); CodeDomHelper.AddCtor(codeClass); CodeDomHelper.AddCtor(codeClass, prop); // for each facet we support, add validation to the setter foreach (object facet in restriction.Facets) { XmlSchemaLengthFacet length = facet as XmlSchemaLengthFacet; if (length != null) { int?value = ToInt32(length.Value); if (value.HasValue) { CodeExpression valueLength = CodeDomHelper.Property(CodeDomHelper.Value(), "Length"); CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(valueLength, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(value)); prop.SetStatements.Add(new CodeConditionStatement(condition, new CodeStatement[] { CodeDomHelper.ThrowFacetViolation("length", value) }, new CodeStatement[0])); } continue; } XmlSchemaPatternFacet pattern = facet as XmlSchemaPatternFacet; if (pattern != null) { // TODO: might want ot validate the pattern value here to make sure that it is a valid Regex. if (!string.IsNullOrEmpty(pattern.Value)) { CodeExpression patternMatch = CodeDomHelper.MethodCall( CodeDomHelper.TypeExpr(typeof(Regex)), "IsMatch", new CodeExpression[] { CodeDomHelper.Value(), CodeDomHelper.Primitive(pattern.Value) }); CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(patternMatch, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(true)); prop.SetStatements.Add(new CodeConditionStatement(condition, new CodeStatement[] { CodeDomHelper.ThrowFacetViolation("pattern", pattern.Value) }, new CodeStatement[0])); } continue; } XmlSchemaMinLengthFacet minLength = facet as XmlSchemaMinLengthFacet; if (minLength != null) { int?value = ToInt32(minLength.Value); if (value.HasValue) { CodeExpression valueLength = CodeDomHelper.Property(CodeDomHelper.Value(), "Length"); CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(valueLength, CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(value)); prop.SetStatements.Add(new CodeConditionStatement(condition, new CodeStatement[] { CodeDomHelper.ThrowFacetViolation("minLength", value) }, new CodeStatement[0])); } continue; } XmlSchemaMaxLengthFacet maxLength = facet as XmlSchemaMaxLengthFacet; if (maxLength != null) { int?value = ToInt32(maxLength.Value); if (value.HasValue) { CodeExpression valueLength = CodeDomHelper.Property(CodeDomHelper.Value(), "Length"); CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(valueLength, CodeBinaryOperatorType.GreaterThan, new CodePrimitiveExpression(value)); prop.SetStatements.Add(new CodeConditionStatement(condition, new CodeStatement[] { CodeDomHelper.ThrowFacetViolation("maxLength", value) }, new CodeStatement[0])); } continue; } } //add ToSrting() Overload and implicit and explicit Cast operators for compatibilty woth previously generated code CodeMemberMethod toString = CodeDomHelper.MethodDecl(typeof(string), "ToString", MemberAttributes.Public | MemberAttributes.Override); toString.Statements.Add(CodeDomHelper.Return(CodeDomHelper.Property("value"))); codeClass.Members.Add(toString); // Unfortunately CodeDom does not support operators, so we have to use CodeSnippet to generate the Cast operators // CodeSnippet is not language aware, so we have to use different snippets for different providers // this version only support c# syntax if (codeProvider is Microsoft.CSharp.CSharpCodeProvider) { string implicitCast = string.Format(" public static implicit operator {0}({1} x) {{ return new {0}(x); }}", codeClass.Name, baseType.FullName); CodeSnippetTypeMember implicitOp = new CodeSnippetTypeMember(implicitCast); codeClass.Members.Add(implicitOp); string explicitCast = string.Format(" public static explicit operator {1}({0} x) {{ return x.Value; }}", codeClass.Name, baseType.FullName); CodeSnippetTypeMember explicitOp = new CodeSnippetTypeMember(explicitCast); codeClass.Members.Add(explicitOp); } return(codeClass.Name); }