Exemple #1
0
        /// <summary>
        /// Finds an apropiate definition for the given property, starting at the * given
        /// definition. First look if the property es an attribute. If not, look at simple and
        /// then child element definitions.
        /// </summary>
        /// <param name="def">the definition to start looking</param>
        /// <param name="property">the property to look for</param>
        /// <returns>
        /// schema element or null if not found
        /// </returns>
        public static SchemaItem FindPropertyMapping(SchemaElementComplex def, String property)
        {
            foreach (SchemaItemAttribute attribute in def.Attributes)
            {
                if (attribute.Name == property)
                {
                    return(attribute);
                }
            }

            foreach (SchemaElementSimple simple in def.SimpleElements)
            {
                if (simple.Name == property)
                {
                    return(simple);
                }
            }

            foreach (SchemaElementComplex complex in def.ComplexElements)
            {
                if (complex.Name == property)
                {
                    return(complex);
                }
            }

            //property not found in schema
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Processes the model group.
        /// </summary>
        /// <param name="xsModel">The schema model.</param>
        /// <param name="xsObject">The schema that represents the 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,
            XmlSchemaObject xsObject,
            IList <SchemaElementSimple> simpleElements,
            IList <SchemaElementComplex> complexElements,
            ElementPathNode node,
            XmlSchemaComplexType complexActualElement,
            SchemaElementComplex complexElement)
        {
            var xsGroup = xsObject as XmlSchemaGroupBase;

            if (xsGroup != null)
            {
                return(ProcessModelGroup(
                           xsModel,
                           xsGroup.Items.Cast <XmlSchemaObject>(),
                           simpleElements,
                           complexElements,
                           node,
                           complexActualElement,
                           complexElement));
            }

            return(complexElement);
        }
Exemple #3
0
        private static SchemaElementComplex RecursiveDeepMatch(SchemaElementComplex parent, String @namespace,
                                                               String elementName)
        {
            if (!string.IsNullOrEmpty(@namespace))
            {
                foreach (SchemaElementComplex complexElement in parent.ComplexElements)
                {
                    if ((complexElement.Namespace.Equals(@namespace)) && (complexElement.Name.Equals(elementName)))
                    {
                        return(complexElement);
                    }
                }
            }
            else
            {
                foreach (SchemaElementComplex complexElement in parent.ComplexElements)
                {
                    if (complexElement.Name.Equals(elementName))
                    {
                        return(complexElement);
                    }
                }
            }

            foreach (SchemaElementComplex complexElement in parent.ComplexElements)
            {
                SchemaElementComplex found = RecursiveDeepMatch(complexElement, @namespace, elementName);
                if (found != null)
                {
                    return(found);
                }
            }

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

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

            Assert.That(model.Components.Count, Is.EqualTo(1));

            SchemaElementComplex simpleEvent = model.Components[0];

            VerifyComplexElement(simpleEvent, "simpleEvent", false);
            VerifySizes(simpleEvent, 0, 0, 3);

            SchemaElementComplex nested1 = simpleEvent.ComplexElements[0];

            VerifyComplexElement(nested1, "nested1", false);
            VerifySizes(nested1, 1, 2, 1);
            Assert.AreEqual("attr1", nested1.Attributes[0].Name);
            Assert.AreEqual(string.Empty, nested1.Attributes[0].Namespace);
            Assert.AreEqual(_schemaTypeString, nested1.Attributes[0].SimpleType);
            Assert.AreEqual("prop1", nested1.SimpleElements[0].Name);
            Assert.AreEqual(_schemaTypeString, nested1.SimpleElements[0].SimpleType);
            Assert.AreEqual("prop2", nested1.SimpleElements[1].Name);
            Assert.AreEqual(_schemaTypeBoolean, nested1.SimpleElements[1].SimpleType);

            SchemaElementComplex nested2 = nested1.ComplexElements[0];

            VerifyComplexElement(nested2, "nested2", false);
            VerifySizes(nested2, 0, 1, 0);
            VerifySimpleElement(nested2.SimpleElements[0], "prop3", _schemaTypeInt);

            SchemaElementComplex prop4 = simpleEvent.ComplexElements[1];

            VerifyElement(prop4, "prop4");
            VerifySizes(prop4, 1, 0, 0);
            Assert.AreEqual("attr2", prop4.Attributes[0].Name);
            Assert.AreEqual(_schemaTypeBoolean, prop4.Attributes[0].SimpleType);
            Assert.AreEqual(_schemaTypeString, prop4.OptionalSimpleType);

            SchemaElementComplex nested3 = simpleEvent.ComplexElements[2];

            VerifyComplexElement(nested3, "nested3", false);
            VerifySizes(nested3, 0, 0, 1);

            SchemaElementComplex nested4 = nested3.ComplexElements[0];

            VerifyComplexElement(nested4, "nested4", true);
            VerifySizes(nested4, 1, 4, 0);
            Assert.AreEqual("id", nested4.Attributes[0].Name);
            Assert.AreEqual(_schemaTypeId, nested4.Attributes[0].SimpleType);
            VerifySimpleElement(nested4.SimpleElements[0], "prop5", _schemaTypeString);
            VerifySimpleElement(nested4.SimpleElements[1], "prop6", _schemaTypeString);
            VerifySimpleElement(nested4.SimpleElements[2], "prop7", _schemaTypeString);
            VerifySimpleElement(nested4.SimpleElements[3], "prop8", _schemaTypeString);
        }
