KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.enhydra.xml.lazydom;
25
26 import org.enhydra.apache.xerces.dom.AttrNSImpl;
27 import org.enhydra.apache.xerces.dom.NodeImpl;
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * Implementation of the DOM Attr that supports lazy instantiation of
35  * a template DOM. This version supports namespaces.
36  */

37 public class LazyAttrNS extends AttrNSImpl implements LazyAttr {
38     /**
39      * Constructor with namespace.
40      * @param ownerDoc The document that owns this node.
41      * @param template If not-null, get the parameters from this template.
42      * @param namespaceURI The namespace for the node, or null for no namespace.
43      * Will be ignored if template is not-null.
44      * @param qualifiedName The attribute name.
45      * Will be ignored if template is not-null.
46      */

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

68     /**
69      * Template for this Attr.
70      */

71     private LazyAttrNS fTemplateNode = null;
72
73     /**
74      * Get the template for this node.
75      * @see LazyNode#getTemplateNode
76      */

77     public LazyAttrNS getTemplateAttr() {
78         return fTemplateNode;
79     }
80
81     /**
82      * @see Node#cloneNode
83      */

84     public Node JavaDoc cloneNode(boolean deep) {
85         if (deep) {
86             // If children are copied, we must expand now.
87
if (!fChildrenExpanded) {
88                 expandChildren();
89             }
90         }
91         
92         // This does a clone(), must clean up all fields.
93
LazyAttrNS newAttr = (LazyAttrNS)super.cloneNode(deep);
94         newAttr.fNodeId = NULL_NODE_ID;
95         newAttr.fParentExpanded = true;
96         newAttr.fChildrenExpanded = true;
97         return newAttr;
98     }
99
100     /**
101      * @see org.w3c.dom.Attr#getValue
102      */

103     public String JavaDoc getValue() {
104         if (!fChildrenExpanded) {
105             expandChildren();
106         }
107         return super.getValue();
108     }
109
110     /**
111      * @see org.w3c.dom.Attr#setValue
112      */

113     public void setValue(String JavaDoc value) throws DOMException JavaDoc {
114         if (!fChildrenExpanded) {
115             expandChildren();
116         }
117         super.setValue(value);
118     }
119
120     /**
121      * @see org.w3c.dom.Node#getNodeValue
122      */

123     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
124         if (!fChildrenExpanded) {
125             expandChildren();
126         }
127         return super.getNodeValue();
128     }
129
130     /**
131      * @see org.w3c.dom.Node#setNodeValue
132      */

133     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
134         if (!fChildrenExpanded) {
135             expandChildren();
136         }
137         super.setNodeValue(nodeValue);
138     }
139
140     //-------------------------------------------------------------------------
141
// LazyNode support
142
//-------------------------------------------------------------------------
143

144     /*
145      * Node id for this element.
146      */

147     private int fNodeId = NULL_NODE_ID;
148
149     /**
150      * Is this a template node?
151      */

152     private boolean fIsTemplateNode;
153
154     /*
155      * @see LazyNode#makeTemplateNode
156      */

157     public void makeTemplateNode(int nodeId) {
158         fNodeId = nodeId;
159         fIsTemplateNode = true;
160     }
161
162     /**
163      * @see LazyNode#getNodeId
164      */

165     public int getNodeId() {
166         return fNodeId;
167     }
168
169     /**
170      * @see LazyNode#isTemplateNode
171      */

172     public boolean isTemplateNode() {
173         return fIsTemplateNode;
174     }
175
176     /**
177      * @see LazyNode#getTemplateNode
178      */

179     public LazyNode getTemplateNode() {
180         return fTemplateNode;
181     }
182
183     /**
184      * @see LazyNode#templateClone
185      */

186     public LazyNode templateClone(Document JavaDoc ownerDocument) {
187         return new LazyAttrNS((LazyDocument)ownerDocument, this, null, null);
188     }
189
190     // NB: Attr has specicia functionality for setNodeValue.
191

192     //-------------------------------------------------------------------------
193
// LazyParent support
194
//-------------------------------------------------------------------------
195

196     /**
197      * Parent and children expanded flags.
198      */

