[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit NodeUtils.java,1.11,1.12 ParsedHTML.java,
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-05 15:21:25
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv29000/src/com/meterware/httpunit Modified Files: NodeUtils.java ParsedHTML.java WebLink.java Log Message: Correct ordering of links array, support read of link.href Index: NodeUtils.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NodeUtils.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- NodeUtils.java 4 Mar 2002 02:10:49 -0000 1.11 +++ NodeUtils.java 5 Aug 2002 15:21:19 -0000 1.12 @@ -73,31 +73,32 @@ return node.getAttributes().getNamedItem( attributeName ) != null; } + + interface NodeAction { + /** + * Does appropriate processing on specified element. Will return false if the subtree below the element + * should be skipped. + */ + public boolean processElement( Element element ); + + /** + * Processes a text node. + */ + public void processTextNodeValue( String value ); + } + /** * Converts the DOM trees rooted at the specified nodes to text, ignoring * any HTML tags. **/ public static String asText( NodeList rootNodes ) { - StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE); - Stack pendingNodes = new Stack(); - pushNodeList( rootNodes, pendingNodes ); - - while (!pendingNodes.empty()) { - Object pending = pendingNodes.pop(); - if (pending instanceof String) { - sb.append( pending ); - } else { - Node node = (Node) pending; - - if (node.getNodeType() == Node.TEXT_NODE) { - sb.append( convertNBSP( node.getNodeValue() ) ); - } else if (node.getNodeType() != Node.ELEMENT_NODE) { - continue; - } else if (node.getNodeName().equalsIgnoreCase( "p" )) { + final StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE); + processNodes( rootNodes, new NodeAction() { + public boolean processElement( Element node ) { + if (node.getNodeName().equalsIgnoreCase( "p" )) { sb.append( "\n" ); } else if (node.getNodeName().equalsIgnoreCase( "tr" )) { sb.append( "\n" ); - pendingNodes.push( " |" ); } else if (node.getNodeName().equalsIgnoreCase( "td" )) { sb.append( " | " ); } else if (node.getNodeName().equalsIgnoreCase( "th" )) { @@ -105,11 +106,36 @@ } else if (node.getNodeName().equalsIgnoreCase( "img" ) && HttpUnitOptions.getImagesTreatedAsAltText()) { sb.append( getNodeAttribute( node, "alt" ) ); } - - pushNodeList( node.getChildNodes(), pendingNodes ); + return true; } - } + public void processTextNodeValue( String value ) { + sb.append( convertNBSP( value ) ); + } + } ); return sb.toString(); + } + + + /** + * Converts the DOM trees rooted at the specified nodes to text, ignoring + * any HTML tags. + **/ + public static void processNodes( NodeList rootNodes, NodeAction action ) { + Stack pendingNodes = new Stack(); + pushNodeList( rootNodes, pendingNodes ); + + while (!pendingNodes.empty()) { + Node node = (Node) pendingNodes.pop(); + + if (node.getNodeType() == Node.TEXT_NODE) { + action.processTextNodeValue( node.getNodeValue() ); + } else if (node.getNodeType() != Node.ELEMENT_NODE) { + continue; + } else + action.processElement( (Element) node ); + + pushNodeList( node.getChildNodes(), pendingNodes ); + } } Index: ParsedHTML.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- ParsedHTML.java 1 Aug 2002 14:58:59 -0000 1.22 +++ ParsedHTML.java 5 Aug 2002 15:21:19 -0000 1.23 @@ -22,6 +22,7 @@ import java.net.URL; import java.util.Vector; +import java.util.ArrayList; import org.w3c.dom.*; @@ -94,23 +95,26 @@ **/ public WebLink[] getLinks() { if (_links == null) { - Vector list = new Vector(); - addLinkAnchors( list, NodeUtils.getElementsByTagName( _rootNode, "a" ) ); - addLinkAnchors( list, NodeUtils.getElementsByTagName( _rootNode, "area" ) ); - _links = new WebLink[ list.size() ]; - list.copyInto( _links ); + final ArrayList list = new ArrayList(); + NodeUtils.processNodes( _rootNode.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 ); + return true; + } + public void processTextNodeValue( String value ) { + } + } ); + _links = (WebLink[]) list.toArray( new WebLink[ list.size() ] ); } return _links; } - private void addLinkAnchors(Vector list, NodeList nl) { - for (int i = 0; i < nl.getLength(); i++) { - Node child = nl.item(i); - if (isLinkAnchor( child )) { - list.addElement( new WebLink( _response, _baseURL, _baseTarget, child ) ); - } + private void addLinkAnchor( ArrayList list, Node child ) { + if (isLinkAnchor( child )) { + list.add( new WebLink( _response, _baseURL, _baseTarget, child ) ); } } Index: WebLink.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebLink.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- WebLink.java 2 Aug 2002 16:26:15 -0000 1.21 +++ WebLink.java 5 Aug 2002 15:21:19 -0000 1.22 @@ -20,6 +20,7 @@ * *******************************************************************************************************************/ import java.net.URL; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; @@ -77,8 +78,8 @@ /** * Submits a request as though the user had clicked on this link. Will also fire the 'onClick' event if defined. **/ - public void click() throws IOException, SAXException { - submitRequest(); + public WebResponse click() throws IOException, SAXException { + return submitRequest(); } @@ -228,6 +229,23 @@ public class Scriptable extends ScriptableObject { + + public Object get( String propertyName ) { + if (propertyName.equalsIgnoreCase( "href" )) { + return getReference().toExternalForm(); + } else { + return super.get( propertyName ); + } + } + + + private URL getReference() { + try { + return getRequest().getURL(); + } catch (MalformedURLException e) { + return WebLink.this.getBaseURL(); + } + } } |