[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HTMLSegment.java,1.3,1.4 ParsedHTML.java,
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-22 16:19:56
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv21232/src/com/meterware/httpunit Modified Files: HTMLSegment.java ParsedHTML.java WebImage.java WebResponse.java Log Message: Added WebImage.getAltText, WebResponse.getImageWithAltText Index: HTMLSegment.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLSegment.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HTMLSegment.java 9 Nov 2001 18:35:14 -0000 1.3 +++ HTMLSegment.java 22 Aug 2002 16:19:54 -0000 1.4 @@ -71,6 +71,34 @@ /** + * Returns the images found in the page in the order in which they appear. + * @exception SAXException thrown if there is an error parsing the segment. + **/ + public WebImage[] getImages() throws SAXException; + + + /** + * Returns the image found in the page with the specified name. + * @exception SAXException thrown if there is an error parsing the segment. + **/ + public WebImage getImageWithName( String name ) throws SAXException; + + + /** + * Returns the first image found in the page with the specified src attribute. + * @exception SAXException thrown if there is an error parsing the segment. + **/ + public WebImage getImageWithSource( String source ) throws SAXException; + + + /** + * Returns the first image found in the page with the specified alt attribute. + * @exception SAXException thrown if there is an error parsing the segment. + **/ + public WebImage getImageWithAltText( String source ) throws SAXException; + + + /** * Returns the top-level tables found in this HTML segment in the order in which * they appear. * @exception SAXException thrown if there is an error parsing the segment. Index: ParsedHTML.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- ParsedHTML.java 5 Aug 2002 17:34:25 -0000 1.24 +++ ParsedHTML.java 22 Aug 2002 16:19:54 -0000 1.25 @@ -135,20 +135,8 @@ * Returns the first link which contains an image with the specified text as its 'alt' attribute. **/ public WebLink getLinkWithImageText( String text ) { - WebLink[] links = getLinks(); - for (int i = 0; i < links.length; i++) { - NodeList nl = ((Element) links[i].getDOMSubtree()).getElementsByTagName( "img" ); - for (int j = 0; j < nl.getLength(); j++) { - NamedNodeMap nnm = nl.item(j).getAttributes(); - if (text.equals( getValue( nnm.getNamedItem( "alt" ) ) )) { - return links[i]; - } else if (HttpUnitOptions.getMatchesIgnoreCase() && - text.equalsIgnoreCase( getValue( nnm.getNamedItem( "alt" ) ) )) { - return links[i]; - } - } - } - return null; + WebImage image = getImageWithAltText( text ); + return image == null ? null : image.getLink(); } @@ -210,13 +198,26 @@ /** - * Returns the image found in the page with the specified src attribute. + * Returns the first image found in the page with the specified src attribute. **/ public WebImage getImageWithSource( String source ) { WebImage[] images = getImages(); for (int i = 0; i < images.length; i++) { if (images[i].getSource().equals( source )) return images[i]; else if (HttpUnitOptions.getMatchesIgnoreCase() && images[i].getSource().equalsIgnoreCase( source )) return images[i]; + } + return null; + } + + + /** + * Returns the first image found in the page with the specified alt attribute. + **/ + public WebImage getImageWithAltText( String altText ) { + WebImage[] images = getImages(); + for (int i = 0; i < images.length; i++) { + if (images[i].getSource().equals( altText )) return images[i]; + else if (HttpUnitOptions.getMatchesIgnoreCase() && images[i].getAltText().equalsIgnoreCase( altText )) return images[i]; } return null; } Index: WebImage.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebImage.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- WebImage.java 8 Aug 2002 20:47:21 -0000 1.3 +++ WebImage.java 22 Aug 2002 16:19:54 -0000 1.4 @@ -36,7 +36,8 @@ private Node _node; private ParsedHTML _parsedHTML; private Scriptable _scriptable; - private String _src; + private String _src; + private String _alt; WebImage( ParsedHTML parsedHTML, URL baseURL, Node node ) { @@ -44,6 +45,7 @@ _node = node; _parsedHTML = parsedHTML; _src = NodeUtils.getNodeAttribute( _node, "src" ); + _alt = NodeUtils.getNodeAttribute( _node, "alt" ); } @@ -54,6 +56,11 @@ public String getSource() { return _src; + } + + + public String getAltText() { + return _alt; } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.75 retrieving revision 1.76 diff -u -r1.75 -r1.76 --- WebResponse.java 21 Aug 2002 19:06:27 -0000 1.75 +++ WebResponse.java 22 Aug 2002 16:19:54 -0000 1.76 @@ -381,11 +381,19 @@ /** - * Returns the image found in the page with the specified src attribute. + * Returns the first image found in the page with the specified src attribute. * @exception SAXException thrown if there is an error parsing the response. **/ public WebImage getImageWithSource( String source ) throws SAXException { return getReceivedPage().getImageWithSource( source ); + } + + + /** + * Returns the first image found in the page with the specified alt attribute. + **/ + public WebImage getImageWithAltText( String altText ) throws SAXException { + return getReceivedPage().getImageWithAltText( altText ); } |