Пример #1
0
 private void ProcessComment(InstanceElement elem)
 {
     if (elem.Comment.Length > 0)
     {
         writer.WriteComment(elem.Comment.ToString());
     }
 }
Пример #2
0
 private void CheckIfMixed(InstanceElement mixedElem)
 {
     if (mixedElem.IsMixed)
     {
         writer.WriteString("text");
     }
 }
Пример #3
0
        private void ProcessElement(InstanceElement elem)
        {
            if (instanceElementsProcessed[elem] != null)
            {
                return;
            }
            instanceElementsProcessed.Add(elem, elem);
            for (int i = 0; i < elem.Occurs; i++)
            {
                writer.WriteStartElement(elem.QualifiedName.Name, elem.QualifiedName.Namespace);
                ProcessElementAttrs(elem);
                ProcessComment(elem);
                CheckIfMixed(elem);
                if (elem.IsNillable)
                {
                    if (elem.GenNil)
                    {
                        WriteNillable();
                        elem.GenNil = false;
                        writer.WriteEndElement();
                        continue;
                    }
                    else
                    {
                        elem.GenNil = true;
                    }
                }

                if (elem.ValueGenerator != null)
                {
                    if (elem.IsFixed)
                    {
                        writer.WriteString(elem.FixedValue);
                    }
                    else if (elem.HasDefault)
                    {
                        writer.WriteString(elem.DefaultValue);
                    }
                    else
                    {
                        writer.WriteString(elem.ValueGenerator.GenerateValue());
                    }
                }
                else
                {
                    var childGroup = elem.Child;
                    while (childGroup != null)
                    {
                        ProcessGroup(childGroup);
                        childGroup = childGroup.Sibling;
                    }
                }
                writer.WriteEndElement();
            }
            instanceElementsProcessed.Remove(elem);
        }
Пример #4
0
        private void GenerateAttribute(XmlSchemaObjectTable attributes, InstanceElement elem)
        {
            var ienum = attributes.GetEnumerator();

            while (ienum.MoveNext())
            {
                if (ienum.Value is XmlSchemaAttribute)
                {
                    GenerateInstanceAttribute((XmlSchemaAttribute)ienum.Value, elem);
                }
            }
        }
Пример #5
0
        private void ProcessElementAttrs(InstanceElement elem)
        {
            if (elem.XsiType != XmlQualifiedName.Empty)
            {
                if (elem.XsiType.Namespace != string.Empty)
                {
                    writer.WriteStartAttribute("xsi", "type", null);
                    writer.WriteQualifiedName(elem.XsiType.Name, elem.XsiType.Namespace);
                    writer.WriteEndAttribute();
                }
                else
                {
                    writer.WriteAttributeString("xsi", "type", null, elem.XsiType.Name);
                }
            }

            var attr = elem.FirstAttribute;

            while (attr != null)
            {
                if (attr.AttrUse != XmlSchemaUse.Prohibited)
                {
                    if (attr.QualifiedName.Namespace == NsXml)
                    {
                        writer.WriteStartAttribute("xml", attr.QualifiedName.Name, attr.QualifiedName.Namespace);
                    }
                    else
                    {
                        writer.WriteStartAttribute(attr.QualifiedName.Name, attr.QualifiedName.Namespace);
                    }
                    if (attr.HasDefault)
                    {
                        writer.WriteString(attr.DefaultValue);
                    }
                    else if (attr.IsFixed)
                    {
                        writer.WriteString(attr.FixedValue);
                    }
                    else
                    {
                        writer.WriteString(attr.ValueGenerator.GenerateValue());
                    }
                    writer.WriteEndAttribute();
                }
                attr = attr.NextAttribute;
            }
        }
