curcuru 2002/07/13 16:30:29
Modified: test/java/src/org/apache/qetest/xsl XHTComparator.java
XHTComparatorXSLTC.java
Log:
XHTComparator: use new Tidy to parse HTML (patch from:
Submitted by: Gunnar Klauberg <[EMAIL PROTECTED]>)
XHTComparatorXSLTC remove parse() method since
parent class impl is now the same
Revision Changes Path
1.10 +15 -4
xml-xalan/test/java/src/org/apache/qetest/xsl/XHTComparator.java
Index: XHTComparator.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/xsl/XHTComparator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- XHTComparator.java 6 Jun 2002 17:17:11 -0000 1.9
+++ XHTComparator.java 13 Jul 2002 23:30:29 -0000 1.10
@@ -64,6 +64,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
+import java.net.URL;
import java.util.Properties;
import java.util.StringTokenizer;
@@ -85,6 +86,10 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
+
+// Tidy from w3c for parsing HTML
+import org.w3c.tidy.Tidy;
+
/**
* Uses an XML/HTML/Text diff comparator to check or diff two files.
* <p>Given two files, an actual test result and a known good or 'gold'
@@ -722,9 +727,15 @@
try
{
- // @todo need to find an HTML to DOM parser we can use!!!
- // doc = someHTMLParser.parse(new InputSource(filename));
- throw new RuntimeException("XHTComparator no HTML parser!");
+ // Use the copy of Tidy that the XSLTC team has checked in
+ // Submitted by: Gunnar Klauberg <[EMAIL PROTECTED]>
+ // Alternate by: [EMAIL PROTECTED]
+ Tidy tidy = new Tidy();
+ tidy.setXHTML(true);
+ tidy.setTidyMark(false);
+ tidy.setShowWarnings(false);
+ tidy.setQuiet(true);
+ doc = tidy.parseDOM(new URL(docURI).openStream(), null);
}
catch (Exception e)
{
1.3 +4 -127
xml-xalan/test/java/src/org/apache/qetest/xsl/XHTComparatorXSLTC.java
Index: XHTComparatorXSLTC.java
===================================================================
RCS file:
/home/cvs/xml-xalan/test/java/src/org/apache/qetest/xsl/XHTComparatorXSLTC.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- XHTComparatorXSLTC.java 21 Jun 2002 14:29:27 -0000 1.2
+++ XHTComparatorXSLTC.java 13 Jul 2002 23:30:29 -0000 1.3
@@ -91,9 +91,6 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
-import org.w3c.tidy.*;
-import java.net.URLConnection;
-
/**
* Defines XSLTC's XML/HTML/Text diff comparator to check or diff two files.
* This comparator uses the expanded name instead of the qname to compare
@@ -108,134 +105,14 @@
*/
public class XHTComparatorXSLTC extends XHTComparator
{
- /**
- * Simple worker method to parse filename to a Document.
- *
- * <p>Attempts XML parse, if that throws an exception, then
- * we attempt an HTML parse (when parser available), if
- * that throws an exception, then we parse as text:
- * we construct a faux document element to hold it all,
- * and then parse by readLine() and put each line of
- * text into a <line> element.</p>
- *
- * @param filename to parse as a local path
- * @param reporter PrintWriter to dump status info to
- * @param which either TEST or GOLD file being parsed
- * @param attributes name=value pairs to set on the
- * DocumentBuilderFactory that we use to parse
- *
- * @return Document object with contents of the file;
- * otherwise throws an unchecked RuntimeException if there
- * is any fatal problem
- */
- Document parse(String filename, PrintWriter reporter, String which,
Properties attributes)
- {
- // Force filerefs to be URI's if needed: note this is independent of
any other files
- String docURI = QetestUtils.filenameToURL(filename);
-
- DocumentBuilderFactory dfactory =
DocumentBuilderFactory.newInstance();
- // Always set namespaces on
- dfactory.setNamespaceAware(true);
- // Set other attributes here as needed
- applyAttributes(dfactory, attributes);
-
- // Local class: cheap non-printing ErrorHandler
- // This is used to suppress validation warnings which
- // would otherwise clutter up the console
- ErrorHandler nullHandler = new ErrorHandler() {
- public void warning(SAXParseException e) throws SAXException {}
- public void error(SAXParseException e) throws SAXException {}
- public void fatalError(SAXParseException e) throws SAXException
- {
- throw e;
- }
- };
-
- String parseType = which + PARSE_TYPE + "[xml];";
- Document doc = null;
- try
- {
- // First, attempt to parse as XML (preferred)...
- DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
- docBuilder.setErrorHandler(nullHandler);
- doc = docBuilder.parse(new InputSource(docURI));
- }
- catch (Throwable se)
- {
- // ... if we couldn't parse as XML, attempt parse as HTML...
- reporter.println(WARNING + se.toString());
- parseType = which + PARSE_TYPE + "[html];";
-
- try
- {
- Tidy tidy = new Tidy();
- tidy.setXHTML(true);
- tidy.setTidyMark(false);
- tidy.setShowWarnings(false);
- tidy.setQuiet(true);
- doc = tidy.parseDOM(new URL(docURI).openStream(), null);
-
- // @todo need to find an HTML to DOM parser we can use!!!
- // doc = someHTMLParser.parse(new InputSource(filename));
- // throw new RuntimeException("XHTComparator no HTML
parser!");
- }
- catch (Exception e)
- {
- // ... if we can't parse as HTML, then just parse the text
- try
- {
- reporter.println(WARNING + e.toString());
- parseType = which + PARSE_TYPE + "[text];";
-
- // First build a faux document with parent element
- DocumentBuilder docBuilder =
dfactory.newDocumentBuilder();
- doc = docBuilder.newDocument();
- Element outElem = doc.createElement("out");
-
- // Parse as text, line by line
- // Since we already know it should be text, this
should
- // work better than parsing by bytes.
- FileReader fr = new FileReader(filename);
- BufferedReader br = new BufferedReader(fr);
- for (;;)
- {
- String tmp = br.readLine();
-
- if (tmp == null)
- {
- break;
- }
- // An additional thing we could do would
- // be to put in the line number in the
- // file in here somehow, so when users
- // view reports, they get that info
- Element lineElem = doc.createElement("line");
- outElem.appendChild(lineElem);
- Text textNode = doc.createTextNode(tmp);
- lineElem.appendChild(textNode);
- }
- // Now stick the whole element into the document to
return
- doc.appendChild(outElem);
- }
- catch (Throwable throwable)
- {
- reporter.println(OTHER_ERROR + filename + SEPARATOR
- + "threw:" + throwable.toString());
- }
- }
- }
-
- // Output a newline here for readability
- reporter.println(parseType);
-
- return doc;
- } // end of parse()
-
/**
* The contract is: when you enter here the gold and test nodes are the
same type,
* both non-null, and both in the same basic position in the tree.
* //@todo verify caller really performs for the contract -sc
+ *
+ * This overridden method does additional checking of namespaces
+ * and local names, instead of just getNodeName().
*
* @param gold gold or expected node
* @param test actual node
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]