KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyElementNS.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import org.enhydra.apache.xerces.dom.ElementNSImpl;
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 with namespaces that supports lazy
38  * instantiation of a template DOM.
39  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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