Пример #6
0
        private void GenerateInstanceAttribute(XmlSchemaAttribute attr, InstanceElement elem)
        {
            if (attr.Use == XmlSchemaUse.Prohibited || attr.AttributeSchemaType == null)
            {
                return;
            }
            var iAttr = new InstanceAttribute(attr.QualifiedName);

            iAttr.DefaultValue   = attr.DefaultValue;
            iAttr.FixedValue     = attr.FixedValue;
            iAttr.AttrUse        = attr.Use;
            iAttr.ValueGenerator = XmlValueGenerator.CreateGenerator(attr.AttributeSchemaType.Datatype, listLength);
            if (iAttr.ValueGenerator != null && iAttr.ValueGenerator.Prefix == null)
            {
                iAttr.ValueGenerator.Prefix = iAttr.QualifiedName.Name;
            }
            elem.AddAttribute(iAttr);
        }
Пример #7
0
 private void ProcessInstanceTree(InstanceElement rootElement)
 {
     if (rootElement != null)
     {
         instanceElementsProcessed.Add(rootElement, rootElement);
         writer.WriteStartElement(rootElement.QualifiedName.Name, rootTargetNamespace);
         writer.WriteAttributeString("xmlns", "xsi", null, NsXsi);
         ProcessElementAttrs(rootElement);
         ProcessComment(rootElement);
         CheckIfMixed(rootElement);
         if (rootElement.ValueGenerator != null)
         {
             if (rootElement.IsFixed)
             {
                 writer.WriteString(rootElement.FixedValue);
             }
             else if (rootElement.HasDefault)
             {
                 writer.WriteString(rootElement.DefaultValue);
             }
             else
             {
                 writer.WriteString(rootElement.ValueGenerator.GenerateValue());
             }
         }
         else
         {
             var group = rootElement.Child;
             while (group != null)
             {
                 ProcessGroup(group);
                 group = group.Sibling;
             }
         }
         writer.WriteEndElement();
     }
     else
     {
         writer.WriteComment("Schema did not lead to generation of a valid XML document");
     }
 }
Пример #8
0
 private void ProcessComplexType(XmlSchemaComplexType ct, InstanceElement elem)
 {
     if (ct.ContentModel != null && ct.ContentModel is XmlSchemaSimpleContent)
     {
         elem.ValueGenerator = XmlValueGenerator.CreateGenerator(ct.Datatype, listLength);
     }
     else
     {
         GenerateParticle(ct.ContentTypeParticle, false, elem);
     }
     //Check for attribute wild card
     if (ct.AttributeWildcard != null)
     {
         GenerateAttributeWildCard(ct, elem);
     }
     //Check for attributes if simple/complex content
     if (ct.AttributeUses.Count > 0)
     {
         GenerateAttribute(ct.AttributeUses, elem);
     }
 }
Пример #9
0
 private void ProcessComment(InstanceElement elem)
 {
     if ( elem.Comment.Length > 0 )
     {
         writer.WriteComment(elem.Comment.ToString());
     }
 }
Пример #10
0
 private void GenerateInstanceAttribute(XmlSchemaAttribute attr, InstanceElement elem)
 {
     if ( attr.Use == XmlSchemaUse.Prohibited || attr.AttributeSchemaType == null )
     {
         return;
     }
     var iAttr = new InstanceAttribute(attr.QualifiedName);
     iAttr.DefaultValue = attr.DefaultValue;
     iAttr.FixedValue = attr.FixedValue;
     iAttr.AttrUse = attr.Use;
     iAttr.ValueGenerator = XmlValueGenerator.CreateGenerator(attr.AttributeSchemaType.Datatype, listLength);
     if ( iAttr.ValueGenerator != null && iAttr.ValueGenerator.Prefix == null )
     {
         iAttr.ValueGenerator.Prefix = iAttr.QualifiedName.Name;
     }
     elem.AddAttribute(iAttr);
 }
