KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tags > TableTag


1 // HTMLParser Library $Name: v1_5_20050313 $ - 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/02/13 20:36:00 $
10
// $Revision: 1.40 $
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 JavaDoc[] mIds = new String JavaDoc[] {"TABLE"};
47
48     /**
49      * The set of end tag names that indicate the end of this tag.
50      */

51     private static final String JavaDoc[] mEndTagEnders = new String JavaDoc[] {"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 JavaDoc[] 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 JavaDoc[] 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      */

120     public int getRowCount ()
121     {
122         return (getRows ().length);
123     }
124
125     /**
126      * Get the row at the given index.
127      */

128     public TableRow getRow (int i)
129     {
130         TableRow[] rows;
131         TableRow ret;
132
133         rows = getRows ();
134         if (i < rows.length)
135             ret = rows[i];
136         else
137             ret = null;
138         
139         return (ret);
140     }
141
142     public String JavaDoc toString()
143     {
144         return
145             "TableTag\n" +
146             "********\n"+
147             toHtml();
148     }
149
150 }
151
Popular Tags