KickJava   Java API By Example, From Geeks To Geeks.

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


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/TableRow.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
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 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 JavaDoc[] mIds = new String JavaDoc[] {"TR"};
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[] {"TABLE"};
52
53     /**
54      * Create a new table row tag.
55      */

56     public TableRow ()
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 tag names that cause this tag to finish.
71      * @return The names of following tags that stop further scanning.
72      */

73     public String JavaDoc[] getEnders ()
74     {
75         return (mIds);
76     }
77
78     /**
79      * Return the set of end tag names that cause this tag to finish.
80      * @return The names of following end tags that stop further scanning.
81      */

82     public String JavaDoc[] getEndTagEnders ()
83     {
84         return (mEndTagEnders);
85     }
86
87     /**
88      * Get the column tags within this row.
89      */

90     public TableColumn[] getColumns ()
91     {
92         NodeList kids;
93         NodeClassFilter cls;
94         HasParentFilter recursion;
95         NodeFilter filter;
96         TableColumn[] ret;
97
98         kids = getChildren ();
99         if (null != kids)
100         {
101             cls = new NodeClassFilter (TableRow.class);
102             recursion = new HasParentFilter (null);
103             filter = new OrFilter (
104                         new AndFilter (
105                             cls,
106                             new IsEqualFilter (this)),
107                         new AndFilter ( // recurse up the parent chain
108
new NotFilter (cls), // but not past the first row
109
recursion));
110             recursion.setParentFilter (filter);
111             kids = kids.extractAllNodesThatMatch (
112                 // it's a column, and has this row as it's enclosing row
113
new AndFilter (
114                     new NodeClassFilter (TableColumn.class),
115                     filter), true);
116             ret = new TableColumn[kids.size ()];
117             kids.copyToNodeArray (ret);
118         }
119         else
120             ret = new TableColumn[0];
121         
122         return (ret);
123     }
124
125     /**
126      * Get the number of columns in this row.
127      */

128     public int getColumnCount ()
129     {
130         return (getColumns ().length);
131     }
132
133     /**
134      * Get the header of this table
135      * @return Table header tags contained in this row.
136      */

137     public TableHeader[] getHeaders ()
138     {
139         NodeList kids;
140         NodeClassFilter cls;
141         HasParentFilter recursion;
142         NodeFilter filter;
143         TableHeader[] ret;
144
145         kids = getChildren ();
146         if (null != kids)
147         {
148             cls = new NodeClassFilter (TableRow.class);
149             recursion = new HasParentFilter (null);
150             filter = new OrFilter (
151                         new AndFilter (
152                             cls,
153                             new IsEqualFilter (this)),
154                         new AndFilter ( // recurse up the parent chain
155
new NotFilter (cls), // but not past the first row
156
recursion));
157             recursion.setParentFilter (filter);
158             kids = kids.extractAllNodesThatMatch (
159                 // it's a header, and has this row as it's enclosing row
160
new AndFilter (
161                     new NodeClassFilter (TableHeader.class),
162                     filter), true);
163             ret = new TableHeader[kids.size ()];
164             kids.copyToNodeArray (ret);
165         }
166         else
167             ret = new TableHeader[0];
168         
169         return (ret);
170     }
171
172     /**
173      * Get the number of headers in this row.
174      * @return The count of header tags in this row.
175      */

176     public int getHeaderCount ()
177     {
178         return (getHeaders ().length);
179     }
180
181     /**
182      * Checks if this table has a header
183      * @return <code>true</code> if there is a header tag.
184      */

185     public boolean hasHeader ()
186     {
187         return (0 != getHeaderCount ());
188     }
189 }
190
Popular Tags