Пример #11
0
        private bool GenerateElement(XmlSchemaElement e, bool root, InstanceGroup parentElem, XmlSchemaAny any)
        {
            var eGlobalDecl = e;

            if ( !e.RefName.IsEmpty )
            {
                eGlobalDecl = (XmlSchemaElement)schemaSet.GlobalElements[e.QualifiedName];
            }
            if ( !eGlobalDecl.IsAbstract )
            {
                var elem = (InstanceElement)elementTypesProcessed[eGlobalDecl];
                if ( elem != null )
                {
                    Debug.Assert(!root);
                    if ( any == null && e.MinOccurs > 0 )
                    { //If not generating for any or optional ref to cyclic global element
                        var occurs = e.MaxOccurs;
                        if ( e.MaxOccurs >= maxThreshold )
                        {
                            occurs = maxThreshold;
                        }
                        if ( e.MinOccurs > occurs )
                        {
                            occurs = e.MinOccurs;
                        }
                        parentElem.AddChild(elem.Clone(occurs));
                    }
                    return false;
                }
                elem = new InstanceElement(eGlobalDecl.QualifiedName);

                if ( root )
                {
                    instanceRoot = elem;
                }
                else
                {
                    parentElem.AddChild(elem);
                }

                //Get minOccurs, maxOccurs alone from the current particle, everything else pick up from globalDecl
                if ( any != null )
                { //Element from any
                    elem.Occurs = any.MaxOccurs >= maxThreshold ? maxThreshold : any.MaxOccurs;
                    elem.Occurs = any.MinOccurs > elem.Occurs ? any.MinOccurs : elem.Occurs;
                }
                else
                {
                    elem.Occurs = e.MaxOccurs >= maxThreshold ? maxThreshold : e.MaxOccurs;
                    elem.Occurs = e.MinOccurs > elem.Occurs ? e.MinOccurs : elem.Occurs;
                }
                elem.DefaultValue = eGlobalDecl.DefaultValue;
                elem.FixedValue = eGlobalDecl.FixedValue;
                elem.IsNillable = eGlobalDecl.IsNillable;

                if ( eGlobalDecl.ElementSchemaType == AnyType )
                {
                    elem.ValueGenerator = XmlValueGenerator.AnyGenerator;
                }
                else
                {
                    var ct = eGlobalDecl.ElementSchemaType as XmlSchemaComplexType;
                    if ( ct != null )
                    {
                        elementTypesProcessed.Add(eGlobalDecl, elem);
                        if ( !ct.IsAbstract )
                        {
                            elem.IsMixed = ct.IsMixed;
                            ProcessComplexType(ct, elem);
                        }
                        else
                        { // Ct is abstract, need to generate instance elements with xsi:type
                            var dt = GetDerivedType(ct);
                            if ( dt != null )
                            {
                                elem.XsiType = dt.QualifiedName;
                                ProcessComplexType(dt, elem);
                            }
                        }
                    }
                    else
                    { //elementType is XmlSchemaSimpleType
                        elem.ValueGenerator = XmlValueGenerator.CreateGenerator(eGlobalDecl.ElementSchemaType.Datatype, listLength);
                    }
                }
                if ( elem.ValueGenerator != null && elem.ValueGenerator.Prefix == null )
                {
                    elem.ValueGenerator.Prefix = elem.QualifiedName.Name;
                }
                return true;
            } // End of e.IsAbstract
            return false;
        }
