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

                        var 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);
        }
        public void TestMap()
        {
            Uri    uri       = _container.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);
        }