[Htmlparser-cvs] htmlparser/src/org/htmlparser/tags AppletTag.java,1.38,1.39 CompositeTag.java,1.73,
Brought to you by:
derrickoswald
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23128/tags Modified Files: AppletTag.java CompositeTag.java FormTag.java SelectTag.java TableRow.java TableTag.java Log Message: Fix bug #882940 empty applet tag contents causes NullPointerException Also found and fixed other similar problems where getChildren() could return null. Then changed table row and column handling to handle rows and columns embedded within other tags. Index: AppletTag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/AppletTag.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** AppletTag.java 2 Jan 2004 16:24:54 -0000 1.38 --- AppletTag.java 24 Jan 2004 23:57:52 -0000 1.39 *************** *** 94,114 **** ret = new Hashtable (); kids = getChildren (); ! for (int i = 0; i < kids.size (); i++) ! { ! node = children.elementAt(i); ! if (node instanceof Tag) { ! tag = (Tag)node; ! if (tag.getTagName().equals ("PARAM")) { ! paramName = tag.getAttribute ("NAME"); ! if (null != paramName && 0 != paramName.length ()) { ! paramValue = tag.getAttribute ("VALUE"); ! ret.put (paramName,paramValue); } } } - } return (ret); --- 94,115 ---- ret = new Hashtable (); kids = getChildren (); ! if (null != kids) ! for (int i = 0; i < kids.size (); i++) { ! node = children.elementAt(i); ! if (node instanceof Tag) { ! tag = (Tag)node; ! if (tag.getTagName().equals ("PARAM")) { ! paramName = tag.getAttribute ("NAME"); ! if (null != paramName && 0 != paramName.length ()) ! { ! paramValue = tag.getAttribute ("VALUE"); ! ret.put (paramName,paramValue); ! } } } } return (ret); *************** *** 195,223 **** kids = getChildren (); ! // erase appletParams from kids ! for (int i = 0; i < kids.size (); ) ! { ! node = kids.elementAt (i); ! if (node instanceof Tag) ! if (((Tag)node).getTagName ().equals ("PARAM")) ! { ! kids.remove (i); ! // remove whitespace too ! if (i < kids.size ()) { ! node = kids.elementAt (i); ! if (node instanceof StringNode) { ! string = (StringNode)node; ! if (0 == string.getText ().trim ().length ()) ! kids.remove (i); ! } } ! } else i++; ! else ! i++; ! } // add newAppletParams to kids --- 196,227 ---- kids = getChildren (); ! if (null == kids) ! kids = new NodeList (); ! else ! // erase appletParams from kids ! for (int i = 0; i < kids.size (); ) ! { ! node = kids.elementAt (i); ! if (node instanceof Tag) ! if (((Tag)node).getTagName ().equals ("PARAM")) { ! kids.remove (i); ! // remove whitespace too ! if (i < kids.size ()) { ! node = kids.elementAt (i); ! if (node instanceof StringNode) ! { ! string = (StringNode)node; ! if (0 == string.getText ().trim ().length ()) ! kids.remove (i); ! } ! } } ! else ! i++; else i++; ! } // add newAppletParams to kids Index: CompositeTag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/CompositeTag.java,v retrieving revision 1.73 retrieving revision 1.74 diff -C2 -d -r1.73 -r1.74 *** CompositeTag.java 24 Jan 2004 17:41:32 -0000 1.73 --- CompositeTag.java 24 Jan 2004 23:57:52 -0000 1.74 *************** *** 84,88 **** public Node getChild (int index) { ! return (getChildren ().elementAt (index)); } --- 84,90 ---- public Node getChild (int index) { ! return ( ! (null == getChildren ()) ? null : ! getChildren ().elementAt (index)); } *************** *** 93,97 **** public Node [] getChildrenAsNodeArray () { ! return (getChildren ().toNodeArray ()); } --- 95,101 ---- public Node [] getChildrenAsNodeArray () { ! return ( ! (null == getChildren ()) ? new Node[0] : ! getChildren ().toNodeArray ()); } *************** *** 102,106 **** public void removeChild (int i) { ! getChildren ().remove (i); } --- 106,111 ---- public void removeChild (int i) { ! if (null != getChildren ()) ! getChildren ().remove (i); } *************** *** 112,116 **** public SimpleNodeIterator elements() { ! return (getChildren ().elements ()); } --- 117,123 ---- public SimpleNodeIterator elements() { ! return ( ! (null == getChildren ()) ? new NodeList ().elements () : ! getChildren ().elements ()); } *************** *** 212,222 **** * Note that this will not check for parent types, and will not * recurse through child tags ! * @param classType ! * @return NodeList */ ! public NodeList searchFor(Class classType) { ! return (getChildren ().searchFor (classType)); } /** * Searches for any node whose text representation contains the search --- 219,233 ---- * Note that this will not check for parent types, and will not * recurse through child tags ! * @param classType The class to search for. ! * @param recursive If true, recursively search through the children. ! * @return A list of children found. */ ! public NodeList searchFor (Class classType, boolean recursive) { ! return ( ! (null == getChildren ()) ? new NodeList () : ! getChildren ().searchFor (classType, recursive)); } + /** * Searches for any node whose text representation contains the search *************** *** 285,293 **** /** * Get child at given index ! * @param index ! * @return Node */ ! public Node childAt(int index) { ! return (getChildren ().elementAt (index)); } --- 296,307 ---- /** * Get child at given index ! * @param index The index into the child node list. ! * @return Node The child node at the given index or null if none. */ ! public Node childAt (int index) ! { ! return ( ! (null == getChildren ()) ? null : ! getChildren ().elementAt (index)); } Index: FormTag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/FormTag.java,v retrieving revision 1.46 retrieving revision 1.47 diff -C2 -d -r1.46 -r1.47 *** FormTag.java 14 Jan 2004 02:53:46 -0000 1.46 --- FormTag.java 24 Jan 2004 23:57:52 -0000 1.47 *************** *** 95,99 **** public NodeList getFormInputs() { ! return (getChildren().searchFor(InputTag.class, true)); } --- 95,99 ---- public NodeList getFormInputs() { ! return (searchFor (InputTag.class, true)); } *************** *** 104,108 **** public NodeList getFormTextareas() { ! return (getChildren().searchFor(TextareaTag.class, true)); } --- 104,108 ---- public NodeList getFormTextareas() { ! return (searchFor (TextareaTag.class, true)); } Index: SelectTag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/SelectTag.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** SelectTag.java 14 Jan 2004 02:53:46 -0000 1.37 --- SelectTag.java 24 Jan 2004 23:57:52 -0000 1.38 *************** *** 88,92 **** OptionTag[] ret; ! list = getChildren ().searchFor (OptionTag.class, true); ret = new OptionTag[list.size()]; list.copyToNodeArray (ret); --- 88,92 ---- OptionTag[] ret; ! list = searchFor (OptionTag.class, true); ret = new OptionTag[list.size()]; list.copyToNodeArray (ret); Index: TableRow.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/TableRow.java,v retrieving revision 1.39 retrieving revision 1.40 diff -C2 -d -r1.39 -r1.40 *** TableRow.java 24 Jan 2004 18:12:41 -0000 1.39 --- TableRow.java 24 Jan 2004 23:57:52 -0000 1.40 *************** *** 27,30 **** --- 27,37 ---- package org.htmlparser.tags; + import org.htmlparser.NodeFilter; + import org.htmlparser.filters.AndFilter; + import org.htmlparser.filters.IsEqualFilter; + import org.htmlparser.filters.NodeClassFilter; + import org.htmlparser.filters.HasParentFilter; + import org.htmlparser.filters.NotFilter; + import org.htmlparser.filters.OrFilter; import org.htmlparser.util.NodeList; *************** *** 79,121 **** /** * Get the number of columns in this row. */ public int getColumnCount () { ! return ( ! (null == getChildren ()) ? 0 : ! getChildren ().searchFor (TableColumn.class).size ()); } /** ! * Get the children (columns) of this row. */ ! public TableColumn [] getColumns () { ! NodeList list; ! TableColumn [] ret; ! if (null != getChildren ()) { ! list = getChildren ().searchFor (TableColumn.class); ! ret = new TableColumn[list.size ()]; ! list.copyToNodeArray (ret); } else ! ret = new TableColumn[0]; ! return (ret); } /** - * Checks if this table has a header - * @return <code>true</code> if there is a header tag. - */ - public boolean hasHeader () - { - return (0 != getHeaderCount ()); - } - - /** * Get the number of headers in this row. * @return The count of header tags in this row. --- 86,174 ---- /** + * Get the column tags within this row. + */ + public TableColumn[] getColumns () + { + NodeList kids; + NodeClassFilter cls; + HasParentFilter recursion; + NodeFilter filter; + TableColumn[] ret; + + kids = getChildren (); + if (null != kids) + { + cls = new NodeClassFilter (TableRow.class); + recursion = new HasParentFilter (null); + filter = new OrFilter ( + new AndFilter ( + cls, + new IsEqualFilter (this)), + new AndFilter ( // recurse up the parent chain + new NotFilter (cls), // but not past the first row + recursion)); + recursion.mFilter = filter; + kids = kids.extractAllNodesThatMatch ( + // it's a column, and has this row as it's enclosing row + new AndFilter ( + new NodeClassFilter (TableColumn.class), + filter), true); + ret = new TableColumn[kids.size ()]; + kids.copyToNodeArray (ret); + } + else + ret = new TableColumn[0]; + + return (ret); + } + + /** * Get the number of columns in this row. */ public int getColumnCount () { ! return (getColumns ().length); } /** ! * Get the header of this table ! * @return Table header tags contained in this row. */ ! public TableHeader[] getHeaders () { ! NodeList kids; ! NodeClassFilter cls; ! HasParentFilter recursion; ! NodeFilter filter; ! TableHeader[] ret; ! kids = getChildren (); ! if (null != kids) { ! cls = new NodeClassFilter (TableRow.class); ! recursion = new HasParentFilter (null); ! filter = new OrFilter ( ! new AndFilter ( ! cls, ! new IsEqualFilter (this)), ! new AndFilter ( // recurse up the parent chain ! new NotFilter (cls), // but not past the first row ! recursion)); ! recursion.mFilter = filter; ! kids = kids.extractAllNodesThatMatch ( ! // it's a header, and has this row as it's enclosing row ! new AndFilter ( ! new NodeClassFilter (TableHeader.class), ! filter), true); ! ret = new TableHeader[kids.size ()]; ! kids.copyToNodeArray (ret); } else ! ret = new TableHeader[0]; ! return (ret); } /** * Get the number of headers in this row. * @return The count of header tags in this row. *************** *** 123,150 **** public int getHeaderCount () { ! return ( ! (null == getChildren ()) ? 0 : ! getChildren ().searchFor (TableHeader.class, false).size ()); } /** ! * Get the header of this table ! * @return Table header tags contained in this row. */ ! public TableHeader[] getHeader () { ! NodeList list; ! TableHeader [] ret; ! ! if (null != getChildren ()) ! { ! list = getChildren ().searchFor (TableHeader.class, false); ! ret = new TableHeader[list.size ()]; ! list.copyToNodeArray (ret); ! } ! else ! ret = new TableHeader[0]; ! ! return (ret); } } --- 176,189 ---- public int getHeaderCount () { ! return (getHeaders ().length); } /** ! * Checks if this table has a header ! * @return <code>true</code> if there is a header tag. */ ! public boolean hasHeader () { ! return (0 != getHeaderCount ()); } } Index: TableTag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/TableTag.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** TableTag.java 2 Jan 2004 16:24:55 -0000 1.38 --- TableTag.java 24 Jan 2004 23:58:04 -0000 1.39 *************** *** 27,30 **** --- 27,39 ---- package org.htmlparser.tags; + import org.htmlparser.NodeFilter; + import org.htmlparser.filters.AndFilter; + import org.htmlparser.filters.IsEqualFilter; + import org.htmlparser.filters.NodeClassFilter; + import org.htmlparser.filters.HasParentFilter; + import org.htmlparser.filters.NotFilter; + import org.htmlparser.filters.OrFilter; + import org.htmlparser.util.NodeList; + /** * A table tag. *************** *** 68,76 **** /** * Get the number of rows in this table. */ ! public int getRowCount() { ! return (getChildren().searchFor(TableRow.class).size()); } --- 77,124 ---- /** + * Get the row tags within this table. + * @return The rows directly contained by this table. + */ + public TableRow[] getRows () + { + NodeList kids; + NodeClassFilter cls; + HasParentFilter recursion; + NodeFilter filter; + TableRow[] ret; + + kids = getChildren (); + if (null != kids) + { + cls = new NodeClassFilter (TableTag.class); + recursion = new HasParentFilter (null); + filter = new OrFilter ( + new AndFilter ( + cls, + new IsEqualFilter (this)), + new AndFilter ( // recurse up the parent chain + new NotFilter (cls), // but not past the first table + recursion)); + recursion.mFilter = filter; + kids = kids.extractAllNodesThatMatch ( + // it's a row, and has this table as it's enclosing table + new AndFilter ( + new NodeClassFilter (TableRow.class), + filter), true); + ret = new TableRow[kids.size ()]; + kids.copyToNodeArray (ret); + } + else + ret = new TableRow[0]; + + return (ret); + } + + /** * Get the number of rows in this table. */ ! public int getRowCount () { ! return (getRows ().length); } *************** *** 78,83 **** * Get the row at the given index. */ ! public TableRow getRow(int i) { ! return (TableRow)(getChildren().searchFor(TableRow.class)).elementAt(i); } --- 126,141 ---- * Get the row at the given index. */ ! public TableRow getRow (int i) ! { ! TableRow[] rows; ! TableRow ret; ! ! rows = getRows (); ! if (i < rows.length) ! ret = rows[i]; ! else ! ret = null; ! ! return (ret); } |