Пример #1
0
        /// <summary>
        /// Returns the attributes within a collection.  Most of the time, this will
        /// simply normalize the name and convert the structure into the resultant
        /// output type.
        /// </summary>
        /// <param name="xsModel">The xs model.</param>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        internal IEnumerable <SchemaItemAttribute> GetAttributes(
            XmlSchema xsModel,
            XmlSchemaObjectCollection attributes)
        {
            foreach (var attr in attributes.Cast <XmlSchemaAttribute>())
            {
                var name = attr.QualifiedName;
                if (name.IsEmpty)
                {
                    if (attr.Form == XmlSchemaForm.Qualified)
                    {
                        name = new XmlQualifiedName(attr.Name, xsModel.TargetNamespace);
                    }
                    else
                    {
                        name = new XmlQualifiedName(attr.Name, null);
                    }
                }

                var schemaType    = ResolveSchemaType(xsModel, attr.SchemaTypeName);
                var itemAttribute = new SchemaItemAttribute(
                    name.Namespace,
                    name.Name,
                    schemaType as XmlSchemaSimpleType,
                    attr.SchemaTypeName.Name);

                yield return(itemAttribute);
            }
        }
Пример #2
0
        private static Pair <String, XPathResultType> MakeAttributeProperty(SchemaItemAttribute attribute, Property property, XPathNamespaceContext ctx)
        {
            var prefix = ctx.LookupPrefix(attribute.Namespace);

            if (String.IsNullOrEmpty(prefix))
            {
                prefix = "";
            }
            else
            {
                prefix += ':';
            }

            if (IsAnySimpleProperty(property))
            {
                XPathResultType type = SchemaUtil.SimpleTypeToResultType(attribute.SimpleType);
                String          path = "/@" + prefix + property.PropertyNameAtomic;
                return(new Pair <String, XPathResultType>(path, type));
            }

            if (IsAnyMappedProperty(property))
            {
                throw new Exception("Mapped properties not applicable to attributes");
            }

            throw new Exception("Indexed properties not applicable to attributes");
        }
