Exemple #1
0
        private Attr GetGlobalAttribute(QName name)
        {
            Attr a = newElements [name] as Attr;

            if (a == null)
            {
                a = schemas.GlobalAttributes [name] as Attr;
            }
            return(a);
        }
Exemple #2
0
        private Attr CreateGlobalAttribute(QName name)
        {
            Attr      attr   = new Attr();
            XmlSchema schema = PopulateSchema(name.Namespace);

            attr.Name = name.Name;
            schema.Items.Add(attr);
            newAttributes.Add(name, attr);
            return(attr);
        }
Exemple #3
0
 private int FindDefaultAttribute(string localName, string ns)
 {
     for (int i = 0; i < this.defaultAttributes.Length; i++)
     {
         XsAttr attr = defaultAttributes [i];
         if (attr.QualifiedName.Name == localName &&
             (ns == null || attr.QualifiedName.Namespace == ns))
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #4
0
        private XmlSchemaAttribute InferNewAttribute(
            QName attrName, bool isNewTypeDefinition, string ns)
        {
            Attr attr           = null;
            bool mergedRequired = false;

            if (attrName.Namespace.Length > 0)
            {
                // global attribute; might be already defined.
                attr = GetGlobalAttribute(attrName) as Attr;
                if (attr == null)
                {
                    attr = CreateGlobalAttribute(attrName);
                    attr.SchemaTypeName =
                        InferSimpleType(source.Value);
                }
                else
                {
                    InferMergedAttribute(attr);
                    mergedRequired =
                        attr.Use == Use.Required;
                }
                attr         = new Attr();
                attr.RefName = attrName;
                AddImport(ns, attrName.Namespace);
            }
            else
            {
                // local attribute
                attr                = new Attr();
                attr.Name           = attrName.Name;
                attr.SchemaTypeName =
                    InferSimpleType(source.Value);
            }
            if (!laxOccurrence &&
                (isNewTypeDefinition || mergedRequired))
            {
                attr.Use = Use.Required;
            }
            else
            {
                attr.Use = Use.Optional;
            }

            return(attr);
        }
Exemple #5
0
        private Hashtable CollectAttrTable(SOMList attList)
        {
            // get attribute definition table.
            Hashtable table = new Hashtable();

            foreach (XmlSchemaObject obj in attList)
            {
                Attr attr = obj as Attr;
                if (attr == null)
                {
                    throw Error(obj, String.Format("Attribute inference only supports direct attribute definition. {0} is not supported.", obj.GetType()));
                }
                if (attr.RefName != QName.Empty)
                {
                    table.Add(attr.RefName, attr);
                }
                else
                {
                    table.Add(new QName(attr.Name, ""),
                              attr);
                }
            }
            return(table);
        }
Exemple #6
0
        public static int ValidateAttributesResolved(
            XmlSchemaObjectTable attributesResolved,
            ValidationEventHandler h,
            XmlSchema schema,
            XmlSchemaObjectCollection attributes,
            XmlSchemaAnyAttribute anyAttribute,
            ref XmlSchemaAnyAttribute anyAttributeUse,
            XmlSchemaAttributeGroup redefined,
            bool skipEquivalent)
        {
            int errorCount = 0;

            if (anyAttribute != null && anyAttributeUse == null)
            {
                anyAttributeUse = anyAttribute;
            }

            ArrayList newAttrNames = new ArrayList();

            foreach (XmlSchemaObject xsobj in attributes)
            {
                XmlSchemaAttributeGroupRef grpRef = xsobj as XmlSchemaAttributeGroupRef;
                if (grpRef != null)
                {
                    // Resolve attributeGroup redefinition.
                    XmlSchemaAttributeGroup grp = null;
                    if (redefined != null && grpRef.RefName == redefined.QualifiedName)
                    {
                        grp = redefined;
                    }
                    else
                    {
                        grp = schema.FindAttributeGroup(grpRef.RefName);
                    }
                    // otherwise, it might be missing sub components.
                    if (grp == null)
                    {
                        if (!schema.missedSubComponents)                        // && schema.Schemas [grpRef.RefName.Namespace] != null)
                        {
                            grpRef.error(h, "Referenced attribute group " + grpRef.RefName + " was not found in the corresponding schema.");
                        }
                        continue;
                    }
                    if (grp.AttributeGroupRecursionCheck)
                    {
                        grp.error(h, "Attribute group recursion was found: " + grpRef.RefName);
                        continue;
                    }
                    try {
                        grp.AttributeGroupRecursionCheck = true;
                        errorCount += grp.Validate(h, schema);
                    } finally {
                        grp.AttributeGroupRecursionCheck = false;
                    }
                    if (grp.AnyAttributeUse != null)
                    {
                        if (anyAttribute == null)
                        {
                            anyAttributeUse = grp.AnyAttributeUse;
                        }
                    }
                    foreach (DictionaryEntry entry in grp.AttributeUses)
                    {
                        XmlSchemaAttribute attr = (XmlSchemaAttribute)entry.Value;

                        if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
                        {
                            continue;
                        }

                        if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
                        {
                            AddToTable(attributesResolved, attr, attr.RefName, h);
                        }
                        else if (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
                        {
                            AddToTable(attributesResolved, attr, attr.QualifiedName, h);
                        }
                    }
                }
                else
                {
                    XmlSchemaAttribute attr = xsobj as XmlSchemaAttribute;
                    if (attr != null)
                    {
                        errorCount += attr.Validate(h, schema);

                        if (newAttrNames.Contains(attr.QualifiedName))
                        {
                            attr.error(h, String.Format("Duplicate attributes was found for '{0}'", attr.QualifiedName));
                        }
                        newAttrNames.Add(attr.QualifiedName);


                        if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
                        {
                            continue;
                        }

                        if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
                        {
                            AddToTable(attributesResolved, attr, attr.RefName, h);
                        }
                        else if (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
                        {
                            AddToTable(attributesResolved, attr, attr.QualifiedName, h);
                        }
                    }
                    else
                    {
                        if (anyAttribute != null)
                        {
                            anyAttributeUse = (XmlSchemaAnyAttribute)xsobj;
                            anyAttribute.Validate(h, schema);
                        }
                    }
                }
            }
            return(errorCount);
        }
Exemple #7
0
        private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
        {
            reader.MoveToElement();
            if (reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
            {
                error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI, null);
            }

            //(include | import | redefine | annotation)*,
            //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchema.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (level <= 1)
                {
                    if (reader.LocalName == "include")
                    {
                        XmlSchemaInclude include = XmlSchemaInclude.Read(reader, h);
                        if (include != null)
                        {
                            schema.includes.Add(include);
                        }
                        continue;
                    }
                    if (reader.LocalName == "import")
                    {
                        XmlSchemaImport import = XmlSchemaImport.Read(reader, h);
                        if (import != null)
                        {
                            schema.includes.Add(import);
                        }
                        continue;
                    }
                    if (reader.LocalName == "redefine")
                    {
                        XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader, h);
                        if (redefine != null)
                        {
                            schema.includes.Add(redefine);
                        }
                        continue;
                    }
                    if (reader.LocalName == "annotation")
                    {
                        XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                        if (annotation != null)
                        {
                            schema.items.Add(annotation);
                        }
                        continue;
                    }
                }
                if (level <= 2)
                {
                    level = 2;
                    if (reader.LocalName == "simpleType")
                    {
                        XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader, h);
                        if (stype != null)
                        {
                            schema.items.Add(stype);
                        }
                        continue;
                    }
                    if (reader.LocalName == "complexType")
                    {
                        XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader, h);
                        if (ctype != null)
                        {
                            schema.items.Add(ctype);
                        }
                        continue;
                    }
                    if (reader.LocalName == "group")
                    {
                        XmlSchemaGroup group = XmlSchemaGroup.Read(reader, h);
                        if (group != null)
                        {
                            schema.items.Add(group);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attributeGroup")
                    {
                        XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader, h);
                        if (attributeGroup != null)
                        {
                            schema.items.Add(attributeGroup);
                        }
                        continue;
                    }
                    if (reader.LocalName == "element")
                    {
                        XmlSchemaElement element = XmlSchemaElement.Read(reader, h);
                        if (element != null)
                        {
                            schema.items.Add(element);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attribute")
                    {
                        XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h);
                        if (attr != null)
                        {
                            schema.items.Add(attr);
                        }
                        continue;
                    }
                    if (reader.LocalName == "notation")
                    {
                        XmlSchemaNotation notation = XmlSchemaNotation.Read(reader, h);
                        if (notation != null)
                        {
                            schema.items.Add(notation);
                        }
                        continue;
                    }
                    if (reader.LocalName == "annotation")
                    {
                        XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                        if (annotation != null)
                        {
                            schema.items.Add(annotation);
                        }
                        continue;
                    }
                }
                reader.RaiseInvalidElementError();
            }
        }
Exemple #8
0
        void DoCompile(ValidationEventHandler handler, List <CompiledSchemaMemo> handledUris, XmlSchemaSet col, XmlResolver resolver)
        {
            SetParent();
            CompilationId = col.CompilationId;
            schemas       = col;
            if (!schemas.Contains(this))              // e.g. xs:import
            {
                schemas.Add(this);
            }

            attributeGroups.Clear();
            attributes.Clear();
            elements.Clear();
            groups.Clear();
            notations.Clear();
            schemaTypes.Clear();
            named_identities.Clear();
            ids.Clear();
            compilationItems.Clear();

            //1. Union and List are not allowed in block default
            if (BlockDefault != XmlSchemaDerivationMethod.All)
            {
                if ((BlockDefault & XmlSchemaDerivationMethod.List) != 0)
                {
                    error(handler, "list is not allowed in blockDefault attribute");
                }
                if ((BlockDefault & XmlSchemaDerivationMethod.Union) != 0)
                {
                    error(handler, "union is not allowed in blockDefault attribute");
                }
            }

            //2. Substitution is not allowed in finaldefault.
            if (FinalDefault != XmlSchemaDerivationMethod.All)
            {
                if ((FinalDefault & XmlSchemaDerivationMethod.Substitution) != 0)
                {
                    error(handler, "substitution is not allowed in finalDefault attribute");
                }
            }

            //3. id must be of type ID
            XmlSchemaUtil.CompileID(Id, this, IDCollection, handler);

            //4. targetNamespace should be of type anyURI or absent
            if (TargetNamespace != null)
            {
                if (TargetNamespace.Length == 0)
                {
                    error(handler, "The targetNamespace attribute cannot have have empty string as its value.");
                }

                if (!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
                {
                    error(handler, TargetNamespace + " is not a valid value for targetNamespace attribute of schema");
                }
            }

            //5. version should be of type normalizedString
            if (!XmlSchemaUtil.CheckNormalizedString(Version))
            {
                error(handler, Version + "is not a valid value for version attribute of schema");
            }

            // Compile the content of this schema

            for (int i = 0; i < Items.Count; i++)
            {
                compilationItems.Add(Items [i]);
            }

            // First, we run into inclusion schemas to collect
            // compilation target items into compiledItems.
            for (int i = 0; i < Includes.Count; i++)
            {
                ProcessExternal(handler, handledUris, resolver, Includes [i] as XmlSchemaExternal, col);
            }

            // Compilation phase.
            // At least each Compile() must give unique (qualified) name for each component.
            // It also checks self-resolvable properties correctness.
            // Post compilation schema information contribution is not done here.
            // It should be done by Validate().
            for (int i = 0; i < compilationItems.Count; i++)
            {
                XmlSchemaObject obj = compilationItems [i];
                if (obj is XmlSchemaAnnotation)
                {
                    int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
                    errorCount += numerr;
                }
                else if (obj is XmlSchemaAttribute)
                {
                    XmlSchemaAttribute attr = (XmlSchemaAttribute)obj;
                    int numerr = attr.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(Attributes, attr, attr.QualifiedName, handler);
                    }
                }
                else if (obj is XmlSchemaAttributeGroup)
                {
                    XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup)obj;
                    int numerr = attrgrp.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            AttributeGroups,
                            attrgrp,
                            attrgrp.QualifiedName,
                            handler);
                    }
                }
                else if (obj is XmlSchemaComplexType)
                {
                    XmlSchemaComplexType ctype = (XmlSchemaComplexType)obj;
                    int numerr = ctype.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            schemaTypes,
                            ctype,
                            ctype.QualifiedName,
                            handler);
                    }
                }
                else if (obj is XmlSchemaSimpleType)
                {
                    XmlSchemaSimpleType stype = (XmlSchemaSimpleType)obj;
                    stype.islocal = false;                     //This simple type is toplevel
                    int numerr = stype.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            SchemaTypes,
                            stype,
                            stype.QualifiedName,
                            handler);
                    }
                }
                else if (obj is XmlSchemaElement)
                {
                    XmlSchemaElement elem = (XmlSchemaElement)obj;
                    elem.parentIsSchema = true;
                    int numerr = elem.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            Elements,
                            elem,
                            elem.QualifiedName,
                            handler);
                    }
                }
                else if (obj is XmlSchemaGroup)
                {
                    XmlSchemaGroup grp    = (XmlSchemaGroup)obj;
                    int            numerr = grp.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            Groups,
                            grp,
                            grp.QualifiedName,
                            handler);
                    }
                }
                else if (obj is XmlSchemaNotation)
                {
                    XmlSchemaNotation ntn = (XmlSchemaNotation)obj;
                    int numerr            = ntn.Compile(handler, this);
                    errorCount += numerr;
                    if (numerr == 0)
                    {
                        XmlSchemaUtil.AddToTable(
                            Notations,
                            ntn,
                            ntn.QualifiedName,
                            handler);
                    }
                }
                else
                {
                    ValidationHandler.RaiseValidationEvent(
                        handler,
                        null,
                        String.Format("Object of Type {0} is not valid in Item Property of Schema", obj.GetType().Name),
                        null,
                        this,
                        null,
                        XmlSeverityType.Error);
                }
            }
        }
