[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit ScriptFilter.java,NONE,1.1 FrameHolder.ja
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-10-24 13:28:59
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv28243/src/com/meterware/httpunit
Modified Files:
FrameHolder.java HTMLPage.java HTMLParser.java
JTidyHTMLParser.java MimeEncodedMessageBody.java
NekoHTMLParser.java ParsedHTML.java WebResponse.java
Added Files:
ScriptFilter.java
Log Message:
Added support for document.write
***** Error reading new file[Errno 2] No such file or directory: 'ScriptFilter.java'
Index: FrameHolder.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/FrameHolder.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- FrameHolder.java 10 Oct 2002 22:44:34 -0000 1.5
+++ FrameHolder.java 24 Oct 2002 13:28:53 -0000 1.6
@@ -79,10 +79,10 @@
_contents.put( target, response );
if (response.isHTML()) {
+ HttpUnitOptions.getScriptingEngine().associate( response );
createSubFrames( target, response.getFrameNames() );
WebRequest[] requests = response.getFrameRequests();
for (int i = 0; i < requests.length; i++) response.getWindow().getResponse( requests[ i ] );
- HttpUnitOptions.getScriptingEngine().associate( response );
}
}
Index: HTMLPage.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLPage.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- HTMLPage.java 14 Oct 2002 04:19:06 -0000 1.13
+++ HTMLPage.java 24 Oct 2002 13:28:53 -0000 1.14
@@ -45,9 +45,8 @@
private Scriptable _scriptable;
- HTMLPage( WebResponse response, URL url, String parentTarget, String pageText, String characterSet ) throws IOException, SAXException {
- super( response, url, parentTarget, getDOM( url, pageText ), characterSet );
- setBaseAttributes();
+ HTMLPage( WebResponse response, URL url, String parentTarget, String characterSet ) throws IOException, SAXException {
+ super( response, url, parentTarget, null, characterSet );
}
@@ -80,13 +79,12 @@
ArrayList scripts = new ArrayList();
for (int i = 0; i < nl.getLength(); i++) {
Node scriptNode = nl.item(i);
- String src = NodeUtils.getNodeAttribute( scriptNode, "src", null );
- if (src == null) {
+ String scriptLocation = NodeUtils.getNodeAttribute( scriptNode, "src", null );
+ if (scriptLocation == null) {
scripts.add( NodeUtils.asText( scriptNode.getChildNodes() ) );
} else {
try {
- WebRequest req = new GetMethodWebRequest( getBaseURL(), src );
- scripts.add( getResponse().getWindow().getResource( req ).getText() );
+ scripts.add( getIncludedScript( scriptLocation ) );
} catch (IOException e) {
throw new RuntimeException( "Error loading included script: " + e );
}
@@ -97,6 +95,18 @@
/**
+ * Returns the contents of an included script, given its src attribute.
+ * @param srcAttribute
+ * @return the contents of the script.
+ * @throws IOException if there is a problem retrieving the script
+ */
+ String getIncludedScript( String srcAttribute ) throws IOException {
+ WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute );
+ return getResponse().getWindow().getResource( req ).getText();
+ }
+
+
+ /**
* Returns the location of the linked stylesheet in the head
* <code>
* <link type="text/css" rel="stylesheet" href="/mystyle.css" />
@@ -182,7 +192,12 @@
} else {
super.set( propertyName, value );
}
- }
+ }
+
+
+ public WebResponse.Scriptable getParent() {
+ return getResponse().getScriptableObject();
+ }
public String getTitle() throws SAXException {
@@ -232,15 +247,18 @@
return _scriptable;
}
-//---------------------------------- private members --------------------------------
- private static Node getDOM( URL url, String pageText ) throws IOException, SAXException {
- return HttpUnitOptions.getHTMLParser().getDocument( url, pageText );
+ public void parse( String text ) throws SAXException, IOException {
+ HttpUnitOptions.getHTMLParser().parse( this, getBaseURL(), text );
+ setBaseAttributes();
}
- private void setBaseAttributes() throws SAXException {
+//---------------------------------- private members --------------------------------
+
+
+ private void setBaseAttributes() {
NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName( "base" );
if (nl.getLength() == 0) return;
try {
Index: HTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HTMLParser.java 14 Oct 2002 04:19:06 -0000 1.1
+++ HTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
@@ -32,10 +32,10 @@
interface HTMLParser {
/**
- * Converts an HTML text string to a Document. Any error reporting will be annotated with the
- * specified URL.
+ * Parses the specified text string as a Document, registering it in the HTMLPage.
+ * Any error reporting will be annotated with the specified URL.
*/
- public Node getDocument( URL url, String pageText ) throws IOException, SAXException;
+ public void parse( HTMLPage page, URL baseURL, String pageText ) throws IOException, SAXException;
/**
Index: JTidyHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/JTidyHTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JTidyHTMLParser.java 14 Oct 2002 04:19:06 -0000 1.1
+++ JTidyHTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
@@ -33,11 +33,12 @@
*
* @author <a href="mailto:rus...@ht...">Russell Gold</a>
**/
-public class JTidyHTMLParser implements HTMLParser {
+class JTidyHTMLParser implements HTMLParser {
- public Node getDocument( URL url, String pageText ) throws IOException, SAXException {
+
+ public void parse( HTMLPage page, URL baseURL, String pageText ) throws IOException, SAXException {
try {
- return getParser( url ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null );
+ page.setRootNode( getParser( baseURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null ) );
} catch (UnsupportedEncodingException e) {
throw new RuntimeException( "UTF-8 encoding failed" );
}
Index: MimeEncodedMessageBody.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/MimeEncodedMessageBody.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- MimeEncodedMessageBody.java 20 May 2002 16:00:43 -0000 1.12
+++ MimeEncodedMessageBody.java 24 Oct 2002 13:28:53 -0000 1.13
@@ -70,7 +70,7 @@
StringBuffer sb = new StringBuffer(chars.length+20);
for (int i = 0; i < chars.length; i++ ) {
if (chars[i] == '\\') {
- sb.append( "\\\\" ); // accomodate MS-DOS file paths XXX is this safe??
+ sb.append( "\\\\" ); // accomodate MS-DOS file paths ??? is this safe??
} else {
sb.append( chars[i] );
}
Index: NekoHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NekoHTMLParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NekoHTMLParser.java 14 Oct 2002 04:19:06 -0000 1.1
+++ NekoHTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
@@ -19,25 +19,38 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import org.w3c.dom.Node;
+import org.apache.xerces.xni.parser.XMLDocumentFilter;
+
+import org.cyberneko.html.HTMLConfiguration;
+
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
-import org.cyberneko.html.parsers.DOMParser;
+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 com.meterware.httpunit.scripting.ScriptableDelegate;
+
/**
*
* @author <a href="mailto:rus...@ht...">Russell Gold</a>
**/
-public class NekoHTMLParser implements HTMLParser {
+class NekoHTMLParser implements HTMLParser {
+
- public Node getDocument( URL url, String pageText ) throws IOException, SAXException {
- DOMParser parser = new DOMParser();
- parser.parse( new InputSource( new StringReader( pageText ) ) );
- return parser.getDocument();
+ public void parse( HTMLPage page, URL baseURL, String pageText ) throws IOException, SAXException {
+ try {
+ DOMParser parser = DOMParser.newParser(page);
+ parser.parse( new InputSource( new StringReader( pageText ) ) );
+ page.setRootNode( parser.getDocument() );
+ } catch (DOMParser.ScriptException e) {
+ throw e.getException();
+ }
}
@@ -46,4 +59,92 @@
}
final private static char NBSP = (char) 160; // non-breaking space, defined by nekoHTML
+}
+
+
+class DOMParser extends org.apache.xerces.parsers.DOMParser {
+
+ private static final String HTML_DOCUMENT_CLASS_NAME = "org.apache.html.dom.HTMLDocumentImpl";
+
+ /** Augmentations feature identifier. */
+ protected static final String AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations";
+
+ /** Filters property identifier. */
+ protected static final String FILTERS = "http://cyberneko.org/html/properties/filters";
+
+
+ private HTMLPage _htmlPage;
+ private ScriptableDelegate _scriptableObject;
+
+
+ static DOMParser newParser( HTMLPage page ) {
+ final HTMLConfiguration configuration = new HTMLConfiguration();
+ 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;
+ }
+
+
+ ScriptableDelegate getScriptableDelegate() {
+ if (_scriptableObject == null) {
+ Node node = getCurrentElementNode();
+ while (!(node instanceof Document)) node = node.getParentNode();
+ _htmlPage.setRootNode( node );
+ _scriptableObject = _htmlPage.getScriptableObject().getParent();
+ }
+ 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 _htmlPage.getIncludedScript( srcAttribute );
+ } catch (IOException e) {
+ throw new ScriptException( e );
+ }
+ }
+
+
+ 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() );
+ }
+ }
+
+
+ static class ScriptException extends RuntimeException {
+ private IOException _cause;
+
+ public ScriptException( IOException cause ) {
+ _cause = cause;
+ }
+
+ public IOException getException() {
+ return _cause;
+ }
+ }
}
Index: ParsedHTML.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -r1.32 -r1.33
--- ParsedHTML.java 22 Oct 2002 22:59:25 -0000 1.32
+++ ParsedHTML.java 24 Oct 2002 13:28:53 -0000 1.33
@@ -60,7 +60,7 @@
**/
public WebForm[] getForms() {
if (_forms == null) {
- NodeList forms = NodeUtils.getElementsByTagName( _rootNode, "form" );
+ NodeList forms = NodeUtils.getElementsByTagName( getRootNode(), "form" );
_forms = new WebForm[ forms.getLength() ];
for (int i = 0; i < _forms.length; i++) {
@@ -103,7 +103,7 @@
public WebLink[] getLinks() {
if (_links == null) {
final ArrayList list = new ArrayList();
- NodeUtils.processNodes( _rootNode.getChildNodes(), new NodeUtils.NodeAction() {
+ NodeUtils.processNodes( getRootNode().getChildNodes(), new NodeUtils.NodeAction() {
public boolean processElement( Element element ) {
if (element.getNodeName().equalsIgnoreCase( "a" )) addLinkAnchor( list, element );
else if (element.getNodeName().equalsIgnoreCase( "area" )) addLinkAnchor( list, element );
@@ -189,7 +189,7 @@
**/
public WebImage[] getImages() {
if (_images == null) {
- NodeList images = NodeUtils.getElementsByTagName( _rootNode, "img" );
+ NodeList images = NodeUtils.getElementsByTagName( getRootNode(), "img" );
_images = new WebImage[ images.getLength() ];
for (int i = 0; i < _images.length; i++) {
@@ -302,17 +302,9 @@
* Returns a copy of the domain object model associated with this page.
**/
public Node getDOM() {
- return _rootNode.cloneNode( /* deep */ true );
+ return getRootNode().cloneNode( /* deep */ true );
}
- /**
- * Returns the domain object model associated with this page, to be used internally.
- **/
- Node getOriginalDOM() {
- return _rootNode;
- }
-
-
//---------------------------------- Object methods --------------------------------
@@ -341,10 +333,31 @@
}
+
+ protected Node getRootNode() {
+ if (_rootNode == null) throw new IllegalStateException( "The root node has not been specified" );
+ return _rootNode;
+ }
+
+
//---------------------------------- package members --------------------------------
/**
+ * Specifies the root node for this HTML fragment.
+ */
+ protected void setRootNode( Node rootNode ) {
+ if (_rootNode != null && rootNode != _rootNode )
+ throw new IllegalStateException( "The root node has already been defined as " + _rootNode + " and cannot be redefined as " + rootNode );
+ _rootNode = rootNode;
+
+ _links = null;
+ _forms = null;
+ _images = null;
+ }
+
+
+ /**
* Returns the base URL for this HTML segment.
**/
URL getBaseURL() {
@@ -354,6 +367,14 @@
WebResponse getResponse() {
return _response;
+ }
+
+
+ /**
+ * Returns the domain object model associated with this page, to be used internally.
+ **/
+ Node getOriginalDOM() {
+ return getRootNode();
}
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.93
retrieving revision 1.94
diff -u -r1.93 -r1.94
--- WebResponse.java 17 Oct 2002 23:50:49 -0000 1.93
+++ WebResponse.java 24 Oct 2002 13:28:53 -0000 1.94
@@ -21,14 +21,12 @@
*******************************************************************************************************************/
import com.meterware.httpunit.scripting.ScriptableDelegate;
import com.meterware.httpunit.cookies.CookieJar;
-import com.meterware.httpunit.cookies.Cookie;
import com.meterware.httpunit.cookies.CookieSource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
-import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
@@ -916,7 +914,8 @@
if (_page == null) {
try {
if (!isHTML()) throw new NotHTMLException( getContentType() );
- _page = new HTMLPage( this, _url, _frameName, getText(), getCharacterSet() );
+ _page = new HTMLPage( this, _url, _frameName, getCharacterSet() );
+ _page.parse( getText() );
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException( e.toString() );
|