KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > LazyElementNoNS


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: LazyElementNoNS.java,v 1.4 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import org.enhydra.apache.xerces.dom.ElementImpl;
27 import org.enhydra.apache.xerces.dom.NodeImpl;
28 import org.enhydra.xml.io.PreFormattedText;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.DOMException JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 /**
37  * Implementation of the DOM Element without namespaces that supports lazy
38  * instantiation of a template DOM. It is used by HTML, where non-standard
39  * element names containing `:' are occasionally invented. If this was
40  * derived from ElementNSImpl, it would do validation on the name and generate
41  * an error.
42  */

43 public class LazyElementNoNS extends ElementImpl implements LazyElement {
44     /**
45      * Constructor.
46      * @param ownerDoc The document that owns this node.
47      * @param template If not-null, get the parameters from this template.
48      * @param qualifiedName The tag name for the node.
49      * Will be ignored if template is not-null.
50      */

51     protected LazyElementNoNS(LazyDocument ownerDoc,
52                               LazyElement template,
53                               String JavaDoc qualifiedName) {
54         super(ownerDoc,
55               (template != null) ? template.getNodeName() : qualifiedName);
56         if (template != null) {
57             fTemplateNode = template;
58             fNodeId = template.getNodeId();
59         } else {
60             // Not created from a template, mark all as expanded.
61
fParentExpanded = true;
62             fChildrenExpanded = true;
63             fAttributesExpanded = true;
64         }
65     }
66
67     //-------------------------------------------------------------------------
68
// LazyElementNoNS specific
69
//-------------------------------------------------------------------------
70

71     /**
72      * Template for this element.
73      */

74     private LazyElement fTemplateNode = null;
75
76     /**
77      * Mark the node as a template node and associated preformatted text.
78      * @see LazyNode#makeTemplateNode
79      * @see PreFormattedText#setPreFormattedText
80      */

81     public void makeTemplateNode(int nodeId,
82                                  String JavaDoc text) {
83         fNodeId = nodeId;
84         fIsTemplateNode = true;
85         fPreFormattedText = text;
86     }
87
88     /**
89      * Get the template for this node.
90      * @see LazyNode#getTemplateNode
91      */

92     public LazyElement getTemplateElement() {
93         return fTemplateNode;
94     }
95
96     /**
97      * @see Node#cloneNode
98      */

99     public Node JavaDoc cloneNode(boolean deep) {
100         // If children are copied, we must expand now.
101
if (deep && !fChildrenExpanded) {
102             expandChildren();
103         }
104         // Attributes are always copied
105
if (!fAttributesExpanded) {
106             expandAttributes();
107         }
108         
109         // This does a clone(), must clean up all fields.
110
LazyElementNoNS newElement = (LazyElementNoNS)super.cloneNode(deep);
111         newElement.fNodeId = NULL_NODE_ID;
112         newElement.fParentExpanded = true;
113         newElement.fChildrenExpanded = true;
114         newElement.fAttributesExpanded = true;
115         return newElement;
116     }
117
118     //-------------------------------------------------------------------------
119
// LazyNode support
120
//-------------------------------------------------------------------------
121

122     /*
123      * Node id for this element.
124      */

125     private int fNodeId = NULL_NODE_ID;
126
127     /**
128      * Is this a template node?
129      */

130     private boolean fIsTemplateNode;
131
132     /*
133      * @see LazyNode#makeTemplateNode
134      */

135     public void makeTemplateNode(int nodeId) {
136         fNodeId = nodeId;
137         fIsTemplateNode = true;
138     }
139
140     /**
141      * @see LazyNode#getNodeId
142      */

143     public int getNodeId() {
144         return fNodeId;
145     }
146
147     /**
148      * @see LazyNode#isTemplateNode
149      */

150     public boolean isTemplateNode() {
151         return fIsTemplateNode;
152     }
153
154     /**
155      * @see LazyNode#getTemplateNode
156      */

157     public LazyNode getTemplateNode() {
158         return fTemplateNode;
159     }
160
161     /**
162      * @see LazyNode#templateClone
163      */

164     public LazyNode templateClone(Document JavaDoc ownerDocument) {
165         return new LazyElementNoNS((LazyDocument)ownerDocument, this, null);
166     }
167
168     /**
169      * Set the node value, invalidating the id. All node data is modified
170      * by this routine.
171      * @see org.w3c.dom.Node#setNodeValue
172      */

173     public void setNodeValue(String JavaDoc value) {
174         fNodeId = NULL_NODE_ID;
175         super.setNodeValue(value);
176     }
177
178     //-------------------------------------------------------------------------
179
// LazyParent support
180
//-------------------------------------------------------------------------
181

182     /**
183      * Parent and children expanded flags.
184      */

185     private boolean fParentExpanded = false;
186     private boolean fChildrenExpanded = false;
187     
188     /**
189      * @see LazyParent#isParentExpanded()
190      */

191     public boolean isParentExpanded() {
192         return fParentExpanded;
193     }
194     
195     /**
196      * @see LazyParent#setParentExpanded
197      */

198     public void setParentExpanded() {
199         fParentExpanded = true;
200     }
201     
202     /**
203      * @see LazyParent#setParentWhileExpanding
204      */

205     public void setParentWhileExpanding(Node JavaDoc parent) {
206         ownerNode = (NodeImpl)parent;
207         flags |= OWNED;
208         fParentExpanded = true;
209     }
210     
211     /**
212      * @see LazyParent#areChildrenExpanded()
213      */

214     public boolean areChildrenExpanded() {
215         return fChildrenExpanded;
216     }
217     
218     /**
219      * @see LazyParent#setChildrenExpanded
220      */

221     public void setChildrenExpanded() {
222         fChildrenExpanded = true;
223     }
224
225     /**
226      * @see LazyParent#appendChildWhileExpanding
227      */

228     public void appendChildWhileExpanding(Node JavaDoc child) {
229         super.insertBefore(child, null);
230     }
231
232     /**
233      * Expand the parent of this element, if it is not already expanded.
234      */

235     private void expandParent() {
236         ((LazyDocument)getOwnerDocument()).doExpandParent(this);
237     }
238
239     /**
240      * Expand the children of this element, if they are not already expanded.
241      */

242     protected void expandChildren() {
243         ((LazyDocument)getOwnerDocument()).doExpandChildren(this);
244     }
245
246     /**
247      * @see org.w3c.dom.Node#getParentNode
248      */

249     public Node JavaDoc getParentNode() {
250         if (!fParentExpanded) {
251             expandParent();
252         }
253         return super.getParentNode();
254     }
255
256     /**
257      * @see org.w3c.dom.Node#getChildNodes
258      */

259     public NodeList JavaDoc getChildNodes() {
260         if (!fChildrenExpanded) {
261             expandChildren();
262         }
263         return super.getChildNodes();
264     }
265
266     /**
267      * @see org.w3c.dom.Node#getFirstChild
268      */

269     public Node JavaDoc getFirstChild() {
270         if (!fChildrenExpanded) {
271             expandChildren();
272         }
273         return super.getFirstChild();
274     }
275
276     /**
277      * @see org.w3c.dom.Node#getLastChild
278      */

279     public Node JavaDoc getLastChild() {
280         if (!fChildrenExpanded) {
281             expandChildren();
282         }
283         return super.getLastChild();
284     }
285
286     /**
287      * @see org.w3c.dom.Node#getPreviousSibling
288      */

289     public Node JavaDoc getPreviousSibling() {
290         if (!fParentExpanded) {
291             expandParent();
292         }
293         return super.getPreviousSibling();
294     }
295
296     /**
297      * @see org.w3c.dom.Node#getNextSibling
298      */

299     public Node JavaDoc getNextSibling() {
300         if (!fParentExpanded) {
301             expandParent();
302         }
303         return super.getNextSibling();
304     }
305
306    /**
307      * @see org.w3c.dom.Node#insertBefore
308      */

309     public Node JavaDoc insertBefore(Node JavaDoc newChild,
310                              Node JavaDoc refChild)
311         throws DOMException JavaDoc {
312         if (!fChildrenExpanded) {
313             expandChildren();
314         }
315         return super.insertBefore(newChild, refChild);
316     }
317
318     /**
319      * @see org.w3c.dom.Node#replaceChild
320      */

321     public Node JavaDoc replaceChild(Node JavaDoc newChild,
322                              Node JavaDoc oldChild)
323         throws DOMException JavaDoc {
324         if (!fChildrenExpanded) {
325             expandChildren();
326         }
327         return super.replaceChild(newChild, oldChild);
328     }
329
330     /**
331      * @see org.w3c.dom.Node#removeChild
332      */

333     public Node JavaDoc removeChild(Node JavaDoc oldChild)
334         throws DOMException JavaDoc {
335         if (!fChildrenExpanded) {
336             expandChildren();
337         }
338         return super.removeChild(oldChild);
339     }
340
341     /**
342      * @see org.w3c.dom.Node#appendChild
343      */

344     public Node JavaDoc appendChild(Node JavaDoc newChild)
345         throws DOMException JavaDoc {
346         if (!fChildrenExpanded) {
347             expandChildren();
348         }
349         return super.appendChild(newChild);
350     }
351
352     /**
353      * @see org.w3c.dom.Node#hasChildNodes
354      */

355     public boolean hasChildNodes() {
356         if (!fChildrenExpanded) {
357             return fTemplateNode.hasChildNodes();
358         } else {
359             return super.hasChildNodes();
360         }
361     }
362     
363     /**
364      * @see org.w3c.dom.Node#normalize
365      */

366     public void normalize() {
367         if (!fChildrenExpanded) {
368             expandChildren();
369         }
370         super.normalize();
371     }
372
373     //-------------------------------------------------------------------------
374
// Attribute support
375
//-------------------------------------------------------------------------
376

377     /**
378      * Attributes expanded flag.
379      */

380     private boolean fAttributesExpanded = false;
381
382     /**
383      * Are the attributes of this node expanded?
384      */

385     public boolean areAttributesExpanded() {
386         return fAttributesExpanded;
387     }
388
389     /**
390      * Do work of expanding attributes. Must be called while
391      * synchronized on the owner document.
392      */

393     private void doExpandAttributes(LazyDocument doc) {
394         doc.enterExpansion();
395         try {
396             NamedNodeMap JavaDoc templateAttrs = fTemplateNode.getAttributes();
397             if (templateAttrs != null) {
398                 int len = templateAttrs.getLength();
399                 for (int idx = 0; idx < len; idx++) {
400                     Attr JavaDoc attr = (Attr JavaDoc)doc.getNodeFromTemplate((LazyNode)templateAttrs.item(idx));
401                     super.setAttributeNode(attr);
402                 }
403             }
404             fAttributesExpanded = true;
405         } finally {
406             doc.leaveExpansion();
407         }
408     }
409
410     /**
411      * Expand the attributes of this element, if they are not already
412      * expanded.
413      */

414     private void expandAttributes() {
415         LazyDocument doc = (LazyDocument)getOwnerDocument();
416         synchronized (doc) {
417             if (!fAttributesExpanded) {
418                 doExpandAttributes(doc);
419             }
420         }
421     }
422
423     /**
424      * @see org.w3c.dom.Node#getAttributes
425      */

426     public NamedNodeMap JavaDoc getAttributes() {
427         if (!fAttributesExpanded) {
428             expandAttributes();
429         }
430         return super.getAttributes();
431     }
432
433      /**
434      * @see org.w3c.dom.Element#getAttribute
435      */

436     public String JavaDoc getAttribute(String JavaDoc name) {
437         if (!fAttributesExpanded) {
438             expandAttributes();
439         }
440         return super.getAttribute(name);
441     }
442
443     /**
444      * @see org.w3c.dom.Element#setAttribute
445      */

446     public void setAttribute(String JavaDoc name,
447                              String JavaDoc value) throws DOMException JavaDoc {
448         if (!fAttributesExpanded) {
449             expandAttributes();
450         }
451         super.setAttribute(name, value);
452     }
453
454     /**
455      * @see org.w3c.dom.Element#removeAttribute
456      */

457     public void removeAttribute(String JavaDoc name) throws DOMException JavaDoc {
458         if (!fAttributesExpanded) {
459             expandAttributes();
460         }
461         super.removeAttribute(name);
462     }
463
464     /**
465      * @see org.w3c.dom.Element#getAttributeNode
466      */

467     public Attr JavaDoc getAttributeNode(String JavaDoc name) {
468         if (!fAttributesExpanded) {
469             expandAttributes();
470         }
471         return super.getAttributeNode(name);
472     }
473
474     /**
475      * @see org.w3c.dom.Element#setAttributeNode
476      */

477     public Attr JavaDoc setAttributeNode(Attr JavaDoc newAttr) throws DOMException JavaDoc {
478         if (!fAttributesExpanded) {
479             expandAttributes();
480         }
481         return super.setAttributeNode(newAttr);
482     }
483
484     /**
485      * @see org.w3c.dom.Element#removeAttributeNode
486      */

487     public Attr JavaDoc removeAttributeNode(Attr JavaDoc oldAttr) throws DOMException JavaDoc {
488         if (!fAttributesExpanded) {
489             expandAttributes();
490         }
491         return super.removeAttributeNode(oldAttr);
492     }
493
494     /**
495      * @see org.w3c.dom.Element#getAttributeNS
496      */

497     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI,
498                                  String JavaDoc localName) {
499         if (!fAttributesExpanded) {
500             expandAttributes();
501         }
502         return super.getAttributeNS(namespaceURI, localName);
503     }
504
505     /**
506      * @see org.w3c.dom.Element#setAttributeNS
507      */

