[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HTMLElementPredicate.java,NONE,1.1 HTMLSe
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv23945/src/com/meterware/httpunit Modified Files: HTMLSegment.java HttpUnitUtils.java ParsedHTML.java WebLink.java WebResponse.java Added Files: HTMLElementPredicate.java Log Message: Make link searching mechanism more generic ***** Error reading new file[Errno 2] No such file or directory: 'HTMLElementPredicate.java' Index: HTMLSegment.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLSegment.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- HTMLSegment.java 22 Aug 2002 16:19:54 -0000 1.4 +++ HTMLSegment.java 13 Sep 2002 14:17:25 -0000 1.5 @@ -71,6 +71,20 @@ /** + * Returns the first link found in the page matching the specified criteria. + * @exception SAXException thrown if there is an error parsing the response. + **/ + public WebLink getFirstMatchingLink( HTMLElementPredicate predicate, Object value ) throws SAXException; + + + /** + * Returns all links found in the page matching the specified criteria. + * @exception SAXException thrown if there is an error parsing the response. + **/ + public WebLink[] getMatchingLinks( HTMLElementPredicate predicate, Object criteria ) throws SAXException; + + + /** * 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. **/ @@ -140,5 +154,6 @@ * @exception SAXException thrown if there is an error parsing the segment. **/ public WebTable getTableWithID( final String ID ) throws SAXException; - + + } Index: HttpUnitUtils.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpUnitUtils.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- HttpUnitUtils.java 29 Aug 2002 18:39:11 -0000 1.10 +++ HttpUnitUtils.java 13 Sep 2002 14:17:25 -0000 1.11 @@ -133,4 +133,43 @@ } return result; } + + + /** + * Return true if the first string contains the second. + * Case sensitivity is according to the setting of HttpUnitOptions.matchesIgnoreCase + */ + static boolean contains( String string, String substring ) { + if (HttpUnitOptions.getMatchesIgnoreCase()) { + return string.toUpperCase().indexOf( substring.toUpperCase() ) >= 0; + } else { + return string.indexOf( substring ) >= 0; + } + } + + + /** + * Return true if the first string starts with the second. + * Case sensitivity is according to the setting of HttpUnitOptions.matchesIgnoreCase + */ + static boolean hasPrefix( String string, String prefix ) { + if (HttpUnitOptions.getMatchesIgnoreCase()) { + return string.toUpperCase().startsWith( prefix.toUpperCase() ); + } else { + return string.startsWith( prefix ); + } + } + + + /** + * Return true if the first string equals the second. + * Case sensitivity is according to the setting of HttpUnitOptions.matchesIgnoreCase + */ + static boolean matches( String string1, String string2 ) { + if (HttpUnitOptions.getMatchesIgnoreCase()) { + return string1.equalsIgnoreCase( string2 ); + } else { + return string1.equals( string2 ); + } + } } Index: ParsedHTML.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- ParsedHTML.java 30 Aug 2002 15:20:09 -0000 1.27 +++ ParsedHTML.java 13 Sep 2002 14:17:25 -0000 1.28 @@ -125,7 +125,7 @@ public WebLink getLinkWith( String text ) { WebLink[] links = getLinks(); for (int i = 0; i < links.length; i++) { - if (contains( links[i].asText(), text )) return links[i]; + if (HttpUnitUtils.contains( links[i].asText(), text )) return links[i]; } return null; } @@ -169,6 +169,31 @@ /** + * Returns the first link found in the page matching the specified criteria. + **/ + public WebLink getFirstMatchingLink( HTMLElementPredicate predicate, Object criteria ) { + WebLink[] links = getLinks(); + for (int i = 0; i < links.length; i++) { + if (predicate.matchesCriteria( links[i], criteria )) return links[i]; + } + return null; + } + + + /** + * Returns all links found in the page matching the specified criteria. + **/ + public WebLink[] getMatchingLinks( HTMLElementPredicate predicate, Object criteria ) { + ArrayList matches = new ArrayList(); + WebLink[] links = getLinks(); + for (int i = 0; i < links.length; i++) { + if (predicate.matchesCriteria( links[i], criteria )) matches.add( links[i] ); + } + return (WebLink[]) matches.toArray( new WebLink[ matches.size() ] ); + } + + + /** * Returns the images found in the page in the order in which they appear. **/ public WebImage[] getImages() { @@ -242,7 +267,7 @@ public boolean isTrue( WebTable table ) { table.purgeEmptyCells(); return table.getRowCount() > 0 && - matches( table.getCellAsText(0,0), text ); + HttpUnitUtils.matches( table.getCellAsText(0,0), text ); } } ); } @@ -258,7 +283,7 @@ public boolean isTrue( WebTable table ) { table.purgeEmptyCells(); return table.getRowCount() > 0 && - hasPrefix( table.getCellAsText(0,0).toUpperCase(), text ); + HttpUnitUtils.hasPrefix( table.getCellAsText(0,0).toUpperCase(), text ); } } ); } @@ -272,7 +297,7 @@ public WebTable getTableWithSummary( final String summary ) { return getTableSatisfyingPredicate( getTables(), new TablePredicate() { public boolean isTrue( WebTable table ) { - return matches( table.getSummary(), summary ); + return HttpUnitUtils.matches( table.getSummary(), summary ); } } ); } @@ -286,7 +311,7 @@ public WebTable getTableWithID( final String ID ) { return getTableSatisfyingPredicate( getTables(), new TablePredicate() { public boolean isTrue( WebTable table ) { - return matches( table.getID(), ID ); + return HttpUnitUtils.matches( table.getID(), ID ); } } ); } @@ -382,33 +407,6 @@ private String _baseTarget; private String _characterSet; - - - private boolean contains( String string, String substring ) { - if (HttpUnitOptions.getMatchesIgnoreCase()) { - return string.toUpperCase().indexOf( substring.toUpperCase() ) >= 0; - } else { - return string.indexOf( substring ) >= 0; - } - } - - - private boolean hasPrefix( String string, String prefix ) { - if (HttpUnitOptions.getMatchesIgnoreCase()) { - return string.toUpperCase().startsWith( prefix.toUpperCase() ); - } else { - return string.startsWith( prefix ); - } - } - - - private boolean matches( String string1, String string2 ) { - if (HttpUnitOptions.getMatchesIgnoreCase()) { - return string1.equalsIgnoreCase( string2 ); - } else { - return string1.equals( string2 ); - } - } private String getValue( Node node ) { Index: WebLink.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebLink.java,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- WebLink.java 5 Sep 2002 19:10:44 -0000 1.27 +++ WebLink.java 13 Sep 2002 14:17:25 -0000 1.28 @@ -39,6 +39,9 @@ **/ public class WebLink extends FixedURLWebRequestSource { + /** Predicate to match part or all of a link's URL string. **/ + public final static HTMLElementPredicate MATCH_URL_STRING; + private Scriptable _scriptable; @@ -144,6 +147,17 @@ Scriptable getScriptableObject() { if (_scriptable == null) _scriptable = new Scriptable(); return _scriptable; + } + + + static { + MATCH_URL_STRING = new HTMLElementPredicate() { + public boolean matchesCriteria( Object htmlElement, Object criteria ) { + return HttpUnitUtils.contains( ((WebLink) htmlElement).getURLString(), (String) criteria ); + }; + }; + + } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.78 retrieving revision 1.79 diff -u -r1.78 -r1.79 --- WebResponse.java 30 Aug 2002 18:17:48 -0000 1.78 +++ WebResponse.java 13 Sep 2002 14:17:25 -0000 1.79 @@ -363,6 +363,24 @@ /** + * Returns the first link found in the page matching the specified criteria. + * @exception SAXException thrown if there is an error parsing the response. + **/ + public WebLink getFirstMatchingLink( HTMLElementPredicate predicate, Object criteria ) throws SAXException { + return getReceivedPage().getFirstMatchingLink( predicate, criteria ); + } + + + /** + * Returns all links found in the page matching the specified criteria. + * @exception SAXException thrown if there is an error parsing the response. + **/ + public WebLink[] getMatchingLinks( HTMLElementPredicate predicate, Object criteria ) throws SAXException { + return getReceivedPage().getMatchingLinks( predicate, criteria ); + } + + + /** * Returns the images found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing the response. **/ |