Exemple #1
0
 void OnParsed(AXmlObject obj)
 {
     AXmlParser.Log("Parsed {0}", obj);
     trackedSegments.AddParsedObject(obj, this.MaxTouchedLocation > this.CurrentLocation ? (int?)this.MaxTouchedLocation : null);
 }
Exemple #2
0
        public AXmlDocument ReadDocument()
        {
            AXmlDocument doc = new AXmlDocument()
            {
                Parser = parser
            };

            // AXmlParser.Log("Flat stream: {0}", PrintObjects(tags));
            List <AXmlObject> valid = MatchTags(tags);
            // AXmlParser.Log("Fixed stream: {0}", PrintObjects(valid));
            IEnumerator <AXmlObject> validStream = valid.GetEnumerator();

            validStream.MoveNext();             // Move to first
            while (true)
            {
                // End of stream?
                try {
                    if (validStream.Current == null)
                    {
                        break;
                    }
                } catch (InvalidCastException) {
                    break;
                }
                doc.AddChild(ReadTextOrElement(validStream));
            }

            if (doc.Children.Count > 0)
            {
                doc.StartOffset = doc.FirstChild.StartOffset;
                doc.EndOffset   = doc.LastChild.EndOffset;
            }

            // Check well formed
            foreach (AXmlTag xmlDeclaration in doc.Children.OfType <AXmlTag>().Where(t => t.IsProcessingInstruction && t.Name.ToLower() == "xml"))
            {
                if (xmlDeclaration.StartOffset != 0)
                {
                    TagReader.OnSyntaxError(doc, xmlDeclaration.StartOffset, xmlDeclaration.StartOffset + 5,
                                            "XML declaration must be at the start of document");
                }
            }
            int elemCount = doc.Children.OfType <AXmlElement>().Count();

            if (elemCount == 0)
            {
                TagReader.OnSyntaxError(doc, doc.EndOffset, doc.EndOffset,
                                        "Root element is missing");
            }
            if (elemCount > 1)
            {
                AXmlElement next = doc.Children.OfType <AXmlElement>().Skip(1).First();
                TagReader.OnSyntaxError(doc, next.StartOffset, next.StartOffset,
                                        "Only one root element is allowed");
            }
            foreach (AXmlTag tag in doc.Children.OfType <AXmlTag>())
            {
                if (tag.IsCData)
                {
                    TagReader.OnSyntaxError(doc, tag.StartOffset, tag.EndOffset,
                                            "CDATA not allowed in document root");
                }
            }
            foreach (AXmlText text in doc.Children.OfType <AXmlText>())
            {
                if (!text.ContainsOnlyWhitespace)
                {
                    TagReader.OnSyntaxError(doc, text.StartOffset, text.EndOffset,
                                            "Only whitespace is allowed in document root");
                }
            }


            AXmlParser.Log("Constructed {0}", doc);
            trackedSegments.AddParsedObject(doc, null);
            return(doc);
        }