Source for gnu.xml.dom.html2.DomHTMLTableElement

   1: /* DomHTMLTableElement.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.xml.dom.html2;
  39: 
  40: import gnu.xml.dom.DomDOMException;
  41: import org.w3c.dom.DOMException;
  42: import org.w3c.dom.Node;
  43: import org.w3c.dom.html2.HTMLCollection;
  44: import org.w3c.dom.html2.HTMLElement;
  45: import org.w3c.dom.html2.HTMLTableCaptionElement;
  46: import org.w3c.dom.html2.HTMLTableElement;
  47: import org.w3c.dom.html2.HTMLTableSectionElement;
  48: 
  49: /**
  50:  * An HTML 'TABLE' element node.
  51:  *
  52:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  53:  */
  54: public class DomHTMLTableElement
  55:   extends DomHTMLElement
  56:   implements HTMLTableElement
  57: {
  58: 
  59:   protected DomHTMLTableElement(DomHTMLDocument owner, String namespaceURI,
  60:                                 String name)
  61:   {
  62:     super(owner, namespaceURI, name);
  63:   }
  64: 
  65:   public HTMLTableCaptionElement getCaption()
  66:   {
  67:     return (HTMLTableCaptionElement) getChildElement("caption");
  68:   }
  69: 
  70:   public void setCaption(HTMLTableCaptionElement caption)
  71:   {
  72:     HTMLTableCaptionElement ref = getCaption();
  73:     if (ref == null)
  74:       {
  75:         appendChild(caption);
  76:       }
  77:     else
  78:       {
  79:         replaceChild(caption, ref);
  80:       }
  81:   }
  82: 
  83:   public HTMLTableSectionElement getTHead()
  84:   {
  85:     return (HTMLTableSectionElement) getChildElement("thead");
  86:   }
  87: 
  88:   public void setTHead(HTMLTableSectionElement tHead)
  89:   {
  90:     HTMLTableSectionElement ref = getTHead();
  91:     if (ref == null)
  92:       {
  93:         appendChild(tHead);
  94:       }
  95:     else
  96:       {
  97:         replaceChild(tHead, ref);
  98:       }
  99:   }
 100: 
 101:   public HTMLTableSectionElement getTFoot()
 102:   {
 103:     return (HTMLTableSectionElement) getChildElement("tfoot");
 104:   }
 105: 
 106:   public void setTFoot(HTMLTableSectionElement tFoot)
 107:   {
 108:     HTMLTableSectionElement ref = getTFoot();
 109:     if (ref == null)
 110:       {
 111:         appendChild(tFoot);
 112:       }
 113:     else
 114:       {
 115:         replaceChild(tFoot, ref);
 116:       }
 117:   }
 118: 
 119:   public HTMLCollection getRows()
 120:   {
 121:     DomHTMLCollection ret =
 122:       new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
 123:     ret.addNodeName("tr");
 124:     ret.evaluate();
 125:     return ret;
 126:   }
 127: 
 128:   public HTMLCollection getTBodies()
 129:   {
 130:     DomHTMLCollection ret =
 131:       new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
 132:     ret.addNodeName("tbody");
 133:     ret.evaluate();
 134:     return ret;
 135:   }
 136: 
 137:   public String getAlign()
 138:   {
 139:     return getHTMLAttribute("align");
 140:   }
 141: 
 142:   public void setAlign(String align)
 143:   {
 144:     setHTMLAttribute("align", align);
 145:   }
 146: 
 147:   public String getBgColor()
 148:   {
 149:     return getHTMLAttribute("bgcolor");
 150:   }
 151: 
 152:   public void setBgColor(String bgColor)
 153:   {
 154:     setHTMLAttribute("bgcolor", bgColor);
 155:   }
 156: 
 157:   public String getBorder()
 158:   {
 159:     return getHTMLAttribute("border");
 160:   }
 161: 
 162:   public void setBorder(String border)
 163:   {
 164:     setHTMLAttribute("border", border);
 165:   }
 166: 
 167:   public String getCellPadding()
 168:   {
 169:     return getHTMLAttribute("cellpadding");
 170:   }
 171: 
 172:   public void setCellPadding(String cellPadding)
 173:   {
 174:     setHTMLAttribute("cellpadding", cellPadding);
 175:   }
 176: 
 177:   public String getCellSpacing()
 178:   {
 179:     return getHTMLAttribute("cellspacing");
 180:   }
 181: 
 182:   public void setCellSpacing(String cellSpacing)
 183:   {
 184:     setHTMLAttribute("cellspacing", cellSpacing);
 185:   }
 186: 
 187:   public String getFrame()
 188:   {
 189:     return getHTMLAttribute("frame");
 190:   }
 191: 
 192:   public void setFrame(String frame)
 193:   {
 194:     setHTMLAttribute("frame", frame);
 195:   }
 196: 
 197:   public String getRules()
 198:   {
 199:     return getHTMLAttribute("rules");
 200:   }
 201: 
 202:   public void setRules(String rules)
 203:   {
 204:     setHTMLAttribute("rules", rules);
 205:   }
 206: 
 207:   public String getSummary()
 208:   {
 209:     return getHTMLAttribute("summary");
 210:   }
 211: 
 212:   public void setSummary(String summary)
 213:   {
 214:     setHTMLAttribute("summary", summary);
 215:   }
 216: 
 217:   public String getWidth()
 218:   {
 219:     return getHTMLAttribute("width");
 220:   }
 221: 
 222:   public void setWidth(String width)
 223:   {
 224:     setHTMLAttribute("width", width);
 225:   }
 226: 
 227:   public HTMLElement createTHead()
 228:   {
 229:     HTMLTableSectionElement ref = getTHead();
 230:     if (ref == null)
 231:       {
 232:         return (HTMLElement) getOwnerDocument().createElement("thead");
 233:       }
 234:     else
 235:       {
 236:         return ref;
 237:       }
 238:   }
 239: 
 240:   public void deleteTHead()
 241:   {
 242:     HTMLTableSectionElement ref = getTHead();
 243:     if (ref != null)
 244:       {
 245:         removeChild(ref);
 246:       }
 247:   }
 248: 
 249:   public HTMLElement createTFoot()
 250:   {
 251:     HTMLTableSectionElement ref = getTFoot();
 252:     if (ref == null)
 253:       {
 254:         return (HTMLElement) getOwnerDocument().createElement("tfoot");
 255:       }
 256:     else
 257:       {
 258:         return ref;
 259:       }
 260:   }
 261: 
 262:   public void deleteTFoot()
 263:   {
 264:     HTMLTableSectionElement ref = getTFoot();
 265:     if (ref != null)
 266:       {
 267:         removeChild(ref);
 268:       }
 269:   }
 270: 
 271:   public HTMLElement createCaption()
 272:   {
 273:     HTMLTableCaptionElement ref = getCaption();
 274:     if (ref == null)
 275:       {
 276:         return (HTMLElement) getOwnerDocument().createElement("caption");
 277:       }
 278:     else
 279:       {
 280:         return ref;
 281:       }
 282:   }
 283: 
 284:   public void deleteCaption()
 285:   {
 286:     HTMLTableCaptionElement ref = getCaption();
 287:     if (ref != null)
 288:       {
 289:         removeChild(ref);
 290:       }
 291:   }
 292: 
 293:   public HTMLElement insertRow(int index)
 294:   {
 295:     Node ref = getRow(index);
 296:     Node row = getOwnerDocument().createElement("tr");
 297:     if (ref == null)
 298:       {
 299:         Node tbody = getChildElement("tbody");
 300:         if (tbody == null)
 301:           {
 302:             tbody = getOwnerDocument().createElement("tfoot");
 303:             appendChild(tbody);
 304:           }
 305:         tbody.appendChild(row);
 306:       }
 307:     else
 308:       {
 309:         ref.getParentNode().insertBefore(row, ref);
 310:       }
 311:     return (HTMLElement) row;
 312:   }
 313: 
 314:   public void deleteRow(int index)
 315:   {
 316:     Node ref = getRow(index);
 317:     if (ref == null)
 318:       {
 319:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 320:       }
 321:     ref.getParentNode().removeChild(ref);
 322:   }
 323: 
 324:   Node getRow(final int index)
 325:   {
 326:     int i = 0;
 327:     Node thead = getChildElement("thead");
 328:     if (thead != null)
 329:       {
 330:         for (Node ctx = thead.getFirstChild(); ctx != null;
 331:              ctx = ctx.getNextSibling())
 332:           {
 333:             String ctxName = ctx.getLocalName();
 334:             if (ctxName == null)
 335:               {
 336:                 ctxName = ctx.getNodeName();
 337:               }
 338:             if (!"tr".equalsIgnoreCase(ctxName))
 339:               {
 340:                 continue;
 341:               }
 342:             if (index == i)
 343:               {
 344:                 return ctx;
 345:               }
 346:             i++;
 347:           }
 348:       }
 349:     Node tbody = getChildElement("tbody");
 350:     if (tbody == null)
 351:       {
 352:         tbody = this;
 353:       }
 354:     for (Node ctx = tbody.getFirstChild(); ctx != null;
 355:          ctx = ctx.getNextSibling())
 356:       {
 357:         String ctxName = ctx.getLocalName();
 358:         if (ctxName == null)
 359:           {
 360:             ctxName = ctx.getNodeName();
 361:           }
 362:         if (!"tr".equalsIgnoreCase(ctxName))
 363:           {
 364:             continue;
 365:           }
 366:         if (index == i)
 367:           {
 368:             return ctx;
 369:           }
 370:         i++;
 371:       }
 372:     Node tfoot = getChildElement("tfoot");
 373:     if (tfoot != null)
 374:       {
 375:         for (Node ctx = tfoot.getFirstChild(); ctx != null;
 376:              ctx = ctx.getNextSibling())
 377:           {
 378:             String ctxName = ctx.getLocalName();
 379:             if (ctxName == null)
 380:               {
 381:                 ctxName = ctx.getNodeName();
 382:               }
 383:             if (!"tr".equalsIgnoreCase(ctxName))
 384:               {
 385:                 continue;
 386:               }
 387:             if (index == i)
 388:               {
 389:                 return ctx;
 390:               }
 391:             i++;
 392:           }
 393:       }
 394:     return null;
 395:   }
 396: 
 397: }