Пример #12
0
        private void GenerateAttributeWildCard(XmlSchemaComplexType ct, InstanceElement elem)
        {
            var whitespace = new char[] { ' ', '\t', '\n', '\r' };
            InstanceAttribute attr = null;
            XmlSchemaAttribute anyAttr = null;

            var attributeWildCard = ct.AttributeWildcard;
            var attributes = ct.AttributeUses;

            var namespaceList = attributeWildCard.Namespace;
            if ( namespaceList == null )
            {
                namespaceList = "##any";
            }
            if ( attributeWildCard.ProcessContents == XmlSchemaContentProcessing.Skip || attributeWildCard.ProcessContents == XmlSchemaContentProcessing.Lax )
            {
                if ( namespaceList == "##any" || namespaceList == "##targetNamespace" )
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", rootTargetNamespace));
                }
                else if ( namespaceList == "##local" )
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", string.Empty));
                }
                else if ( namespaceList == "##other" )
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", "otherNS"));
                }
                if ( attr != null )
                {
                    attr.ValueGenerator = XmlValueGenerator.AnySimpleTypeGenerator;
                    elem.AddAttribute(attr);
                    return;
                }
            }
            switch ( namespaceList )
            {
                case "##any":
                case "##targetNamespace":
                    anyAttr = GetAttributeFromNS(rootTargetNamespace, attributes);
                    break;

                case "##other":
                    var anySchema = GetParentSchema(attributeWildCard);
                    anyAttr = GetAttributeFromNS(anySchema.TargetNamespace, true, attributes);
                    break;

                case "##local": //Shd get local elements in some schema
                    anyAttr = GetAttributeFromNS(string.Empty, attributes);
                    break;

                default:
                    foreach ( string ns in attributeWildCard.Namespace.Split(whitespace) )
                    {
                        if ( ns == "##local" )
                        {
                            anyAttr = GetAttributeFromNS(string.Empty, attributes);
                        }
                        else if ( ns == "##targetNamespace" )
                        {
                            anyAttr = GetAttributeFromNS(rootTargetNamespace, attributes);
                        }
                        else
                        {
                            anyAttr = GetAttributeFromNS(ns, attributes);
                        }
                        if ( anyAttr != null )
                        { //Found match
                            break;
                        }
                    }
                    break;
            }
            if ( anyAttr != null )
            {
                GenerateInstanceAttribute(anyAttr, elem);
            }
            else
            { //Write comment in generated XML that match for wild card cd not be found.
                if ( elem.Comment.Length == 0 )
                { //For multiple attribute wildcards in the same element, generate comment only once
                    elem.Comment.Append(" Attribute Wild card could not be matched. Generated XML may not be valid. ");
                }
            }
        }
Пример #13
0
 private void GenerateAttribute(XmlSchemaObjectTable attributes, InstanceElement elem)
 {
     var ienum = attributes.GetEnumerator();
     while ( ienum.MoveNext() )
     {
         if ( ienum.Value is XmlSchemaAttribute )
         {
             GenerateInstanceAttribute((XmlSchemaAttribute)ienum.Value, elem);
         }
     }
 }
