Thread: [Htmlparser-cvs] htmlparser/src/org/htmlparser/filters HasSiblingFilter.java,NONE,1.1 HasChildFilter
Brought to you by:
derrickoswald
From: Derrick O. <der...@us...> - 2005-02-13 20:36:44
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8914/src/org/htmlparser/filters Modified Files: HasChildFilter.java NodeClassFilter.java RegexFilter.java NotFilter.java TagNameFilter.java AndFilter.java HasAttributeFilter.java OrFilter.java HasParentFilter.java StringFilter.java Added Files: HasSiblingFilter.java Log Message: FilterBuilder Index: AndFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/AndFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AndFilter.java 8 Nov 2003 21:30:58 -0000 1.1 --- AndFilter.java 13 Feb 2005 20:36:00 -0000 1.2 *************** *** 31,50 **** /** ! * This class accepts all nodes matching both filters (AND operation). */ ! public class AndFilter implements NodeFilter { /** ! * The left hand side. */ ! protected NodeFilter mLeft; /** ! * The right hand side. */ ! protected NodeFilter mRight; /** ! * Creates a new instance of AndFilter that accepts nodes acceptable to both filters. * @param left One filter. * @param right The other filter. --- 31,58 ---- /** ! * Accepts nodes matching all of it's predicate filters (AND operation). */ ! public class AndFilter ! implements ! NodeFilter { /** ! * The predicates that are to be and'ed together; */ ! protected NodeFilter[] mPredicates; /** ! * Creates a new instance of an AndFilter. ! * With no predicates, this would always answer <code>true</code> ! * to {@link #accept}. ! * @see #setPredicates */ ! public AndFilter () ! { ! setPredicates (null); ! } /** ! * Creates a new instance of an AndFilter that accepts nodes acceptable to both filters. * @param left One filter. * @param right The other filter. *************** *** 52,66 **** public AndFilter (NodeFilter left, NodeFilter right) { ! mLeft = left; ! mRight = right; } /** ! * Accept nodes that are acceptable to both filters. * @param node The node to check. */ public boolean accept (Node node) { ! return (mLeft.accept (node) && mRight.accept (node)); } } --- 60,110 ---- public AndFilter (NodeFilter left, NodeFilter right) { ! NodeFilter[] predicates; ! ! predicates = new NodeFilter[2]; ! predicates[0] = left; ! predicates[1] = right; ! setPredicates (predicates); } /** ! * Get the predicates used by this AndFilter. ! * @return The predicates currently in use. ! */ ! public NodeFilter[] getPredicates () ! { ! return (mPredicates); ! } ! ! /** ! * Set the predicates for this AndFilter. ! * @param predicates The list of predidcates to use in {@link #accept}. ! */ ! public void setPredicates (NodeFilter[] predicates) ! { ! if (null == predicates) ! predicates = new NodeFilter[0]; ! mPredicates = predicates; ! } ! ! // ! // NodeFilter interface ! // ! ! /** ! * Accept nodes that are acceptable to all of it's predicate filters. * @param node The node to check. */ public boolean accept (Node node) { ! boolean ret; ! ! ret = true; ! ! for (int i = 0; ret && (i < mPredicates.length); i++) ! if (!mPredicates[i].accept (node)) ! ret = false; ! ! return (ret); } } Index: StringFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/StringFilter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** StringFilter.java 24 May 2004 16:18:13 -0000 1.4 --- StringFilter.java 13 Feb 2005 20:36:00 -0000 1.5 *************** *** 35,40 **** /** * This class accepts all string nodes containing the given string. */ ! public class StringFilter implements NodeFilter { /** --- 35,45 ---- /** * This class accepts all string nodes containing the given string. + * This is a fairly simplistic filter, so for more sophisticated + * string matching, for example newline and whitespace handling, + * use a {@link RegexFilter} instead. */ ! public class StringFilter ! implements ! NodeFilter { /** *************** *** 44,59 **** /** * Case sensitive toggle. */ protected boolean mCaseSensitive; /** ! * The locale to use converting to uppercase in the case insensitive searches. */ protected Locale mLocale; /** ! * Creates a new instance of StringFilter that accepts string nodes containing a certain string. ! * The comparison is case insensitive. * @param pattern The pattern to search for. */ --- 49,78 ---- /** + * The string to really search for (converted to uppercase if necessary). + */ + protected String mUpperPattern; + + /** * Case sensitive toggle. + * If <code>true</code> strings are compared with case sensitivity. */ protected boolean mCaseSensitive; /** ! * The locale to use converting to uppercase in case insensitive searches. */ protected Locale mLocale; /** ! * Creates a new instance of StringFilter that accepts all string nodes. ! */ ! public StringFilter () ! { ! this ("", false); ! } ! ! /** ! * Creates a new instance of a StringFilter that accepts string nodes containing a certain string. ! * The comparison is case insensitive, with conversions done using the default <code>Locale</code>. * @param pattern The pattern to search for. */ *************** *** 64,71 **** /** ! * Creates a new instance of StringFilter that accepts string nodes containing a certain string. * @param pattern The pattern to search for. * @param case_sensitive If <code>true</code>, comparisons are performed ! * respecting case. */ public StringFilter (String pattern, boolean case_sensitive) --- 83,90 ---- /** ! * Creates a new instance of a StringFilter that accepts string nodes containing a certain string. * @param pattern The pattern to search for. * @param case_sensitive If <code>true</code>, comparisons are performed ! * respecting case, with conversions done using the default <code>Locale</code>. */ public StringFilter (String pattern, boolean case_sensitive) *************** *** 75,98 **** /** ! * Creates a new instance of StringFilter that accepts string nodes containing a certain string. * @param pattern The pattern to search for. * @param case_sensitive If <code>true</code>, comparisons are performed * respecting case. */ public StringFilter (String pattern, boolean case_sensitive, Locale locale) { mCaseSensitive = case_sensitive; ! if (mCaseSensitive) ! mPattern = pattern; else ! { ! mLocale = (null == locale) ? Locale.ENGLISH : locale; ! mPattern = pattern.toUpperCase (mLocale); ! } } /** * Accept string nodes that contain the string. * @param node The node to check. */ public boolean accept (Node node) --- 94,194 ---- /** ! * Creates a new instance of a StringFilter that accepts string nodes containing a certain string. * @param pattern The pattern to search for. * @param case_sensitive If <code>true</code>, comparisons are performed * respecting case. + * @param locale The locale to use when converting to uppercase. + * If <code>null</code>, the default <code>Locale</code> is used. */ public StringFilter (String pattern, boolean case_sensitive, Locale locale) { + mPattern = pattern; mCaseSensitive = case_sensitive; ! mLocale = (null == locale) ? Locale.getDefault () : locale; ! setUpperPattern (); ! } ! ! // ! // protected methods ! // ! ! /** ! * Set the real (upper case) comparison string. ! */ ! protected void setUpperPattern () ! { ! if (getCaseSensitive ()) ! mUpperPattern = getPattern (); else ! mUpperPattern = getPattern ().toUpperCase (getLocale ()); ! } ! ! /** ! * Get the case sensitivity. ! * @return Returns the case sensitivity. ! */ ! public boolean getCaseSensitive () ! { ! return (mCaseSensitive); ! } ! ! /** ! * Set case sensitivity on or off. ! * @param sensitive If <code>false</code> searches for the ! * string are case insensitive. ! */ ! public void setCaseSensitive (boolean sensitive) ! { ! mCaseSensitive = sensitive; ! setUpperPattern (); ! } ! ! /** ! * Get the locale for uppercase conversion. ! * @return Returns the locale. ! */ ! public Locale getLocale () ! { ! return (mLocale); ! } ! ! /** ! * Set the locale for uppercase conversion. ! * @param locale The locale to set. ! */ ! public void setLocale (Locale locale) ! { ! mLocale = locale; ! setUpperPattern (); ! } ! ! /** ! * Get the search pattern. ! * @return Returns the pattern. ! */ ! public String getPattern () ! { ! return (mPattern); ! } ! ! /** ! * Set the search pattern. ! * @param pattern The pattern to set. ! */ ! public void setPattern (String pattern) ! { ! mPattern = pattern; ! setUpperPattern (); } + // + // NodeFilter interface + // + /** * Accept string nodes that contain the string. * @param node The node to check. + * @return <code>true</code> if <code>node</code> is a {@link Text} node + * and contains the pattern string, <code>false</code> otherwise. */ public boolean accept (Node node) *************** *** 105,111 **** { string = ((Text)node).getText (); ! if (!mCaseSensitive) ! string = string.toUpperCase (mLocale); ! ret = -1 != string.indexOf (mPattern); } --- 201,207 ---- { string = ((Text)node).getText (); ! if (!getCaseSensitive ()) ! string = string.toUpperCase (getLocale ()); ! ret = (-1 != string.indexOf (mUpperPattern)); } Index: RegexFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/RegexFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RegexFilter.java 24 May 2004 19:36:26 -0000 1.1 --- RegexFilter.java 13 Feb 2005 20:36:00 -0000 1.2 *************** *** 83,86 **** --- 83,91 ---- /** + * The regular expression to search for. + */ + protected String mPatternString; + + /** * The compiled regular expression to search for. */ *************** *** 95,98 **** --- 100,112 ---- /** * Creates a new instance of RegexFilter that accepts string nodes matching + * the regular expression ".*" using the FIND strategy. + */ + public RegexFilter () + { + this (".*", FIND); + } + + /** + * Creates a new instance of RegexFilter that accepts string nodes matching * a regular expression using the FIND strategy. * @param pattern The pattern to search for. *************** *** 116,120 **** --- 130,171 ---- public RegexFilter (String pattern, int strategy) { + setPattern (pattern); + setStrategy (strategy); + } + + /** + * Get the search pattern. + * @return Returns the pattern. + */ + public String getPattern () + { + return (mPatternString); + } + + /** + * Set the search pattern. + * @param pattern The pattern to set. + */ + public void setPattern (String pattern) + { + mPatternString = pattern; mPattern = Pattern.compile (pattern); + } + + /** + * Get the search strategy. + * @return Returns the strategy. + */ + public int getStrategy () + { + return (mStrategy); + } + + /** + * Set the search pattern. + * @param strategy The strategy to use. One of MATCH, LOOKINGAT or FIND. + */ + public void setStrategy (int strategy) + { mStrategy = strategy; } Index: HasParentFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasParentFilter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** HasParentFilter.java 31 Jul 2004 16:42:34 -0000 1.5 --- HasParentFilter.java 13 Feb 2005 20:36:00 -0000 1.6 *************** *** 29,54 **** import org.htmlparser.Node; import org.htmlparser.NodeFilter; /** * This class accepts all tags that have a parent acceptable to another filter. */ ! public class HasParentFilter implements NodeFilter { /** * The filter to apply to the parent. */ ! public NodeFilter mFilter; /** ! * Creates a new instance of HasParentFilter that accepts tags with parent acceptable to the filter. * @param filter The filter to apply to the parent. */ public HasParentFilter (NodeFilter filter) { ! mFilter = filter; } /** * Accept tags with parent acceptable to the filter. * @param node The node to check. */ --- 29,130 ---- import org.htmlparser.Node; import org.htmlparser.NodeFilter; + import org.htmlparser.Tag; /** * This class accepts all tags that have a parent acceptable to another filter. + * It can be set to operate recursively, that is perform a scan up the node + * heirarchy looking for any ancestor that matches the predicate filter. + * End tags are not considered to be children of any tag. */ ! public class HasParentFilter ! implements ! NodeFilter { /** * The filter to apply to the parent. */ ! public NodeFilter mParentFilter; /** ! * Performs a recursive search up the node heirarchy if <code>true</code>. ! */ ! public boolean mRecursive; ! ! /** ! * Creates a new instance of HasParentFilter. ! * With no parent filter, this would always return <code>false</code> ! * from {@link #accept}. ! */ ! public HasParentFilter () ! { ! this (null); ! } ! ! /** ! * Creates a new instance of HasParentFilter that accepts nodes with direct parent acceptable to the filter. * @param filter The filter to apply to the parent. */ public HasParentFilter (NodeFilter filter) { ! this (filter, false); ! } ! ! /** ! * Creates a new instance of HasParentFilter that accepts nodes with a parent acceptable to the filter. ! * @param filter The filter to apply to the parent. ! * @param recursive If <code>true</code>, any enclosing node acceptable ! * to the given filter causes the node being tested to be accepted ! * (i.e. a recursive scan through the parent nodes up the node ! * heirarchy is performed). ! */ ! public HasParentFilter (NodeFilter filter, boolean recursive) ! { ! setParentFilter (filter); ! setRecursive (recursive); ! } ! ! /** ! * Get the filter used by this HasParentFilter. ! * @return The filter to apply to parents. ! */ ! public NodeFilter getParentFilter () ! { ! return (mParentFilter); ! } ! ! /** ! * Set the filter for this HasParentFilter. ! * @param filter The filter to apply to parents in {@link #accept}. ! */ ! public void setParentFilter (NodeFilter filter) ! { ! mParentFilter = filter; ! } ! ! /** ! * Get the recusion setting for the filter. ! * @return Returns <code>true</code> if the filter is recursive ! * up the node heirarchy. ! */ ! public boolean getRecursive () ! { ! return mRecursive; ! } ! ! /** ! * Sets whether the filter is recursive or not. ! * @param recursive The recursion setting for the filter. ! */ ! public void setRecursive (boolean recursive) ! { ! mRecursive = recursive; } /** * Accept tags with parent acceptable to the filter. + * If recursion is enabled, each parent in turn up to + * the topmost enclosing node is checked. + * Recursion only proceeds while no parent satisfies the + * filter. * @param node The node to check. */ *************** *** 59,65 **** ret = false; ! parent = node.getParent (); ! if (null != parent) ! ret = mFilter.accept (parent); return (ret); --- 135,148 ---- ret = false; ! if (!(node instanceof Tag) || !((Tag)node).isEndTag ()) ! { ! parent = node.getParent (); ! if ((null != parent) && (null != getParentFilter ())) ! { ! ret = getParentFilter ().accept (parent); ! if (!ret && getRecursive ()) ! ret = accept (parent); ! } ! } return (ret); Index: NotFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/NotFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NotFilter.java 8 Nov 2003 21:30:58 -0000 1.1 --- NotFilter.java 13 Feb 2005 20:36:00 -0000 1.2 *************** *** 31,35 **** /** ! * This class accepts all nodes not acceptable to the filter. */ public class NotFilter implements NodeFilter --- 31,35 ---- /** ! * Accepts all nodes not acceptable to it's predicate filter. */ public class NotFilter implements NodeFilter *************** *** 38,59 **** * The filter to gainsay. */ ! protected NodeFilter mFilter; /** ! * Creates a new instance of NotFilter that accepts nodes not acceptable to the filter. ! * @param filter The filter to consult. */ ! public NotFilter (NodeFilter filter) { ! mFilter = filter; } /** ! * Accept nodes that are not acceptable to the filter. * @param node The node to check. */ public boolean accept (Node node) { ! return (!mFilter.accept (node)); } } --- 38,92 ---- * The filter to gainsay. */ ! protected NodeFilter mPredicate; /** ! * Creates a new instance of a NotFilter. ! * With no predicates, this would always return <code>false</code> ! * from {@link #accept}. ! * @see #setPredicates */ ! public NotFilter () { ! setPredicate (null); } /** ! * Creates a new instance of NotFilter that accepts nodes not acceptable to the predicate filter. ! * @param predicate The filter to consult. ! */ ! public NotFilter (NodeFilter predicate) ! { ! setPredicate (predicate); ! } ! ! /** ! * Get the predicate used by this NotFilter. ! * @return The predicate currently in use. ! */ ! public NodeFilter getPredicate () ! { ! return (mPredicate); ! } ! ! /** ! * Set the predicate for this NotFilter. ! * @param predicate The predidcate to use in {@link #accept}. ! */ ! public void setPredicate (NodeFilter predicate) ! { ! mPredicate = predicate; ! } ! ! // ! // NodeFilter interface ! // ! ! /** ! * Accept nodes that are not acceptable to the predicate filter. * @param node The node to check. */ public boolean accept (Node node) { ! return ((null != mPredicate) && !mPredicate.accept (node)); } } Index: OrFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/OrFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** OrFilter.java 8 Nov 2003 21:30:58 -0000 1.1 --- OrFilter.java 13 Feb 2005 20:36:00 -0000 1.2 *************** *** 31,50 **** /** ! * This class accepts all nodes matching either filter (OR operation). */ public class OrFilter implements NodeFilter { /** ! * The left hand side. */ ! protected NodeFilter mLeft; /** ! * The right hand side. */ ! protected NodeFilter mRight; /** ! * Creates a new instance of OrFilter that accepts nodes acceptable to either filter. * @param left One filter. * @param right The other filter. --- 31,56 ---- /** ! * Accepts nodes matching any of it's predicates filters (OR operation). */ public class OrFilter implements NodeFilter { /** ! * The predicates that are to be or'ed together; */ ! protected NodeFilter[] mPredicates; /** ! * Creates a new instance of an OrFilter. ! * With no predicates, this would always answer <code>false</code> ! * to {@link #accept}. ! * @see #setPredicates */ ! public OrFilter () ! { ! setPredicates (null); ! } /** ! * Creates a new instance of an OrFilter that accepts nodes acceptable to either filter. * @param left One filter. * @param right The other filter. *************** *** 52,66 **** public OrFilter (NodeFilter left, NodeFilter right) { ! mLeft = left; ! mRight = right; } /** ! * Accept nodes that are acceptable to either filter. * @param node The node to check. */ public boolean accept (Node node) { ! return (mLeft.accept (node) || mRight.accept (node)); } } --- 58,108 ---- public OrFilter (NodeFilter left, NodeFilter right) { ! NodeFilter[] predicates; ! ! predicates = new NodeFilter[2]; ! predicates[0] = left; ! predicates[1] = right; ! setPredicates (predicates); } /** ! * Get the predicates used by this OrFilter. ! * @return The predicates currently in use. ! */ ! public NodeFilter[] getPredicates () ! { ! return (mPredicates); ! } ! ! /** ! * Set the predicates for this OrFilter. ! * @param predicates The list of predidcates to use in {@link #accept}. ! */ ! public void setPredicates (NodeFilter[] predicates) ! { ! if (null == predicates) ! predicates = new NodeFilter[0]; ! mPredicates = predicates; ! } ! ! // ! // NodeFilter interface ! // ! ! /** ! * Accept nodes that are acceptable to any of it's predicate filters. * @param node The node to check. */ public boolean accept (Node node) { ! boolean ret; ! ! ret = false; ! ! for (int i = 0; !ret && (i < mPredicates.length); i++) ! if (mPredicates[i].accept (node)) ! ret = true; ! ! return (ret); } } Index: HasChildFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasChildFilter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** HasChildFilter.java 24 Jan 2004 23:57:48 -0000 1.2 --- HasChildFilter.java 13 Feb 2005 20:36:00 -0000 1.3 *************** *** 35,55 **** * This class accepts all tags that have a child acceptable to the filter. */ ! public class HasChildFilter implements NodeFilter { /** * The filter to apply to children. */ ! protected NodeFilter mFilter; /** ! * Creates a new instance of HasChildFilter that accepts tags with children acceptable to the filter. ! * Similar to asking for the parent of a node returned by the given ! * filter, but where multiple children may be acceptable, this class ! * will only accept the parent once. ! * @param filter The filter to apply to children. */ public HasChildFilter (NodeFilter filter) { ! mFilter = filter; } --- 35,121 ---- * This class accepts all tags that have a child acceptable to the filter. */ ! public class HasChildFilter ! implements ! NodeFilter { /** * The filter to apply to children. */ ! protected NodeFilter mChildFilter; /** ! * Performs a recursive search down the node heirarchy if <code>true</code>. ! */ ! public boolean mRecursive; ! ! /** ! * Creates a new instance of a HasChildFilter. ! * With no child filter, this would always return <code>false</code> ! * from {@link #accept}. ! */ ! public HasChildFilter () ! { ! this (null); ! } ! ! /** ! * Creates a new instance of HasChildFilter that accepts nodes with a direct child acceptable to the filter. ! * @param filter The filter to apply to the children. */ public HasChildFilter (NodeFilter filter) { ! this (filter, false); ! } ! ! /** ! * Creates a new instance of HasChildFilter that accepts nodes with a child acceptable to the filter. ! * Of necessity, this applies only to composite tags, i.e. those that can ! * contain other nodes, for example <HTML></HTML>. ! * @param filter The filter to apply to children. ! * @param recursive If <code>true</code>, any enclosed node acceptable ! * to the given filter causes the node being tested to be accepted ! * (i.e. a recursive scan through the child nodes down the node ! * heirarchy is performed). ! */ ! public HasChildFilter (NodeFilter filter, boolean recursive) ! { ! setChildFilter (filter); ! setRecursive (recursive); } ! ! /** ! * Get the filter used by this HasParentFilter. ! * @return The filter to apply to parents. ! */ ! public NodeFilter getChildFilter () ! { ! return (mChildFilter); ! } ! ! /** ! * Set the filter for this HasParentFilter. ! * @param filter The filter to apply to parents in {@link #accept}. ! */ ! public void setChildFilter (NodeFilter filter) ! { ! mChildFilter = filter; ! } ! ! /** ! * Get the recusion setting for the filter. ! * @return Returns <code>true</code> if the filter is recursive ! * up the node heirarchy. ! */ ! public boolean getRecursive () ! { ! return mRecursive; ! } ! ! /** ! * Sets whether the filter is recursive or not. ! * @param recursive The recursion setting for the filter. ! */ ! public void setRecursive (boolean recursive) ! { ! mRecursive = recursive; } *************** *** 70,79 **** children = tag.getChildren (); if (null != children) ! for (int i = 0; i < children.size (); i++) ! if (mFilter.accept (children.elementAt (i))) ! { ret = true; ! break; ! } } --- 136,149 ---- children = tag.getChildren (); if (null != children) ! { ! for (int i = 0; !ret && i < children.size (); i++) ! if (getChildFilter ().accept (children.elementAt (i))) ret = true; ! // do recursion after all children checked to get breadth first traversal ! if (!ret && getRecursive ()) ! for (int i = 0; !ret && i < children.size (); i++) ! if (accept (children.elementAt (i))) ! ret = true; ! } } --- NEW FILE: HasSiblingFilter.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2005 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasSiblingFilter.java,v $ // $Author: derrickoswald $ // $Date: 2005/02/13 20:36:00 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.filters; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Tag; import org.htmlparser.util.NodeList; /** * This class accepts all tags that have a sibling acceptable to another filter. * End tags are not considered to be siblings of any tag. */ public class HasSiblingFilter implements NodeFilter { /** * The filter to apply to the sibling. */ public NodeFilter mSiblingFilter; /** * Creates a new instance of HasSiblingFilter. * With no sibling filter, this would always return <code>false</code> * from {@link #accept}. */ public HasSiblingFilter () { this (null); } /** * Creates a new instance of HasSiblingFilter that accepts nodes with sibling acceptable to the filter. * @param filter The filter to apply to the sibling. */ public HasSiblingFilter (NodeFilter filter) { setSiblingFilter (filter); } /** * Get the filter used by this HasSiblingFilter. * @return The filter to apply to siblings. */ public NodeFilter getSiblingFilter () { return (mSiblingFilter); } /** * Set the filter for this HasSiblingFilter. * @param filter The filter to apply to siblings in {@link #accept}. */ public void setSiblingFilter (NodeFilter filter) { mSiblingFilter = filter; } /** * Accept tags with a sibling acceptable to the filter. * @param node The node to check. */ public boolean accept (Node node) { Node parent; NodeList siblings; int count; boolean ret; ret = false; if (!(node instanceof Tag) || !((Tag)node).isEndTag ()) { parent = node.getParent (); if (null != parent) { siblings = parent.getChildren (); if (null != siblings) { count = siblings.size (); for (int i = 0; !ret && (i < count); i++) if (getSiblingFilter ().accept (siblings.elementAt (i))) ret = true; } else System.out.println("gotcha"); } } return (ret); } } Index: TagNameFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/TagNameFilter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** TagNameFilter.java 24 May 2004 16:18:13 -0000 1.3 --- TagNameFilter.java 13 Feb 2005 20:36:00 -0000 1.4 *************** *** 46,49 **** --- 46,59 ---- /** + * Creates a new instance of TagNameFilter. + * With no name, this would always return <code>false</code> + * from {@link #accept}. + */ + public TagNameFilter () + { + this (""); + } + + /** * Creates a new instance of TagNameFilter that accepts tags with the given name. * @param name The tag name to match. *************** *** 55,58 **** --- 65,86 ---- /** + * Get the tag name. + * @return Returns the name of acceptable tags. + */ + public String getName () + { + return (mName); + } + + /** + * Set the tag name. + * @param name The name of the tag to accept. + */ + public void setName (String name) + { + mName = name; + } + + /** * Accept nodes that are tags and have a matching tag name. * This discards non-tag nodes and end tags. Index: HasAttributeFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasAttributeFilter.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** HasAttributeFilter.java 24 May 2004 16:18:13 -0000 1.4 --- HasAttributeFilter.java 13 Feb 2005 20:36:00 -0000 1.5 *************** *** 50,53 **** --- 50,63 ---- /** + * Creates a new instance of HasAttributeFilter. + * With no attribute name, this would always return <code>false</code> + * from {@link #accept}. + */ + public HasAttributeFilter () + { + this ("", null); + } + + /** * Creates a new instance of HasAttributeFilter that accepts tags with the given attribute. * @param attribute The attribute to search for. *************** *** 70,73 **** --- 80,120 ---- /** + * Get the attribute name. + * @return Returns the name of the attribute that is acceptable. + */ + public String getAttributeName () + { + return (mAttribute); + } + + /** + * Set the attribute name. + * @param name The name of the attribute to accept. + */ + public void setAttributeName (String name) + { + mAttribute = name; + } + + /** + * Get the attribute value. + * @return Returns the value of the attribute that is acceptable. + */ + public String getAttributeValue () + { + return (mValue); + } + + /** + * Set the attribute value. + * @param value The value of the attribute to accept. + * If <code>null</code>, any tag with the attribute, no matter it's value is acceptable. + */ + public void setAttributeValue (String value) + { + mValue = value; + } + + /** * Accept tags with a certain attribute. * @param node The node to check. Index: NodeClassFilter.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/NodeClassFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NodeClassFilter.java 8 Nov 2003 21:30:58 -0000 1.1 --- NodeClassFilter.java 13 Feb 2005 20:36:00 -0000 1.2 *************** *** 41,44 **** --- 41,52 ---- /** + * Creates a new instance of NodeClassFilter that accepts tags of the Html (top level) class. + */ + public NodeClassFilter () + { + this (org.htmlparser.tags.Html.class); + } + + /** * Creates a new instance of NodeClassFilter that accepts tags of the given class. * @param cls The cls to match. *************** *** 50,53 **** --- 58,79 ---- /** + * Get the class to match. + * @return Returns the class. + */ + public Class getMatchClass () + { + return (mClass); + } + + /** + * Set the class to match. + * @param cls The node class to match. + */ + public void setMatchClass (Class cls) + { + mClass = cls; + } + + /** * Accept nodes that are assignable from the class provided in the constructor. * @param node The node to check. *************** *** 55,59 **** public boolean accept (Node node) { ! return (mClass.isAssignableFrom (node.getClass ())); } } --- 81,85 ---- public boolean accept (Node node) { ! return ((null != mClass) && mClass.isAssignableFrom (node.getClass ())); } } |