Exemplo n.º 1
0
        /// <summary>
        ///   Validates the XML against the schema.
        /// </summary>
        /// <returns><lang langref="true" /> if the XML is valid; <lang langref="false" /> otherwise.</returns>
        public bool ValidateXml()
        {
            m_tmrValidator.Stop();

            var docDocument = ActiveTextAreaControl.TextArea.Document;

            docDocument.MarkerStrategy.RemoveAll(x =>
            {
                return(x.TextMarkerType == TextMarkerType.WaveLine);
            });
            m_booMalformedXml = false;

            if (docDocument.TextLength == 0)
            {
                return(true);
            }

            var stkBadTags = XmlParser.ParseTags(docDocument, docDocument.TotalNumberOfLines - 1, null,
                                                 HighlightMalformedTag);

            //this deals with extra tags at beginning of file
            if ((stkBadTags.Count > 0) || m_booMalformedXml)
            {
                while (stkBadTags.Count > 0)
                {
                    var tpsTag   = stkBadTags.Pop();
                    var tlcStart = new TextLocation(tpsTag.Column, tpsTag.LineNumber);
                    var tlcEnd   = new TextLocation(tpsTag.Column + tpsTag.Name.Length, tpsTag.LineNumber);
                    HighlightMalformedTag(docDocument, tpsTag.Name, tlcStart, tlcEnd);
                }
                return(false);
            }

            var intBadLineNum = Int32.MaxValue;

            using (var xrdValidator = XmlReader.Create(new StringReader(Text), m_xrsSettings))
            {
                try
                {
                    while (xrdValidator.Read())
                    {
                    }
                    if (m_booMalformedXml)
                    {
                        return(false);
                    }
                }
                catch (XmlException err2)
                {
                    intBadLineNum = err2.LineNumber;
                    HighlightValidationErrors(err2.Message, new TextLocation(err2.LinePosition - 1, err2.LineNumber - 1));
                }
                finally
                {
                    xrdValidator.Close();
                }
            }
            for (var i = intBadLineNum; i < docDocument.TotalNumberOfLines; i++)
            {
                var strLine        = docDocument.GetText(docDocument.GetLineSegment(i));
                var intLastOpenPos = strLine.LastIndexOf('<');
                if (intLastOpenPos < 0)
                {
                    continue;
                }
                var intLastClosePos = strLine.LastIndexOf('>');
                if ((intLastClosePos > -1) && (intLastOpenPos > intLastClosePos))
                {
                    var stbLines = new StringBuilder(strLine);
                    //there is an open tag on this line - read lines until it is closed.
                    for (; i < docDocument.TotalNumberOfLines; i++)
                    {
                        var strNextLine = docDocument.GetText(docDocument.GetLineSegment(i));
                        intLastClosePos = strLine.LastIndexOf('>');
                        stbLines.Append(strNextLine);
                        if (intLastClosePos < 0)
                        {
                            i--;
                            break;
                        }
                    }
                    strLine = stbLines.ToString();
                }

                var mclLineTags = rgxTagContents.Matches(strLine);
                foreach (Match mtcTag in mclLineTags)
                {
                    var strTag = mtcTag.Groups[1].Value.Trim();
                    if (strTag.StartsWith("/"))
                    {
                        HighlightValidationErrors("Unexpected end tag.", new TextLocation(mtcTag.Groups[1].Index + 1, i));
                    }
                    else
                    {
                        HighlightValidationErrors("Invalid tag.", new TextLocation(mtcTag.Groups[1].Index, i));
                    }
                }
            }
            return(intBadLineNum == Int32.MaxValue);
        }