Пример #14
0
        private void GenerateAny(XmlSchemaAny any, InstanceGroup grp)
        {
            var parentElem = grp as InstanceElement;
            var whitespace = new char[] { ' ', '\t', '\n', '\r' };
            InstanceElement elem = null;
            XmlSchemaElement anyElem = null;
            var namespaceList = any.Namespace;
            if ( namespaceList == null )
            { //no namespace defaults to "##any"
                namespaceList = "##any";
            }
            if ( any.ProcessContents == XmlSchemaContentProcessing.Skip || any.ProcessContents == XmlSchemaContentProcessing.Lax )
            {
                if ( namespaceList == "##any" || namespaceList == "##targetNamespace" )
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", rootTargetNamespace));
                }
                else if ( namespaceList == "##local" )
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", string.Empty));
                }
                else if ( namespaceList == "##other" )
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", "otherNS"));
                }
                if ( elem != null )
                {
                    elem.ValueGenerator = XmlValueGenerator.AnyGenerator;
                    elem.Occurs = any.MaxOccurs >= maxThreshold ? maxThreshold : any.MaxOccurs;
                    elem.Occurs = any.MinOccurs > elem.Occurs ? any.MinOccurs : elem.Occurs;
                    grp.AddChild(elem);
                    return;
                }
            }
            //ProcessContents = strict || namespaceList is actually a list of namespaces
            switch ( namespaceList )
            {
                case "##any":
                case "##targetNamespace":
                    anyElem = GetElementFromNS(rootTargetNamespace);
                    break;

                case "##other":
                    var anySchema = GetParentSchema(any);
                    anyElem = GetElementFromNS(anySchema.TargetNamespace, true);
                    break;

                case "##local": //Shd get local elements in some schema
                    anyElem = GetElementFromNS(string.Empty);
                    break;

                default:
                    foreach ( string ns in namespaceList.Split(whitespace) )
                    {
                        if ( ns == "##targetNamespace" )
                        {
                            anyElem = GetElementFromNS(rootTargetNamespace);
                        }
                        else if ( ns == "##local" )
                        {
                            anyElem = GetElementFromNS(string.Empty);
                        }
                        else
                        {
                            anyElem = GetElementFromNS(ns);
                        }
                        if ( anyElem != null )
                        { //found a match
                            break;
                        }
                    }
                    break;
            }
            if ( anyElem != null && GenerateElement(anyElem, false, grp, any) )
            {
                return;
            }
            else
            { //Write comment in generated XML that match for wild card cd not be found.
                if ( parentElem == null )
                {
                    parentElem = GetParentInstanceElement(grp);
                }
                if ( parentElem.Comment.Length == 0 )
                { //For multiple wildcards in the same element, generate comment only once
                    parentElem.Comment.Append(" Element Wild card could not be matched. Generated XML may not be valid. ");
                }
            }
        }
Пример #15
0
        private void ProcessElementAttrs(InstanceElement elem)
        {
            if ( elem.XsiType != XmlQualifiedName.Empty )
            {
                if ( elem.XsiType.Namespace != string.Empty )
                {
                    writer.WriteStartAttribute("xsi", "type", null);
                    writer.WriteQualifiedName(elem.XsiType.Name, elem.XsiType.Namespace);
                    writer.WriteEndAttribute();
                }
                else
                {
                    writer.WriteAttributeString("xsi", "type", null, elem.XsiType.Name);
                }
            }

            var attr = elem.FirstAttribute;
            while ( attr != null )
            {
                if ( attr.AttrUse != XmlSchemaUse.Prohibited )
                {
                    if ( attr.QualifiedName.Namespace == NsXml )
                    {
                        writer.WriteStartAttribute("xml", attr.QualifiedName.Name, attr.QualifiedName.Namespace);
                    }
                    else
                    {
                        writer.WriteStartAttribute(attr.QualifiedName.Name, attr.QualifiedName.Namespace);
                    }
                    if ( attr.HasDefault )
                    {
                        writer.WriteString(attr.DefaultValue);
                    }
                    else if ( attr.IsFixed )
                    {
                        writer.WriteString(attr.FixedValue);
                    }
                    else
                    {
                        writer.WriteString(attr.ValueGenerator.GenerateValue());
                    }
                    writer.WriteEndAttribute();
                }
                attr = attr.NextAttribute;
            }
        }
Пример #16
0
 private void ProcessComplexType(XmlSchemaComplexType ct, InstanceElement elem)
 {
     if ( ct.ContentModel != null && ct.ContentModel is XmlSchemaSimpleContent )
     {
         elem.ValueGenerator = XmlValueGenerator.CreateGenerator(ct.Datatype, listLength);
     }
     else
     {
         GenerateParticle(ct.ContentTypeParticle, false, elem);
     }
     //Check for attribute wild card
     if ( ct.AttributeWildcard != null )
     {
         GenerateAttributeWildCard(ct, elem);
     }
     //Check for attributes if simple/complex content
     if ( ct.AttributeUses.Count > 0 )
     {
         GenerateAttribute(ct.AttributeUses, elem);
     }
 }
