/ org.htmlparser / src / org / htmlparser / tags / TableTag.java
TableTag.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/TableTag.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/04/10 23:20:45 $
 10  // $Revision: 1.41 $
 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 tag.
 40   */
 41  public class TableTag extends CompositeTag
 42  {
 43      /**
 44       * The set of names handled by this tag.
 45       */
 46      private static final String[] mIds = new String[] {"TABLE"};
 47  
 48      /**
 49       * The set of end tag names that indicate the end of this tag.
 50       */
 51      private static final String[] mEndTagEnders = new String[] {"BODY", "HTML"};
 52  
 53      /**
 54       * Create a new table tag.
 55       */
 56      public TableTag ()
 57      {
 58      }
 59  
 60      /**
 61       * Return the set of names handled by this tag.
 62       * @return The names to be matched that create tags of this type.
 63       */
 64      public String[] getIds ()
 65      {
 66          return (mIds);
 67      }
 68  
 69      /**
 70       * Return the set of end tag names that cause this tag to finish.
 71       * @return The names of following end tags that stop further scanning.
 72       */
 73      public String[] getEndTagEnders ()
 74      {
 75          return (mEndTagEnders);
 76      }
 77  
 78      /**
 79       * Get the row tags within this table.
 80       * @return The rows directly contained by this table.
 81       */
 82      public TableRow[] getRows ()
 83      {
 84          NodeList kids;
 85          NodeClassFilter cls;
 86          HasParentFilter recursion;
 87          NodeFilter filter;
 88          TableRow[] ret;
 89  
 90          kids = getChildren ();
 91          if (null != kids)
 92          {
 93              cls = new NodeClassFilter (TableTag.class);
 94              recursion = new HasParentFilter (null);
 95              filter = new OrFilter (
 96                          new AndFilter (
 97                              cls, 
 98                              new IsEqualFilter (this)),
 99                          new AndFilter ( // recurse up the parent chain
100                              new NotFilter (cls), // but not past the first table
101                              recursion));
102              recursion.setParentFilter (filter);
103              kids = kids.extractAllNodesThatMatch (
104                  // it's a row, and has this table as it's enclosing table
105                  new AndFilter (
106                      new NodeClassFilter (TableRow.class),
107                      filter), true);
108              ret = new TableRow[kids.size ()];
109              kids.copyToNodeArray (ret);
110          }
111          else
112              ret = new TableRow[0];
113          
114          return (ret);
115      }
116  
117      /**
118       * Get the number of rows in this table.
119       * @return The number of rows in this table.
120       * <em>Note: this is a a simple count of the number of {@.html <TR>} tags and
121       * may be incorrect if the {@.html <TR>} tags span multiple rows.</em>
122       */
123      public int getRowCount ()
124      {
125          return (getRows ().length);
126      }
127  
128      /**
129       * Get the row at the given index.
130       * @param index The row number (zero based) to get. 
131       * @return The row for the given index.
132       */
133      public TableRow getRow (int index)
134      {
135          TableRow[] rows;
136          TableRow ret;
137  
138          rows = getRows ();
139          if (index < rows.length)
140              ret = rows[index];
141          else
142              ret = null;
143          
144          return (ret);
145      }
146  
147      /**
148       * Return a string suitable for debugging display.
149       * @return The table as HTML, sorry.
150       */
151      public String toString()
152      {
153          return
154              "TableTag\n" +
155              "********\n"+
156              toHtml();
157      }
158  
159  }