private void GenerateGroupBase(XmlSchemaGroupBase gBase, InstanceGroup grp)
 {
     foreach (XmlSchemaParticle particle1 in gBase.Items)
     {
         GenerateParticle(particle1, false, grp);
     }
 }
 private void ProcessGroup(InstanceGroup grp)
 {
     if (grp is InstanceElement)
     {
         ProcessElement((InstanceElement)grp);
     }
     else   //Its a group node of sequence or choice
     {
         if (!grp.IsChoice)
         {
             for (int i = 0; i < grp.Occurs; i++)
             {
                 InstanceGroup childGroup = grp.Child;
                 while (childGroup != null)
                 {
                     ProcessGroup(childGroup);
                     childGroup = childGroup.Sibling;
                 }
             }
         }
         else
         {
             ProcessChoiceGroup(grp);
         }
     }
 }
示例#3
0
        private void ProcessElement(InstanceElement elem)
        {
            if (instanceElementsProcessed[elem] != null)
            {
                return;
            }
            instanceElementsProcessed.Add(elem, elem);
            for (int i = 0; i < elem.Occurs; i++)
            {
                if (elem.Optional)
                {
                    writer.WriteComment("Optional:");
                }
                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
                {
                    InstanceGroup childGroup = elem.Child;
                    while (childGroup != null)
                    {
                        ProcessGroup(childGroup);
                        childGroup = childGroup.Sibling;
                    }
                }
                writer.WriteEndElement();
            }
            instanceElementsProcessed.Remove(elem);
        }
 private void ProcessChoiceGroup(InstanceGroup grp)
 {
     for (int i = 0; i < grp.Occurs; i++) //Cyclically iterate over the children of choice
     {
         ProcessGroup(grp.GetChild(i % grp.NoOfChildren));
     }
 }
        private void GenerateParticle(XmlSchemaParticle particle, bool root, InstanceGroup iGrp)
        {
            decimal max;

            max = particle.MaxOccurs >= maxThreshold ? maxThreshold : particle.MaxOccurs;
            max = particle.MinOccurs > max ? particle.MinOccurs : max;

            if (particle is XmlSchemaSequence)
            {
                XmlSchemaSequence seq = (XmlSchemaSequence)particle;
                InstanceGroup     grp = new InstanceGroup();
                grp.Occurs = max;
                iGrp.AddChild(grp);
                GenerateGroupBase(seq, grp);
            }
            else if (particle is XmlSchemaChoice)
            {
                XmlSchemaChoice ch = (XmlSchemaChoice)particle;
                if (ch.MaxOccurs == 1)
                {
                    XmlSchemaParticle pt = (XmlSchemaParticle)(ch.Items[0]);
                    GenerateParticle(pt, false, iGrp);
                }
                else
                {
                    InstanceGroup grp = new InstanceGroup();
                    grp.Occurs   = max;
                    grp.IsChoice = true;
                    iGrp.AddChild(grp);
                    GenerateGroupBase(ch, grp);
                }
            }
            else if (particle is XmlSchemaAll)
            {
                GenerateAll((XmlSchemaAll)particle, iGrp);
            }
            else if (particle is XmlSchemaElement)
            {
                XmlSchemaElement elem = particle as XmlSchemaElement;
                XmlSchemaChoice  ch   = null;
                if (!elem.RefName.IsEmpty)
                {
                    ch = GetSubstitutionChoice(elem);
                }
                if (ch != null)
                {
                    GenerateParticle(ch, false, iGrp);
                }
                else
                {
                    GenerateElement(elem, false, iGrp, null);
                }
            }
            else if (particle is XmlSchemaAny && particle.MinOccurs > 0)   //Generate any only if we should
            {
                GenerateAny((XmlSchemaAny)particle, iGrp);
            }
        }
        private void GenerateAll(XmlSchemaAll all, InstanceGroup grp)
        {
            XmlSchemaParticle pt;

            for (int i = all.Items.Count; i > 0; i--)
            {
                pt = (XmlSchemaParticle)(all.Items[i - 1]);
                GenerateParticle(pt, false, grp);
            }
        }
        private InstanceElement GetParentInstanceElement(InstanceGroup grp)
        {
            InstanceElement elem = grp as InstanceElement;

            while (elem == null && grp != null)
            {
                grp  = grp.Parent;
                elem = grp as InstanceElement;
            }
            return(elem);
        }
 internal void AddChild (InstanceGroup obj) {
     obj.Parent = this;
     if (this.child == null) { //If first child
         this.child = obj;
     }
     else {
         InstanceGroup prev = null;
         InstanceGroup next = this.child;
         while (next != null) {
             prev = next;
             next = next.Sibling;
         }
         prev.Sibling = obj;
     }
 }
        internal InstanceGroup GetChild(int index)
        {
            int           curIndex     = 0;
            InstanceGroup currentGroup = this.child;

            while (currentGroup != null)
            {
                if (curIndex == index)
                {
                    return(currentGroup);
                }
                curIndex++;
                currentGroup = currentGroup.Sibling;
            }
            return(null);
        }
        internal InstanceGroup Clone(decimal occurs)
        {
            InstanceGroup newElem = MemberwiseClone() as InstanceGroup;

            newElem.Occurs = occurs;
            newElem.Child  = null;
            int noOfChildren = this.NoOfChildren;

            for (int i = 0; i < noOfChildren; i++)
            {
                var existingChild = this.GetChild(i);
                newElem.AddChild(existingChild.Clone(existingChild.Occurs));
            }
            newElem.Parent  = null;
            newElem.Sibling = null;
            return(newElem);
        }
 internal void AddChild(InstanceGroup obj)
 {
     obj.Parent = this;
     if (this.child == null)   //If first child
     {
         this.child = obj;
     }
     else
     {
         InstanceGroup prev = null;
         InstanceGroup next = this.child;
         while (next != null)
         {
             prev = next;
             next = next.Sibling;
         }
         prev.Sibling = obj;
     }
 }
