1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Map ; 45 import java.util.NoSuchElementException ; 46 47 import com.gargoylesoftware.htmlunit.ElementNotFoundException; 48 49 57 public class HtmlTable extends ClickableElement { 58 59 60 public static final String TAG_NAME = "table"; 61 62 68 public HtmlTable( final HtmlPage page, final Map attributes ) { 69 super( page, attributes ); 70 } 71 72 75 public String getTagName() { 76 return TAG_NAME; 77 } 78 79 88 public final HtmlTableCell getCellAt( final int rowIndex, final int columnIndex ) 89 { 90 final RowIterator rowIterator = getRowIterator(); 91 for(int rowNo = 0; rowIterator.hasNext(); rowNo++) { 92 final HtmlTableRow row = rowIterator.nextRow(); 93 94 final HtmlTableRow.CellIterator cellIterator = row.getCellIterator(); 95 for(int colNo = 0; cellIterator.hasNext(); colNo++) { 96 final HtmlTableCell cell = cellIterator.nextCell(); 97 if(rowNo + cell.getRowSpan() > rowIndex) { 98 if(colNo <= columnIndex && colNo + cell.getColumnSpan() > columnIndex) { 99 return cell; 100 } 101 } 102 } 103 } 104 return null; 105 } 106 107 110 private RowIterator getRowIterator() { 111 return new RowIterator(); 112 } 113 114 118 public List getRows() { 119 final List result = new ArrayList (); 120 for(final RowIterator iterator = getRowIterator(); iterator.hasNext(); ) { 121 result.add(iterator.next()); 122 } 123 return Collections.unmodifiableList(result); 124 } 125 126 132 public HtmlTableRow getRow(final int index) throws IndexOutOfBoundsException { 133 int count = 0; 134 for(final RowIterator iterator = getRowIterator(); iterator.hasNext(); count++) { 135 final HtmlTableRow next = iterator.nextRow(); 136 if(count == index) { 137 return next; 138 } 139 } 140 throw new IndexOutOfBoundsException (); 141 } 142 143 149 public final int getRowCount() { 150 int count = 0; 151 for(final RowIterator iterator = getRowIterator(); iterator.hasNext(); iterator.next()) { 152 count++; 153 } 154 return count; 155 } 156 157 158 165 public final HtmlTableRow getRowById( final String id ) throws ElementNotFoundException { 166 final RowIterator iterator = new RowIterator(); 167 while( iterator.hasNext() ) { 168 final HtmlTableRow row = (HtmlTableRow)iterator.next(); 169 if( row.getIdAttribute().equals(id) ) { 170 return row; 171 } 172 } 173 throw new ElementNotFoundException( "tr", "id", id ); 174 } 175 176 177 182 public String getCaptionText() { 183 final Iterator iterator = getChildElementsIterator(); 184 while( iterator.hasNext() ) { 185 final HtmlElement element = (HtmlElement)iterator.next(); 186 if( element instanceof HtmlCaption ) { 187 return element.asText(); 188 } 189 } 190 return null; 191 } 192 193 194 199 public HtmlTableHeader getHeader() { 200 final Iterator iterator = getChildElementsIterator(); 201 while( iterator.hasNext() ) { 202 final HtmlElement element = (HtmlElement)iterator.next(); 203 if( element instanceof HtmlTableHeader ) { 204 return (HtmlTableHeader)element; 205 } 206 } 207 return null; 208 } 209 210 211 216 public HtmlTableFooter getFooter() { 217 final Iterator iterator = getChildElementsIterator(); 218 while( iterator.hasNext() ) { 219 final HtmlElement element = (HtmlElement)iterator.next(); 220 if( element instanceof HtmlTableFooter ) { 221 return (HtmlTableFooter)element; 222 } 223 } 224 return null; 225 } 226 227 228 234 public List getBodies() { 235 final List bodies = new ArrayList (); 236 final Iterator iterator = getChildElementsIterator(); 237 while( iterator.hasNext() ) { 238 final HtmlElement element = (HtmlElement)iterator.next(); 239 if( element instanceof HtmlTableBody ) { 240 bodies.add( element ); 241 } 242 } 243 return bodies; 244 } 245 246 247 255 public final String getSummaryAttribute() { 256 return getAttributeValue("summary"); 257 } 258 259 260 268 public final String getWidthAttribute() { 269 return getAttributeValue("width"); 270 } 271 272 273 281 public final String getBorderAttribute() { 282 return getAttributeValue("border"); 283 } 284 285 286 294 public final String getFrameAttribute() { 295 return getAttributeValue("frame"); 296 } 297 298 299 307 public final String getRulesAttribute() { 308 return getAttributeValue("rules"); 309 } 310 311 312 320 public final String getCellSpacingAttribute() { 321 return getAttributeValue("cellspacing"); 322 } 323 324 325 333 public final String getCellPaddingAttribute() { 334 return getAttributeValue("cellpadding"); 335 } 336 337 338 346 public final String getAlignAttribute() { 347 return getAttributeValue("align"); 348 } 349 350 351 359 public final String getBgcolorAttribute() { 360 return getAttributeValue("bgcolor"); 361 } 362 363 364 368 private class RowIterator implements Iterator { 369 370 private HtmlTableRow nextRow_; 371 private TableRowGroup currentGroup_; 372 373 374 public RowIterator() { 375 setNextRow(getFirstChild()); 376 } 377 378 381 public boolean hasNext() { 382 return nextRow_ != null; 383 } 384 385 389 public Object next() throws NoSuchElementException { 390 return nextRow(); 391 } 392 393 397 public void remove() throws IllegalStateException { 398 if(nextRow_ == null) { 399 throw new IllegalStateException (); 400 } 401 if(nextRow_.getPreviousSibling() != null) { 402 nextRow_.getPreviousSibling().remove(); 403 } 404 } 405 406 410 public HtmlTableRow nextRow() throws NoSuchElementException { 411 if(nextRow_ != null) { 412 final HtmlTableRow result = nextRow_; 413 setNextRow(nextRow_.getNextSibling()); 414 return result; 415 } 416 else { 417 throw new NoSuchElementException (); 418 } 419 } 420 421 426 private void setNextRow(final DomNode node) { 427 428 nextRow_ = null; 429 for(DomNode next = node; next != null; next = next.getNextSibling()) { 430 if(next instanceof HtmlTableRow) { 431 nextRow_ = (HtmlTableRow)next; 432 return; 433 } 434 else if(currentGroup_ == null && next instanceof TableRowGroup) { 435 currentGroup_ = (TableRowGroup)next; 436 setNextRow(next.getFirstChild()); 437 return; 438 } 439 } 440 if(currentGroup_ != null) { 441 final DomNode group = currentGroup_; 442 currentGroup_ = null; 443 setNextRow(group.getNextSibling()); 444 } 445 } 446 } 447 } 448 | Popular Tags |