Пример #17
0
        private void GenerateAttributeWildCard(XmlSchemaComplexType ct, InstanceElement elem)
        {
            var whitespace             = new char[] { ' ', '\t', '\n', '\r' };
            InstanceAttribute  attr    = null;
            XmlSchemaAttribute anyAttr = null;

            var attributeWildCard = ct.AttributeWildcard;
            var attributes        = ct.AttributeUses;

            var namespaceList = attributeWildCard.Namespace;

            if (namespaceList == null)
            {
                namespaceList = "##any";
            }
            if (attributeWildCard.ProcessContents == XmlSchemaContentProcessing.Skip || attributeWildCard.ProcessContents == XmlSchemaContentProcessing.Lax)
            {
                if (namespaceList == "##any" || namespaceList == "##targetNamespace")
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", rootTargetNamespace));
                }
                else if (namespaceList == "##local")
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", string.Empty));
                }
                else if (namespaceList == "##other")
                {
                    attr = new InstanceAttribute(new XmlQualifiedName("any_Attr", "otherNS"));
                }
                if (attr != null)
                {
                    attr.ValueGenerator = XmlValueGenerator.AnySimpleTypeGenerator;
                    elem.AddAttribute(attr);
                    return;
                }
            }
            switch (namespaceList)
            {
            case "##any":
            case "##targetNamespace":
                anyAttr = GetAttributeFromNS(rootTargetNamespace, attributes);
                break;

            case "##other":
                var anySchema = GetParentSchema(attributeWildCard);
                anyAttr = GetAttributeFromNS(anySchema.TargetNamespace, true, attributes);
                break;

            case "##local":                     //Shd get local elements in some schema
                anyAttr = GetAttributeFromNS(string.Empty, attributes);
                break;

            default:
                foreach (string ns in attributeWildCard.Namespace.Split(whitespace))
                {
                    if (ns == "##local")
                    {
                        anyAttr = GetAttributeFromNS(string.Empty, attributes);
                    }
                    else if (ns == "##targetNamespace")
                    {
                        anyAttr = GetAttributeFromNS(rootTargetNamespace, attributes);
                    }
                    else
                    {
                        anyAttr = GetAttributeFromNS(ns, attributes);
                    }
                    if (anyAttr != null)
                    {                             //Found match
                        break;
                    }
                }
                break;
            }
            if (anyAttr != null)
            {
                GenerateInstanceAttribute(anyAttr, elem);
            }
            else
            {             //Write comment in generated XML that match for wild card cd not be found.
                if (elem.Comment.Length == 0)
                {         //For multiple attribute wildcards in the same element, generate comment only once
                    elem.Comment.Append(" Attribute Wild card could not be matched. Generated XML may not be valid. ");
                }
            }
        }
