Exemple #1
0
        /// <summary> Create XML text from a document </summary>
        public static string PrettyPrint(AXmlDocument doc)
        {
            PrettyPrintAXmlVisitor visitor = new PrettyPrintAXmlVisitor();

            visitor.VisitDocument(doc);
            return(visitor.Output);
        }
Exemple #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);
        }
		/// <summary> Create XML text from a document </summary>
		public static string PrettyPrint(AXmlDocument doc)
		{
			PrettyPrintAXmlVisitor visitor = new PrettyPrintAXmlVisitor();
			visitor.VisitDocument(doc);
			return visitor.Output;
		}
		void Button_Click(object sender, RoutedEventArgs e)
		{
			if (!textDirty) return;
			AXmlDocument doc;
			parser.Lock.EnterWriteLock();
			try {
				doc = parser.Parse(editor.Document.Text, changes);
				changes.Clear();
			} finally {
				parser.Lock.ExitWriteLock();
			}
			if (treeView.Items.Count == 0) {
				treeView.Items.Add(doc);
			}
			PrettyPrintAXmlVisitor visitor = new PrettyPrintAXmlVisitor();
			visitor.VisitDocument(doc);
			string prettyPrintedText = visitor.Output;
			if (prettyPrintedText != editor.Document.Text) {
				MessageBox.Show("Error - Original and pretty printed version of XML differ");
			}
			markerService.RemoveAll(m => true);
			foreach(var error in doc.SyntaxErrors) {
				var marker = markerService.Create(error.StartOffset, error.EndOffset - error.StartOffset);
				marker.Tag = error.Message;
				marker.BackgroundColor = Color.FromRgb(255, 150, 150);
			}
			textDirty = false;
		}