1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.html.internal.dom;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.html.HTMLCollection;
  60. import org.w3c.dom.html.HTMLElement;
  61. import org.w3c.dom.html.HTMLTableCaptionElement;
  62. import org.w3c.dom.html.HTMLTableElement;
  63. import org.w3c.dom.html.HTMLTableRowElement;
  64. import org.w3c.dom.html.HTMLTableSectionElement;
  65. /**
  66. * @version $Revision: 1.10 $ $Date: 2003/07/05 18:38:14 $
  67. * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
  68. * @see org.w3c.dom.html.HTMLAnchorElement
  69. * @see com.sun.org.apache.xerces.internal.dom.ElementImpl
  70. */
  71. public class HTMLTableElementImpl
  72. extends HTMLElementImpl
  73. implements HTMLTableElement
  74. {
  75. public synchronized HTMLTableCaptionElement getCaption()
  76. {
  77. Node child;
  78. child = getFirstChild();
  79. while ( child != null )
  80. {
  81. if ( child instanceof HTMLTableCaptionElement &&
  82. child.getNodeName().equals( "CAPTION" ) )
  83. return (HTMLTableCaptionElement) child;
  84. child = child.getNextSibling();
  85. }
  86. return null;
  87. }
  88. public synchronized void setCaption( HTMLTableCaptionElement caption )
  89. {
  90. if ( caption != null && ! caption.getTagName().equals( "CAPTION" ) )
  91. throw new IllegalArgumentException( "HTM016 Argument 'caption' is not an element of type <CAPTION>." );
  92. deleteCaption();
  93. if ( caption != null )
  94. appendChild( caption );
  95. }
  96. public synchronized HTMLElement createCaption()
  97. {
  98. HTMLElement section;
  99. section = getCaption();
  100. if ( section != null )
  101. return section;
  102. section = new HTMLTableCaptionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "CAPTION" );
  103. appendChild( section );
  104. return section;
  105. }
  106. public synchronized void deleteCaption()
  107. {
  108. Node old;
  109. old = getCaption();
  110. if ( old != null )
  111. removeChild ( old );
  112. }
  113. public synchronized HTMLTableSectionElement getTHead()
  114. {
  115. Node child;
  116. child = getFirstChild();
  117. while ( child != null )
  118. {
  119. if ( child instanceof HTMLTableSectionElement &&
  120. child.getNodeName().equals( "THEAD" ) )
  121. return (HTMLTableSectionElement) child;
  122. child = child.getNextSibling();
  123. }
  124. return null;
  125. }
  126. public synchronized void setTHead( HTMLTableSectionElement tHead )
  127. {
  128. if ( tHead != null && ! tHead.getTagName().equals( "THEAD" ) )
  129. throw new IllegalArgumentException( "HTM017 Argument 'tHead' is not an element of type <THEAD>." );
  130. deleteTHead();
  131. if ( tHead != null )
  132. appendChild( tHead );
  133. }
  134. public synchronized HTMLElement createTHead()
  135. {
  136. HTMLElement section;
  137. section = getTHead();
  138. if ( section != null )
  139. return section;
  140. section = new HTMLTableSectionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "THEAD" );
  141. appendChild( section );
  142. return section;
  143. }
  144. public synchronized void deleteTHead()
  145. {
  146. Node old;
  147. old = getTHead();
  148. if ( old != null )
  149. removeChild ( old );
  150. }
  151. public synchronized HTMLTableSectionElement getTFoot()
  152. {
  153. Node child;
  154. child = getFirstChild();
  155. while ( child != null )
  156. {
  157. if ( child instanceof HTMLTableSectionElement &&
  158. child.getNodeName().equals( "TFOOT" ) )
  159. return (HTMLTableSectionElement) child;
  160. child = child.getNextSibling();
  161. }
  162. return null;
  163. }
  164. public synchronized void setTFoot( HTMLTableSectionElement tFoot )
  165. {
  166. if ( tFoot != null && ! tFoot.getTagName().equals( "TFOOT" ) )
  167. throw new IllegalArgumentException( "HTM018 Argument 'tFoot' is not an element of type <TFOOT>." );
  168. deleteTFoot();
  169. if ( tFoot != null )
  170. appendChild( tFoot );
  171. }
  172. public synchronized HTMLElement createTFoot()
  173. {
  174. HTMLElement section;
  175. section = getTFoot();
  176. if ( section != null )
  177. return section;
  178. section = new HTMLTableSectionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "TFOOT" );
  179. appendChild( section );
  180. return section;
  181. }
  182. public synchronized void deleteTFoot()
  183. {
  184. Node old;
  185. old = getTFoot();
  186. if ( old != null )
  187. removeChild ( old );
  188. }
  189. public HTMLCollection getRows()
  190. {
  191. if ( _rows == null )
  192. _rows = new HTMLCollectionImpl( this, HTMLCollectionImpl.ROW );
  193. return _rows;
  194. }
  195. public HTMLCollection getTBodies()
  196. {
  197. if ( _bodies == null )
  198. _bodies = new HTMLCollectionImpl( this, HTMLCollectionImpl.TBODY );
  199. return _bodies;
  200. }
  201. public String getAlign()
  202. {
  203. return capitalize( getAttribute( "align" ) );
  204. }
  205. public void setAlign( String align )
  206. {
  207. setAttribute( "align", align );
  208. }
  209. public String getBgColor()
  210. {
  211. return getAttribute( "bgcolor" );
  212. }
  213. public void setBgColor( String bgColor )
  214. {
  215. setAttribute( "bgcolor", bgColor );
  216. }
  217. public String getBorder()
  218. {
  219. return getAttribute( "border" );
  220. }
  221. public void setBorder( String border )
  222. {
  223. setAttribute( "border", border );
  224. }
  225. public String getCellPadding()
  226. {
  227. return getAttribute( "cellpadding" );
  228. }
  229. public void setCellPadding( String cellPadding )
  230. {
  231. setAttribute( "cellpadding", cellPadding );
  232. }
  233. public String getCellSpacing()
  234. {
  235. return getAttribute( "cellspacing" );
  236. }
  237. public void setCellSpacing( String cellSpacing )
  238. {
  239. setAttribute( "cellspacing", cellSpacing );
  240. }
  241. public String getFrame()
  242. {
  243. return capitalize( getAttribute( "frame" ) );
  244. }
  245. public void setFrame( String frame )
  246. {
  247. setAttribute( "frame", frame );
  248. }
  249. public String getRules()
  250. {
  251. return capitalize( getAttribute( "rules" ) );
  252. }
  253. public void setRules( String rules )
  254. {
  255. setAttribute( "rules", rules );
  256. }
  257. public String getSummary()
  258. {
  259. return getAttribute( "summary" );
  260. }
  261. public void setSummary( String summary )
  262. {
  263. setAttribute( "summary", summary );
  264. }
  265. public String getWidth()
  266. {
  267. return getAttribute( "width" );
  268. }
  269. public void setWidth( String width )
  270. {
  271. setAttribute( "width", width );
  272. }
  273. public HTMLElement insertRow( int index )
  274. {
  275. HTMLTableRowElementImpl newRow;
  276. newRow = new HTMLTableRowElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "TR" );
  277. //newRow.insertCell( 0 );
  278. insertRowX( index, newRow );
  279. return newRow;
  280. }
  281. void insertRowX( int index, HTMLTableRowElementImpl newRow )
  282. {
  283. Node child;
  284. Node lastSection = null;
  285. child = getFirstChild();
  286. while ( child != null )
  287. {
  288. if ( child instanceof HTMLTableRowElement )
  289. {
  290. if ( index == 0 )
  291. {
  292. insertBefore( newRow, child );
  293. return;
  294. }
  295. }
  296. else
  297. if ( child instanceof HTMLTableSectionElementImpl )
  298. {
  299. lastSection = child;
  300. index = ( (HTMLTableSectionElementImpl) child ).insertRowX( index, newRow );
  301. if ( index < 0 )
  302. return;
  303. }
  304. child = child.getNextSibling();
  305. }
  306. if ( lastSection != null )
  307. lastSection.appendChild( newRow );
  308. else
  309. appendChild( newRow );
  310. }
  311. public synchronized void deleteRow( int index )
  312. {
  313. Node child;
  314. child = getFirstChild();
  315. while ( child != null )
  316. {
  317. if ( child instanceof HTMLTableRowElement )
  318. {
  319. if ( index == 0 )
  320. {
  321. removeChild ( child );
  322. return;
  323. }
  324. --index;
  325. }
  326. else
  327. if ( child instanceof HTMLTableSectionElementImpl )
  328. {
  329. index = ( (HTMLTableSectionElementImpl) child ).deleteRowX( index );
  330. if ( index < 0 )
  331. return;
  332. }
  333. child = child.getNextSibling();
  334. }
  335. }
  336. /**
  337. * Constructor requires owner document.
  338. *
  339. * @param owner The owner HTML document
  340. */
  341. public HTMLTableElementImpl( HTMLDocumentImpl owner, String name )
  342. {
  343. super( owner, name );
  344. }
  345. private HTMLCollectionImpl _rows;
  346. private HTMLCollectionImpl _bodies;
  347. }