[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HTMLSegment.java,1.5,1.6 ParsedHTML.java,
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-10-04 02:16:03
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv16519/src/com/meterware/httpunit Modified Files: HTMLSegment.java ParsedHTML.java WebImage.java WebResponse.java WebTable.java Log Message: Made search mechanism for tables generic, similar to that for links Index: HTMLSegment.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLSegment.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- HTMLSegment.java 13 Sep 2002 14:17:25 -0000 1.5 +++ HTMLSegment.java 4 Oct 2002 02:16:00 -0000 1.6 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000-2001, Russell Gold +* Copyright (c) 2000-2002, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -118,6 +118,14 @@ * @exception SAXException thrown if there is an error parsing the segment. **/ public WebTable[] getTables() throws SAXException; + + + /** + * Returns the first table in the response which matches the specified predicate and value. + * Will recurse into any nested tables, as needed. + * @return the selected table, or null if none is found + **/ + public WebTable getFirstMatchingTable( HTMLElementPredicate predicate, Object criteria ) throws SAXException; /** Index: ParsedHTML.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- ParsedHTML.java 2 Oct 2002 16:20:45 -0000 1.29 +++ ParsedHTML.java 4 Oct 2002 02:16:00 -0000 1.30 @@ -242,18 +242,22 @@ /** + * Returns the first table in the response which matches the specified predicate and value. + * Will recurse into any nested tables, as needed. + * @return the selected table, or null if none is found + **/ + public WebTable getFirstMatchingTable( HTMLElementPredicate predicate, Object criteria ) { + return getTableSatisfyingPredicate( getTables(), predicate, criteria ); + } + + + /** * Returns the first table in the response which has the specified text as the full text of * its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. * @return the selected table, or null if none is found **/ - public WebTable getTableStartingWith( final String text ) { - return getTableSatisfyingPredicate( getTables(), new TablePredicate() { - public boolean isTrue( WebTable table ) { - table.purgeEmptyCells(); - return table.getRowCount() > 0 && - HttpUnitUtils.matches( table.getCellAsText(0,0), text ); - } - } ); + public WebTable getTableStartingWith( String text ) { + return getFirstMatchingTable( WebTable.MATCH_FIRST_NONBLANK_CELL, text ); } @@ -262,14 +266,8 @@ * in its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. * @return the selected table, or null if none is found **/ - public WebTable getTableStartingWithPrefix( final String text ) { - return getTableSatisfyingPredicate( getTables(), new TablePredicate() { - public boolean isTrue( WebTable table ) { - table.purgeEmptyCells(); - return table.getRowCount() > 0 && - HttpUnitUtils.hasPrefix( table.getCellAsText(0,0).toUpperCase(), text ); - } - } ); + public WebTable getTableStartingWithPrefix( String text ) { + return getFirstMatchingTable( WebTable.MATCH_FIRST_NONBLANK_CELL_PREFIX, text ); } @@ -278,12 +276,8 @@ * Will recurse into any nested tables, as needed. * @return the selected table, or null if none is found **/ - public WebTable getTableWithSummary( final String summary ) { - return getTableSatisfyingPredicate( getTables(), new TablePredicate() { - public boolean isTrue( WebTable table ) { - return HttpUnitUtils.matches( table.getSummary(), summary ); - } - } ); + public WebTable getTableWithSummary( String summary ) { + return getFirstMatchingTable( WebTable.MATCH_SUMMARY, summary ); } @@ -292,12 +286,8 @@ * Will recurse into any nested tables, as needed. * @return the selected table, or null if none is found **/ - public WebTable getTableWithID( final String ID ) { - return getTableSatisfyingPredicate( getTables(), new TablePredicate() { - public boolean isTrue( WebTable table ) { - return HttpUnitUtils.matches( table.getID(), ID ); - } - } ); + public WebTable getTableWithID( String ID ) { + return getFirstMatchingTable( WebTable.MATCH_ID, ID ); } @@ -361,25 +351,6 @@ } - interface TablePredicate { - public boolean isTrue( WebTable table ); - } - - - interface LinkPredicate { - public boolean isTrue( WebLink link ); - } - - - WebLink getLinkSatisfyingPredicate( LinkPredicate predicate ) { - WebLink[] links = getLinks(); - for (int i = 0; i < links.length; i++) { - if (predicate.isTrue( links[ i ] )) return links[ i ]; - } - return null; - } - - //---------------------------------- private members -------------------------------- private Node _rootNode; @@ -416,9 +387,9 @@ /** * Returns the table with the specified text in its summary attribute. **/ - private WebTable getTableSatisfyingPredicate( WebTable[] tables, TablePredicate predicate ) { + private WebTable getTableSatisfyingPredicate( WebTable[] tables, HTMLElementPredicate predicate, Object value ) { for (int i = 0; i < tables.length; i++) { - if (predicate.isTrue( tables[i] )) { + if (predicate.matchesCriteria( tables[i], value )) { return tables[i]; } else { for (int j = 0; j < tables[i].getRowCount(); j++) { @@ -427,7 +398,7 @@ if (cell != null) { WebTable[] innerTables = cell.getTables(); if (innerTables.length != 0) { - WebTable result = getTableSatisfyingPredicate( innerTables, predicate ); + WebTable result = getTableSatisfyingPredicate( innerTables, predicate, value ); if (result != null) return result; } } Index: WebImage.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebImage.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- WebImage.java 5 Sep 2002 19:10:44 -0000 1.6 +++ WebImage.java 4 Oct 2002 02:16:00 -0000 1.7 @@ -67,15 +67,15 @@ public WebLink getLink() { - return _parsedHTML.getLinkSatisfyingPredicate( new ParsedHTML.LinkPredicate() { + return _parsedHTML.getFirstMatchingLink( new HTMLElementPredicate() { - public boolean isTrue( WebLink link ) { - for (Node parent = _node.getParentNode(); parent != null; parent = parent.getParentNode()) { - if (parent.equals( link.getNode() )) return true; + public boolean matchesCriteria( Object link, Object parentNode ) { + for (Node parent = (Node) parentNode; parent != null; parent = parent.getParentNode()) { + if (parent.equals( ((WebLink) link).getNode() )) return true; } return false; } - } ); + }, _node.getParentNode() ); } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.81 retrieving revision 1.82 diff -u -r1.81 -r1.82 --- WebResponse.java 4 Oct 2002 00:40:28 -0000 1.81 +++ WebResponse.java 4 Oct 2002 02:16:00 -0000 1.82 @@ -408,16 +408,6 @@ /** - * Returns the top-level tables found in this page in the order in which - * they appear. - * @exception SAXException thrown if there is an error parsing the response. - **/ - public WebTable[] getTables() throws SAXException { - return getReceivedPage().getTables(); - } - - - /** * Returns a copy of the domain object model tree associated with this response. * If the response is HTML, it will use a special parser which can transform HTML into an XML DOM. * @exception SAXException thrown if there is an error parsing the response. @@ -432,6 +422,26 @@ throw new SAXException( e ); } } + } + + + /** + * Returns the top-level tables found in this page in the order in which + * they appear. + * @exception SAXException thrown if there is an error parsing the response. + **/ + public WebTable[] getTables() throws SAXException { + return getReceivedPage().getTables(); + } + + + /** + * Returns the first table in the response which matches the specified predicate and value. + * Will recurse into any nested tables, as needed. + * @return the selected table, or null if none is found + **/ + public WebTable getFirstMatchingTable( HTMLElementPredicate predicate, Object criteria ) throws SAXException { + return getReceivedPage().getFirstMatchingTable( predicate, criteria ); } Index: WebTable.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebTable.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- WebTable.java 6 Aug 2002 21:42:08 -0000 1.18 +++ WebTable.java 4 Oct 2002 02:16:00 -0000 1.19 @@ -34,6 +34,19 @@ public class WebTable { + /** Predicate to match the complete text of a table's first non-blank cell. **/ + public final static HTMLElementPredicate MATCH_FIRST_NONBLANK_CELL; + + /** Predicate to match a prefix of a table's first non-blank cell. **/ + public final static HTMLElementPredicate MATCH_FIRST_NONBLANK_CELL_PREFIX; + + /** Predicate to match a table's summary attribute. **/ + public final static HTMLElementPredicate MATCH_SUMMARY; + + /** Predicate to match a table's ID. **/ + public final static HTMLElementPredicate MATCH_ID; + + /** * Returns the number of rows in the table. **/ @@ -406,6 +419,41 @@ } } + static { + MATCH_FIRST_NONBLANK_CELL = new HTMLElementPredicate() { + public boolean matchesCriteria( Object htmlElement, Object criteria ) { + WebTable table = ((WebTable) htmlElement); + table.purgeEmptyCells(); + return table.getRowCount() > 0 && + HttpUnitUtils.matches( table.getCellAsText(0,0), (String) criteria ); + }; + }; + + + MATCH_FIRST_NONBLANK_CELL_PREFIX = new HTMLElementPredicate() { + public boolean matchesCriteria( Object htmlElement, Object criteria ) { + WebTable table = ((WebTable) htmlElement); + table.purgeEmptyCells(); + return table.getRowCount() > 0 && + HttpUnitUtils.hasPrefix( table.getCellAsText(0,0).toUpperCase(), (String) criteria ); + }; + }; + + + MATCH_ID = new HTMLElementPredicate() { + public boolean matchesCriteria( Object htmlElement, Object criteria ) { + return HttpUnitUtils.matches( ((WebTable) htmlElement).getID(), (String) criteria ); + }; + }; + + + MATCH_SUMMARY = new HTMLElementPredicate() { + public boolean matchesCriteria( Object htmlElement, Object criteria ) { + return HttpUnitUtils.matches( ((WebTable) htmlElement).getSummary(), (String) criteria ); + }; + }; + + } } |