KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > dom > DOMAccess


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

23
24 package org.enhydra.xml.dom;
25
26 import org.enhydra.xml.lazydom.LazyAttr;
27 import org.enhydra.xml.lazydom.LazyDocument;
28 import org.enhydra.xml.lazydom.LazyElement;
29 import org.enhydra.xml.lazydom.LazyNode;
30 import org.enhydra.xml.lazydom.LazyParent;
31 import org.w3c.dom.Attr JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.DocumentType JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 // FIXME: Some the the access funtionallity might be better implemented
38
// in the nodes themselves. Maybe this just belongs in the Document
39
// although they would only be valid in the template document.
40

41 //FIXME: most of this not really tested, don';t export until tests
42
// are written.
43

44 /**
45  * Method to support accessing the LazyDOM without expanding it. This works
46  * both with standard DOMs and LazyDOMs, supporting the development of code
47  * that works with either DOM.
48  * <p>
49  * Most methods come in two flavors, static ones that take the instance
50  * document as an argument, an instance methods that use a document contained
51  * in an instance of this object. Great care should be used if the return
52  * nodes are to be modified, as read-only template nodes maybe returned.
53  * These methods also operate on any W3C DOM.
54  */

55 public final class DOMAccess {
56     /**
57      * Document being accessed.
58      */

59     private Document fDocument;
60
61     /**
62      * Create a object to access a specific document.
63      *
64      * @param document The document to access. if this is a LazyDOM instance
65      * document, access will switch between the instance and template
66      * as appropriate.
67      */

68     public DOMAccess(Document document) {
69         fDocument = document;
70     }
71
72     /**
73      * Access the document type node.
74      * @param document The instance document object.
75      * @return The document type object, or null if there is none.
76      */

77     public static DocumentType JavaDoc accessDocumentType(Document document) {
78         if (document instanceof LazyDocument) {
79             LazyDocument lazyDoc = (LazyDocument)document;
80             if (lazyDoc.isTemplateNode() || lazyDoc.isDocTypeExpanded()) {
81                 return lazyDoc.getDoctype();
82             } else {
83                 return lazyDoc.getTemplateDocument().getDoctype();
84             }
85         } else {
86             return document.getDoctype();
87         }
88     }
89
90     /**
91      * Access the document type node.
92      * @return The document type object, or null if there is none.
93      */

94     public DocumentType JavaDoc accessDocumentType() {
95         return accessDocumentType(fDocument);
96     }
97
98     /**
99      * Access first child of template node, switching to instance document
100      * if child is expanded.
101      */

102     private static Node accessTemplateFirstChild(LazyDocument lazyDoc,
103                                                  LazyParent lazyParent) {
104         LazyNode templateChild = (LazyNode)lazyParent.getFirstChild();
105         if (templateChild == null) {
106             return null; // no child
107
} else if (lazyDoc.isTemplateNode()) {
108             return templateChild; // Document is a template, not instance
109
} else {
110             LazyNode instanceChild = lazyDoc.getExpandedNode(templateChild.getNodeId());
111             if (instanceChild != null) {
112                 return instanceChild;
113             } else {
114                 return templateChild;
115             }
116         }
117     }
118     
119     /**
120      * Access the first child of a node.
121      *
122      * @param document The instance document object.
123      * @param parent The parent node of the desired child.
124      * @return The first child or null if there is none.
125      */

126     public static Node accessFirstChild(Document document,
127                                         Node parent) {
128         if (parent instanceof LazyParent) {
129             LazyParent lazyParent = (LazyParent)parent;
130             if (lazyParent.isTemplateNode()) {
131                 // parent is template: check if child is expanded
132
return accessTemplateFirstChild((LazyDocument)document,
133                                                 lazyParent);
134             } else if (lazyParent.areChildrenExpanded()) {
135                 // expanded instance children, handle as normal
136
return lazyParent.getFirstChild();
137             } else {
138                 // switch to template
139
return lazyParent.getTemplateNode().getFirstChild();
140             }
141         } else {
142             // standard DOM or LazyDOM node that is not a LazyParent
143
return parent.getFirstChild();
144         }
145     }
146
147     /**
148      * Access the first child of a node.
149      *
150      * @param parent The parent node of the desired child.
151      * @return The first child or null if there is none.
152      */

153     public Node accessFirstChild(Node parent) {
154         return accessFirstChild(fDocument, parent);
155     }
156
157     /**
158      * Access next sibling child of template node, switching to instance
159      * document if sibling is expanded.
160      */

161     private static Node accessTemplateNextSibling(LazyDocument lazyDoc,
162                                                   LazyNode templateNode) {
163         LazyNode templateSibling = (LazyNode)templateNode.getNextSibling();
164         if (templateSibling == null) {
165             return null; // no sibling
166
} else {
167             LazyNode instanceSibling = lazyDoc.getExpandedNode(templateSibling.getNodeId());
168             if (instanceSibling != null) {
169                 return instanceSibling;
170             } else {
171                 return templateSibling;
172             }
173         }
174     }
175     
176     /**
177      * Access next sibling child of instance node, switching to template
178      * document if sibling is not expanded.
179      */

180     private static Node accessInstanceNextSibling(LazyDocument lazyDoc,
181                                                   LazyNode instanceNode) {
182         if (instanceNode instanceof LazyParent) {
183             // Node is a LazyParent, sibling will be expanded if parent is
184
// expanded. It may or may not be expanded if parent is not
185
// expanded.
186
if (((LazyParent)instanceNode).isParentExpanded()) {
187                 return instanceNode.getNextSibling();
188             } else {
189                 // Check if sibling is expanded is expanded
190
LazyNode templateNode = instanceNode.getTemplateNode();
191                 LazyNode templateSibling = (LazyNode)instanceNode.getNextSibling();
192                 if (templateSibling != null) {
193                     LazyNode instanceSibling = lazyDoc.getExpandedNode(templateSibling.getNodeId());
194                     if (instanceSibling != null) {
195                         return instanceSibling;
196                     } else {
197                         return templateSibling;
198                     }
199                 } else {
200                     return null; // No sibling
201
}
202             }
203         } else {
204             // Node is not a LazyParent, sibling must be expanded
205
return instanceNode.getNextSibling();
206         }
207     }
208
209     /**
210      * Access the next sibling of a node.
211      *
212      * @param document The instance document object.
213      * @param node Get the next sibling of this node
214      * @return The first child or null if there is none.
215      */

216     public static Node accessNextSibling(Document document,
217                                          Node node) {
218         if (node instanceof LazyNode) {
219             LazyNode lazyNode = (LazyNode)node;
220             if (lazyNode.isTemplateNode()) {
221                 // node is template: check if sibling is expanded
222
return accessTemplateNextSibling((LazyDocument)document,
223                                                  lazyNode);
224             } else {
225                 return accessInstanceNextSibling((LazyDocument)document,
226                                                  lazyNode);
227             }
228         } else {
229             // standard DOM
230
return node.getNextSibling();
231         }
232     }
233
234     /**
235      * Access the next sibling of a node.
236      *
237      * @param node Get the next sibling of this node
238      * @return The first child or null if there is none.
239      */

240     public Node accessNextSibling(Node node) {
241         return accessNextSibling(fDocument, node);
242     }
243
244     /**
245      * Access the document element of a document.
246      *
247      * @param document The instance document object.
248      * @return The document element.
249      */

250     public static Element accessDocumentElement(Document document) {
251         if (document instanceof LazyDocument) {
252             Node child = accessFirstChild(document, document);
253             while (child != null) {
254                 if (child instanceof Element) {
255                     break;
256                 }
257                 child = accessNextSibling(document, child);
258             }
259             return (Element)child;
260         } else {
261             return document.getDocumentElement();
262         }
263     }
264
265     /**
266      * Access the document element of the document.
267      *
268      * @return The document element.
269      */

270     public Element accessDocumentElement() {
271         return accessDocumentElement(fDocument);
272     }
273
274     /**
275      * Access an attribute of an Element.
276      *
277      * @param document The instance document object.
278      * @param element The Element node.
279      * @param namespaceURI The namespace URI, or null if there is none.
280      * @param name The name of the attribute.
281      * @return The attribute node, or null if it does not exist. The value
282      * should be accessed via accessAttributeValue().
283      */

284     public static Attr accessAttribute(Document document,
285                                        Element element,
286                                        String JavaDoc namespaceURI,
287                                        String JavaDoc name) {
288         // Find the element (template or instance) to use to obtain the
289
// attribute
290
Element accessElement;
291         if (element instanceof LazyElement) {
292             LazyElement lazyElement = (LazyElement)element;
293             if (lazyElement.areAttributesExpanded()) {
294                 // template or expanded instance, just get attribute
295
accessElement = lazyElement;
296             } else {
297                 // instance and attributes not expanded, get from template
298
accessElement = lazyElement.getTemplateElement();
299             }
300         } else {
301             // not a Lazy DOM, just get attribute
302
accessElement = element;
303         }
304
305         // Get with or without namespace
306
if (namespaceURI != null) {
307             return accessElement.getAttributeNodeNS(namespaceURI, name);
308         } else {
309             return accessElement.getAttributeNode(name);
310         }
311     }
312
313     /**
314      * Access an attribute of an Element.
315      *
316      * @param element The Element node.
317      * @param namespaceURI The namespace URI, or null if there is none.
318      * @param name The name of the attribute.
319      * @return The attribute node, or null if it does not exist. The value
320      * should be accessed via accessAttributeValue().
321      */

322     public Attr accessAttribute(Element element,
323                                 String JavaDoc namespaceURI,
324                                 String JavaDoc name) {
325         return accessAttribute(fDocument, element, namespaceURI, name);
326     }
327
328     /**
329      * Access the value of an attribute
330      *
331      * @param document The instance document object.
332      * @param attr The Attr node.
333      * @return The value of the attribute, as a string.
334      */

335     public static String JavaDoc accessAttributeValue(Document document,
336                                               Attr attr) {
337         if (attr instanceof LazyAttr) {
338             LazyAttr lazyAttr = (LazyAttr)attr;
339             if (lazyAttr.areChildrenExpanded()) {
340                 // template or expanded instance, just get value
341
return lazyAttr.getValue();
342             } else {
343                 // instance and children not expanded, get from template
344
return ((LazyAttr)lazyAttr.getTemplateNode()).getValue();
345             }
346         } else {
347             // not a Lazy DOM, just get the value
348
return attr.getValue();
349         }
350     }
351
352     /**
353      * Access the value of an attribute
354      *
355      * @param attr The Attr node.
356      * @return The value of the attribute, as a string.
357      */

358     public String JavaDoc accessAttributeValue(Attr attr) {
359         return accessAttributeValue(fDocument, attr);
360     }
361
362     /**
363      * If a Node is a LazyDOM template node, expand it.
364      *
365      * @param document The instance document object.
366      * @param node A node of the document or it's template.
367      * @return The expanded node, or the node unchanged
368      * if it is not a LazyDOM template.
369      */

370     public static Node getExpandedNode(Document document,
371                                        Node node) {
372         if ((node instanceof LazyNode) && ((LazyNode)node).isTemplateNode()) {
373             return (Node)((LazyDocument)document).getNodeById(((LazyNode)node).getNodeId());
374         } else {
375             return node;
376         }
377     }
378
379     /**
380      * If a Node is a LazyDOM template node, expand it.
381      *
382      * @param node A node of the document or it's template.
383      * @return The expanded node, or the node unchanged
384      * if it is not a LazyDOM template.
385      */

386     public Node getExpandedNode(Node node) {
387         return getExpandedNode(fDocument, node);
388     }
389
390     /**
391      * If an Element node is a LazyDOM template, expand it.
392      * This is a special case of <tt>getExpandedNode()</tt>.
393      *
394      * @param document The instance document object.
395      * @param element An element of the document or it's template.
396      * @return The expanded element, or the element unchanged
397      * if it is not a LazyDOM template.
398      */

399     public static Element getExpandedElement(Document document,
400                                              Element element) {
401         if ((element instanceof LazyElement) && ((LazyElement)element).isTemplateNode()) {
402             return (Element)((LazyDocument)document).getNodeById(((LazyElement)element).getNodeId());
403         } else {
404             return element;
405         }
406     }
407
408     /**
409      * If an Element node is a LazyDOM template, expand it
410      * This is a special case of <tt>getExpandedNode()</tt>.
411      *
412      * @param element An element of the document or template.
413      * @return The expanded element, or the element unchanged
414      * if it is not a LazyDOM template.
415      */

416     public Element getExpandedElement(Element element) {
417         return getExpandedElement(fDocument, element);
418     }
419
420 }
421
Popular Tags