Exemple #9
0
        //<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);
        }
Exemple #10
0
        /// <remarks>
        /// </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 (this.isRedefinedComponent)
            {
                if (Annotation != null)
                {
                    Annotation.isRedefinedComponent = true;
                }
                if (AnyAttribute != null)
                {
                    AnyAttribute.isRedefinedComponent = true;
                }
                foreach (XmlSchemaObject obj in Attributes)
                {
                    obj.isRedefinedComponent = true;
                }
                if (Particle != null)
                {
                    Particle.isRedefinedComponent = true;
                }
            }

            if (BaseTypeName == null || BaseTypeName.IsEmpty)
            {
                error(h, "base must be present, as a QName");
            }
            else if (!XmlSchemaUtil.CheckQName(BaseTypeName))
            {
                error(h, "BaseTypeName is not a valid XmlQualifiedName");
            }

            if (this.AnyAttribute != null)
            {
                errorCount += AnyAttribute.Compile(h, schema);
            }

            foreach (XmlSchemaObject obj in Attributes)
            {
                if (obj is XmlSchemaAttribute)
                {
                    XmlSchemaAttribute attr = (XmlSchemaAttribute)obj;
                    errorCount += attr.Compile(h, schema);
                }
                else if (obj is XmlSchemaAttributeGroupRef)
                {
                    XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef)obj;
                    errorCount += atgrp.Compile(h, schema);
                }
                else
                {
                    error(h, obj.GetType() + " is not valid in this place::ComplexConetnetExtension");
                }
            }

            if (Particle != null)
            {
                if (Particle is XmlSchemaGroupRef)
                {
                    errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
                }
                else if (Particle is XmlSchemaAll)
                {
                    errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
                }
                else if (Particle is XmlSchemaChoice)
                {
                    errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
                }
                else if (Particle is XmlSchemaSequence)
                {
                    errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
                }
                else
                {
                    error(h, "Particle of a restriction is limited only to group, sequence, choice and all.");
                }
            }

            XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);

            this.CompilationId = schema.CompilationId;
            return(errorCount);
        }