Exemple #5
0
        private SchemaElementComplex Process(
            XmlSchema xsModel,
            XmlQualifiedName complexElementName,
            XmlSchemaComplexType complexActualElement,
            bool isArray,
            ElementPathNode node)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(
                    "Processing complex {0} {1} stack {2}",
                    complexElementName.Namespace,
                    complexElementName.Name,
                    node);
            }

            var attributes      = new List <SchemaItemAttribute>();
            var simpleElements  = new List <SchemaElementSimple>();
            var complexElements = new List <SchemaElementComplex>();

            XmlSchemaSimpleType optionalSimpleType     = null;
            XmlQualifiedName    optionalSimpleTypeName = null;

            DetermineOptionalSimpleType(
                xsModel,
                complexActualElement,
                out optionalSimpleType,
                out optionalSimpleTypeName
                );

            var complexElement = new SchemaElementComplex(
                complexElementName.Name,
                complexElementName.Namespace,
                attributes,
                complexElements,
                simpleElements,
                isArray,
                optionalSimpleType,
                optionalSimpleTypeName);

            // add attributes
            attributes.AddRange(GetAttributes(xsModel, complexActualElement));

            var complexParticles = GetContentModelParticles(
                xsModel, complexActualElement);

            complexElement = ProcessModelGroup(
                xsModel, complexParticles, simpleElements, complexElements, node,
                complexActualElement, complexElement);

            return(complexElement);
        }
Exemple #6
0
        private void Import(XmlSchema xsModel, Uri schemaLocation)
        {
            ImportNamespaces(xsModel);
            ImportIncludes(xsModel, schemaLocation, Import);

            BuildElementDictionary(xsModel);
            BuildTypeDictionary(xsModel);

            // get top-level complex elements
            foreach (var schemaElement in xsModel.Items.OfType <XmlSchemaElement>())
            {
                var schemaType = schemaElement.SchemaType;
                if (schemaType == null)
                {
                    var schemaTypeName = schemaElement.SchemaTypeName;
                    if (!Equals(schemaTypeName, XmlQualifiedName.Empty))
                    {
                        schemaType = ResolveSchemaType(xsModel, schemaTypeName);
                    }
                }

                var complexElementType = schemaType as XmlSchemaComplexType;
                if (complexElementType != null)
                {
                    var complexActualName = schemaElement.QualifiedName;
                    if (Equals(complexActualName, XmlQualifiedName.Empty))
                    {
                        complexActualName = new XmlQualifiedName(
                            schemaElement.Name, xsModel.TargetNamespace);
                    }

                    var rootNode = new ElementPathNode(null, complexActualName);

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug(string.Format("Processing component {0}", complexActualName));
                    }

                    SchemaElementComplex complexElement = Process(
                        xsModel, complexActualName, complexElementType, false, rootNode);

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Adding component {0}", complexActualName);
                    }

                    _components.Add(complexElement);
                }
            }
        }
