/// <summary> /// Parse an XML document. /// <para> /// The application can use this method to instruct the XML /// reader to begin parsing an XML document from any valid input /// source (a character stream, a byte stream, or a URI). /// </para> /// <para> /// Applications may not invoke this method while a parse is in /// progress (they should create a new XMLReader instead for each /// nested XML document). Once a parse is complete, an /// application may reuse the same XMLReader object, possibly with a /// different input source. /// Configuration of the XMLReader object (such as handler bindings and /// values established for feature flags and properties) is unchanged /// by completion of a parse, unless the definition of that aspect of /// the configuration explicitly specifies other behavior. /// (For example, feature flags or properties exposing /// characteristics of the document being parsed.) /// </para> /// <para> /// During the parse, the XMLReader will provide information /// about the XML document through the registered event /// handlers. /// </para> /// <para> /// This method is synchronous: it will not return until parsing /// has ended. If a client application wants to terminate /// parsing early, it should throw an exception. /// </para> /// </summary> /// <param name="input"> /// The input source for the top-level of the /// XML document. /// </param> /// <exception cref="SAXException"> /// Any SAX exception, possibly /// wrapping another exception. /// </exception> /// <exception cref="IOException"> /// An IO exception from the parser, /// possibly from a byte stream or character stream /// supplied by the application. /// </exception> /// <seealso cref="InputSource" /> /// <seealso cref="Parse(string)" /> /// <seealso cref="EntityResolver" /> /// <seealso cref="DTDHandler" /> /// <seealso cref="ContentHandler" /> /// <seealso cref="ErrorHandler" /> public virtual void Parse(InputSource input) { SetupParse(); _parent.Parse(input); }
public void Parse(InputSource input) { SetupParse(); parent.Parse(input); }
public TestSuite(InputSource input, Stream output, string testPattern, IXmlReader reader, string saxGenerate, string saxExpected, bool console) { this.reader = reader; this.saxGenerate = saxGenerate; this.saxExpected = saxExpected; this.console = console; this.input = input; this.output = output; this.testPattern = testPattern; this.writer = new StreamWriter(output); writer.AutoFlush = true; // Test features and properties PrepareTest(reader); // Set the reader for the test document and parse reader.ContentHandler = this; reader.Parse(input); // Write the end FinalizeTest(); }
/// <summary> /// Main application entry point. /// </summary> /// <param name="args"></param> public static void Main(params string[] args) { Console.WriteLine("************************************" + "************************************"); Console.WriteLine("* Testing SAX2"); Console.WriteLine("************************************" + "************************************"); Console.Write("\n"); IXmlReader reader = XmlReaderFactory.Current.CreateXmlReader(); Console.WriteLine("XMLReader created successfully\n"); // // Check features. // Console.WriteLine("Checking defaults for some well-known features:"); checkFeature(reader, "http://xml.org/sax/features/namespaces"); checkFeature(reader, "http://xml.org/sax/features/namespace-prefixes"); checkFeature(reader, "http://xml.org/sax/features/string-interning"); checkFeature(reader, "http://xml.org/sax/features/validation"); checkFeature(reader, "http://xml.org/sax/features/external-general-entities"); checkFeature(reader, "http://xml.org/sax/features/external-parameter-entities"); Console.Write("\n"); // // Assign handlers. // Console.WriteLine("Creating and assigning handlers\n"); var handler = new SAXTest(); reader.ContentHandler = handler; reader.ErrorHandler = handler; // // Parse documents. // if (args.Length > 0) { foreach (string arg in args) { String systemId = makeAbsoluteURL(arg); Console.WriteLine("Trying file " + systemId); try { reader.Parse(systemId); } catch (SAXException e1) { Console.WriteLine(systemId + " failed with XML error: " + e1.Message); } catch (IOException e2) { Console.WriteLine(systemId + " failed with I/O error: " + e2.Message); } Console.Write("\n"); } } else { Console.WriteLine("No documents supplied on command line; " + "parsing skipped."); } // // Done. // Console.WriteLine("SAX2 test finished."); }
/// <summary> /// This takes the collected test information, creates the parser and attempts to run the /// test. In general, it is a safe process and output will be written to the output stream. /// The RunTest() function attempts to configure the parser based on the requirements of /// the test. It also increments the testCount, unsupportedCount and failCount (if applicable). /// </summary> public void RunTest(IXmlReader parser) { bool expectFatalError = true; bool expectError = true; bool optionalFatalError = false; bool optionalError = false; // Check if this matches the testPattern if (testPattern != null) if (!Regex.IsMatch(currId, testPattern)) return; try { // All wrapped in a try try { Uri f = new Uri(new Uri(testLoc.SystemId), currUri); writer.WriteLine("<TEST ENTITIES='" + currEntities + "' " + "ID='" + currId + "' "+ "NAMESPACES='" + currNamespaces + "' "+ "RECOMMENDATION='" + currRecommendation + "' "+ "SECTIONS='" + currSections + "' "+ "TYPE='" + currType + "' "+ "URI='" + f.AbsoluteUri + "' "+ "VERSION='" + currVersion + "'>"); writer.WriteLine("<message>" + Escape(currContent.ToString()) + "</message>"); // Check whether or not NAMESPACES are required if ("yes".Equals(currNamespaces)) { if (!supportsNamespaces) { writer.WriteLine("<unsupported>Namespace processing</unsupported>"); unsupportedCount++; expectFatalError = false; expectError = false; fatalCount = 0; errorCount = 0; warningCount = 0; return; } else { SetFeature(SaxConsts.NamespacesFeature, parser, true); SetFeature(SaxConsts.NamespacePrefixesFeature, parser, false); } } else { SetFeature(SaxConsts.NamespacesFeature, parser, false); SetFeature(SaxConsts.NamespacePrefixesFeature, parser, true); } // Check whether or not ENTITIES are required if ("both".Equals(currEntities)) { if (!supportsGeneralEntityResolution || !supportsParameterEntityResolution) { writer.WriteLine("<unsupported>Entity resolution</unsupported>"); unsupportedCount++; expectFatalError = false; expectError = false; fatalCount = 0; errorCount = 0; warningCount = 0; return; } else { SetFeature(SaxConsts.ExternalGeneralFeature, parser, true); SetFeature(SaxConsts.ExternalParameterFeature, parser, true); } } else if ("parameter".Equals(currEntities)) { if (!supportsParameterEntityResolution) { writer.WriteLine("<unsupported>Parameter entity resolution</unsupported>"); unsupportedCount++; expectFatalError = false; expectError = false; fatalCount = 0; errorCount = 0; warningCount = 0; return; } else { SetFeature(SaxConsts.ExternalParameterFeature, parser, true); } } else if ("general".Equals(currEntities)) { if (!supportsGeneralEntityResolution) { writer.WriteLine("<unsupported>General entity resolution</unsupported>"); unsupportedCount++; expectFatalError = false; expectError = false; fatalCount = 0; errorCount = 0; warningCount = 0; return; } else { SetFeature(SaxConsts.ExternalGeneralFeature, parser, true); } } // Check for the RECOMMENDATION if ("1.1".Equals(currVersion) || "XML1.1".Equals(currRecommendation) || "NS1.1".Equals(currRecommendation)) { if (!supportsXml11) { writer.WriteLine("<unsupported>XML 1.1</unsupported>"); unsupportedCount++; expectFatalError = false; expectError = false; fatalCount = 0; errorCount = 0; warningCount = 0; return; } else { SetFeature(SaxConsts.Xml11Feature, parser, true); } } // Check the TYPE if ("valid".Equals(currType)) { expectFatalError = false; expectError = false; if (supportsValidation) SetFeature(SaxConsts.ValidationFeature, parser, true); } else if ("invalid".Equals(currType)) { expectFatalError = false; if (supportsValidation) { expectError = true; SetFeature(SaxConsts.ValidationFeature, parser, true); } else { expectError = false; // This is debateable, a non-validating parser may or may not raise an Error optionalError = true; } } else if ("not-wf".Equals(currType)) { // no validation expectFatalError = true; expectError = false; optionalError = true; } else if ("error".Equals(currType)) { // not required expectFatalError = false; expectError = false; optionalFatalError = true; optionalError = true; } // reset error count fatalCount = 0; errorCount = 0; warningCount = 0; // Try to parse if (saxGenerate == null) { parser.ErrorHandler = this; parser.Parse(f.AbsoluteUri); } else { DirectoryInfo confDir = new DirectoryInfo(saxGenerate); Uri baseUri = new Uri(testLoc.SystemId); if (!baseUri.IsFile) throw new ApplicationException("Not a file URI"); DirectoryInfo baseDir = new DirectoryInfo(baseUri.LocalPath); string basePath = ""; while (baseDir != null && baseDir.Name != "xmlconf") { basePath = Path.Combine(baseDir.Name, basePath); baseDir = baseDir.Parent; } string confPath = Path.Combine(confDir.FullName, basePath); confPath = Path.GetDirectoryName(confPath); FileInfo confFile = new FileInfo(Path.Combine(confPath, currUri)); Directory.CreateDirectory(confFile.DirectoryName); StreamWriter sw = new StreamWriter(confFile.FullName); sw.NewLine = "\n"; XmlTextWriter confWriter = new XmlTextWriter(sw); confWriter.Formatting = Formatting.Indented; confWriter.Indentation = 4; ConformanceReportHandler handler = new ConformanceReportHandler(confWriter, this); handler.Initialize(); parser.ContentHandler = handler; parser.DtdHandler = handler; parser.ErrorHandler = handler; parser.EntityResolver = handler; try { parser.Parse(f.AbsoluteUri); } catch (SaxException) { } catch (IOException) { } catch (Exception ex) { confWriter.WriteStartElement("bug"); confWriter.WriteAttributeString("reason", "Parser should only throw SAXExceptions"); confWriter.WriteAttributeString("type", ex.GetType().Name); confWriter.WriteString(Escape(ex.Message)); confWriter.WriteEndElement(); } handler.Finish(); // Close the writer confWriter.Close(); // Now, let's just go ahead and make the comparisson while we are at it if (saxExpected != null) { DirectoryInfo expectedDir = new DirectoryInfo(saxExpected); string expectedPath = Path.Combine(expectedDir.FullName, basePath); expectedPath = Path.GetDirectoryName(expectedPath); FileInfo expectedFile = new FileInfo(Path.Combine(expectedPath, currUri)); ResultComparer rc = new ResultComparer(); string confError = ""; bool confPassed = rc.compare(expectedFile.FullName, confFile.FullName, out confError); rc.makeResultDoc(f.LocalPath, expectedFile.FullName, confFile.FullName, Escape(currContent.ToString()), currId, confPassed, confError); writer.WriteLine("<conformance>"); if (confPassed) writer.WriteLine("<pass>true</pass>"); else { writer.WriteLine("<pass>false</pass>"); conformanceFailCount++; } writer.WriteLine("<actual>" + confFile.FullName + "</actual>"); writer.WriteLine("<expected>" + expectedFile.FullName + "</expected>"); writer.WriteLine("<output>" + Path.ChangeExtension(confFile.FullName, ".html") + "</output>"); writer.WriteLine("</conformance>"); } } } catch (Exception e) { writer.WriteLine("<exception>" + Escape(e.Message) + "</exception>"); fatalCount++; } } finally { if ((((expectError || optionalError) && errorCount > 0) || (!expectError && errorCount == 0)) && (((expectFatalError || optionalFatalError) && fatalCount > 0) || (!expectFatalError && fatalCount == 0))) { writer.WriteLine("<pass>true</pass>"); passCount++; } else { writer.WriteLine("<pass>false</pass>"); failCount++; } writer.WriteLine("</TEST>"); } }