Exemple #1
0
        public void TestMap()
        {
            Uri    uri       = ResourceManager.ResolveResourceURL("regression/simpleSchema.xsd");
            String schemaUri = uri.ToString();

            SchemaModel model = XSDSchemaMapper.LoadAndMap(schemaUri, null);

            Assert.AreEqual(1, model.Components.Count);

            SchemaElementComplex component = model.Components[0];

            Assert.AreEqual("simpleEvent", component.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", component.Namespace);
            Assert.AreEqual(0, component.Attributes.Count);
            Assert.AreEqual(0, component.SimpleElements.Count);
            Assert.AreEqual(3, component.ComplexElements.Count);
            Assert.IsFalse(component.IsArray);
            Assert.IsNull(component.OptionalSimpleType);

            SchemaElementComplex nested1Element = component.ComplexElements[0];

            Assert.AreEqual("nested1", nested1Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", nested1Element.Namespace);
            Assert.AreEqual(1, nested1Element.Attributes.Count);
            Assert.AreEqual(2, nested1Element.SimpleElements.Count);
            Assert.AreEqual(1, nested1Element.ComplexElements.Count);
            Assert.IsFalse(nested1Element.IsArray);
            Assert.IsNull(nested1Element.OptionalSimpleType);

            var schemaTypeString  = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String);
            var schemaTypeBoolean = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean);
            var schemaTypeInteger = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int);
            var schemaTypeId      = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Id);

            Assert.AreEqual("attr1", nested1Element.Attributes[0].Name);
            Assert.AreEqual(String.Empty, nested1Element.Attributes[0].Namespace);
            Assert.AreEqual(schemaTypeString, nested1Element.Attributes[0].SimpleType);
            Assert.AreEqual("prop1", nested1Element.SimpleElements[0].Name);
            Assert.AreEqual(schemaTypeString, nested1Element.SimpleElements[0].SimpleType);
            Assert.AreEqual("prop2", nested1Element.SimpleElements[1].Name);
            Assert.AreEqual(schemaTypeBoolean, nested1Element.SimpleElements[1].SimpleType);

            SchemaElementComplex nested2Element = nested1Element.ComplexElements[0];

            Assert.AreEqual("nested2", nested2Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", nested2Element.Namespace);
            Assert.AreEqual(0, nested2Element.Attributes.Count);
            Assert.AreEqual(1, nested2Element.SimpleElements.Count);
            Assert.AreEqual(0, nested2Element.ComplexElements.Count);
            Assert.IsFalse(nested2Element.IsArray);
            Assert.IsNull(nested2Element.OptionalSimpleType);

            SchemaElementSimple simpleProp3 = nested2Element.SimpleElements[0];

            Assert.AreEqual("prop3", simpleProp3.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", simpleProp3.Namespace);
            Assert.AreEqual(schemaTypeInteger, simpleProp3.SimpleType);
            Assert.IsTrue(simpleProp3.IsArray);

            SchemaElementComplex prop4Element = component.ComplexElements[1];

            Assert.AreEqual("prop4", prop4Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", prop4Element.Namespace);
            Assert.AreEqual(1, prop4Element.Attributes.Count);
            Assert.AreEqual(0, prop4Element.SimpleElements.Count);
            Assert.AreEqual(0, prop4Element.ComplexElements.Count);
            Assert.AreEqual("attr2", prop4Element.Attributes[0].Name);
            Assert.AreEqual(schemaTypeBoolean, prop4Element.Attributes[0].SimpleType);
            Assert.IsFalse(prop4Element.IsArray);
            Assert.AreEqual(schemaTypeString, prop4Element.OptionalSimpleType);

            SchemaElementComplex nested3Element = component.ComplexElements[2];

            Assert.AreEqual("nested3", nested3Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", nested3Element.Namespace);
            Assert.AreEqual(0, nested3Element.Attributes.Count);
            Assert.AreEqual(0, nested3Element.SimpleElements.Count);
            Assert.AreEqual(1, nested3Element.ComplexElements.Count);
            Assert.IsFalse(nested3Element.IsArray);
            Assert.IsNull(nested3Element.OptionalSimpleType);

            SchemaElementComplex nested4Element = nested3Element.ComplexElements[0];

            Assert.AreEqual("nested4", nested4Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", nested4Element.Namespace);
            Assert.AreEqual(1, nested4Element.Attributes.Count);
            Assert.AreEqual(1, nested4Element.SimpleElements.Count);
            Assert.AreEqual(0, nested4Element.ComplexElements.Count);
            Assert.AreEqual("id", nested4Element.Attributes[0].Name);
            Assert.AreEqual(schemaTypeId, nested4Element.Attributes[0].SimpleType);
            Assert.IsTrue(nested4Element.IsArray);
            Assert.IsNull(nested4Element.OptionalSimpleType);

            SchemaElementSimple prop5Element = nested4Element.SimpleElements[0];

            Assert.AreEqual("prop5", prop5Element.Name);
            Assert.AreEqual("samples:schemas:simpleSchema", prop5Element.Namespace);
            Assert.AreEqual(schemaTypeString, prop5Element.SimpleType);
            Assert.IsTrue(prop5Element.IsArray);
        }
Exemple #2
0
        /// <summary>
        /// Processes the model group.
        /// </summary>
        /// <param name="xsModel">The schema model.</param>
        /// <param name="childParticles">The schema objects in this model group.</param>
        /// <param name="simpleElements">The simple elements.</param>
        /// <param name="complexElements">The complex elements.</param>
        /// <param name="node">The node.</param>
        /// <param name="complexActualElement">The complex actual element.</param>
        /// <param name="complexElement">The complex element.</param>
        /// <returns></returns>
        private SchemaElementComplex ProcessModelGroup(
            XmlSchema xsModel,
            IEnumerable <XmlSchemaObject> childParticles,
            IList <SchemaElementSimple> simpleElements,
            IList <SchemaElementComplex> complexElements,
            ElementPathNode node,
            XmlSchemaComplexType complexActualElement,
            SchemaElementComplex complexElement)
        {
            foreach (var childParticle in childParticles)
            {
                if (childParticle is XmlSchemaElement)
                {
                    var schemaElement = (XmlSchemaElement)childParticle;
                    var isArrayFlag   = IsArray(schemaElement);

                    // the name for this element
                    XmlQualifiedName elementName;
                    // the type for this element ... this may take different paths
                    // depending upon how the type is provided to us.
                    XmlSchemaType    schemaType;
                    XmlQualifiedName schemaTypeName;

                    if (schemaElement.RefName.IsEmpty)
                    {
                        elementName = schemaElement.QualifiedName;
                        if (Equals(elementName, XmlQualifiedName.Empty))
                        {
                            elementName = new XmlQualifiedName(
                                schemaElement.Name, xsModel.TargetNamespace);
                        }

                        schemaType     = schemaElement.SchemaType;
                        schemaTypeName = schemaElement.SchemaTypeName;
                        if ((schemaType == null) && (!schemaTypeName.IsEmpty))
                        {
                            schemaType = ResolveSchemaType(xsModel, schemaTypeName);
                        }
                    }
                    else
                    {
                        // this element contains a reference to another element... the element will
                        // share the name of the reference and the type of the reference.  the reference
                        // type should be a complex type.
                        var referenceElement     = ResolveElement(schemaElement.RefName);
                        var referenceElementType = referenceElement.SchemaType;

                        var elementNamespace = string.IsNullOrEmpty(schemaElement.RefName.Namespace)
                            ? xsModel.TargetNamespace
                            : schemaElement.RefName.Namespace;
                        elementName = new XmlQualifiedName(
                            schemaElement.RefName.Name, elementNamespace);

                        schemaType     = referenceElementType;
                        schemaTypeName = referenceElement.SchemaTypeName;
                        // TODO
                    }

                    var simpleType = schemaType as XmlSchemaSimpleType;
                    if (simpleType != null)
                    {
                        var fractionDigits = GetFractionRestriction(simpleType);
                        var simpleElement  = new SchemaElementSimple(
                            elementName.Name,
                            elementName.Namespace,
                            simpleType,
                            schemaTypeName.Name,
                            isArrayFlag,
                            fractionDigits);
                        simpleElements.Add(simpleElement);
                    }
                    else
                    {
                        var complexType = schemaType as XmlSchemaComplexType;
                        var newChild    = node.AddChild(elementName);
                        if (newChild.DoesNameAlreadyExistInHierarchy())
                        {
                            continue;
                        }

                        complexActualElement = complexType;

                        SchemaElementComplex innerComplex = Process(
                            xsModel,
                            elementName,
                            complexActualElement,
                            isArrayFlag,
                            newChild
                            );

                        if (Log.IsDebugEnabled)
                        {
                            Log.Debug("Adding complex {0}", complexElement);
                        }
                        complexElements.Add(innerComplex);
                    }
                }

                ProcessModelGroup(
                    xsModel, childParticle, simpleElements, complexElements, node, complexActualElement,
                    complexElement);
            }

            return(complexElement);
        }
Exemple #3
0
 private static void VerifySimpleElement(SchemaElementSimple element, String name, XmlSchemaType type)
 {
     Assert.That(type, Is.EqualTo(element.SimpleType));
     VerifyElement(element, name);
 }
Exemple #4
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);
        }