199     private boolean fParentExpanded = false;
200     private boolean fChildrenExpanded = false;
201     
202     /**
203      * @see LazyParent#isParentExpanded()
204      */

205     public boolean isParentExpanded() {
206         return fParentExpanded;
207     }
208     
209     /**
210      * @see LazyParent#setParentExpanded
211      */

212     public void setParentExpanded() {
213         fParentExpanded = true;
214     }
215     
216     /**
217      * @see LazyParent#setParentWhileExpanding
218      */

219     public void setParentWhileExpanding(Node JavaDoc parent) {
220         ownerNode = (NodeImpl)parent;
221         flags |= OWNED;
222         fParentExpanded = true;
223     }
224     
225     /**
226      * @see LazyParent#areChildrenExpanded()
227      */

228     public boolean areChildrenExpanded() {
229         return fChildrenExpanded;
230     }
231     
232     /**
233      * @see LazyParent#setChildrenExpanded
234      */

235     public void setChildrenExpanded() {
236         fChildrenExpanded = true;
237     }
238
239     /**
240      * @see LazyParent#appendChildWhileExpanding
241      */

242     public void appendChildWhileExpanding(Node JavaDoc child) {
243         super.insertBefore(child, null);
244     }
245
246     /**
247      * Expand the parent of this element, if it is not already expanded.
248      */

249     private void expandParent() {
250         ((LazyDocument)getOwnerDocument()).doExpandParent(this);
251     }
252
253     /**
254      * Expand the children of this element, if they are not already expanded.
255      */

256     private void expandChildren() {
257         ((LazyDocument)getOwnerDocument()).doExpandChildren(this);
258     }
259
260     /**
261      * @see org.w3c.dom.Node#getParentNode
262      */

263     public Node JavaDoc getParentNode() {
264         if (!fParentExpanded) {
265             expandParent();
266         }
267         return super.getParentNode();
268     }
269
270     /**
271      * @see org.w3c.dom.Node#getChildNodes
272      */

273     public NodeList JavaDoc getChildNodes() {
274         if (!fChildrenExpanded) {
275             expandChildren();
276         }
277         return super.getChildNodes();
278     }
279
280     /**
281      * @see org.w3c.dom.Node#getFirstChild
282      */

283     public Node JavaDoc getFirstChild() {
284         if (!fChildrenExpanded) {
285             expandChildren();
286         }
287         return super.getFirstChild();
288     }
289
290     /**
291      * @see org.w3c.dom.Node#getLastChild
292      */

293     public Node JavaDoc getLastChild() {
294         if (!fChildrenExpanded) {
295             expandChildren();
296         }
297         return super.getLastChild();
298     }
299
300     /**
301      * @see org.w3c.dom.Node#getPreviousSibling
302      */

303     public Node JavaDoc getPreviousSibling() {
304         if (!fParentExpanded) {
305             expandParent();
306         }
307         return super.getPreviousSibling();
308     }
309
310     /**
311      * @see org.w3c.dom.Node#getNextSibling
312      */

313     public Node JavaDoc getNextSibling() {
314         if (!fParentExpanded) {
315             expandParent();
316         }
317         return super.getNextSibling();
318     }
319
320     /**
321      * @see org.w3c.dom.Node#insertBefore
322      */

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

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

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

358     public Node JavaDoc appendChild(Node JavaDoc newChild)
359         throws DOMException JavaDoc {
360         if (!fChildrenExpanded) {
361             expandChildren();
362         }
363         return super.appendChild(newChild);
364     }
365
366     /**
367      * @see org.w3c.dom.Node#hasChildNodes
368      */

369     public boolean hasChildNodes() {
370         if (!fChildrenExpanded) {
371             return fTemplateNode.hasChildNodes();
372         } else {
373             return super.hasChildNodes();
374         }
375     }
376     
377     /**
378      * @see org.w3c.dom.Node#normalize
379      */

380     public void normalize() {
381         if (!fChildrenExpanded) {
382             expandChildren();
383         }
384         super.normalize();
385     }
386
387     /**
388      * Return string for debugging. Xerces toString() method calls
389      * getValue(), which can cause expansion, so we override.
390      */

391     public String JavaDoc toString() {
392         return getName();
393     }
394 }
395
Popular Tags