KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyEntity.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.EntityImpl;
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 Entity that supports lazy instantiation of
35  * a template DOM.
36  */

37 public class LazyEntity extends EntityImpl implements LazyParent {
38     /**
39      * Constructor from template.
40      * @param ownerDoc The document that owns this node.
41      * @param template If not-null, get the parameters from this template.
42      * @param name The entity name.
43      * Will be ignored if template is not-null.
44      * @param publicId The public id.
45      * Will be ignored if template is not-null.
46      * @param systemId The system id.
47      * Will be ignored if template is not-null.
48      * @param notationName The notation name.
49      * Will be ignored if template is not-null.
50      */

51     protected LazyEntity(LazyDocument ownerDoc,
52                          LazyEntity template,
53                          String JavaDoc name,
54                          String JavaDoc publicId,
55                          String JavaDoc systemId,
56                          String JavaDoc notationName) {
57         super(ownerDoc,
58               (template != null) ? template.getNodeName() : name);
59         if (template != null) {
60             fTemplateNode = template;
61             fNodeId = template.getNodeId();
62             if (template.getPublicId() != null) {
63                 setPublicId(template.getPublicId());
64             }
65             if (template.getSystemId() != null) {
66                 setSystemId(template.getSystemId());
67             }
68             if (template.getNotationName() != null) {
69                 setNotationName(template.getNotationName());
70             }
71         } else {
72             if (publicId != null) {
73                 setPublicId(publicId);
74             }
75             if (systemId != null) {
76                 setSystemId(systemId);
77             }
78             if (notationName != null) {
79                 setNotationName(notationName);
80             }
81             // Not created from a template, mark all as expanded.
82
fParentExpanded = true;
83             fChildrenExpanded = true;
84         }
85     }
86
87     //-------------------------------------------------------------------------
88
// LazyEntity specific
89
//-------------------------------------------------------------------------
90

91     /**
92      * Template for this Entity.
93      */

94     private LazyEntity fTemplateNode = null;
95
96     /**
97      * Get the template for this node.
98      * @see LazyNode#getTemplateNode
99      */

100     public LazyEntity getTemplateEntity() {
101         return fTemplateNode;
102     }
103
104     /**
105      * @see Node#cloneNode
106      */

107     public Node JavaDoc cloneNode(boolean deep) {
108         if (deep) {
109             // If children are copied, we must expand now.
110
if (!fChildrenExpanded) {
111                 expandChildren();
112             }
113         }
114         
115         // This does a clone(), must clean up all fields.
116
LazyEntity newEntity = (LazyEntity)super.cloneNode(deep);
117         newEntity.fNodeId = NULL_NODE_ID;
118         newEntity.fParentExpanded = true;
119         newEntity.fChildrenExpanded = true;
120         return newEntity;
121     }
122
123     //-------------------------------------------------------------------------
124
// LazyNode support
125
//-------------------------------------------------------------------------
126

127     /*
128      * Node id for this element.
129      */

130     private int fNodeId = NULL_NODE_ID;
131
132     /**
133      * Is this a template node?
134      */

135     private boolean fIsTemplateNode;
136
137     /*
138      * @see LazyNode#makeTemplateNode
139      */

140     public void makeTemplateNode(int nodeId) {
141         fNodeId = nodeId;
142         fIsTemplateNode = true;
143     }
144
145     /**
146      * @see LazyNode#getNodeId
147      */

148     public int getNodeId() {
149         return fNodeId;
150     }
151
152     /**
153      * @see LazyNode#isTemplateNode
154      */

155     public boolean isTemplateNode() {
156         return fIsTemplateNode;
157     }
158
159     /**
160      * @see LazyNode#getTemplateNode
161      */

162     public LazyNode getTemplateNode() {
163         return fTemplateNode;
164     }
165
166     /**
167      * @see LazyNode#templateClone
168      */

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

179     public void setNodeValue(String JavaDoc value) {
180         fNodeId = NULL_NODE_ID;
181         super.setNodeValue(value);
182     }
183
184     //-------------------------------------------------------------------------
185
// LazyParent support
186
//-------------------------------------------------------------------------
187

188     /**
189      * Parent and children expanded flags.
190      */

191     private boolean fParentExpanded = false;
192     private boolean fChildrenExpanded = false;
193     
194     /**
195      * @see LazyParent#isParentExpanded()
196      */

197     public boolean isParentExpanded() {
198         return fParentExpanded;
199     }
200     
201     /**
202      * @see LazyParent#setParentExpanded
203      */

204     public void setParentExpanded() {
205         fParentExpanded = true;
206     }
207     
208     /**
209      * @see LazyParent#setParentWhileExpanding
210      */

211     public void setParentWhileExpanding(Node JavaDoc parent) {
212         ownerNode = (NodeImpl)parent;
213         flags |= OWNED;
214         fParentExpanded = true;
215     }
216     
217     /**
218      * @see LazyParent#areChildrenExpanded()
219      */

220     public boolean areChildrenExpanded() {
221         return fChildrenExpanded;
222     }
223     
224     /**
225      * @see LazyParent#setChildrenExpanded
226      */

227     public void setChildrenExpanded() {
228         fChildrenExpanded = true;
229     }
230
231     /**
232      * @see LazyParent#appendChildWhileExpanding
233      */

234     public void appendChildWhileExpanding(Node JavaDoc child) {
235         super.insertBefore(child, null);
236     }
237
238     /**
239      * Expand the parent of this element, if it is not already expanded.
240      */

241     private void expandParent() {
242         ((LazyDocument)getOwnerDocument()).doExpandParent(this);
243     }
244
245     /**
246      * Expand the children of this element, if they are not already expanded.
247      */

248     private void expandChildren() {
249         ((LazyDocument)getOwnerDocument()).doExpandChildren(this);
250     }
251
252     /**
253      * @see org.w3c.dom.Node#getParentNode
254      */

255     public Node JavaDoc getParentNode() {
256         if (!fParentExpanded) {
257             expandParent();
258         }
259         return super.getParentNode();
260     }
261
262     /**
263      * @see org.w3c.dom.Node#getChildNodes
264      */

265     public NodeList JavaDoc getChildNodes() {
266         if (!fChildrenExpanded) {
267             expandChildren();
268         }
269         return super.getChildNodes();
270     }
271
272     /**
273      * @see org.w3c.dom.Node#getFirstChild
274      */

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

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

295     public Node JavaDoc getPreviousSibling() {
296         if (!fParentExpanded) {
297             expandParent();
298         }
299         return super.getPreviousSibling();
300     }
301
302     /**
303      * @see org.w3c.dom.Node#getNextSibling
304      */

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

315     public Node JavaDoc insertBefore(Node JavaDoc newChild,
316                              Node JavaDoc refChild)
317         throws DOMException JavaDoc {
318         if (!fChildrenExpanded) {
319             expandChildren();
320         }
321         return super.insertBefore(newChild, refChild);
322     }
323
324     /**
325      * @see org.w3c.dom.Node#replaceChild
326      */

327     public Node JavaDoc replaceChild(Node JavaDoc newChild,
328                              Node JavaDoc oldChild)
329         throws DOMException JavaDoc {
330         if (!fChildrenExpanded) {
331             expandChildren();
332         }
333         return super.replaceChild(newChild, oldChild);
334     }
335
336     /**
337      * @see org.w3c.dom.Node#removeChild
338      */

339     public Node JavaDoc removeChild(Node JavaDoc oldChild)
340         throws DOMException JavaDoc {
341         if (!fChildrenExpanded) {
342             expandChildren();
343         }
344         return super.removeChild(oldChild);
345     }
346
347     /**
348      * @see org.w3c.dom.Node#appendChild
349      */

350     public Node JavaDoc appendChild(Node JavaDoc newChild)
351         throws DOMException JavaDoc {
352         if (!fChildrenExpanded) {
353             expandChildren();
354         }
355         return super.appendChild(newChild);
356     }
357
358     /**
359      * @see org.w3c.dom.Node#hasChildNodes
360      */

361     public boolean hasChildNodes() {
362         if (!fChildrenExpanded) {
363             return fTemplateNode.hasChildNodes();
364         } else {
365             return super.hasChildNodes();
366         }
367     }
368     
369     /**
370      * @see org.w3c.dom.Node#normalize
371      */

372     public void normalize() {
373         if (!fChildrenExpanded) {
374             expandChildren();
375         }
376         super.normalize();
377     }
378 }
379
Popular Tags