Exemple #11
0
        /// <summary>
        /// Schema Component:
        ///			QName, SimpleType, Scope, Default|Fixed, annotation
        /// </summary>
        internal override int Validate(ValidationEventHandler h, XmlSchema schema)
        {
            if (IsValidated(schema.ValidationId))
            {
                return(errorCount);
            }

            // -- Attribute Declaration Schema Component --
            // {name}, {target namespace} -> QualifiedName. Already Compile()d.
            // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
            // {scope} -> ParentIsSchema | isRedefineChild.
            // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
            // {annotation}
            // -- Attribute Use Schema Component --
            // {required}
            // {attribute declaration}
            // {value constraint}

            // First, fill type information for type reference
            if (SchemaType != null)
            {
                SchemaType.Validate(h, schema);
                attributeType = SchemaType;
            }
            else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
            {
                // If type is null, then it is missing sub components .
                XmlSchemaType type = schema.FindSchemaType(SchemaTypeName);
                if (type is XmlSchemaComplexType)
                {
                    error(h, "An attribute can't have complexType Content");
                }
                else if (type != null)                          // simple type
                {
                    errorCount   += type.Validate(h, schema);
                    attributeType = type;
                }
                else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
                {
                    attributeType = XmlSchemaComplexType.AnyType;
                }
                else if (XmlSchemaUtil.IsBuiltInDatatypeName(SchemaTypeName))
                {
                    attributeType = XmlSchemaDatatype.FromName(SchemaTypeName);
                    if (attributeType == null)
                    {
                        error(h, "Invalid xml schema namespace datatype was specified.");
                    }
                }
                // otherwise, it might be missing sub components.
                else if (!schema.IsNamespaceAbsent(SchemaTypeName.Namespace))
                {
                    error(h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
                }
            }

            // Then, fill type information for the type references for the referencing attributes
            if (RefName != null && RefName != XmlQualifiedName.Empty)
            {
                referencedAttribute = schema.FindAttribute(RefName);
                // If el is null, then it is missing sub components .
                if (referencedAttribute != null)
                {
                    errorCount += referencedAttribute.Validate(h, schema);
                }
                // otherwise, it might be missing sub components.
                else if (!schema.IsNamespaceAbsent(RefName.Namespace))
                {
                    error(h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
                }
            }

            if (attributeType == null)
            {
                attributeType = XmlSchemaSimpleType.AnySimpleType;
            }

            // Validate {value constraints}
            if (defaultValue != null || fixedValue != null)
            {
                XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
                if (datatype == null)
                {
                    datatype = ((XmlSchemaSimpleType)attributeType).Datatype;
                }
                if (datatype.TokenizedType == XmlTokenizedType.QName)
                {
                    error(h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
                }
                else
                {
                    try {
                        if (defaultValue != null)
                        {
                            validatedDefaultValue = datatype.Normalize(defaultValue);
                            datatype.ParseValue(validatedDefaultValue, null, null);
                        }
                    } catch (Exception ex) {
                        // FIXME: This is not a good way to handle exception.
                        error(h, "The Attribute's default value is invalid with its type definition.", ex);
                    }
                    try {
                        if (fixedValue != null)
                        {
                            validatedFixedValue      = datatype.Normalize(fixedValue);
                            validatedFixedTypedValue = datatype.ParseValue(validatedFixedValue, null, null);
                        }
                    } catch (Exception ex) {
                        // FIXME: This is not a good way to handle exception.
                        error(h, "The Attribute's fixed value is invalid with its type definition.", ex);
                    }
                }
            }
            if (Use == XmlSchemaUse.None)
            {
                validatedUse = XmlSchemaUse.Optional;
            }
            else
            {
                validatedUse = Use;
            }

#if NET_2_0
            if (attributeType != null)
            {
                attributeSchemaType = attributeType as XmlSchemaSimpleType;
                if (attributeType == XmlSchemaSimpleType.AnySimpleType)
                {
                    attributeSchemaType = XmlSchemaSimpleType.XsAnySimpleType;
                }
                if (attributeSchemaType == null)
                {
                    attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType(SchemaTypeName);
                }
            }
#endif

            ValidationId = schema.ValidationId;
            return(errorCount);
        }
        //<restriction
        //  base = QName
        //  id = ID
        //  {any attributes with non-schema namespace . . .}>
        //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
        //</restriction>
        internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();

            reader.MoveToElement();

            if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
            {
                error(h, "Should not happen :1: XmlSchemaComplexContentRestriction.Read, name=" + reader.Name, null);
                reader.Skip();
                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.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchemaComplexContentRestriction.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)
                {
                    if (reader.LocalName == "group")
                    {
                        level = 3;
                        XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader, h);
                        if (group != null)
                        {
                            restriction.particle = group;
                        }
                        continue;
                    }
                    if (reader.LocalName == "all")
                    {
                        level = 3;
                        XmlSchemaAll all = XmlSchemaAll.Read(reader, h);
                        if (all != null)
                        {
                            restriction.particle = all;
                        }
                        continue;
                    }
                    if (reader.LocalName == "choice")
                    {
                        level = 3;
                        XmlSchemaChoice choice = XmlSchemaChoice.Read(reader, h);
                        if (choice != null)
                        {
                            restriction.particle = choice;
                        }
                        continue;
                    }
                    if (reader.LocalName == "sequence")
                    {
                        level = 3;
                        XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader, h);
                        if (sequence != null)
                        {
                            restriction.particle = sequence;
                        }
                        continue;
                    }
                }
                if (level <= 3)
                {
                    if (reader.LocalName == "attribute")
                    {
                        level = 3;
                        XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h);
                        if (attr != null)
                        {
                            restriction.Attributes.Add(attr);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attributeGroup")
                    {
                        level = 3;
                        XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader, h);
                        if (attr != null)
                        {
                            restriction.attributes.Add(attr);
                        }
                        continue;
                    }
                }
                if (level <= 4 && reader.LocalName == "anyAttribute")
                {
                    level = 5;
                    XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader, h);
                    if (anyattr != null)
                    {
                        restriction.AnyAttribute = anyattr;
                    }
                    continue;
                }
                reader.RaiseInvalidElementError();
            }
            return(restriction);
        }
