KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyEntityReference.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.EntityReferenceImpl;
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  * Lazy text node.
35  */

36 public class LazyEntityReference extends EntityReferenceImpl implements LazyParent {
37     /**
38      * Constructor.
39      * @param ownerDoc The document that owns this node.
40      * @param template If not-null, get the parameters from this template.
41      * @param name The entity name.
42      * Will be ignored if template is not-null.
43      */

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

63     /**
64      * Template for this EntityReference.
65      */

66     private LazyEntityReference fTemplateNode = null;
67
68     /**
69      * Get the template for this node.
70      * @see LazyNode#getTemplateNode
71      */

72     public LazyEntityReference getTemplateEntityReference() {
73         return fTemplateNode;
74     }
75
76     /**
77      * @see Node#cloneNode
78      */

79     public Node JavaDoc cloneNode(boolean deep) {
80         if (deep) {
81             // If children are copied, we must expand now.
82
if (!fChildrenExpanded) {
83                 expandChildren();
84             }
85         }
86         
87         // This does a clone(), must clean up all fields.
88
LazyEntityReference newEntityReference = (LazyEntityReference)super.cloneNode(deep);
89         newEntityReference.fNodeId = NULL_NODE_ID;
90         newEntityReference.fParentExpanded = true;
91         newEntityReference.fChildrenExpanded = true;
92         return newEntityReference;
93     }
94
95     //-------------------------------------------------------------------------
96
// LazyNode support
97
//-------------------------------------------------------------------------
98

99     /*
100      * Node id for this element.
101      */

102     private int fNodeId = NULL_NODE_ID;
103
104     /**
105      * Is this a template node?
106      */

107     private boolean fIsTemplateNode;
108
109     /*
110      * @see LazyNode#makeTemplateNode
111      */

112     public void makeTemplateNode(int nodeId) {
113         fNodeId = nodeId;
114         fIsTemplateNode = true;
115     }
116
117     /**
118      * @see LazyNode#getNodeId
119      */

120     public int getNodeId() {
121         return fNodeId;
122     }
123
124     /**
125      * @see LazyNode#isTemplateNode
126      */

127     public boolean isTemplateNode() {
128         return fIsTemplateNode;
129     }
130
131     /**
132      * @see LazyNode#getTemplateNode
133      */

134     public LazyNode getTemplateNode() {
135         return fTemplateNode;
136     }
137
138     /**
139      * @see LazyNode#templateClone
140      */

141     public LazyNode templateClone(Document JavaDoc ownerDocument) {
142         return new LazyEntityReference((LazyDocument)ownerDocument, this, null);
143     }
144
145     /**
146      * Set the node value, invalidating the id. All node data is modified
147      * by this routine.
148      * @see org.w3c.dom.Node#setNodeValue
149      */

150     public void setNodeValue(String JavaDoc value) {
151         fNodeId = NULL_NODE_ID;
152         super.setNodeValue(value);
153     }
154
155     //-------------------------------------------------------------------------
156
// LazyParent support
157
//-------------------------------------------------------------------------
158

159     /**
160      * Parent and children expanded flags.
161      */

162     private boolean fParentExpanded = false;
163     private boolean fChildrenExpanded = false;
164     
165     /**
166      * @see LazyParent#isParentExpanded()
167      */

168     public boolean isParentExpanded() {
169         return fParentExpanded;
170     }
171     
172     /**
173      * @see LazyParent#setParentExpanded
174      */

175     public void setParentExpanded() {
176         fParentExpanded = true;
177     }
178     
179     /**
180      * @see LazyParent#setParentWhileExpanding
181      */

182     public void setParentWhileExpanding(Node JavaDoc parent) {
183         ownerNode = (NodeImpl)parent;
184         flags |= OWNED;
185         fParentExpanded = true;
186     }
187     
188     /**
189      * @see LazyParent#areChildrenExpanded()
190      */

191     public boolean areChildrenExpanded() {
192         return fChildrenExpanded;
193     }
194     
195     /**
196      * @see LazyParent#setChildrenExpanded
197      */

198     public void setChildrenExpanded() {
199         fChildrenExpanded = true;
200     }
201
202     /**
203      * @see LazyParent#appendChildWhileExpanding
204      */

205     public void appendChildWhileExpanding(Node JavaDoc child) {
206         super.insertBefore(child, null);
207     }
208
209     /**
210      * Expand the parent of this element, if it is not already expanded.
211      */

212     private void expandParent() {
213         ((LazyDocument)getOwnerDocument()).doExpandParent(this);
214     }
215
216     /**
217      * Expand the children of this element, if they are not already expanded.
218      */

219     private void expandChildren() {
220         ((LazyDocument)getOwnerDocument()).doExpandChildren(this);
221     }
222
223     /**
224      * @see org.w3c.dom.Node#getParentNode
225      */

226     public Node JavaDoc getParentNode() {
227         if (!fParentExpanded) {
228             expandParent();
229         }
230         return super.getParentNode();
231     }
232
233     /**
234      * @see org.w3c.dom.Node#getChildNodes
235      */

236     public NodeList JavaDoc getChildNodes() {
237         if (!fChildrenExpanded) {
238             expandChildren();
239         }
240         return super.getChildNodes();
241     }
242
243     /**
244      * @see org.w3c.dom.Node#getFirstChild
245      */

246     public Node JavaDoc getFirstChild() {
247         if (!fChildrenExpanded) {
248             expandChildren();
249         }
250         return super.getFirstChild();
251     }
252
253     /**
254      * @see org.w3c.dom.Node#getLastChild
255      */

256     public Node JavaDoc getLastChild() {
257         if (!fChildrenExpanded) {
258             expandChildren();
259         }
260         return super.getLastChild();
261     }
262
263     /**
264      * @see org.w3c.dom.Node#getPreviousSibling
265      */

266     public Node JavaDoc getPreviousSibling() {
267         if (!fParentExpanded) {
268             expandParent();
269         }
270         return super.getPreviousSibling();
271     }
272
273     /**
274      * @see org.w3c.dom.Node#getNextSibling
275      */

276     public Node JavaDoc getNextSibling() {
277         if (!fParentExpanded) {
278             expandParent();
279         }
280         return super.getNextSibling();
281     }
282
283    /**
284      * @see org.w3c.dom.Node#insertBefore
285      */

286     public Node JavaDoc insertBefore(Node JavaDoc newChild,
287                              Node JavaDoc refChild)
288         throws DOMException JavaDoc {
289         if (!fChildrenExpanded) {
290             expandChildren();
291         }
292         return super.insertBefore(newChild, refChild);
293     }
294
295     /**
296      * @see org.w3c.dom.Node#replaceChild
297      */

298     public Node JavaDoc replaceChild(Node JavaDoc newChild,
299                              Node JavaDoc oldChild)
300         throws DOMException JavaDoc {
301         if (!fChildrenExpanded) {
302             expandChildren();
303         }
304         return super.replaceChild(newChild, oldChild);
305     }
306
307     /**
308      * @see org.w3c.dom.Node#removeChild
309      */

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

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

332     public boolean hasChildNodes() {
333         if (!fChildrenExpanded) {
334             return fTemplateNode.hasChildNodes();
335         } else {
336             return super.hasChildNodes();
337         }
338     }
339     
340     /**
341      * @see org.w3c.dom.Node#normalize
342      */

343     public void normalize() {
344         if (!fChildrenExpanded) {
345             expandChildren();
346         }
347         super.normalize();
348     }
349
350 }
351
Popular Tags