//How can this be made a lot better? or can the algorithm be improved so calling this is not required?
        private bool IsNextChildValid(XmlSchemaElement elem, int startIndex)
        {
            var ct = elem.ElementSchemaType as XmlSchemaComplexType;
            var x  = ct.ContentTypeParticle;

            if (x is XmlSchemaSequence)
            {
                var seq = (XmlSchemaSequence)x;
                if (seq.Items != null && seq.Items.Count > 0)
                {
                    var firstChild = seq.Items[0];
                    if (firstChild is XmlSchemaElement && ((XmlSchemaElement)firstChild).ElementSchemaType is XmlSchemaComplexType)
                    {
                        var dfdlProperties = new DfdlProperties((XmlSchemaElement)firstChild);

                        if (dfdlProperties.Initiator != null && _startPos == 0)
                        {
                            _startPos = dfdlProperties.Initiator.Length;
                        }

                        if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        public string Parse(string filename)
        {
            var fileContents = "";

            var doc            = new XmlDocument();
            var xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            var root = doc.DocumentElement;

            doc.InsertBefore(xmlDeclaration, root);

            var rootNode = doc.AppendChild(doc.CreateElement(string.Empty, "root", string.Empty));

            using (var sr = new StreamReader(filename))
            {
                fileContents = sr.ReadToEnd();
            }

            _lines = fileContents.Split(new string[] { GetVar(_dfdlProperties["outputNewLine"]) }, StringSplitOptions.None);

            int startIndex = 0;

            foreach (XmlSchemaElement elem in
                     _dfdlSchema.Elements.Values)
            {
                var dfdlProperties = new DfdlProperties(elem);


                ProcessElement(elem, ref startIndex, doc, rootNode, dfdlProperties);
            }



            var result = "";
            //var stream = new MemoryStream();
            var stream    = new FileStream(@"d:\dfdltests\output.xml", FileMode.Create);
            var xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);

            xmlWriter.Formatting = Formatting.Indented;

            if (!string.IsNullOrEmpty(this.TranslationXslt))
            {
                var xslTransform = new XslCompiledTransform();
                xslTransform.Load(TranslationXslt);
                xslTransform.Transform(doc, xmlWriter);
            }
            else
            {
                doc.WriteContentTo(xmlWriter);
            }


            xmlWriter.Flush();
            stream.Flush();
            stream.Position = 0;

            return(result);
        }
        private bool ProcessElement(XmlSchemaElement elem, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties)
        {
            bool doMore    = true;
            int  loopIndex = 0;

            if (elem.ElementSchemaType is XmlSchemaComplexType)
            {
                //Repopulate dfdlProperties
                dfdlProperties = new DfdlProperties(elem);
                while ((doMore || IsNextChildValid(elem, startIndex)) && loopIndex < dfdlProperties.MaxOccurs)
                //while (doMore && loopIndex < dfdlProperties.MaxOccurs)
                {
                    if (IsValidInitiator(elem, startIndex))
                    {
                        if (loopIndex == 0)
                        {
                            xmlElement = xmlElement.AppendChild(xmlDoc.CreateElement(elem.Name));
                        }
                        else
                        {
                            xmlElement = xmlElement.ParentNode.InsertAfter(xmlDoc.CreateElement(elem.Name), xmlElement);
                        }
                        var ct =
                            elem.ElementSchemaType as XmlSchemaComplexType;

                        doMore = ProcessSchemaObject(ct.ContentTypeParticle, ref startIndex, xmlDoc, xmlElement,
                                                     dfdlProperties, doMore);
                        loopIndex++;
                    }
                    else
                    {
                        doMore = false;
                    }
                }
            }

            if (xmlElement != null)
            {
                if (dfdlProperties.Initiator != null && _startPos == 0)
                {
                    _startPos = dfdlProperties.Initiator.Length;
                }

                if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
                {
                    ParseField(elem, ref startIndex, xmlDoc, xmlElement);
                    if (_lines[startIndex].Length <= _startPos + 1)
                    {
                        startIndex++;
                        _startPos = 0;
                    }
                    return(true);
                }
            }
            return(false);
        }
        private bool IsValidInitiator(XmlSchemaElement elem, int startIndex)
        {
            var dfdlProperties = new DfdlProperties(elem);


            if (dfdlProperties.Initiator != null && _startPos == 0)
            {
                _startPos = dfdlProperties.Initiator.Length;
            }

            if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
            {
                return(true);
            }

            return(false);
        }
 private bool ProcessItemCollection(XmlSchemaObjectCollection objs, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     foreach (XmlSchemaObject obj in objs)
         doMore = ProcessSchemaObject(obj, ref startIndex, xmlDoc,xmlElement, dfdlProperties, doMore);
     return doMore;
 }
 private bool ProcessChoice(XmlSchemaChoice choice, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     Console.WriteLine("Choice");
     ProcessItemCollection(choice.Items, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
     return true;
 }
 private bool ProcessSequence(XmlSchemaSequence sequence, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     doMore = ProcessItemCollection(sequence.Items, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
     return doMore;
 }
        private bool IsValidInitiator(XmlSchemaElement elem, int startIndex)
        {
            var dfdlProperties = new DfdlProperties(elem);

            
            if (dfdlProperties.Initiator != null && _startPos == 0)
                _startPos = dfdlProperties.Initiator.Length;

            if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
            {
                return true;
            }
                    
            return false;
        }
        //How can this be made a lot better? or can the algorithm be improved so calling this is not required?
        private bool IsNextChildValid(XmlSchemaElement elem, int startIndex)
        {
            var ct = elem.ElementSchemaType as XmlSchemaComplexType;
            var x = ct.ContentTypeParticle;

            if (x is XmlSchemaSequence)
            {
                var seq = (XmlSchemaSequence) x;
                if (seq.Items != null && seq.Items.Count > 0)
                {
                    var firstChild = seq.Items[0];
                    if (firstChild is XmlSchemaElement && ((XmlSchemaElement)firstChild).ElementSchemaType is XmlSchemaComplexType)
                    {
                        var dfdlProperties = new DfdlProperties((XmlSchemaElement) firstChild);

                        if (dfdlProperties.Initiator != null && _startPos == 0)
                            _startPos = dfdlProperties.Initiator.Length;

                        if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
                        {
                            return true;
                        }

                    }
                }
            }

            return false;
        }
        private bool ProcessElement(XmlSchemaElement elem, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties)
        {
            bool doMore = true;
            int loopIndex = 0;
            
            if (elem.ElementSchemaType is XmlSchemaComplexType)
            {
                //Repopulate dfdlProperties
                dfdlProperties = new DfdlProperties(elem);
                while ((doMore || IsNextChildValid(elem, startIndex) ) && loopIndex < dfdlProperties.MaxOccurs)
                //while (doMore && loopIndex < dfdlProperties.MaxOccurs)
                {
                    if (IsValidInitiator(elem, startIndex))
                    {
                        if (loopIndex == 0)
                            xmlElement = xmlElement.AppendChild(xmlDoc.CreateElement(elem.Name));
                        else
                            xmlElement = xmlElement.ParentNode.InsertAfter(xmlDoc.CreateElement(elem.Name), xmlElement);
                        var ct =
                            elem.ElementSchemaType as XmlSchemaComplexType;

                        doMore = ProcessSchemaObject(ct.ContentTypeParticle, ref startIndex, xmlDoc, xmlElement,
                            dfdlProperties, doMore);
                        loopIndex++;
                    }
                    else
                    {
                        doMore = false;
                    }
                    
                }


            }

            if (xmlElement != null)
            {
                if(dfdlProperties.Initiator != null && _startPos == 0)
                    _startPos = dfdlProperties.Initiator.Length;

                if (startIndex < _lines.Count() && _lines[startIndex].StartsWith(dfdlProperties.Initiator))
                {
                    ParseField(elem, ref startIndex, xmlDoc, xmlElement);
                    if (_lines[startIndex].Length <= _startPos + 1)
                    {
                        startIndex++;
                        _startPos = 0;
                    }
                    return true;
                }

                    
            }
            return false;

        }
Esempio n. 11
0
 private bool ProcessItemCollection(XmlSchemaObjectCollection objs, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     foreach (XmlSchemaObject obj in objs)
     {
         doMore = ProcessSchemaObject(obj, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
     }
     return(doMore);
 }
Esempio n. 12
0
 private bool ProcessChoice(XmlSchemaChoice choice, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     Console.WriteLine("Choice");
     ProcessItemCollection(choice.Items, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
     return(true);
 }
Esempio n. 13
0
 private bool ProcessSequence(XmlSchemaSequence sequence, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
 {
     doMore = ProcessItemCollection(sequence.Items, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
     return(doMore);
 }
Esempio n. 14
0
        private void ParseField(XmlSchemaElement elem, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties)
        {
            var attr = elem.UnhandledAttributes.SingleOrDefault(x => x.Name == "dfdl:length");

            if (attr != null)
            {
                int length = int.Parse(attr.Value);
                var value  = _lines[startIndex].Substring(_startPos, length);

                var newElement = xmlDoc.CreateElement(elem.Name);
                newElement.InnerText = value;
                xmlElement.AppendChild(newElement);
                _startPos += length;
            }
        }
        private bool ProcessSchemaObject(XmlSchemaObject obj, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
        {
            if (obj is XmlSchemaElement)
                doMore = ProcessElement(obj as XmlSchemaElement, ref startIndex, xmlDoc, xmlElement, dfdlProperties);
            if (obj is XmlSchemaChoice)
                doMore = ProcessChoice(obj as XmlSchemaChoice, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
            if (obj is XmlSchemaSequence)
                doMore = ProcessSequence( obj as XmlSchemaSequence, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);

            return doMore;
        }
Esempio n. 16
0
        private bool ProcessSchemaObject(XmlSchemaObject obj, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties, bool doMore)
        {
            if (obj is XmlSchemaElement)
            {
                doMore = ProcessElement(obj as XmlSchemaElement, ref startIndex, xmlDoc, xmlElement, dfdlProperties);
            }
            if (obj is XmlSchemaChoice)
            {
                doMore = ProcessChoice(obj as XmlSchemaChoice, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
            }
            if (obj is XmlSchemaSequence)
            {
                doMore = ProcessSequence(obj as XmlSchemaSequence, ref startIndex, xmlDoc, xmlElement, dfdlProperties, doMore);
            }

            return(doMore);
        }
        public string Parse(string filename)
        {
            var fileContents = "";

            var doc = new XmlDocument();
            var xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            var root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);

            var rootNode = doc.AppendChild(doc.CreateElement(string.Empty, "root", string.Empty));

            using (var sr = new StreamReader(filename))
            {
                fileContents = sr.ReadToEnd();
            }

            _lines = fileContents.Split(new string[] { GetVar(_dfdlProperties["outputNewLine"]) }, StringSplitOptions.None);

            int startIndex = 0;

            foreach (XmlSchemaElement elem in
                                _dfdlSchema.Elements.Values)
            {
                var dfdlProperties = new DfdlProperties(elem);


                ProcessElement(elem, ref startIndex, doc, rootNode, dfdlProperties);
            }

            
            
            var result = "";
            //var stream = new MemoryStream();
            var stream = new FileStream(@"d:\dfdltests\output.xml", FileMode.Create);
            var xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);
            xmlWriter.Formatting = Formatting.Indented;

            if (!string.IsNullOrEmpty(this.TranslationXslt))
            {
                var xslTransform = new XslCompiledTransform();
                xslTransform.Load(TranslationXslt);
                xslTransform.Transform(doc, xmlWriter);
            }
            else
            {
                doc.WriteContentTo(xmlWriter);
            }

            
            xmlWriter.Flush();
            stream.Flush();
            stream.Position = 0;

            return result;


        }
        private void ParseField(XmlSchemaElement elem, ref int startIndex, XmlDocument xmlDoc, XmlNode xmlElement, DfdlProperties dfdlProperties)
        {
            var attr = elem.UnhandledAttributes.SingleOrDefault(x => x.Name == "dfdl:length");
            if (attr != null)
            {
                int length = int.Parse(attr.Value);
                var value = _lines[startIndex].Substring(_startPos, length);

                var newElement = xmlDoc.CreateElement(elem.Name);
                newElement.InnerText = value;
                xmlElement.AppendChild(newElement);
                _startPos += length;
            }
            


        }