Пример #3
0
        /// <summary>
        /// Processes the complex element.
        /// </summary>
        /// <param name="xsModel">The schema model.</param>
        /// <param name="complexElementName">Name of the complex element.</param>
        /// <param name="complexElementNamespace">The complex element namespace.</param>
        /// <param name="complexActualElement">The complex actual element.</param>
        /// <param name="complexType">Type of the complex.</param>
        /// <param name="isArray">if set to <c>true</c> [is array].</param>
        /// <param name="nameNamespaceStack">The name namespace stack.</param>
        /// <param name="maxRecursiveDepth">The max recursive depth.</param>
        /// <returns></returns>
        private SchemaElementComplex ProcessComplexElement(XmlSchema xsModel,
                                                           String complexElementName,
                                                           String complexElementNamespace,
                                                           XmlSchemaElement complexActualElement,
                                                           XmlSchemaComplexType complexType,
                                                           bool isArray,
                                                           Stack <NamespaceNamePair> nameNamespaceStack,
                                                           int maxRecursiveDepth)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Processing complex {0} {1} stack {2}",
                          complexElementNamespace,
                          complexElementName,
                          nameNamespaceStack);
            }

            // Obtain the actual complex schema type
            //var complexType = (XmlSchemaComplexType)complexActualElement.SchemaType;
            // Represents the mapping of element attributes
            var attributes = new List <SchemaItemAttribute>();
            // Represents the mapping of child elements that are simple
            var simpleElements = new List <SchemaElementSimple>();
            // Represents the mapping of child elements that are complex
            var complexElements = new List <SchemaElementComplex>();
            // Represents the complex element - the above are encapsulated in the structure
            var complexElement = new SchemaElementComplex(
                complexElementName,
                complexElementNamespace,
                attributes,
                complexElements,
                simpleElements,
                isArray);

            // Map the schema attributes into internal form
            if (complexType != null)
            {
                var attrs = complexType.Attributes;
                foreach (var uattr in attrs)
                {
                    var attr = (XmlSchemaAttribute)uattr;
                    var name = attr.QualifiedName;
                    if (Equals(name, XmlQualifiedName.Empty))
                    {
                        name = new XmlQualifiedName(attr.Name, null);
                    }

                    var schemaType = ResolveSchemaType(xsModel, attr.SchemaTypeName);

                    var itemAttribute = new SchemaItemAttribute(
                        name.Namespace,
                        name.Name,
                        schemaType as XmlSchemaSimpleType,
                        attr.SchemaTypeName.Name);
                    attributes.Add(itemAttribute);
                }

                var contentModel = complexType.ContentModel;
                if (contentModel is XmlSchemaSimpleContent)
                {
                    XmlSchemaSimpleContent simpleContent = (XmlSchemaSimpleContent)contentModel;
                    if (simpleContent.Content is XmlSchemaSimpleContentExtension)
                    {
                        XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension)simpleContent.Content;
                        foreach (var eattr in extension.Attributes)
                        {
                            if (eattr is XmlSchemaAttribute)
                            {
                                XmlSchemaAttribute sattr  = (XmlSchemaAttribute)eattr;
                                XmlQualifiedName   sqname = sattr.QualifiedName;
                                if (Equals(sqname, XmlQualifiedName.Empty))
                                {
                                    sqname = new XmlQualifiedName(sattr.Name, xsModel.TargetNamespace);
                                }

                                XmlSchemaSimpleType simpleType    = ResolveSchemaType(xsModel, sattr.SchemaTypeName) as XmlSchemaSimpleType;
                                SchemaItemAttribute itemAttribute = new SchemaItemAttribute(
                                    sqname.Namespace,
                                    sqname.Name,
                                    simpleType,
                                    sattr.SchemaTypeName.Name);
                                attributes.Add(itemAttribute);
                            }
                            else if (eattr is XmlSchemaAttributeGroupRef)
                            {
                            }
                        }

                        XmlQualifiedName optionalSimpleTypeName = extension.BaseTypeName;
                        if (!Equals(optionalSimpleTypeName, XmlQualifiedName.Empty))
                        {
                            XmlSchemaSimpleType optionalSimpleType = XmlSchemaSimpleType.GetBuiltInSimpleType(optionalSimpleTypeName);
                            complexElement.OptionalSimpleType     = optionalSimpleType;
                            complexElement.OptionalSimpleTypeName = optionalSimpleTypeName;
                        }
                    }
                }

                var complexParticle = complexType.Particle;
                if (complexParticle is XmlSchemaGroupBase)
                {
                    XmlSchemaGroupBase particleGroup = (XmlSchemaGroupBase)complexParticle;
                    foreach (var artifact in particleGroup.Items)
                    {
                        XmlSchemaElement myComplexElement     = null;
                        XmlQualifiedName myComplexElementName = null;

                        if (artifact is XmlSchemaElement)
                        {
                            var schemaElement = (XmlSchemaElement)artifact;
                            var isArrayFlag   = IsArray(schemaElement);
                            var refName       = schemaElement.RefName;

                            // Resolve complex elements that are a child of the sequence.  Complex
                            // elements come in one of two forms... the first is through reference
                            // the second is a direct child.  Of course you can have simple types
                            // too.

                            if (Equals(refName, XmlQualifiedName.Empty))
                            {
                                var schemaTypeName = schemaElement.SchemaTypeName;
                                if (!Equals(schemaTypeName, XmlQualifiedName.Empty))
                                {
                                    var schemaType = ResolveSchemaType(xsModel, schemaTypeName);
                                    if (schemaType is XmlSchemaSimpleType)
                                    {
                                        var simpleElementName = schemaElement.QualifiedName;
                                        if (Equals(simpleElementName, XmlQualifiedName.Empty))
                                        {
                                            simpleElementName = new XmlQualifiedName(schemaElement.Name, xsModel.TargetNamespace);
                                        }

                                        var fractionDigits = GetFractionRestriction((XmlSchemaSimpleType)schemaType);
                                        var simpleElement  = new SchemaElementSimple(
                                            simpleElementName.Name,
                                            simpleElementName.Namespace,
                                            (XmlSchemaSimpleType)schemaType,
                                            schemaTypeName.Name,
                                            isArrayFlag,
                                            fractionDigits);
                                        simpleElements.Add(simpleElement);
                                    }
                                    else
                                    {
                                        myComplexElement            = schemaElement;
                                        myComplexElement.SchemaType = schemaType;
                                        myComplexElementName        = schemaElement.QualifiedName;
                                        if (Equals(myComplexElementName, XmlQualifiedName.Empty))
                                        {
                                            myComplexElementName = new XmlQualifiedName(
                                                myComplexElement.Name, xsModel.TargetNamespace);
                                        }
                                    }
                                }
                                else
                                {
                                    myComplexElement     = schemaElement;
                                    myComplexElementName =
                                        !Equals(schemaElement.QualifiedName, XmlQualifiedName.Empty)
                                            ? schemaElement.QualifiedName
                                            : new XmlQualifiedName(schemaElement.Name, xsModel.TargetNamespace);
                                }
                            }
                            else
                            {
                                myComplexElement     = ResolveElement(refName);
                                myComplexElementName = refName;
                                if (myComplexElementName.Namespace == null)
                                {
                                    myComplexElementName = new XmlQualifiedName(refName.Name, xsModel.TargetNamespace);
                                }
                            }

                            if (myComplexElement != null)
                            {
                                if (myComplexElement.SchemaType == null)
                                {
                                    if (!Equals(myComplexElement.SchemaTypeName, XmlQualifiedName.Empty))
                                    {
                                        myComplexElement.SchemaType = ResolveSchemaType(xsModel, myComplexElement.SchemaTypeName);
                                    }
                                }

                                if (myComplexElement.SchemaType is XmlSchemaSimpleType)
                                {
                                    XmlSchemaSimpleType simpleSchemaType = (XmlSchemaSimpleType)myComplexElement.SchemaType;
                                    SchemaElementSimple innerSimple      =
                                        new SchemaElementSimple(
                                            myComplexElementName.Name,
                                            myComplexElementName.Namespace,
                                            simpleSchemaType,
                                            simpleSchemaType.Name,
                                            isArrayFlag,
                                            GetFractionRestriction(simpleSchemaType));

                                    if (Log.IsDebugEnabled)
                                    {
                                        Log.Debug("Adding simple " + innerSimple);
                                    }

                                    simpleElements.Add(innerSimple);
                                }
                                else
                                {
                                    // Reference to complex type
                                    NamespaceNamePair nameNamespace = new NamespaceNamePair(
                                        myComplexElementName.Namespace,
                                        myComplexElementName.Name);
                                    nameNamespaceStack.Push(nameNamespace);

                                    // if the stack contains
                                    if (maxRecursiveDepth != Int32.MaxValue)
                                    {
                                        int containsCount = 0;
                                        foreach (NamespaceNamePair pair in nameNamespaceStack)
                                        {
                                            if (Equals(nameNamespace, pair))
                                            {
                                                containsCount++;
                                            }
                                        }

                                        if (containsCount >= maxRecursiveDepth)
                                        {
                                            continue;
                                        }
                                    }

                                    SchemaElementComplex innerComplex = ProcessComplexElement(
                                        xsModel,
                                        myComplexElementName.Name,
                                        myComplexElementName.Namespace,
                                        myComplexElement,
                                        (XmlSchemaComplexType)myComplexElement.SchemaType,
                                        isArrayFlag,
                                        nameNamespaceStack,
                                        maxRecursiveDepth);

                                    nameNamespaceStack.Pop();

                                    if (Log.IsDebugEnabled)
                                    {
                                        Log.Debug("Adding complex " + complexElement);
                                    }

                                    complexElements.Add(innerComplex);
                                }
                            }
                        }
                    }
                }
                else if (complexParticle is XmlSchemaGroupRef)
                {
                    var groupRefParticle = (XmlSchemaGroupRef)complexParticle;
                }
            }

            return(complexElement);
        }