Exemple #7
0
        public void TestExtendedElements()
        {
            Uri    uri       = ResourceManager.ResolveResourceURL("regression/schemaWithExtensions.xsd");
            String schemaUri = uri.ToString();

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

            SchemaElementComplex complexEvent = model.Components[0];

            VerifyComplexElement(complexEvent, "complexEvent", false);
            VerifySizes(complexEvent, 0, 0, 1);

            SchemaElementComplex mainElement = complexEvent.ComplexElements[0];

            VerifyComplexElement(mainElement, "mainElement", false);
            VerifySizes(mainElement, 0, 0, 4);

            SchemaElementComplex baseType4 = mainElement.ComplexElements[0];

            VerifyComplexElement(baseType4, "baseType4", false);
            VerifySizes(baseType4, 0, 0, 0);

            SchemaElementComplex aType2 = mainElement.ComplexElements[1];

            VerifyComplexElement(aType2, "aType2", false);
            VerifySizes(aType2, 0, 2, 1);

            SchemaElementComplex aType3 = mainElement.ComplexElements[2];

            VerifyComplexElement(aType3, "aType3", false);
            VerifySizes(aType3, 0, 1, 2);

            SchemaElementComplex aType3baseType4 = aType3.ComplexElements[0];

            VerifyComplexElement(aType3baseType4, "baseType4", false);
            VerifySizes(aType3baseType4, 0, 0, 0);

            SchemaElementComplex aType3type2 = aType3.ComplexElements[1];

            VerifyComplexElement(aType3type2, "aType2", false);
            VerifySizes(aType3type2, 0, 2, 1);

            SchemaElementComplex aType4 = mainElement.ComplexElements[3];

            VerifyComplexElement(aType4, "aType4", false);
            VerifySizes(aType4, 0, 0, 1);
        }
Exemple #8
0
        public static SchemaElementComplex FindRootElement(SchemaModel schema, String @namespace, String elementName)
        {
            if (!string.IsNullOrEmpty(@namespace))
            {
                foreach (SchemaElementComplex complexElement in schema.Components)
                {
                    if ((complexElement.Namespace.Equals(@namespace)) && (complexElement.Name.Equals(elementName)))
                    {
                        return(complexElement);
                    }
                }
            }
            else
            {
                foreach (SchemaElementComplex complexElement in schema.Components)
                {
                    if (complexElement.Name.Equals(elementName))
                    {
                        return(complexElement);
                    }
                }
            }

            if (elementName.StartsWith("//"))
            {
                elementName = elementName.Substring(2);
                foreach (SchemaElementComplex complexElement in schema.Components)
                {
                    SchemaElementComplex match = RecursiveDeepMatch(complexElement, @namespace, elementName);
                    if (match != null)
                    {
                        return(match);
                    }
                }
            }

            String text = "Could not find root element declaration in schema for element name '" + elementName + '\'';

            if (@namespace != null)
            {
                text = text + " in namespace '" + @namespace + '\'';
            }
            throw new EPException(text);
        }
        private static Pair <String, XPathResultType> MakeProperty(SchemaElementComplex parent, Property property, XPathNamespaceContext ctx, bool isLast, bool isDynamic, String defaultNamespacePrefix)
        {
            var text = property.PropertyNameAtomic;
            var obj  = SchemaUtil.FindPropertyMapping(parent, text);

            if ((obj is SchemaElementSimple) || (obj is SchemaElementComplex))
            {
                return(MakeElementProperty((SchemaElement)obj, property, ctx, isLast, isDynamic, defaultNamespacePrefix));
            }
            if (obj != null)
            {
                return(MakeAttributeProperty((SchemaItemAttribute)obj, property, ctx));
            }
            if (isDynamic)
            {
                return(MakeElementProperty(null, property, ctx, isLast, isDynamic, defaultNamespacePrefix));
            }

            return(null);
        }
Exemple #10
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 #11
0
 private static void VerifySizes(SchemaElementComplex element, int expectedNumberOfAttributes, int expectedNumberOfSimpleElements, int expectedNumberOfChildren)
 {
     Assert.That(element.Attributes.Count, Is.EqualTo(expectedNumberOfAttributes));
     Assert.That(element.SimpleElements.Count, Is.EqualTo(expectedNumberOfSimpleElements));
     Assert.That(element.ComplexElements.Count, Is.EqualTo(expectedNumberOfChildren));
 }
Exemple #12
0
 private static void VerifyComplexElement(SchemaElementComplex element, string name, bool isArray)
 {
     Assert.That(element.OptionalSimpleType, Is.Null);
     Assert.That(element.IsArray, Is.EqualTo(isArray));
     VerifyElement(element, name);
 }
