[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HTMLPage.java,1.16,1.17 JTidyHTMLParser.j
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv14937/src/com/meterware/httpunit
Modified Files:
HTMLPage.java JTidyHTMLParser.java NekoHTMLParser.java
ParsedHTML.java WebRequest.java WebResponse.java
Log Message:
Process base tag before parsing HTML
Index: HTMLPage.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLPage.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- HTMLPage.java 17 Nov 2002 16:43:40 -0000 1.16
+++ HTMLPage.java 25 Nov 2002 22:41:06 -0000 1.17
@@ -28,7 +28,6 @@
import org.xml.sax.SAXException;
import java.io.IOException;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
@@ -45,8 +44,8 @@
private Scriptable _scriptable;
- HTMLPage( WebResponse response, URL url, String parentTarget, String characterSet ) throws IOException, SAXException {
- super( response, url, parentTarget, null, characterSet );
+ HTMLPage( WebResponse response, URL baseURL, String baseTarget, String characterSet ) throws IOException, SAXException {
+ super( response, baseURL, baseTarget, null, characterSet );
}
@@ -250,34 +249,8 @@
- public void parse( String text ) throws SAXException, IOException {
- HttpUnitOptions.getHTMLParser().parse( this, getBaseURL(), text );
- setBaseAttributes();
- }
-
-
-//---------------------------------- private members --------------------------------
-
-
- private void setBaseAttributes() {
- NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName( "base" );
- if (nl.getLength() == 0) return;
- try {
- applyBaseAttributes( NodeUtils.getNodeAttribute( nl.item(0), "href" ),
- NodeUtils.getNodeAttribute( nl.item(0), "target" ) );
- } catch (MalformedURLException e) {
- throw new RuntimeException( "Unable to set document base: " + e );
- }
- }
-
-
- private void applyBaseAttributes( String baseURLString, String baseTarget ) throws MalformedURLException {
- if (baseURLString.length() > 0) {
- this.setBaseURL( new URL( baseURLString ) );
- }
- if (baseTarget.length() > 0) {
- this.setBaseTarget( baseTarget );
- }
+ public void parse( String text, URL pageURL ) throws SAXException, IOException {
+ HttpUnitOptions.getHTMLParser().parse( this, pageURL, text );
}
Index: JTidyHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/JTidyHTMLParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JTidyHTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
+++ JTidyHTMLParser.java 25 Nov 2002 22:41:07 -0000 1.3
@@ -36,9 +36,9 @@
class JTidyHTMLParser implements HTMLParser {
- public void parse( HTMLPage page, URL baseURL, String pageText ) throws IOException, SAXException {
+ public void parse( HTMLPage page, URL pageURL, String pageText ) throws IOException, SAXException {
try {
- page.setRootNode( getParser( baseURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null ) );
+ page.setRootNode( getParser( pageURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null ) );
} catch (UnsupportedEncodingException e) {
throw new RuntimeException( "UTF-8 encoding failed" );
}
Index: NekoHTMLParser.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NekoHTMLParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NekoHTMLParser.java 24 Oct 2002 13:28:53 -0000 1.2
+++ NekoHTMLParser.java 25 Nov 2002 22:41:07 -0000 1.3
@@ -43,7 +43,7 @@
class NekoHTMLParser implements HTMLParser {
- public void parse( HTMLPage page, URL baseURL, String pageText ) throws IOException, SAXException {
+ public void parse( HTMLPage page, URL pageURL, String pageText ) throws IOException, SAXException {
try {
DOMParser parser = DOMParser.newParser(page);
parser.parse( new InputSource( new StringReader( pageText ) ) );
Index: ParsedHTML.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- ParsedHTML.java 12 Nov 2002 13:41:54 -0000 1.34
+++ ParsedHTML.java 25 Nov 2002 22:41:07 -0000 1.35
@@ -331,32 +331,6 @@
}
-//---------------------------------- protected members ------------------------------
-
-
- /**
- * Overrides the base URL for this HTML segment.
- **/
- protected void setBaseURL( URL baseURL ) {
- _baseURL = baseURL;
- }
-
-
- /**
- * Overrides the base target for this HTML segment.
- **/
- protected void setBaseTarget( String baseTarget ) {
- _baseTarget = baseTarget;
- }
-
-
-
- protected Node getRootNode() {
- if (_rootNode == null) throw new IllegalStateException( "The root node has not been specified" );
- return _rootNode;
- }
-
-
//---------------------------------- package members --------------------------------
@@ -396,6 +370,14 @@
//---------------------------------- private members --------------------------------
+
+
+ private Node getRootNode() {
+ if (_rootNode == null) throw new IllegalStateException( "The root node has not been specified" );
+ return _rootNode;
+ }
+
+
/**
* Returns true if the node is a link anchor node.
Index: WebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- WebRequest.java 10 Oct 2002 21:10:19 -0000 1.51
+++ WebRequest.java 25 Nov 2002 22:41:07 -0000 1.52
@@ -252,7 +252,7 @@
public String toString() {
- return getMethod() + " request for " + getURLString();
+ return getMethod() + " request for (" + getURLBase() + ") " + getURLString();
}
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -r1.102 -r1.103
--- WebResponse.java 19 Nov 2002 04:57:17 -0000 1.102
+++ WebResponse.java 25 Nov 2002 22:41:07 -0000 1.103
@@ -28,6 +28,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
+import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
@@ -48,8 +49,11 @@
**/
abstract
public class WebResponse implements HTMLSegment, CookieSource {
- private String _refreshHeader;
+
+ private String _refreshHeader;
private boolean _hasSubframes;
+ private URL _baseURL;
+ private String _baseTarget;
/**
@@ -73,7 +77,7 @@
* Returns the URL which invoked this response.
**/
public URL getURL() {
- return _url;
+ return _pageURL;
}
@@ -655,12 +659,12 @@
public void setLocation( String relativeURL ) throws IOException, SAXException {
- getWindow().getResponse( new GetMethodWebRequest( _url, relativeURL, _frameName ) );
+ getWindow().getResponse( new GetMethodWebRequest( _pageURL, relativeURL, _frameName ) );
}
public URL getURL() {
- return WebResponse.this._url;
+ return WebResponse.this._pageURL;
}
}
@@ -681,8 +685,8 @@
**/
protected WebResponse( WebClient client, String frameName, URL url ) {
_client = client;
- _url = url;
- _frameName = frameName;
+ _baseURL = _pageURL = url;
+ _baseTarget = _frameName = frameName;
}
@@ -800,7 +804,7 @@
// the following variables are essentially final; however, the JDK 1.1 compiler does not handle blank final variables properly with
// multiple constructors that call each other, so the final qualifiers have been removed.
- private URL _url;
+ private URL _pageURL;
private String _frameName;
@@ -838,17 +842,24 @@
}
- private void readTags( byte[] rawMessage ) throws UnsupportedEncodingException {
+ private void readTags( byte[] rawMessage ) throws UnsupportedEncodingException, MalformedURLException {
ByteTagParser parser = new ByteTagParser( rawMessage );
ByteTag tag = parser.getNextTag();
while (tag != null && !tag.getName().equalsIgnoreCase( "body" )) {
if (tag.getName().equalsIgnoreCase( "meta" )) processMetaTag( tag );
+ if (tag.getName().equalsIgnoreCase( "base" )) processBaseTag( tag );
if (tag.getName().equalsIgnoreCase( "frameset" )) _hasSubframes = true;
tag = parser.getNextTag();
}
}
+ private void processBaseTag( ByteTag tag ) throws MalformedURLException {
+ if (tag.getAttribute( "href" ) != null) _baseURL = new URL( tag.getAttribute( "href" ) );
+ if (tag.getAttribute( "target" ) != null) _baseTarget = tag.getAttribute( "target" );
+ }
+
+
private void processMetaTag( ByteTag tag ) {
if (isHttpEquivMetaTag( tag, "content-type" )) {
inferContentType( tag.getAttribute( "content" ) );
@@ -883,7 +894,7 @@
if (splitIndex < 0) splitIndex = 0;
try {
_refreshDelay = Integer.parseInt( refreshHeader.substring( 0, splitIndex ) );
- _refreshRequest = new GetMethodWebRequest( _url, getRefreshURL( refreshHeader.substring( splitIndex+1 ) ), _frameName );
+ _refreshRequest = new GetMethodWebRequest( _pageURL, getRefreshURL( refreshHeader.substring( splitIndex+1 ) ), _frameName );
} catch (NumberFormatException e) {
_refreshDelay = 0;
System.out.println( "Unable to interpret refresh tag: \"" + refreshHeader + '"' );
@@ -958,8 +969,8 @@
if (_page == null) {
try {
if (!isHTML()) throw new NotHTMLException( getContentType() );
- _page = new HTMLPage( this, _url, _frameName, getCharacterSet() );
- _page.parse( getText() );
+ _page = new HTMLPage( this, _baseURL, _baseTarget, getCharacterSet() );
+ _page.parse( getText(), _pageURL );
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException( e.toString() );
|