KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyDocumentType.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.DocumentTypeImpl;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.NamedNodeMap JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 /**
32  * Implementation of the DOM Attr that supports lazy instantiation of
33  * a template DOM.
34  */

35 public class LazyDocumentType extends DocumentTypeImpl implements LazyNode {
36     /**
37      * Constructor.
38      * @param ownerDoc The document that owns this node.
39      * @param template If not-null, get the parameters from this template.
40      * @param name Document type name.
41      * Will be ignored if template is not-null.
42      * @param publicId The public id.
43      * Will be ignored if template is not-null.
44      * @param systemId The system id.
45      * Will be ignored if template is not-null.
46      * @param internalSubset A string representation of the internal subset.
47      * Will be ignored if template is not-null.
48      */

49     public LazyDocumentType(LazyDocument ownerDoc,
50                             LazyDocumentType template,
51                             String JavaDoc name,
52                             String JavaDoc publicId,
53                             String JavaDoc systemId,
54                             String JavaDoc internalSubset) {
55         super(ownerDoc,
56               (template != null) ? template.getNodeName() : name,
57               (template != null) ? template.getPublicId() : publicId,
58               (template != null) ? template.getSystemId() : systemId);
59         if (template != null) {
60             fTemplateNode = template;
61             fNodeId = template.getNodeId();
62             setInternalSubset(template.getInternalSubset());
63         } else {
64             // No template, mark as expanded.
65
fContentsExpanded = true;
66             setInternalSubset(internalSubset);
67         }
68     }
69
70     /**
71      * Set the owner of the document. Use to solve chicken and egg
72      * problem with creating document with document type.
73      */

74     protected void setOwnerDocument(LazyDocument doc) {
75         super.setOwnerDocument(doc);
76     }
77
78     //-------------------------------------------------------------------------
79
// LazyDocumentType specific
80
//-------------------------------------------------------------------------
81

82     /**
83      * Template for this DocumentType.
84      */

85     private LazyDocumentType fTemplateNode = null;
86
87     /**
88      * Have the contained nodes been expanded
89      */

90     private boolean fContentsExpanded = false;
91
92     /**
93      * Get the template for this node.
94      * @see LazyNode#getTemplateNode
95      */

96     public LazyDocumentType getTemplateDocumentType() {
97         return fTemplateNode;
98     }
99
100     /**
101      * @see Node#cloneNode
102      */

103     public Node JavaDoc cloneNode(boolean deep) {
104         if (deep) {
105             // If children are copied, we must expand now.
106
if (!fContentsExpanded) {
107                 expandContents();
108             }
109         }
110         
111         // This does a clone(), must clean up all fields.
112
LazyDocumentType newDocumentType = (LazyDocumentType)super.cloneNode(deep);
113         newDocumentType.fNodeId = NULL_NODE_ID;
114         newDocumentType.fContentsExpanded = true;
115         return newDocumentType;
116     }
117
118     /**
119      * Is the contents expanded?
120      */

121     public boolean isContentsExpanded() {
122         return fContentsExpanded;
123     }
124
125     /**
126      * Do work of expanding the contents. Must be called synchronized
127      * on the document.
128      */

129     private void doExpandContents(LazyDocument doc) {
130         NamedNodeMap JavaDoc templateEntities = fTemplateNode.getEntities();
131         int numEntities = templateEntities.getLength();
132         NamedNodeMap JavaDoc entities = super.getEntities();
133         for (int idx = 0; idx < numEntities; idx++) {
134             entities.setNamedItem(((LazyNode)templateEntities.item(idx)).templateClone(doc));
135         }
136
137         NamedNodeMap JavaDoc templateNotations = fTemplateNode.getNotations();
138         int numNotations = templateNotations.getLength();
139         NamedNodeMap JavaDoc notations = super.getNotations();
140         for (int idx = 0; idx < numNotations; idx++) {
141             notations.setNamedItem(((LazyNode)templateNotations.item(idx)).templateClone(doc));
142         }
143
144         fContentsExpanded = true;
145     }
146
147     /*
148      * Expand the contents.
149      */

150     private void expandContents() {
151         LazyDocument doc = (LazyDocument)getOwnerDocument();
152         synchronized (doc) {
153             if (fContentsExpanded) {
154                 doExpandContents(doc);
155             }
156         }
157     }
158
159     /**
160      * @see org.w3c.dom.DocumentType#getEntities
161      */

162     public NamedNodeMap JavaDoc getEntities() {
163         if (!fContentsExpanded) {
164             expandContents();
165         }
166         return super.getEntities();
167     }
168
169     /**
170      * @see org.w3c.dom.DocumentType#getNotations
171      */

172     public NamedNodeMap JavaDoc getNotations() {
173         if (!fContentsExpanded) {
174             expandContents();
175         }
176         return super.getNotations();
177     }
178
179     //-------------------------------------------------------------------------
180
// LazyNode support
181
//-------------------------------------------------------------------------
182

183     /*
184      * Node id for this element.
185      */

186     private int fNodeId = NULL_NODE_ID;
187
188     /**
189      * Is this a template node?
190      */

191     private boolean fIsTemplateNode;
192
193     /*
194      * @see LazyNode#makeTemplateNode
195      */

196     public void makeTemplateNode(int nodeId) {
197         fNodeId = nodeId;
198         fIsTemplateNode = true;
199     }
200
201     /**
202      * @see LazyNode#getNodeId
203      */

204     public int getNodeId() {
205         return fNodeId;
206     }
207
208     /**
209      * @see LazyNode#isTemplateNode
210      */

211     public boolean isTemplateNode() {
212         return fIsTemplateNode;
213     }
214
215     /**
216      * @see LazyNode#getTemplateNode
217      */

218     public LazyNode getTemplateNode() {
219         return fTemplateNode;
220     }
221
222     /**
223      * @see LazyNode#templateClone
224      */

225     public LazyNode templateClone(Document JavaDoc ownerDocument) {
226         return new LazyDocumentType((LazyDocument)ownerDocument, this,
227                                     null, null, null, null);
228     }
229
230     /**
231      * Set the node value, invalidating the id. All node data is modified
232      * by this routine.
233      * @see org.w3c.dom.Node#setNodeValue
234      */

235     public void setNodeValue(String JavaDoc value) {
236         fNodeId = NULL_NODE_ID;
237         super.setNodeValue(value);
238     }
239
240 }
241
Popular Tags