KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlTableRow


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import java.util.Map JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.NoSuchElementException JavaDoc;
46
47
48 /**
49  * Wrapper for the html element "tr"
50  *
51  * @version $Revision: 100 $
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  * @author David K. Taylor
54  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
55  */

56 public class HtmlTableRow extends ClickableElement {
57
58     /** the HTML tag represented by this element */
59     public static final String JavaDoc TAG_NAME = "tr";
60
61     /**
62      * Create an instance
63      *
64      * @param page The page that this element is contained within
65      * @param attributes the initial attributes
66      */

67     public HtmlTableRow( final HtmlPage page, final Map JavaDoc attributes ) {
68         super( page, attributes );
69     }
70
71     /**
72      * @return the HTML tag name
73      */

74     public String JavaDoc getTagName() {
75         return TAG_NAME;
76     }
77
78     /**
79      * @return an Iterator over the all HtmlTableCell objects in this row
80      */

81     public CellIterator getCellIterator() {
82         return new CellIterator();
83     }
84
85     /**
86      * @return an immutable list containing all the HtmlTableCells held by this object
87      * @see #getCellIterator
88      */

89     public List JavaDoc getCells() {
90         final List JavaDoc result = new ArrayList JavaDoc();
91         for(final CellIterator iterator = getCellIterator(); iterator.hasNext(); ) {
92             result.add(iterator.next());
93         }
94         return Collections.unmodifiableList(result);
95     }
96
97     /**
98      * @param index the 0-based index
99      * @return the cell at the given index
100      * @throws IndexOutOfBoundsException if there is no cell at the given index
101      */

102     public HtmlTableCell getCell(final int index) throws IndexOutOfBoundsException JavaDoc {
103         int count = 0;
104         for(final CellIterator iterator = getCellIterator(); iterator.hasNext(); count++) {
105             final HtmlTableCell next = iterator.nextCell();
106             if(count == index) {
107                 return next;
108             }
109         }
110         throw new IndexOutOfBoundsException JavaDoc();
111     }
112
113
114
115     /**
116      * Return the value of the attribute "align". Refer to the
117      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
118      * documentation for details on the use of this attribute.
119      *
120      * @return The value of the attribute "align"
121      * or an empty string if that attribute isn't defined.
122      */

123     public final String JavaDoc getAlignAttribute() {
124         return getAttributeValue("align");
125     }
126
127
128     /**
129      * Return the value of the attribute "char". Refer to the
130      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
131      * documentation for details on the use of this attribute.
132      *
133      * @return The value of the attribute "char"
134      * or an empty string if that attribute isn't defined.
135      */

136     public final String JavaDoc getCharAttribute() {
137         return getAttributeValue("char");
138     }
139
140
141     /**
142      * Return the value of the attribute "charoff". Refer to the
143      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
144      * documentation for details on the use of this attribute.
145      *
146      * @return The value of the attribute "charoff"
147      * or an empty string if that attribute isn't defined.
148      */

149     public final String JavaDoc getCharoffAttribute() {
150         return getAttributeValue("charoff");
151     }
152
153
154     /**
155      * Return the value of the attribute "valign". Refer to the
156      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
157      * documentation for details on the use of this attribute.
158      *
159      * @return The value of the attribute "valign"
160      * or an empty string if that attribute isn't defined.
161      */

162     public final String JavaDoc getValignAttribute() {
163         return getAttributeValue("valign");
164     }
165
166     /**
167      * Gets the table containing this row
168      * @return the table
169      */

170     public HtmlTable getEnclosingTable() {
171         return (HtmlTable) getEnclosingElement("table");
172     }
173
174
175     /**
176      * Return the value of the attribute "bgcolor". Refer to the
177      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
178      * documentation for details on the use of this attribute.
179      *
180      * @return The value of the attribute "bgcolor"
181      * or an empty string if that attribute isn't defined.
182      */

183     public final String JavaDoc getBgcolorAttribute() {
184         return getAttributeValue("bgcolor");
185     }
186
187     /**
188      * an Iterator over the HtmlTableCells contained in this row. It will also dive
189      * into nested forms, even though that is illegal HTML
190      */

191     public class CellIterator implements Iterator JavaDoc {
192
193         private HtmlTableCell nextCell_;
194         private HtmlForm currentForm_;
195
196         /** create an instance */
197         public CellIterator() {
198             setNextCell(getFirstChild());
199         }
200
201         /** @return whether there is another cell available */
202         public boolean hasNext() {
203             return nextCell_ != null;
204         }
205
206         /**
207          * @return the next cell
208          * @throws NoSuchElementException if no cell is available
209          */

210         public Object JavaDoc next() throws NoSuchElementException JavaDoc {
211             return nextCell();
212         }
213
214         /**
215          * remove the cell under the cursor from the current row
216          * @throws IllegalStateException if there is no currenr row
217          */

218         public void remove() throws IllegalStateException JavaDoc {
219             if(nextCell_ == null) {
220                 throw new IllegalStateException JavaDoc();
221             }
222             if(nextCell_.getPreviousSibling() != null) {
223                 nextCell_.getPreviousSibling().remove();
224             }
225         }
226
227         /**
228          * @return the next cell
229          * @throws NoSuchElementException if no cell is available
230          */

231         public HtmlTableCell nextCell() throws NoSuchElementException JavaDoc {
232
233             if(nextCell_ != null) {
234                 final HtmlTableCell result = nextCell_;
235                 setNextCell(nextCell_.getNextSibling());
236                 return result;
237             }
238             else {
239                 throw new NoSuchElementException JavaDoc();
240             }
241         }
242
243         /**
244          * set the internal position to the next cell, starting at the given node
245          * @param node the node to mark as the next cell. If this is not a cell, the
246          * next reachable cell will be marked
247          */

248         private void setNextCell(final DomNode node) {
249
250             nextCell_ = null;
251             for(DomNode next = node; next != null; next = next.getNextSibling()) {
252                 if(next instanceof HtmlTableCell) {
253                     nextCell_ = (HtmlTableCell)next;
254                     return;
255                 }
256                 else if(currentForm_ == null && next instanceof HtmlForm) {
257                     // Completely illegal html but some of the big sites (ie amazon) do this
258
currentForm_ = (HtmlForm)next;
259                     setNextCell(next.getFirstChild());
260                     return;
261                 }
262             }
263             if(currentForm_ != null) {
264                 final DomNode form = currentForm_;
265                 currentForm_ = null;
266                 setNextCell(form.getNextSibling());
267             }
268         }
269     }
270 }
271
Popular Tags