508     public void setAttributeNS(String JavaDoc namespaceURI,
509                                String JavaDoc qualifiedName,
510                                String JavaDoc value) throws DOMException JavaDoc {
511         if (!fAttributesExpanded) {
512             expandAttributes();
513         }
514         super.setAttributeNS(namespaceURI, qualifiedName, value);
515     }
516
517     /**
518      * @see org.w3c.dom.Element#removeAttributeNS
519      */

520     public void removeAttributeNS(String JavaDoc namespaceURI,
521                                   String JavaDoc localName) throws DOMException JavaDoc {
522         if (!fAttributesExpanded) {
523             expandAttributes();
524         }
525         super.removeAttributeNS(namespaceURI, localName);
526     }
527
528     /**
529      * @see org.w3c.dom.Element#getAttributeNodeNS
530      */

531     public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI,
532                                    String JavaDoc localName) {
533         if (!fAttributesExpanded) {
534             expandAttributes();
535         }
536         return super.getAttributeNodeNS(namespaceURI, localName);
537     }
538
539     /**
540      * @see org.w3c.dom.Element#setAttributeNodeNS
541      */

542     public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc newAttr) throws DOMException JavaDoc {
543         if (!fAttributesExpanded) {
544             expandAttributes();
545         }
546         return super.setAttributeNodeNS(newAttr);
547     }
548
549     //-------------------------------------------------------------------------
550
// PreFormattedText specific
551
//-------------------------------------------------------------------------
552

553     /**
554      * Pre-formatted text associated with the node.
555      */

556     private String JavaDoc fPreFormattedText;
557
558     /**
559      * @see PreFormattedText#getPreFormattedText
560      */

561     public String JavaDoc getPreFormattedText() {
562         return fPreFormattedText;
563     }
564
565     /**
566      * @see PreFormattedText#setPreFormattedText
567      */

568     public void setPreFormattedText(String JavaDoc text) {
569         fPreFormattedText = text;
570     }
571 }
572
Popular Tags