예제 #1
0
        /// <summary> Create canonical text from a document </summary>
        public static string Print(AXmlDocument doc)
        {
            CanonicalPrintAXmlVisitor visitor = new CanonicalPrintAXmlVisitor();

            visitor.VisitDocument(doc);
            return(visitor.Output);
        }
예제 #2
0
        /// <remarks>
        /// If using DTD, canonical representation is not checked
        /// If using DTD, uknown entiry references are not error
        /// </remarks>
        bool TestFile(TestFile testFile, bool isWellFormed)
        {
            bool passed = true;

            string content = testFile.Content;

            Debug.WriteLine("Testing " + testFile.Name + "...");
            AXmlParser parser = new AXmlParser();

            bool usingDTD = content.Contains("<!DOCTYPE") && (content.Contains("<!ENTITY") || content.Contains(" SYSTEM "));

            if (usingDTD)
            {
                parser.UnknownEntityReferenceIsError = false;
            }

            AXmlDocument document;

            parser.Lock.EnterWriteLock();
            try {
                document = parser.Parse(content, null);
            } finally {
                parser.Lock.ExitWriteLock();
            }

            string printed = PrettyPrintAXmlVisitor.PrettyPrint(document);

            if (content != printed)
            {
                errorOutput.AppendFormat("Output of pretty printed XML for \"{0}\" does not match the original.\n", testFile.Name);
                errorOutput.AppendFormat("Pretty printed:\n{0}\n", Indent(printed));
                passed = false;
            }

            if (isWellFormed && !usingDTD)
            {
                string canonicalPrint = CanonicalPrintAXmlVisitor.Print(document);
                if (testFile.Canonical != null)
                {
                    if (testFile.Canonical != canonicalPrint)
                    {
                        errorOutput.AppendFormat("Canonical XML for \"{0}\" does not match the excpected.\n", testFile.Name);
                        errorOutput.AppendFormat("Expected:\n{0}\n", Indent(testFile.Canonical));
                        errorOutput.AppendFormat("Seen:\n{0}\n", Indent(canonicalPrint));
                        passed = false;
                    }
                }
                else
                {
                    errorOutput.AppendFormat("Can not find canonical output for \"{0}\"", testFile.Name);
                    errorOutput.AppendFormat("Suggested canonical output:\n{0}\n", Indent(canonicalPrint));
                    passed = false;
                }
            }

            bool hasErrors = document.SyntaxErrors.FirstOrDefault() != null;

            if (isWellFormed && hasErrors)
            {
                errorOutput.AppendFormat("Syntax error(s) in well formed file \"{0}\":\n", testFile.Name);
                foreach (var error in document.SyntaxErrors)
                {
                    string followingText = content.Substring(error.StartOffset, Math.Min(10, content.Length - error.StartOffset));
                    errorOutput.AppendFormat("Error ({0}-{1}): {2} (followed by \"{3}\")\n", error.StartOffset, error.EndOffset, error.Message, followingText);
                }
                passed = false;
            }

            if (!isWellFormed && !hasErrors)
            {
                errorOutput.AppendFormat("No syntax errors reported for mallformed file \"{0}\"\n", testFile.Name);
                passed = false;
            }

            // Epilog
            if (!passed)
            {
                if (testFile.Description != null)
                {
                    errorOutput.AppendFormat("Test description:\n{0}\n", Indent(testFile.Description));
                }
                errorOutput.AppendFormat("File content:\n{0}\n", Indent(content));
                errorOutput.AppendLine();
            }

            return(passed);
        }
예제 #3
0
		/// <summary> Create canonical text from a document </summary>
		public static string Print(AXmlDocument doc)
		{
			CanonicalPrintAXmlVisitor visitor = new CanonicalPrintAXmlVisitor();
			visitor.VisitDocument(doc);
			return visitor.Output;
		}