Пример #18
0
        private void GenerateAny(XmlSchemaAny any, InstanceGroup grp)
        {
            var              parentElem    = grp as InstanceElement;
            var              whitespace    = new char[] { ' ', '\t', '\n', '\r' };
            InstanceElement  elem          = null;
            XmlSchemaElement anyElem       = null;
            var              namespaceList = any.Namespace;

            if (namespaceList == null)
            {             //no namespace defaults to "##any"
                namespaceList = "##any";
            }
            if (any.ProcessContents == XmlSchemaContentProcessing.Skip || any.ProcessContents == XmlSchemaContentProcessing.Lax)
            {
                if (namespaceList == "##any" || namespaceList == "##targetNamespace")
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", rootTargetNamespace));
                }
                else if (namespaceList == "##local")
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", string.Empty));
                }
                else if (namespaceList == "##other")
                {
                    elem = new InstanceElement(new XmlQualifiedName("any_element", "otherNS"));
                }
                if (elem != null)
                {
                    elem.ValueGenerator = XmlValueGenerator.AnyGenerator;
                    elem.Occurs         = any.MaxOccurs >= maxThreshold ? maxThreshold : any.MaxOccurs;
                    elem.Occurs         = any.MinOccurs > elem.Occurs ? any.MinOccurs : elem.Occurs;
                    grp.AddChild(elem);
                    return;
                }
            }
            //ProcessContents = strict || namespaceList is actually a list of namespaces
            switch (namespaceList)
            {
            case "##any":
            case "##targetNamespace":
                anyElem = GetElementFromNS(rootTargetNamespace);
                break;

            case "##other":
                var anySchema = GetParentSchema(any);
                anyElem = GetElementFromNS(anySchema.TargetNamespace, true);
                break;

            case "##local":                     //Shd get local elements in some schema
                anyElem = GetElementFromNS(string.Empty);
                break;

            default:
                foreach (string ns in namespaceList.Split(whitespace))
                {
                    if (ns == "##targetNamespace")
                    {
                        anyElem = GetElementFromNS(rootTargetNamespace);
                    }
                    else if (ns == "##local")
                    {
                        anyElem = GetElementFromNS(string.Empty);
                    }
                    else
                    {
                        anyElem = GetElementFromNS(ns);
                    }
                    if (anyElem != null)
                    {                             //found a match
                        break;
                    }
                }
                break;
            }
            if (anyElem != null && GenerateElement(anyElem, false, grp, any))
            {
                return;
            }
            else
            {             //Write comment in generated XML that match for wild card cd not be found.
                if (parentElem == null)
                {
                    parentElem = GetParentInstanceElement(grp);
                }
                if (parentElem.Comment.Length == 0)
                {                 //For multiple wildcards in the same element, generate comment only once
                    parentElem.Comment.Append(" Element Wild card could not be matched. Generated XML may not be valid. ");
                }
            }
        }
Пример #19
0
        private void ProcessElement(InstanceElement elem)
        {
            if ( instanceElementsProcessed[elem] != null )
            {
                return;
            }
            instanceElementsProcessed.Add(elem, elem);
            for ( int i = 0; i < elem.Occurs; i++ )
            {
                writer.WriteStartElement(elem.QualifiedName.Name, elem.QualifiedName.Namespace);
                ProcessElementAttrs(elem);
                ProcessComment(elem);
                CheckIfMixed(elem);
                if ( elem.IsNillable )
                {
                    if ( elem.GenNil )
                    {
                        WriteNillable();
                        elem.GenNil = false;
                        writer.WriteEndElement();
                        continue;
                    }
                    else
                    {
                        elem.GenNil = true;
                    }
                }

                if ( elem.ValueGenerator != null )
                {
                    if ( elem.IsFixed )
                    {
                        writer.WriteString(elem.FixedValue);
                    }
                    else if ( elem.HasDefault )
                    {
                        writer.WriteString(elem.DefaultValue);
                    }
                    else
                    {
                        writer.WriteString(elem.ValueGenerator.GenerateValue());
                    }
                }
                else
                {
                    var childGroup = elem.Child;
                    while ( childGroup != null )
                    {
                        ProcessGroup(childGroup);
                        childGroup = childGroup.Sibling;
                    }
                }
                writer.WriteEndElement();
            }
            instanceElementsProcessed.Remove(elem);
        }
Пример #20
0
 private void CheckIfMixed(InstanceElement mixedElem)
 {
     if ( mixedElem.IsMixed )
     {
         writer.WriteString("text");
     }
 }
Пример #21
0
 private void ProcessInstanceTree(InstanceElement rootElement)
 {
     if ( rootElement != null )
     {
         instanceElementsProcessed.Add(rootElement, rootElement);
         writer.WriteStartElement(rootElement.QualifiedName.Name, rootTargetNamespace);
         writer.WriteAttributeString("xmlns", "xsi", null, NsXsi);
         ProcessElementAttrs(rootElement);
         ProcessComment(rootElement);
         CheckIfMixed(rootElement);
         if ( rootElement.ValueGenerator != null )
         {
             if ( rootElement.IsFixed )
             {
                 writer.WriteString(rootElement.FixedValue);
             }
             else if ( rootElement.HasDefault )
             {
                 writer.WriteString(rootElement.DefaultValue);
             }
             else
             {
                 writer.WriteString(rootElement.ValueGenerator.GenerateValue());
             }
         }
         else
         {
             var group = rootElement.Child;
             while ( group != null )
             {
                 ProcessGroup(group);
                 group = group.Sibling;
             }
         }
         writer.WriteEndElement();
     }
     else
     {
         writer.WriteComment("Schema did not lead to generation of a valid XML document");
     }
 }
