001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.html.dom;
018:
019: import org.w3c.dom.Node;
020: import org.w3c.dom.html.HTMLCollection;
021: import org.w3c.dom.html.HTMLElement;
022: import org.w3c.dom.html.HTMLTableCaptionElement;
023: import org.w3c.dom.html.HTMLTableElement;
024: import org.w3c.dom.html.HTMLTableRowElement;
025: import org.w3c.dom.html.HTMLTableSectionElement;
026:
027: /**
028: * @xerces.internal
029: * @version $Revision: 447255 $ $Date: 2006-09-18 01:36:42 -0400 (Mon, 18 Sep 2006) $
030: * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
031: * @see org.w3c.dom.html.HTMLAnchorElement
032: * @see org.apache.xerces.dom.ElementImpl
033: */
034: public class HTMLTableElementImpl extends HTMLElementImpl implements
035: HTMLTableElement {
036:
037: private static final long serialVersionUID = -1824053099870917532L;
038:
039: public synchronized HTMLTableCaptionElement getCaption() {
040: Node child;
041:
042: child = getFirstChild();
043: while (child != null) {
044: if (child instanceof HTMLTableCaptionElement
045: && child.getNodeName().equals("CAPTION"))
046: return (HTMLTableCaptionElement) child;
047: child = child.getNextSibling();
048: }
049: return null;
050: }
051:
052: public synchronized void setCaption(HTMLTableCaptionElement caption) {
053: if (caption != null && !caption.getTagName().equals("CAPTION"))
054: throw new IllegalArgumentException(
055: "HTM016 Argument 'caption' is not an element of type <CAPTION>.");
056: deleteCaption();
057: if (caption != null)
058: appendChild(caption);
059: }
060:
061: public synchronized HTMLElement createCaption() {
062: HTMLElement section;
063:
064: section = getCaption();
065: if (section != null)
066: return section;
067: section = new HTMLTableCaptionElementImpl(
068: (HTMLDocumentImpl) getOwnerDocument(), "CAPTION");
069: appendChild(section);
070: return section;
071: }
072:
073: public synchronized void deleteCaption() {
074: Node old;
075:
076: old = getCaption();
077: if (old != null)
078: removeChild(old);
079: }
080:
081: public synchronized HTMLTableSectionElement getTHead() {
082: Node child;
083:
084: child = getFirstChild();
085: while (child != null) {
086: if (child instanceof HTMLTableSectionElement
087: && child.getNodeName().equals("THEAD"))
088: return (HTMLTableSectionElement) child;
089: child = child.getNextSibling();
090: }
091: return null;
092: }
093:
094: public synchronized void setTHead(HTMLTableSectionElement tHead) {
095: if (tHead != null && !tHead.getTagName().equals("THEAD"))
096: throw new IllegalArgumentException(
097: "HTM017 Argument 'tHead' is not an element of type <THEAD>.");
098: deleteTHead();
099: if (tHead != null)
100: appendChild(tHead);
101: }
102:
103: public synchronized HTMLElement createTHead() {
104: HTMLElement section;
105:
106: section = getTHead();
107: if (section != null)
108: return section;
109: section = new HTMLTableSectionElementImpl(
110: (HTMLDocumentImpl) getOwnerDocument(), "THEAD");
111: appendChild(section);
112: return section;
113: }
114:
115: public synchronized void deleteTHead() {
116: Node old;
117:
118: old = getTHead();
119: if (old != null)
120: removeChild(old);
121: }
122:
123: public synchronized HTMLTableSectionElement getTFoot() {
124: Node child;
125:
126: child = getFirstChild();
127: while (child != null) {
128: if (child instanceof HTMLTableSectionElement
129: && child.getNodeName().equals("TFOOT"))
130: return (HTMLTableSectionElement) child;
131: child = child.getNextSibling();
132: }
133: return null;
134: }
135:
136: public synchronized void setTFoot(HTMLTableSectionElement tFoot) {
137: if (tFoot != null && !tFoot.getTagName().equals("TFOOT"))
138: throw new IllegalArgumentException(
139: "HTM018 Argument 'tFoot' is not an element of type <TFOOT>.");
140: deleteTFoot();
141: if (tFoot != null)
142: appendChild(tFoot);
143: }
144:
145: public synchronized HTMLElement createTFoot() {
146: HTMLElement section;
147:
148: section = getTFoot();
149: if (section != null)
150: return section;
151: section = new HTMLTableSectionElementImpl(
152: (HTMLDocumentImpl) getOwnerDocument(), "TFOOT");
153: appendChild(section);
154: return section;
155: }
156:
157: public synchronized void deleteTFoot() {
158: Node old;
159:
160: old = getTFoot();
161: if (old != null)
162: removeChild(old);
163: }
164:
165: public HTMLCollection getRows() {
166: if (_rows == null)
167: _rows = new HTMLCollectionImpl(this , HTMLCollectionImpl.ROW);
168: return _rows;
169: }
170:
171: public HTMLCollection getTBodies() {
172: if (_bodies == null)
173: _bodies = new HTMLCollectionImpl(this ,
174: HTMLCollectionImpl.TBODY);
175: return _bodies;
176: }
177:
178: public String getAlign() {
179: return capitalize(getAttribute("align"));
180: }
181:
182: public void setAlign(String align) {
183: setAttribute("align", align);
184: }
185:
186: public String getBgColor() {
187: return getAttribute("bgcolor");
188: }
189:
190: public void setBgColor(String bgColor) {
191: setAttribute("bgcolor", bgColor);
192: }
193:
194: public String getBorder() {
195: return getAttribute("border");
196: }
197:
198: public void setBorder(String border) {
199: setAttribute("border", border);
200: }
201:
202: public String getCellPadding() {
203: return getAttribute("cellpadding");
204: }
205:
206: public void setCellPadding(String cellPadding) {
207: setAttribute("cellpadding", cellPadding);
208: }
209:
210: public String getCellSpacing() {
211: return getAttribute("cellspacing");
212: }
213:
214: public void setCellSpacing(String cellSpacing) {
215: setAttribute("cellspacing", cellSpacing);
216: }
217:
218: public String getFrame() {
219: return capitalize(getAttribute("frame"));
220: }
221:
222: public void setFrame(String frame) {
223: setAttribute("frame", frame);
224: }
225:
226: public String getRules() {
227: return capitalize(getAttribute("rules"));
228: }
229:
230: public void setRules(String rules) {
231: setAttribute("rules", rules);
232: }
233:
234: public String getSummary() {
235: return getAttribute("summary");
236: }
237:
238: public void setSummary(String summary) {
239: setAttribute("summary", summary);
240: }
241:
242: public String getWidth() {
243: return getAttribute("width");
244: }
245:
246: public void setWidth(String width) {
247: setAttribute("width", width);
248: }
249:
250: public HTMLElement insertRow(int index) {
251: HTMLTableRowElementImpl newRow;
252:
253: newRow = new HTMLTableRowElementImpl(
254: (HTMLDocumentImpl) getOwnerDocument(), "TR");
255: //newRow.insertCell( 0 );
256: insertRowX(index, newRow);
257: return newRow;
258: }
259:
260: void insertRowX(int index, HTMLTableRowElementImpl newRow) {
261: Node child;
262: Node lastSection = null;
263:
264: child = getFirstChild();
265: while (child != null) {
266: if (child instanceof HTMLTableRowElement) {
267: if (index == 0) {
268: insertBefore(newRow, child);
269: return;
270: }
271: } else if (child instanceof HTMLTableSectionElementImpl) {
272: lastSection = child;
273: index = ((HTMLTableSectionElementImpl) child)
274: .insertRowX(index, newRow);
275: if (index < 0)
276: return;
277: }
278: child = child.getNextSibling();
279: }
280: if (lastSection != null)
281: lastSection.appendChild(newRow);
282: else
283: appendChild(newRow);
284: }
285:
286: public synchronized void deleteRow(int index) {
287: Node child;
288:
289: child = getFirstChild();
290: while (child != null) {
291: if (child instanceof HTMLTableRowElement) {
292: if (index == 0) {
293: removeChild(child);
294: return;
295: }
296: --index;
297: } else if (child instanceof HTMLTableSectionElementImpl) {
298: index = ((HTMLTableSectionElementImpl) child)
299: .deleteRowX(index);
300: if (index < 0)
301: return;
302: }
303: child = child.getNextSibling();
304: }
305: }
306:
307: /**
308: * Explicit implementation of cloneNode() to ensure that cache used
309: * for getRows() and getTBodies() gets cleared.
310: */
311: public Node cloneNode(boolean deep) {
312: HTMLTableElementImpl clonedNode = (HTMLTableElementImpl) super
313: .cloneNode(deep);
314: clonedNode._rows = null;
315: clonedNode._bodies = null;
316: return clonedNode;
317: }
318:
319: /**
320: * Constructor requires owner document.
321: *
322: * @param owner The owner HTML document
323: */
324: public HTMLTableElementImpl(HTMLDocumentImpl owner, String name) {
325: super (owner, name);
326: }
327:
328: private HTMLCollectionImpl _rows;
329:
330: private HTMLCollectionImpl _bodies;
331:
332: }
|