Exemple #13
0
 // validate string value agains attr and
 // if invalid, then relax the type.
 private void InferMergedAttribute(Attr attr)
 {
     attr.SchemaTypeName = InferMergedType(source.Value,
                                           attr.SchemaTypeName);
     attr.SchemaType = null;
 }
Exemple #14
0
        private void InferAttributes(Element el, string ns, bool isNew)
        {
            // Now this element is going to have complexType.
            // It currently not, then we have to replace it.
            ComplexType ct      = null;
            SOMList     attList = null;
            Hashtable   table   = null;

            do
            {
                switch (source.NamespaceURI)
                {
                case NamespaceXml:
                    if (schemas.Schemas(
                            NamespaceXml).Count == 0)
                    {
                        IncludeXmlAttributes();
                    }
                    break;

                case XmlSchema.InstanceNamespace:
                    if (source.LocalName == "nil")
                    {
                        el.IsNillable = true;
                    }
                    // all other xsi:* atts are ignored
                    continue;

                case NamespaceXmlns:
                    continue;
                }
                if (ct == null)
                {
                    ct      = ToComplexType(el);
                    attList = GetAttributes(ct);
                    table   = CollectAttrTable(attList);
                }
                QName attrName = new QName(
                    source.LocalName, source.NamespaceURI);
                Attr attr = table [attrName] as Attr;
                if (attr == null)
                {
                    attList.Add(InferNewAttribute(
                                    attrName, isNew, ns));
                }
                else
                {
                    table.Remove(attrName);
                    if (attr.RefName != null &&
                        attr.RefName != QName.Empty)
                    {
                        continue;                         // just a reference
                    }
                    InferMergedAttribute(attr);
                }
            } while (source.MoveToNextAttribute());

            // mark all attr definitions that did not appear
            // as optional.
            if (table != null)
            {
                foreach (Attr attr in table.Values)
                {
                    attr.Use = Use.Optional;
                }
            }
        }
        //<extension
        //base = QName
        //id = ID
        //{any attributes with non-schema namespace . . .}>
        //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
        //</extension>
        internal static XmlSchemaSimpleContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();

            reader.MoveToElement();

            if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
            {
                error(h, "Should not happen :1: XmlSchemaAttributeGroup.Read, name=" + reader.Name, null);
                reader.Skip();
                return(null);
            }

            extension.LineNumber   = reader.LineNumber;
            extension.LinePosition = reader.LinePosition;
            extension.SourceUri    = reader.BaseURI;

            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "base")
                {
                    Exception innerex;
                    extension.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")
                {
                    extension.Id = reader.Value;
                }
                else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
                {
                    error(h, reader.Name + " is not a valid attribute for extension in this context", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, extension);
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(extension);
            }

            //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchemaSimpleContentExtension.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)
                    {
                        extension.Annotation = annotation;
                    }
                    continue;
                }
                if (level <= 2)
                {
                    if (reader.LocalName == "attribute")
                    {
                        level = 2;
                        XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h);
                        if (attr != null)
                        {
                            extension.Attributes.Add(attr);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attributeGroup")
                    {
                        level = 2;
                        XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader, h);
                        if (attr != null)
                        {
                            extension.attributes.Add(attr);
                        }
                        continue;
                    }
                }
                if (level <= 3 && reader.LocalName == "anyAttribute")
                {
                    level = 4;
                    XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader, h);
                    if (anyattr != null)
                    {
                        extension.AnyAttribute = anyattr;
                    }
                    continue;
                }
                reader.RaiseInvalidElementError();
            }
            return(extension);
        }
        ///<remarks>
        /// 1. Base must be present and a QName
        ///</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 (this.isRedefinedComponent)
            {
                if (Annotation != null)
                {
                    Annotation.isRedefinedComponent = true;
                }
                if (AnyAttribute != null)
                {
                    AnyAttribute.isRedefinedComponent = true;
                }
                foreach (XmlSchemaObject obj in Attributes)
                {
                    obj.isRedefinedComponent = true;
                }
            }

            if (BaseTypeName == null || BaseTypeName.IsEmpty)
            {
                error(h, "base must be present, as a QName");
            }
            else if (!XmlSchemaUtil.CheckQName(BaseTypeName))
            {
                error(h, "BaseTypeName must be a QName");
            }

            if (this.AnyAttribute != null)
            {
                errorCount += AnyAttribute.Compile(h, schema);
            }

            foreach (XmlSchemaObject obj in Attributes)
            {
                if (obj is XmlSchemaAttribute)
                {
                    XmlSchemaAttribute attr = (XmlSchemaAttribute)obj;
                    errorCount += attr.Compile(h, schema);
                }
                else if (obj is XmlSchemaAttributeGroupRef)
                {
                    XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef)obj;
                    errorCount += atgrp.Compile(h, schema);
                }
                else
                {
                    error(h, obj.GetType() + " is not valid in this place::SimpleConentExtension");
                }
            }

            XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);

            this.CompilationId = schema.CompilationId;
            return(errorCount);
        }