Пример #22
0
        private bool GenerateElement(XmlSchemaElement e, bool root, InstanceGroup parentElem, XmlSchemaAny any)
        {
            var eGlobalDecl = e;

            if (!e.RefName.IsEmpty)
            {
                eGlobalDecl = (XmlSchemaElement)schemaSet.GlobalElements[e.QualifiedName];
            }
            if (!eGlobalDecl.IsAbstract)
            {
                var elem = (InstanceElement)elementTypesProcessed[eGlobalDecl];
                if (elem != null)
                {
                    Debug.Assert(!root);
                    if (any == null && e.MinOccurs > 0)
                    {                     //If not generating for any or optional ref to cyclic global element
                        var occurs = e.MaxOccurs;
                        if (e.MaxOccurs >= maxThreshold)
                        {
                            occurs = maxThreshold;
                        }
                        if (e.MinOccurs > occurs)
                        {
                            occurs = e.MinOccurs;
                        }
                        parentElem.AddChild(elem.Clone(occurs));
                    }
                    return(false);
                }
                elem = new InstanceElement(eGlobalDecl.QualifiedName);

                if (root)
                {
                    instanceRoot = elem;
                }
                else
                {
                    parentElem.AddChild(elem);
                }

                //Get minOccurs, maxOccurs alone from the current particle, everything else pick up from globalDecl
                if (any != null)
                {                 //Element from any
                    elem.Occurs = any.MaxOccurs >= maxThreshold ? maxThreshold : any.MaxOccurs;
                    elem.Occurs = any.MinOccurs > elem.Occurs ? any.MinOccurs : elem.Occurs;
                }
                else
                {
                    elem.Occurs = e.MaxOccurs >= maxThreshold ? maxThreshold : e.MaxOccurs;
                    elem.Occurs = e.MinOccurs > elem.Occurs ? e.MinOccurs : elem.Occurs;
                }
                elem.DefaultValue = eGlobalDecl.DefaultValue;
                elem.FixedValue   = eGlobalDecl.FixedValue;
                elem.IsNillable   = eGlobalDecl.IsNillable;

                if (eGlobalDecl.ElementSchemaType == AnyType)
                {
                    elem.ValueGenerator = XmlValueGenerator.AnyGenerator;
                }
                else
                {
                    var ct = eGlobalDecl.ElementSchemaType as XmlSchemaComplexType;
                    if (ct != null)
                    {
                        elementTypesProcessed.Add(eGlobalDecl, elem);
                        if (!ct.IsAbstract)
                        {
                            elem.IsMixed = ct.IsMixed;
                            ProcessComplexType(ct, elem);
                        }
                        else
                        {                         // Ct is abstract, need to generate instance elements with xsi:type
                            var dt = GetDerivedType(ct);
                            if (dt != null)
                            {
                                elem.XsiType = dt.QualifiedName;
                                ProcessComplexType(dt, elem);
                            }
                        }
                    }
                    else
                    {                     //elementType is XmlSchemaSimpleType
                        elem.ValueGenerator = XmlValueGenerator.CreateGenerator(eGlobalDecl.ElementSchemaType.Datatype, listLength);
                    }
                }
                if (elem.ValueGenerator != null && elem.ValueGenerator.Prefix == null)
                {
                    elem.ValueGenerator.Prefix = elem.QualifiedName.Name;
                }
                return(true);
            }             // End of e.IsAbstract
            return(false);
        }