[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/parsing NekoDOMParser.java,NONE,1.1 NekoH
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-03-09 20:35:51
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing
In directory sc8-pr-cvs1:/tmp/cvs-serv26420/src/com/meterware/httpunit/parsing
Modified Files:
NekoHTMLParser.java ScriptFilter.java
Added Files:
NekoDOMParser.java
Log Message:
support <noscript> tags
--- NEW FILE ---
package com.meterware.httpunit.parsing;
import com.meterware.httpunit.scripting.ScriptableDelegate;
import java.net.URL;
import java.io.IOException;
import java.util.Enumeration;
import org.cyberneko.html.HTMLConfiguration;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLErrorHandler;
import org.apache.xerces.xni.parser.XMLParseException;
import org.apache.xerces.xni.XNIException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
/**
*
*
* @author <a href="rus...@ac...">Russell Gold</a>
* @author <a href="mailto:Art...@ly...">Artashes Aghajanyan</a>
**/
class NekoDOMParser extends org.apache.xerces.parsers.DOMParser {
private static final String HTML_DOCUMENT_CLASS_NAME = "org.apache.html.dom.HTMLDocumentImpl";
/** Error reporting feature identifier. */
private static final String REPORT_ERRORS = "http://cyberneko.org/html/features/report-errors";
/** Augmentations feature identifier. */
private static final String AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations";
/** Filters property identifier. */
private static final String FILTERS = "http://cyberneko.org/html/properties/filters";
/** Element case settings. possible values: "upper", "lower", "match" */
private static final String TAG_NAME_CASE = "http://cyberneko.org/html/properties/names/elems";
/** Attribute case settings. possible values: "upper", "lower", "no-change" */
private static final String ATTRIBUTE_NAME_CASE = "http://cyberneko.org/html/properties/names/attrs";
private DocumentAdapter _documentAdapter;
private ScriptableDelegate _scriptableObject;
static NekoDOMParser newParser( DocumentAdapter adapter, URL url ) {
final HTMLConfiguration configuration = new HTMLConfiguration();
if (!HTMLParserFactory.getHTMLParserListeners().isEmpty() || HTMLParserFactory.isParserWarningsEnabled()) {
configuration.setErrorHandler( new ErrorHandler( url ) );
configuration.setFeature( REPORT_ERRORS, true);
}
configuration.setFeature( AUGMENTATIONS, true );
final ScriptFilter javaScriptFilter = new ScriptFilter( configuration );
configuration.setProperty( FILTERS, new XMLDocumentFilter[] { javaScriptFilter } );
if (HTMLParserFactory.isPreserveTagCase()) {
configuration.setProperty( TAG_NAME_CASE, "match" );
configuration.setProperty( ATTRIBUTE_NAME_CASE, "no-change" );
}
try {
final NekoDOMParser domParser = new NekoDOMParser( configuration, adapter );
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() );
}
}
ScriptableDelegate getScriptableDelegate() {
if (_scriptableObject == null) {
Node node = getCurrentElementNode();
while (!(node instanceof Document)) node = node.getParentNode();
_documentAdapter.setRootNode( node );
_scriptableObject = _documentAdapter.getScriptableObject();
}
return _scriptableObject;
}
private Node getCurrentElementNode() {
try {
final Node node = (Node) getProperty( CURRENT_ELEMENT_NODE );
return node;
} catch (SAXNotRecognizedException e) {
throw new RuntimeException( CURRENT_ELEMENT_NODE + " property not recognized" );
} catch (SAXNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException( CURRENT_ELEMENT_NODE + " property not supported" );
}
}
String getIncludedScript( String srcAttribute ) {
try {
return _documentAdapter.getIncludedScript( srcAttribute );
} catch (IOException e) {
throw new ScriptException( e );
}
}
NekoDOMParser( HTMLConfiguration configuration, DocumentAdapter adapter ) {
super( configuration );
_documentAdapter = adapter;
}
static class ScriptException extends RuntimeException {
private IOException _cause;
public ScriptException( IOException cause ) {
_cause = cause;
}
public IOException getException() {
return _cause;
}
}
}
class ErrorHandler implements XMLErrorHandler {
private URL _url = null;
ErrorHandler( URL url ) {
_url = url;
}
public void warning( String domain, String key, XMLParseException warningException ) throws XNIException {
if (HTMLParserFactory.isParserWarningsEnabled()) {
System.out.println( "At line " + warningException.getLineNumber() + ", column " + warningException.getColumnNumber() + ": " + warningException.getMessage() );
}
Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
while (enum.hasMoreElements()) {
((HTMLParserListener) enum.nextElement()).warning( _url, warningException.getMessage(), warningException.getLineNumber(), warningException.getColumnNumber() );
}
}
public void error( String domain, String key, XMLParseException errorException ) throws XNIException {
Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
while (enum.hasMoreElements()) {
((HTMLParserListener) enum.nextElement()).error( _url, errorException.getMessage(), errorException.getLineNumber(), errorException.getColumnNumber() );
}
}
public void fatalError( String domain, String key, XMLParseException fatalError ) throws XNIException {
error( domain, key, fatalError );
throw fatalError;
}
}
Index: NekoHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/NekoHTMLParser.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- NekoHTMLParser.java 28 Feb 2003 17:30:26 -0000 1.3
+++ NekoHTMLParser.java 9 Mar 2003 20:35:47 -0000 1.4
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
- * Copyright (c) 2002, Russell Gold
+ * Copyright (c) 2002-2003, 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,26 +19,12 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import org.apache.xerces.xni.parser.XMLDocumentFilter;
-import org.apache.xerces.xni.parser.XMLErrorHandler;
-import org.apache.xerces.xni.parser.XMLParseException;
-import org.apache.xerces.xni.XNIException;
-
-import org.cyberneko.html.HTMLConfiguration;
-
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
-import org.xml.sax.SAXNotRecognizedException;
-import org.xml.sax.SAXNotSupportedException;
-import org.w3c.dom.Node;
-import org.w3c.dom.Document;
import java.net.URL;
import java.io.IOException;
import java.io.StringReader;
-import java.util.Enumeration;
-
-import com.meterware.httpunit.scripting.ScriptableDelegate;
/**
*
@@ -51,10 +37,10 @@
public void parse( URL pageURL, String pageText, DocumentAdapter adapter ) throws IOException, SAXException {
try {
- DOMParser parser = DOMParser.newParser( adapter, pageURL );
+ NekoDOMParser parser = NekoDOMParser.newParser( adapter, pageURL );
parser.parse( new InputSource( new StringReader( pageText ) ) );
adapter.setRootNode( parser.getDocument() );
- } catch (DOMParser.ScriptException e) {
+ } catch (NekoDOMParser.ScriptException e) {
throw e.getException();
}
}
@@ -84,141 +70,4 @@
}
-class DOMParser extends org.apache.xerces.parsers.DOMParser {
-
- private static final String HTML_DOCUMENT_CLASS_NAME = "org.apache.html.dom.HTMLDocumentImpl";
-
- /** Error reporting feature identifier. */
- private static final String REPORT_ERRORS = "http://cyberneko.org/html/features/report-errors";
-
- /** Augmentations feature identifier. */
- private static final String AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations";
-
- /** Filters property identifier. */
- private static final String FILTERS = "http://cyberneko.org/html/properties/filters";
-
- /** Element case settings. possible values: "upper", "lower", "match" */
- private static final String TAG_NAME_CASE = "http://cyberneko.org/html/properties/names/elems";
- /** Attribute case settings. possible values: "upper", "lower", "no-change" */
- private static final String ATTRIBUTE_NAME_CASE = "http://cyberneko.org/html/properties/names/attrs";
-
- private DocumentAdapter _documentAdapter;
- private ScriptableDelegate _scriptableObject;
-
-
- static DOMParser newParser( DocumentAdapter adapter, URL url ) {
- final HTMLConfiguration configuration = new HTMLConfiguration();
- if (!HTMLParserFactory.getHTMLParserListeners().isEmpty() || HTMLParserFactory.isParserWarningsEnabled()) {
- configuration.setErrorHandler( new ErrorHandler( url ) );
- configuration.setFeature( REPORT_ERRORS, true);
- }
- configuration.setFeature( AUGMENTATIONS, true );
- final ScriptFilter javaScriptFilter = new ScriptFilter( configuration );
- configuration.setProperty( FILTERS, new XMLDocumentFilter[] { javaScriptFilter } );
- if (HTMLParserFactory.isPreserveTagCase()) {
- configuration.setProperty( TAG_NAME_CASE, "match" );
- configuration.setProperty( ATTRIBUTE_NAME_CASE, "no-change" );
- }
-
- try {
- final DOMParser domParser = new DOMParser( configuration, adapter );
- 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() );
- }
-
- }
-
-
- ScriptableDelegate getScriptableDelegate() {
- if (_scriptableObject == null) {
- Node node = getCurrentElementNode();
- while (!(node instanceof Document)) node = node.getParentNode();
- _documentAdapter.setRootNode( node );
- _scriptableObject = _documentAdapter.getScriptableObject();
- }
- return _scriptableObject;
- }
-
-
- private Node getCurrentElementNode() {
- try {
- final Node node = (Node) getProperty( CURRENT_ELEMENT_NODE );
- return node;
- } catch (SAXNotRecognizedException e) {
- throw new RuntimeException( CURRENT_ELEMENT_NODE + " property not recognized" );
- } catch (SAXNotSupportedException e) {
- e.printStackTrace();
- throw new RuntimeException( CURRENT_ELEMENT_NODE + " property not supported" );
- }
- }
-
-
- String getIncludedScript( String srcAttribute ) {
- try {
- return _documentAdapter.getIncludedScript( srcAttribute );
- } catch (IOException e) {
- throw new ScriptException( e );
- }
- }
-
-
- DOMParser( HTMLConfiguration configuration, DocumentAdapter adapter ) {
- super( configuration );
- _documentAdapter = adapter;
- }
-
-
- static class ScriptException extends RuntimeException {
- private IOException _cause;
-
- public ScriptException( IOException cause ) {
- _cause = cause;
- }
-
- public IOException getException() {
- return _cause;
- }
- }
-}
-
-
-class ErrorHandler implements XMLErrorHandler {
-
- private URL _url = null;
-
- ErrorHandler( URL url ) {
- _url = url;
- }
-
- public void warning( String domain, String key, XMLParseException warningException ) throws XNIException {
- if (HTMLParserFactory.isParserWarningsEnabled()) {
- System.out.println( "At line " + warningException.getLineNumber() + ", column " + warningException.getColumnNumber() + ": " + warningException.getMessage() );
- }
-
- Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
- while (enum.hasMoreElements()) {
- ((HTMLParserListener) enum.nextElement()).warning( _url, warningException.getMessage(), warningException.getLineNumber(), warningException.getColumnNumber() );
- }
- }
-
-
- public void error( String domain, String key, XMLParseException errorException ) throws XNIException {
- Enumeration enum = HTMLParserFactory.getHTMLParserListeners().elements();
- while (enum.hasMoreElements()) {
- ((HTMLParserListener) enum.nextElement()).error( _url, errorException.getMessage(), errorException.getLineNumber(), errorException.getColumnNumber() );
- }
- }
-
-
- public void fatalError( String domain, String key, XMLParseException fatalError ) throws XNIException {
- error( domain, key, fatalError );
- throw fatalError;
- }
-}
Index: ScriptFilter.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/ScriptFilter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ScriptFilter.java 10 Jan 2003 18:21:46 -0000 1.3
+++ ScriptFilter.java 9 Mar 2003 20:35:48 -0000 1.4
@@ -40,14 +40,15 @@
**/
class ScriptFilter extends DefaultFilter {
- protected static final String SCRIPT_TYPE = "javascript";
-
/** The NekoHTML configuration. */
private HTMLConfiguration _configuration;
/** A string buffer to collect the script. */
private StringBuffer _activeScriptBlock;
+ /** The name of the current script language. **/
+ private String _scriptLanguage;
+
/** The system identifier of the source document. */
private String _systemID = "";
@@ -55,7 +56,7 @@
private int _scriptIndex;
/** The parser in which this filter is running. **/
- private DOMParser _domParser;
+ private NekoDOMParser _domParser;
/** Constructs a script object with the specified configuration. */
@@ -64,7 +65,7 @@
}
- public void setParser( DOMParser domParser ) {
+ public void setParser( NekoDOMParser domParser ) {
_domParser = domParser;
}
@@ -77,11 +78,16 @@
}
+ /**
+ * Invoked for a start element. If the element is a <script>, overrides the normal behavior to begin collecting
+ * the script text.
+ */
public void startElement( QName element, XMLAttributes attrs, Augmentations augs ) throws XNIException {
if (!isSupportedScript( element, attrs )) {
super.startElement( element, attrs, augs );
} else {
_activeScriptBlock = new StringBuffer();
+ _scriptLanguage = getScriptLanguage( attrs );
String srcAttribute = attrs.getValue( "src" );
if (srcAttribute != null) _activeScriptBlock.append( _domParser.getIncludedScript( srcAttribute ) );
}
@@ -90,8 +96,13 @@
private boolean isSupportedScript( QName element, XMLAttributes attrs ) {
if (!element.rawname.equalsIgnoreCase( "script" ) || attrs == null) return false;
- String value = attrs.getValue( "language" );
- return value == null || value.toLowerCase().startsWith( SCRIPT_TYPE );
+ String value = getScriptLanguage( attrs );
+ return _domParser.getScriptableDelegate().supportsScript( value );
+ }
+
+
+ private String getScriptLanguage( XMLAttributes attrs ) {
+ return attrs == null ? null : attrs.getValue( "language" );
}
@@ -117,7 +128,7 @@
} else {
try {
final String scriptText = _activeScriptBlock.toString();
- String replacementText = getTranslatedScript( scriptText );
+ String replacementText = getTranslatedScript( _scriptLanguage, scriptText );
_configuration.pushInputSource( newInputSource( replacementText ) );
} catch (IOException e) { // ignore
} finally {
@@ -135,8 +146,8 @@
}
- protected String getTranslatedScript( final String scriptText ) throws IOException {
- return _domParser.getScriptableDelegate().runScript( scriptText );
+ protected String getTranslatedScript( final String language, final String scriptText ) throws IOException {
+ return _domParser.getScriptableDelegate().runScript( language, scriptText );
}
|