[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HTMLParserFactory.java,NONE,1.1 HTMLPage.
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-12-20 01:59:50
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv24372/src/com/meterware/httpunit
Modified Files:
HTMLPage.java HTMLParser.java HttpUnitOptions.java
JTidyHTMLParser.java NekoHTMLParser.java NodeUtils.java
Added Files:
HTMLParserFactory.java
Log Message:
Add support for parser customization
***** Error reading new file[Errno 2] No such file or directory: 'HTMLParserFactory.java'
Index: HTMLPage.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLPage.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- HTMLPage.java 13 Dec 2002 12:57:11 -0000 1.18
+++ HTMLPage.java 20 Dec 2002 01:59:47 -0000 1.19
@@ -260,7 +260,7 @@
public void parse( String text, URL pageURL ) throws SAXException, IOException {
- HttpUnitOptions.getHTMLParser().parse( this, pageURL, text );
+ HTMLParserFactory.getHTMLParser().parse( this, pageURL, text );
}
Index: HTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
+++ HTMLParser.java 20 Dec 2002 01:59:47 -0000 1.3
@@ -19,15 +19,16 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import java.net.URL;
import java.io.IOException;
/**
+ * A front end to a DOM parser that can handle HTML.
*
* @author <a href="mailto:rus...@ht...">Russell Gold</a>
+ * @author <a href="mailto:bw...@xm...">Bernhard Wagner</a>
**/
interface HTMLParser {
@@ -43,4 +44,16 @@
* an HTML entity as a special character. This method should convert that character to normal text.
*/
public String getCleanedText( String string );
+
+
+ /**
+ * Returns true if this parser supports preservation of the case of tag and attribute names.
+ */
+ public boolean supportsPreserveTagCase();
+
+
+ /**
+ * Returns true if this parser can return an HTMLDocument object.
+ */
+ public boolean supportsReturnHTMLDocument();
}
Index: HttpUnitOptions.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- HttpUnitOptions.java 26 Nov 2002 11:55:57 -0000 1.34
+++ HttpUnitOptions.java 20 Dec 2002 01:59:47 -0000 1.35
@@ -61,17 +61,6 @@
}
- public static HTMLParser getHTMLParser() {
- if (_nekoParser != null) {
- return _nekoParser;
- } else if (_jtidyParser != null) {
- return _jtidyParser;
- } else {
- throw new RuntimeException( "No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath" );
- }
- }
-
-
/**
* Returns true if HttpUnit is accepting and saving cookies. The default is to accept them.
*/
@@ -517,28 +506,10 @@
private static boolean _exceptionsThrownOnScriptError = true;
- private static HTMLParser _jtidyParser;
-
- private static HTMLParser _nekoParser;
static {
_listeners = new Vector();
reset();
- try {
- Class.forName( "org.w3c.tidy.Parser" );
- _jtidyParser = (HTMLParser) Class.forName( "com.meterware.httpunit.JTidyHTMLParser" ).newInstance();
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- } catch (ClassNotFoundException e) {
- }
-
- try {
- Class.forName( "org.cyberneko.html.HTMLConfiguration" );
- _nekoParser = (HTMLParser) Class.forName( "com.meterware.httpunit.NekoHTMLParser" ).newInstance();
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- } catch (ClassNotFoundException e) {
- }
}
}
Index: JTidyHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/JTidyHTMLParser.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JTidyHTMLParser.java 25 Nov 2002 22:41:07 -0000 1.3
+++ JTidyHTMLParser.java 20 Dec 2002 01:59:47 -0000 1.4
@@ -19,7 +19,6 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import org.w3c.dom.Node;
import org.w3c.tidy.Tidy;
import org.xml.sax.SAXException;
@@ -48,6 +47,17 @@
public String getCleanedText( String string ) {
return (string == null) ? "" : string.replace( NBSP, ' ' );
}
+
+
+ public boolean supportsPreserveTagCase() {
+ return false;
+ }
+
+
+ public boolean supportsReturnHTMLDocument() {
+ return false;
+ }
+
final private static char NBSP = (char) 160; // non-breaking space, defined by JTidy
Index: NekoHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NekoHTMLParser.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- NekoHTMLParser.java 25 Nov 2002 22:41:07 -0000 1.3
+++ NekoHTMLParser.java 20 Dec 2002 01:59:47 -0000 1.4
@@ -39,13 +39,14 @@
/**
*
* @author <a href="mailto:rus...@ht...">Russell Gold</a>
+ * @author <a href="mailto:bw...@xm...">Bernhard Wagner</a>
**/
class NekoHTMLParser implements HTMLParser {
public void parse( HTMLPage page, URL pageURL, String pageText ) throws IOException, SAXException {
try {
- DOMParser parser = DOMParser.newParser(page);
+ DOMParser parser = DOMParser.newParser( page );
parser.parse( new InputSource( new StringReader( pageText ) ) );
page.setRootNode( parser.getDocument() );
} catch (DOMParser.ScriptException e) {
@@ -58,6 +59,17 @@
return (string == null) ? "" : string.replace( NBSP, ' ' );
}
+
+ public boolean supportsPreserveTagCase() {
+ return true;
+ }
+
+
+ public boolean supportsReturnHTMLDocument() {
+ return true;
+ }
+
+
final private static char NBSP = (char) 160; // non-breaking space, defined by nekoHTML
}
@@ -72,6 +84,12 @@
/** Filters property identifier. */
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";
+
+ /** Attribute case settings. possible values: "upper", "lower", "no-change" */
+ protected static final String ATTRS = "http://cyberneko.org/html/properties/names/attrs";
+
private HTMLPage _htmlPage;
private ScriptableDelegate _scriptableObject;
@@ -82,9 +100,23 @@
configuration.setFeature( AUGMENTATIONS, true );
final ScriptFilter javaScriptFilter = new ScriptFilter( configuration );
configuration.setProperty( FILTERS, new XMLDocumentFilter[] { javaScriptFilter } );
- final DOMParser domParser = new DOMParser( configuration, page );
- javaScriptFilter.setParser( domParser );
- return domParser;
+ if (HTMLParserFactory.isPreserveTagCase()) {
+ configuration.setProperty( ELEMS, "match" );
+ configuration.setProperty( ATTRS, "no-change" );
+ }
+
+ try {
+ final DOMParser domParser = new DOMParser( configuration, page );
+ domParser.setFeature( DEFER_NODE_EXPANSION, false );
+ if (HTMLParserFactory.isReturnHTMLDocument()) domParser.setProperty( DOCUMENT_CLASS_NAME, HTML_DOCUMENT_CLASS_NAME );
+ javaScriptFilter.setParser( domParser );
+ return domParser;
+ } catch (SAXNotRecognizedException e) {
+ throw new RuntimeException( e.toString() );
+ } catch (SAXNotSupportedException e) {
+ throw new RuntimeException( e.toString() );
+ }
+
}
@@ -124,15 +156,6 @@
DOMParser( HTMLConfiguration configuration, HTMLPage page ) {
super( configuration );
_htmlPage = page;
-
- try {
- setFeature( DEFER_NODE_EXPANSION, false );
- setProperty( DOCUMENT_CLASS_NAME, HTML_DOCUMENT_CLASS_NAME );
- } catch (SAXNotRecognizedException e) {
- throw new RuntimeException( e.toString() );
- } catch (SAXNotSupportedException e) {
- throw new RuntimeException( e.toString() );
- }
}
Index: NodeUtils.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NodeUtils.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- NodeUtils.java 14 Oct 2002 04:19:06 -0000 1.13
+++ NodeUtils.java 20 Dec 2002 01:59:47 -0000 1.14
@@ -109,7 +109,7 @@
return true;
}
public void processTextNodeValue( String value ) {
- sb.append( HttpUnitOptions.getHTMLParser().getCleanedText( value ) );
+ sb.append( HTMLParserFactory.getHTMLParser().getCleanedText( value ) );
}
} );
return sb.toString();
|