Exemple #17
0
        //<attribute
        //  default = string
        //  fixed = string
        //  form = (qualified | unqualified)
        //  id = ID
        //  name = NCName
        //  ref = QName
        //  type = QName
        //  use = (optional | prohibited | required) : optional
        //  {any attributes with non-schema namespace . . .}>
        //  Content: (annotation?, (simpleType?))
        //</attribute>
        internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaAttribute attribute = new XmlSchemaAttribute();

            reader.MoveToElement();

            if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
            {
                error(h, "Should not happen :1: XmlSchemaAttribute.Read, name=" + reader.Name, null);
                reader.SkipToEnd();
                return(null);
            }

            attribute.LineNumber   = reader.LineNumber;
            attribute.LinePosition = reader.LinePosition;
            attribute.SourceUri    = reader.BaseURI;

            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "default")
                {
                    attribute.defaultValue = reader.Value;
                }
                else if (reader.Name == "fixed")
                {
                    attribute.fixedValue = reader.Value;
                }
                else if (reader.Name == "form")
                {
                    Exception innerex;
                    attribute.form = XmlSchemaUtil.ReadFormAttribute(reader, out innerex);
                    if (innerex != null)
                    {
                        error(h, reader.Value + " is not a valid value for form attribute", innerex);
                    }
                }
                else if (reader.Name == "id")
                {
                    attribute.Id = reader.Value;
                }
                else if (reader.Name == "name")
                {
                    attribute.name = reader.Value;
                }
                else if (reader.Name == "ref")
                {
                    Exception innerex;
                    attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader, out innerex);
                    if (innerex != null)
                    {
                        error(h, reader.Value + " is not a valid value for ref attribute", innerex);
                    }
                }
                else if (reader.Name == "type")
                {
                    Exception innerex;
                    attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader, out innerex);
                    if (innerex != null)
                    {
                        error(h, reader.Value + " is not a valid value for type attribute", innerex);
                    }
                }
                else if (reader.Name == "use")
                {
                    Exception innerex;
                    attribute.use = XmlSchemaUtil.ReadUseAttribute(reader, out innerex);
                    if (innerex != null)
                    {
                        error(h, reader.Value + " is not a valid value for use attribute", innerex);
                    }
                }
                else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
                {
                    error(h, reader.Name + " is not a valid attribute for attribute", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, attribute);
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(attribute);
            }

            //  Content: (annotation?, (simpleType?))
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchemaAttribute.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)
                    {
                        attribute.Annotation = annotation;
                    }
                    continue;
                }
                if (level <= 2 && reader.LocalName == "simpleType")
                {
                    level = 3;
                    XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader, h);
                    if (stype != null)
                    {
                        attribute.schemaType = stype;
                    }
                    continue;
                }
                reader.RaiseInvalidElementError();
            }
            return(attribute);
        }