示例#12
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
         {
             InstanceGroup 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");
     }
 }
示例#13
0
 private void GenerateAll(XmlSchemaAll all, InstanceGroup grp)
 {
     XmlSchemaParticle pt;
     for (int i=all.Items.Count; i > 0; i--) {
         pt = (XmlSchemaParticle)(all.Items[i-1]);
         GenerateParticle(pt,false, grp);
     }
 }
示例#14
0
        private void GenerateAny(XmlSchemaAny any, InstanceGroup grp)
        {
            InstanceElement parentElem = grp as InstanceElement;
            char[] whitespace = new char[] {' ', '\t', '\n', '\r'};
            InstanceElement elem = null;
            XmlSchemaElement anyElem = null;
            string 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" :
                    XmlSchema 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 GenerateAny(XmlSchemaAny any, InstanceGroup grp)
        {
            InstanceElement parentElem = grp as InstanceElement;

            char[]           whitespace    = new char[] { ' ', '\t', '\n', '\r' };
            InstanceElement  elem          = null;
            XmlSchemaElement anyElem       = null;
            string           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":
                XmlSchema 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. ");
                }
            }
        }
示例#16
0
        private bool GenerateElement(XmlSchemaElement e, bool root, InstanceGroup parentElem, XmlSchemaAny any)
        {
            XmlSchemaElement eGlobalDecl = e;

              if (!e.RefName.IsEmpty) {
                eGlobalDecl = (XmlSchemaElement)schemaSet.GlobalElements[e.QualifiedName];
              }
              if (!eGlobalDecl.IsAbstract) {
                InstanceElement 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
                        decimal 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 {
                    XmlSchemaComplexType 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
                            XmlSchemaComplexType 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;
        }
示例#17
0
 private void GenerateGroupBase(XmlSchemaGroupBase gBase, InstanceGroup grp)
 {
     foreach(XmlSchemaParticle particle1 in gBase.Items) {
                 GenerateParticle(particle1, false, grp);
     }
 }
示例#18
0
        private void GenerateParticle(XmlSchemaParticle particle, bool root, InstanceGroup iGrp)
        {
            decimal max;
            max = particle.MaxOccurs >= maxThreshold ? maxThreshold : particle.MaxOccurs;
            max = particle.MinOccurs > max ? particle.MinOccurs : max;

            if (particle is XmlSchemaSequence ) {
                XmlSchemaSequence seq = (XmlSchemaSequence)particle;
                InstanceGroup grp = new InstanceGroup();
                grp.Occurs = max;
                iGrp.AddChild(grp);
                GenerateGroupBase(seq, grp);
            }
            else if (particle is XmlSchemaChoice) {
                XmlSchemaChoice ch = (XmlSchemaChoice)particle;
                if (ch.MaxOccurs == 1) {
                    XmlSchemaParticle pt = (XmlSchemaParticle)(ch.Items[0]);
                    GenerateParticle(pt, false, iGrp);
                }
                else {
                    InstanceGroup grp = new InstanceGroup();
                    grp.Occurs = max;
                    grp.IsChoice = true;
                    iGrp.AddChild(grp);
                    GenerateGroupBase(ch,grp);
                }
            }
            else if (particle is XmlSchemaAll) {
                GenerateAll((XmlSchemaAll)particle, iGrp);
            }
            else if (particle is XmlSchemaElement) {
                XmlSchemaElement elem = particle as XmlSchemaElement;
                XmlSchemaChoice ch = null;
                if (!elem.RefName.IsEmpty) {
                    ch = GetSubstitutionChoice(elem);
                }
                if (ch != null) {
                    GenerateParticle(ch, false, iGrp);
                }
                else {
                    GenerateElement(elem, false, iGrp, null);
                }
            }
            else if (particle is XmlSchemaAny && particle.MinOccurs > 0) { //Generate any only if we should
                GenerateAny((XmlSchemaAny)particle, iGrp);
            }
        }
示例#19
0
        private bool GenerateElement(XmlSchemaElement e, bool root, InstanceGroup parentElem, XmlSchemaAny any)
        {
            XmlSchemaElement eGlobalDecl = e;

            if (!e.RefName.IsEmpty)
            {
                eGlobalDecl = (XmlSchemaElement)schemaSet.GlobalElements[e.QualifiedName];
            }
            if (!eGlobalDecl.IsAbstract)
            {
                InstanceElement 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
                    {
                        decimal 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
                {
                    XmlSchemaComplexType 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
                        {
                            XmlSchemaComplexType 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);
        }
示例#20
0
 private InstanceElement GetParentInstanceElement(InstanceGroup grp)
 {
     InstanceElement elem = grp as InstanceElement;
     while (elem == null && grp != null) {
         grp = grp.Parent;
         elem = grp as InstanceElement;
     }
     return elem;
 }
示例#21
0
 private void ProcessGroup(InstanceGroup grp)
 {
     if(grp is InstanceElement) {
         ProcessElement((InstanceElement)grp);
     }
     else { //Its a group node of sequence or choice
         if(!grp.IsChoice) {
             for (int i=0; i < grp.Occurs; i++) {
                 InstanceGroup childGroup = grp.Child;
                 while (childGroup != null) {
                     ProcessGroup(childGroup);
                     childGroup = childGroup.Sibling;
                 }
             }
         }
         else {
             ProcessChoiceGroup(grp);
         }
     }
 }
示例#22
0
 private void ProcessChoiceGroup(InstanceGroup grp)
 {
     for (int i=0; i < grp.Occurs; i++) { //Cyclically iterate over the children of choice
         ProcessGroup(grp.GetChild(i % grp.NoOfChildren));
     }
 }