/ org.htmlparser / src / org / htmlparser / tags / TableRow.java
TableRow.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
  2  // http://sourceforge.org/projects/htmlparser
  3  // Copyright (C) 2004 Somik Raha
  4  //
  5  // Revision Control Information
  6  //
  7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/TableRow.java,v $
  8  // $Author: ian_macfarlane $
  9  // $Date: 2005/10/31 16:26:11 $
 10  // $Revision: 1.43 $
 11  //
 12  // This library is free software; you can redistribute it and/or
 13  // modify it under the terms of the GNU Lesser General Public
 14  // License as published by the Free Software Foundation; either
 15  // version 2.1 of the License, or (at your option) any later version.
 16  //
 17  // This library is distributed in the hope that it will be useful,
 18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20  // Lesser General Public License for more details.
 21  //
 22  // You should have received a copy of the GNU Lesser General Public
 23  // License along with this library; if not, write to the Free Software
 24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  //
 26  
 27  package org.htmlparser.tags;
 28  
 29  import org.htmlparser.NodeFilter;
 30  import org.htmlparser.filters.AndFilter;
 31  import org.htmlparser.filters.IsEqualFilter;
 32  import org.htmlparser.filters.NodeClassFilter;
 33  import org.htmlparser.filters.HasParentFilter;
 34  import org.htmlparser.filters.NotFilter;
 35  import org.htmlparser.filters.OrFilter;
 36  import org.htmlparser.util.NodeList;
 37  
 38  /**
 39   * A table row tag.
 40   */
 41  public class TableRow extends CompositeTag
 42  {
 43      /**
 44       * The set of names handled by this tag.
 45       */
 46      private static final String[] mIds = new String[] {"TR"};
 47      
 48      /**
 49       * The set of tag names that indicate the end of this tag.
 50       */
 51      private static final String[] mEnders = new String[] {"TBODY", "TFOOT", "THEAD"};
 52      
 53      /**
 54       * The set of end tag names that indicate the end of this tag.
 55       */
 56      private static final String[] mEndTagEnders = new String[] {"TBODY", "TFOOT", "THEAD", "TABLE"};
 57  
 58      /**
 59       * Create a new table row tag.
 60       */
 61      public TableRow ()
 62      {
 63      }
 64  
 65      /**
 66       * Return the set of names handled by this tag.
 67       * @return The names to be matched that create tags of this type.
 68       */
 69      public String[] getIds ()
 70      {
 71          return (mIds);
 72      }
 73  
 74      /**
 75       * Return the set of tag names that cause this tag to finish.
 76       * @return The names of following tags that stop further scanning.
 77       */
 78      public String[] getEnders ()
 79      {
 80          return (mEnders);
 81      }
 82  
 83      /**
 84       * Return the set of end tag names that cause this tag to finish.
 85       * @return The names of following end tags that stop further scanning.
 86       */
 87      public String[] getEndTagEnders ()
 88      {
 89          return (mEndTagEnders);
 90      }
 91  
 92      /**
 93       * Get the column tags within this <code>TR</code> (table row) tag.
 94       * @return The {@.html <TD>} tags contained by this tag.
 95       */
 96      public TableColumn[] getColumns ()
 97      {
 98          NodeList kids;
 99          NodeClassFilter cls;
100          HasParentFilter recursion;
101          NodeFilter filter;
102          TableColumn[] ret;
103  
104          kids = getChildren ();
105          if (null != kids)
106          {
107              cls = new NodeClassFilter (TableRow.class);
108              recursion = new HasParentFilter (null);
109              filter = new OrFilter (
110                          new AndFilter (
111                              cls, 
112                              new IsEqualFilter (this)),
113                          new AndFilter ( // recurse up the parent chain
114                              new NotFilter (cls), // but not past the first row
115                              recursion));
116              recursion.setParentFilter (filter);
117              kids = kids.extractAllNodesThatMatch (
118                  // it's a column, and has this row as it's enclosing row
119                  new AndFilter (
120                      new NodeClassFilter (TableColumn.class),
121                      filter), true);
122              ret = new TableColumn[kids.size ()];
123              kids.copyToNodeArray (ret);
124          }
125          else
126              ret = new TableColumn[0];
127          
128          return (ret);
129      }
130  
131      /**
132       * Get the number of columns in this row.
133       * @return The number of columns in this row.
134       * <em>Note: this is a a simple count of the number of {@.html <TD>} tags and
135       * may be incorrect if the {@.html <TD>} tags span multiple columns.</em>
136       */
137      public int getColumnCount ()
138      {
139          return (getColumns ().length);
140      }
141  
142      /**
143       * Get the header of this table
144       * @return Table header tags contained in this row.
145       */
146      public TableHeader[] getHeaders ()
147      {
148          NodeList kids;
149          NodeClassFilter cls;
150          HasParentFilter recursion;
151          NodeFilter filter;
152          TableHeader[] ret;
153  
154          kids = getChildren ();
155          if (null != kids)
156          {
157              cls = new NodeClassFilter (TableRow.class);
158              recursion = new HasParentFilter (null);
159              filter = new OrFilter (
160                          new AndFilter (
161                              cls, 
162                              new IsEqualFilter (this)),
163                          new AndFilter ( // recurse up the parent chain
164                              new NotFilter (cls), // but not past the first row
165                              recursion));
166              recursion.setParentFilter (filter);
167              kids = kids.extractAllNodesThatMatch (
168                  // it's a header, and has this row as it's enclosing row
169                  new AndFilter (
170                      new NodeClassFilter (TableHeader.class),
171                      filter), true);
172              ret = new TableHeader[kids.size ()];
173              kids.copyToNodeArray (ret);
174          }
175          else
176              ret = new TableHeader[0];
177          
178          return (ret);
179      }
180  
181      /**
182       * Get the number of headers in this row.
183       * @return The count of header tags in this row.
184       */
185      public int getHeaderCount ()
186      {
187          return (getHeaders ().length);
188      }
189  
190      /**
191       * Checks if this table has a header
192       * @return <code>true</code> if there is a header tag.
193       */
194      public boolean hasHeader ()
195      {
196          return (0 != getHeaderCount ());
197      }
198  }