KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyAttrNoNS.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.AttrImpl;
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 does not support namespaaces. It is
36  * used by HTML, where non-standard attributes containing `:' are
37  * occasionally invented. If this was derived from AttrNSImpl, it
38  * would do validation on the name and generate an error.
39  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

191     //-------------------------------------------------------------------------
192
// LazyParent support
193
//-------------------------------------------------------------------------
194

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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