///<remarks> /// 1. Content must be present and one of restriction or extention ///</remarks> internal override int Compile(ValidationEventHandler h, XmlSchema schema) { // If this is already compiled this time, simply skip. if (CompilationId == schema.CompilationId) { return(0); } if (Content == null) { error(h, "Content must be present in a simpleContent"); } else { if (Content is XmlSchemaSimpleContentRestriction) { XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction)Content; errorCount += xscr.Compile(h, schema); } else if (Content is XmlSchemaSimpleContentExtension) { XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension)Content; errorCount += xsce.Compile(h, schema); } else { error(h, "simpleContent can't have any value other than restriction or extention"); } } XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h); this.CompilationId = schema.CompilationId; return(errorCount); }
private SOMList GetAttributes(ComplexType ct) { if (ct.ContentModel == null) { return(ct.Attributes); } SimpleModel sc = ct.ContentModel as SimpleModel; if (sc != null) { SimpleExt sce = sc.Content as SimpleExt; if (sce != null) { return(sce.Attributes); } SimpleRst scr = sc.Content as SimpleRst; if (scr != null) { return(scr.Attributes); } else { throw Error(sc, "Invalid simple content model."); } } ComplexModel cc = ct.ContentModel as ComplexModel; if (cc != null) { ComplexExt cce = cc.Content as ComplexExt; if (cce != null) { return(cce.Attributes); } ComplexRst ccr = cc.Content as ComplexRst; if (ccr != null) { return(ccr.Attributes); } else { throw Error(cc, "Invalid simple content model."); } } throw Error(cc, "Invalid complexType. Should not happen."); }
private void ToEmptiableSimpleContent( SimpleModel sm, bool isNew) { SimpleExt se = sm.Content as SimpleExt; if (se != null) { se.BaseTypeName = QNameString; } else { SimpleRst sr = sm.Content as SimpleRst; if (sr == null) { throw Error(sm, "Invalid simple content model was passed."); } sr.BaseTypeName = QNameString; sr.BaseType = null; } }
//<restriction //base = QName //id = ID //{any attributes with non-schema namespace . . .}> //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?)) //</restriction> internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction(); reader.MoveToElement(); if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h, "Should not happen :1: XmlSchemaComplexContentRestriction.Read, name=" + reader.Name, null); reader.SkipToEnd(); return(null); } restriction.LineNumber = reader.LineNumber; restriction.LinePosition = reader.LinePosition; restriction.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "base") { Exception innerex; restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader, out innerex); if (innerex != null) { error(h, reader.Value + " is not a valid value for base attribute", innerex); } } else if (reader.Name == "id") { restriction.Id = reader.Value; } else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h, reader.Name + " is not a valid attribute for restriction", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, restriction); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(restriction); } //Content: 1.annotation?, // 2.simpleType?, // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*, // 4.(attribute | attributeGroup)*, // 5.anyAttribute? int level = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name=" + reader.Name, null); } break; } if (level <= 1 && reader.LocalName == "annotation") { level = 2; //Only one annotation XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { restriction.Annotation = annotation; } continue; } if (level <= 2 && reader.LocalName == "simpleType") { level = 3; XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader, h); if (stype != null) { restriction.baseType = stype; } continue; } if (level <= 3) { if (reader.LocalName == "minExclusive") { level = 3; XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader, h); if (minex != null) { restriction.facets.Add(minex); } continue; } else if (reader.LocalName == "minInclusive") { level = 3; XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader, h); if (mini != null) { restriction.facets.Add(mini); } continue; } else if (reader.LocalName == "maxExclusive") { level = 3; XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader, h); if (maxex != null) { restriction.facets.Add(maxex); } continue; } else if (reader.LocalName == "maxInclusive") { level = 3; XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader, h); if (maxi != null) { restriction.facets.Add(maxi); } continue; } else if (reader.LocalName == "totalDigits") { level = 3; XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader, h); if (total != null) { restriction.facets.Add(total); } continue; } else if (reader.LocalName == "fractionDigits") { level = 3; XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader, h); if (fraction != null) { restriction.facets.Add(fraction); } continue; } else if (reader.LocalName == "length") { level = 3; XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader, h); if (length != null) { restriction.facets.Add(length); } continue; } else if (reader.LocalName == "minLength") { level = 3; XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader, h); if (minlen != null) { restriction.facets.Add(minlen); } continue; } else if (reader.LocalName == "maxLength") { level = 3; XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader, h); if (maxlen != null) { restriction.facets.Add(maxlen); } continue; } else if (reader.LocalName == "enumeration") { level = 3; XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader, h); if (enumeration != null) { restriction.facets.Add(enumeration); } continue; } else if (reader.LocalName == "whiteSpace") { level = 3; XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader, h); if (ws != null) { restriction.facets.Add(ws); } continue; } else if (reader.LocalName == "pattern") { level = 3; XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader, h); if (pattern != null) { restriction.facets.Add(pattern); } continue; } } if (level <= 4) { if (reader.LocalName == "attribute") { level = 4; XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h); if (attr != null) { restriction.Attributes.Add(attr); } continue; } if (reader.LocalName == "attributeGroup") { level = 4; XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader, h); if (attr != null) { restriction.attributes.Add(attr); } continue; } } if (level <= 5 && reader.LocalName == "anyAttribute") { level = 6; XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader, h); if (anyattr != null) { restriction.AnyAttribute = anyattr; } continue; } reader.RaiseInvalidElementError(); } return(restriction); }
//<simpleContent // id = ID // {any attributes with non-schema namespace . . .}> // Content: (annotation?, (restriction | extension)) //</simpleContent> internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent(); reader.MoveToElement(); if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h, "Should not happen :1: XmlSchemaComplexContent.Read, name=" + reader.Name, null); reader.SkipToEnd(); return(null); } simple.LineNumber = reader.LineNumber; simple.LinePosition = reader.LinePosition; simple.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { simple.Id = reader.Value; } else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h, reader.Name + " is not a valid attribute for simpleContent", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, simple); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(simple); } //Content: (annotation?, (restriction | extension)) int level = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchemaSimpleContent.Read, name=" + reader.Name, null); } break; } if (level <= 1 && reader.LocalName == "annotation") { level = 2; //Only one annotation XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { simple.Annotation = annotation; } continue; } if (level <= 2) { if (reader.LocalName == "restriction") { level = 3; XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader, h); if (restriction != null) { simple.content = restriction; } continue; } if (reader.LocalName == "extension") { level = 3; XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader, h); if (extension != null) { simple.content = extension; } continue; } } reader.RaiseInvalidElementError(); } return(simple); }
private void InferTextContent(Element el, bool isNew) { string value = source.ReadString(); if (el.SchemaType == null) { if (el.SchemaTypeName == QName.Empty) { // no type information -> infer type if (isNew) { el.SchemaTypeName = InferSimpleType( value); } else { el.SchemaTypeName = QNameString; } return; } switch (el.SchemaTypeName.Namespace) { case XmlSchema.Namespace: case XdtNamespace: // existing primitive type el.SchemaTypeName = InferMergedType( value, el.SchemaTypeName); break; default: ComplexType ct = schemas.GlobalTypes [ el.SchemaTypeName] as ComplexType; // If it is complex, then just set // mixed='true' (type cannot be set.) // If it is simple, then we cannot // make sure that string value is // valid. So just set as xs:string. if (ct != null) { MarkAsMixed(ct); } else { el.SchemaTypeName = QNameString; } break; } return; } // simpleType SimpleType st = el.SchemaType as SimpleType; if (st != null) { // If simple, then (described above) el.SchemaType = null; el.SchemaTypeName = QNameString; return; } // complexType ComplexType ect = el.SchemaType as ComplexType; SimpleModel sm = ect.ContentModel as SimpleModel; if (sm == null) { // - ComplexContent MarkAsMixed(ect); return; } // - SimpleContent SimpleExt se = sm.Content as SimpleExt; if (se != null) { se.BaseTypeName = InferMergedType(value, se.BaseTypeName); } SimpleRst sr = sm.Content as SimpleRst; if (sr != null) { sr.BaseTypeName = InferMergedType(value, sr.BaseTypeName); sr.BaseType = null; } }
private ComplexType ToComplexType(Element el) { QName name = el.SchemaTypeName; XmlSchemaType type = el.SchemaType; // 1. element type is complex. ComplexType ct = type as ComplexType; if (ct != null) { return(ct); } // 2. reference to global complexType. XmlSchemaType globalType = schemas.GlobalTypes [name] as XmlSchemaType; ct = globalType as ComplexType; if (ct != null) { return(ct); } ct = new ComplexType(); el.SchemaType = ct; el.SchemaTypeName = QName.Empty; // 3. base type name is xs:anyType or no specification. // <xs:complexType /> if (name == QNameAnyType) { return(ct); } else if (type == null && name == QName.Empty) { return(ct); } SimpleModel sc = new SimpleModel(); ct.ContentModel = sc; // 4. type is simpleType // -> extension of existing simple type. SimpleType st = type as SimpleType; if (st != null) { SimpleRst scr = new SimpleRst(); scr.BaseType = st; sc.Content = scr; return(ct); } SimpleExt sce = new SimpleExt(); sc.Content = sce; // 5. type name points to primitive type // -> simple extension of a primitive type st = XmlSchemaType.GetBuiltInSimpleType(name); if (st != null) { sce.BaseTypeName = name; return(ct); } // 6. type name points to global simpleType. st = globalType as SimpleType; if (st != null) { sce.BaseTypeName = name; return(ct); } throw Error(el, "Unexpected schema component that contains simpleTypeName that could not be resolved."); }