[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/parsing HTMLParserListener.java,NONE,1.1
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-12-25 15:23:14
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing
In directory sc8-pr-cvs1:/tmp/cvs-serv12005/src/com/meterware/httpunit/parsing
Modified Files:
DocumentAdapter.java HTMLParser.java HTMLParserFactory.java
JTidyHTMLParser.java JTidyPrintWriter.java NekoHTMLParser.java
ScriptFilter.java
Added Files:
HTMLParserListener.java package.html
Log Message:
General cleanup of the parser classes
***** Error reading new file[Errno 2] No such file or directory: 'HTMLParserListener.java'
--- NEW FILE ---
<HTML><BODY>
<p>Classes to control HTML parsing. The primary access to this package is through
{@link com.meterware.httpunit.parsing.HTMLParserFactory}, which allows a user to select the HTML parser that will be used
to parse HTML pages returned by a {@link com.meterware.httpunit.WebClient}. In addition to selecting the actual class,
the factory also permits various options to be set on whichever parser is chosen - with the caveat that not every
parser supports every property. If the current parser does not in fact support a property, setting it on the factory
will have no effect.
</body></html>
Index: DocumentAdapter.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/DocumentAdapter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DocumentAdapter.java 24 Dec 2002 17:17:03 -0000 1.1
+++ DocumentAdapter.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -1,11 +1,4 @@
package com.meterware.httpunit.parsing;
-
-import org.w3c.dom.Node;
-
-import java.io.IOException;
-
-import com.meterware.httpunit.HTMLPage;
-
/********************************************************************************************************************
* $Id$
*
@@ -26,6 +19,12 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
+import com.meterware.httpunit.scripting.ScriptableDelegate;
+
+import org.w3c.dom.Node;
+
+import java.io.IOException;
+
/**
*
@@ -52,5 +51,5 @@
/**
* Returns the Scriptable object associated with the document
*/
- public HTMLPage.Scriptable getScriptableObject();
+ public ScriptableDelegate getScriptableObject();
}
Index: HTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/HTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HTMLParser.java 24 Dec 2002 17:17:03 -0000 1.1
+++ HTMLParser.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -56,4 +56,10 @@
* Returns true if this parser can return an HTMLDocument object.
*/
public boolean supportsReturnHTMLDocument();
+
+
+ /**
+ * Returns true if this parser can display parser warnings.
+ */
+ public boolean supportsParserWarnings();
}
Index: HTMLParserFactory.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/HTMLParserFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HTMLParserFactory.java 24 Dec 2002 17:17:03 -0000 1.1
+++ HTMLParserFactory.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -19,10 +19,11 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import com.meterware.httpunit.parsing.HTMLParser;
+import java.util.Vector;
+
/**
- * Factory for creating HTML parsers. Parsers customization properties can be specified but do not necessarily work
+ * Factory for creating HTML parsers. Parser customization properties can be specified but do not necessarily work
* for every parser type.
*
* @author <a href="mailto:rus...@ht...">Russell Gold</a>
@@ -30,12 +31,25 @@
**/
abstract public class HTMLParserFactory {
- private static HTMLParser _htmlParser;
+ private static Vector _listeners = new Vector();
private static HTMLParser _jtidyParser;
private static HTMLParser _nekoParser;
- private static boolean _preserveTagCase = false;
- private static boolean _returnHTMLDocument = true;
+ private static HTMLParser _htmlParser;
+ private static boolean _preserveTagCase;
+ private static boolean _returnHTMLDocument;
+ private static boolean _parserWarningsEnabled;
+
+
+ /**
+ * Resets all settings to their default values. This includes the parser selection.
+ */
+ public static void reset() {
+ _preserveTagCase = false;
+ _returnHTMLDocument = true;
+ _parserWarningsEnabled = false;
+ _htmlParser = null;
+ }
/**
@@ -119,9 +133,46 @@
}
- static {
- _jtidyParser = loadParserIfSupported( "org.w3c.tidy.Parser", "com.meterware.httpunit.parsing.JTidyHTMLParser" );
- _nekoParser = loadParserIfSupported( "org.cyberneko.html.HTMLConfiguration", "com.meterware.httpunit.parsing.NekoHTMLParser" );
+ /**
+ * Returns true if parser warnings are enabled.
+ **/
+ public static boolean isParserWarningsEnabled() {
+ return _parserWarningsEnabled && getHTMLParser().supportsParserWarnings();
+ }
+
+
+ /**
+ * If true, tells the parser to display warning messages. The default is false (warnings are not shown).
+ **/
+ public static void setParserWarningsEnabled( boolean enabled ) {
+ _parserWarningsEnabled = enabled;
+ }
+
+
+ /**
+ * Remove an HTML Parser listener.
+ **/
+ public static void removeHTMLParserListener( HTMLParserListener el ) {
+ _listeners.removeElement( el );
+ }
+
+
+ /**
+ * Add an HTML Parser listener.
+ **/
+ public static void addHTMLParserListener( HTMLParserListener el ) {
+ _listeners.addElement( el );
+ }
+
+
+//------------------------------------- package protected members ------------------------------------------------------
+
+
+ /**
+ * Get the list of Html Error Listeners
+ **/
+ static Vector getHTMLParserListeners() {
+ return _listeners;
}
@@ -137,8 +188,11 @@
}
- public static void reset() {
- _preserveTagCase = false;
- _returnHTMLDocument = true;
+ static {
+ _jtidyParser = loadParserIfSupported( "org.w3c.tidy.Parser", "com.meterware.httpunit.parsing.JTidyHTMLParser" );
+ _nekoParser = loadParserIfSupported( "org.cyberneko.html.HTMLConfiguration", "com.meterware.httpunit.parsing.NekoHTMLParser" );
+ reset();
}
+
+
}
Index: JTidyHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/JTidyHTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JTidyHTMLParser.java 24 Dec 2002 17:17:03 -0000 1.1
+++ JTidyHTMLParser.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -27,9 +27,6 @@
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
-import com.meterware.httpunit.parsing.HTMLParser;
-import com.meterware.httpunit.HttpUnitOptions;
-
/**
*
@@ -62,6 +59,11 @@
}
+ public boolean supportsParserWarnings() {
+ return true;
+ }
+
+
final private static char NBSP = (char) 160; // non-breaking space, defined by JTidy
final private static String UTF_ENCODING = "UTF-8";
@@ -71,9 +73,9 @@
Tidy tidy = new Tidy();
tidy.setCharEncoding( org.w3c.tidy.Configuration.UTF8 );
tidy.setQuiet( true );
- tidy.setShowWarnings( HttpUnitOptions.getParserWarningsEnabled() );
- if (!HttpUnitOptions.getHtmlErrorListeners().isEmpty()) {
- tidy.setErrout(new JTidyPrintWriter( url ));
+ tidy.setShowWarnings( HTMLParserFactory.isParserWarningsEnabled() );
+ if (!HTMLParserFactory.getHTMLParserListeners().isEmpty()) {
+ tidy.setErrout( new JTidyPrintWriter( url ) );
}
return tidy;
}
Index: JTidyPrintWriter.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/JTidyPrintWriter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JTidyPrintWriter.java 24 Dec 2002 17:17:03 -0000 1.1
+++ JTidyPrintWriter.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2001, Russell Gold
+* Copyright (c) 2001-2002, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -19,9 +19,6 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import com.meterware.httpunit.HttpUnitOptions;
-import com.meterware.httpunit.HtmlErrorListener;
-
import java.util.StringTokenizer;
import java.util.Enumeration;
import java.io.PrintWriter;
@@ -201,17 +198,17 @@
}
private void reportError( String msg, int line, int column ) {
- Enumeration enum = HttpUnitOptions.getHtmlErrorListeners().elements();
+ Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
while (enum.hasMoreElements()) {
- ((HtmlErrorListener) enum.nextElement()).error( _url, msg, line, column );
+ ((HTMLParserListener) enum.nextElement()).error( _url, msg, line, column );
}
}
private void reportWarning( String msg, int line, int column ) {
- Enumeration enum = HttpUnitOptions.getHtmlErrorListeners().elements();
+ Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
while (enum.hasMoreElements()) {
- ((HtmlErrorListener) enum.nextElement()).warning( _url, msg, line, column );
+ ((HTMLParserListener) enum.nextElement()).warning( _url, msg, line, column );
}
}
}
Index: NekoHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/NekoHTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NekoHTMLParser.java 24 Dec 2002 17:17:03 -0000 1.1
+++ NekoHTMLParser.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -70,6 +70,11 @@
}
+ public boolean supportsParserWarnings() {
+ return false;
+ }
+
+
final private static char NBSP = (char) 160; // non-breaking space, defined by nekoHTML
}
@@ -85,10 +90,10 @@
protected static final String FILTERS = "http://cyberneko.org/html/properties/filters";
/** Element case settings. possible values: "upper", "lower", "match" */
- protected static final String ELEMS = "http://cyberneko.org/html/properties/names/elems";
+ protected static final String TAG_NAME_CASE = "http://cyberneko.org/html/properties/names/elems";
/** Attribute case settings. possible values: "upper", "lower", "no-change" */
- protected static final String ATTRS = "http://cyberneko.org/html/properties/names/attrs";
+ protected static final String ATTRIBUTE_NAME_CASE = "http://cyberneko.org/html/properties/names/attrs";
private DocumentAdapter _documentAdapter;
@@ -101,8 +106,8 @@
final ScriptFilter javaScriptFilter = new ScriptFilter( configuration );
configuration.setProperty( FILTERS, new XMLDocumentFilter[] { javaScriptFilter } );
if (HTMLParserFactory.isPreserveTagCase()) {
- configuration.setProperty( ELEMS, "match" );
- configuration.setProperty( ATTRS, "no-change" );
+ configuration.setProperty( TAG_NAME_CASE, "match" );
+ configuration.setProperty( ATTRIBUTE_NAME_CASE, "no-change" );
}
try {
@@ -125,7 +130,7 @@
Node node = getCurrentElementNode();
while (!(node instanceof Document)) node = node.getParentNode();
_documentAdapter.setRootNode( node );
- _scriptableObject = _documentAdapter.getScriptableObject().getParent();
+ _scriptableObject = _documentAdapter.getScriptableObject();
}
return _scriptableObject;
}
Index: ScriptFilter.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/ScriptFilter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptFilter.java 24 Dec 2002 17:17:03 -0000 1.1
+++ ScriptFilter.java 25 Dec 2002 15:23:11 -0000 1.2
@@ -22,9 +22,6 @@
import java.io.IOException;
import java.io.StringReader;
-import org.cyberneko.html.HTMLConfiguration;
-import org.cyberneko.html.filters.DefaultFilter;
-
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
@@ -32,6 +29,8 @@
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLInputSource;
+
+import org.cyberneko.html.HTMLConfiguration;
import org.cyberneko.html.filters.DefaultFilter;
/**
|