Exemple #13
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="eventTypeMetadata">event type metadata</param>
        /// <param name="eventTypeId">The event type id.</param>
        /// <param name="config">configuration for type</param>
        /// <param name="schemaModel">the schema representation</param>
        /// <param name="eventAdapterService">type lookup and registration</param>
        public SchemaXMLEventType(EventTypeMetadata eventTypeMetadata, int eventTypeId, ConfigurationEventTypeXMLDOM config, SchemaModel schemaModel, EventAdapterService eventAdapterService)
            : base(eventTypeMetadata, eventTypeId, config, eventAdapterService)
        {
            _propertyGetterCache       = new Dictionary <String, EventPropertyGetter>();
            _schemaModel               = schemaModel;
            _rootElementNamespace      = config.RootElementNamespace;
            _schemaModelRoot           = SchemaUtil.FindRootElement(schemaModel, _rootElementNamespace, RootElementName);
            _isPropertyExpressionXPath = config.IsXPathPropertyExpr;

            // Set of namespace context for XPath expressions
            var ctx = new XPathNamespaceContext();

            if (config.DefaultNamespace != null)
            {
                ctx.SetDefaultNamespace(config.DefaultNamespace);
            }

            foreach (var entry in config.NamespacePrefixes)
            {
                ctx.AddNamespace(entry.Key, entry.Value);
            }

            NamespaceContext = ctx;

            // add properties for the root element
            var additionalSchemaProps = new List <ExplicitPropertyDescriptor>();

            // Add a property for each complex child element
            foreach (SchemaElementComplex complex in _schemaModelRoot.ComplexElements)
            {
                var  propertyName          = complex.Name;
                var  returnType            = typeof(XmlNode);
                Type propertyComponentType = null;

                if (complex.OptionalSimpleType != null)
                {
                    returnType = SchemaUtil.ToReturnType(complex);
                }
                if (complex.IsArray)
                {
                    // We use XmlNode[] for arrays and NodeList for XPath-Expressions returning Nodeset
                    returnType            = typeof(XmlNode[]);
                    propertyComponentType = typeof(XmlNode);
                }

                bool isFragment = false;
                if (ConfigurationEventTypeXMLDOM.IsAutoFragment && (!ConfigurationEventTypeXMLDOM.IsXPathPropertyExpr))
                {
                    isFragment = CanFragment(complex);
                }

                var indexType = returnType.GetIndexType();
                var isIndexed = indexType != null;
                var getter    = DoResolvePropertyGetter(propertyName, true);
                var desc      = new EventPropertyDescriptor(propertyName, returnType, indexType, false, false, isIndexed, false, isFragment);
                var @explicit = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Add a property for each simple child element
            foreach (SchemaElementSimple simple in _schemaModelRoot.SimpleElements)
            {
                var propertyName = simple.Name;
                var returnType   = SchemaUtil.ToReturnType(simple);
                var getter       = DoResolvePropertyGetter(propertyName, true);
                var indexType    = returnType.GetIndexType();
                var isIndexed    = indexType != null;
                var desc         = new EventPropertyDescriptor(propertyName, returnType, indexType, false, false, isIndexed, false, false);
                var @explicit    = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Add a property for each attribute
            foreach (SchemaItemAttribute attribute in _schemaModelRoot.Attributes)
            {
                var propertyName = attribute.Name;
                var returnType   = SchemaUtil.ToReturnType(attribute);
                var getter       = DoResolvePropertyGetter(propertyName, true);
                var indexType    = returnType.GetIndexType();
                var isIndexed    = indexType != null;
                var desc         = new EventPropertyDescriptor(propertyName, returnType, indexType, false, false, isIndexed, false, false);
                var @explicit    = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Finally add XPath properties as that may depend on the rootElementNamespace
            Initialize(config.XPathProperties.Values, additionalSchemaProps);
        }
Exemple #14
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 #15
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="component">top level component</param>
 /// <param name="namespaces">list of namespaces</param>
 public SchemaModel(SchemaElementComplex component, IList <String> namespaces)
 {
     Components = new List <SchemaElementComplex>(1);
     Components.Add(component);
     Namespaces = namespaces;
 }
Exemple #16
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);
        }