KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyDOMTraversal.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.xml.dom.DOMOps;
27 import org.enhydra.xml.dom.DOMTraversal;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.DocumentType JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 // FIXME: might be able to speed this up if we could recognize the case where
34
// we have switch over to the template and can't switch back. For instance,
35
// attributes of a template element or contents of a doctype.
36

37 //FIXME: Maybe LazyDOMOps.access methods should be used here to provide one
38
// common method for safely traversing the tree.
39

40 /**
41  * Lazy DOM traversal class, calling handler methods for each node based on if
42  * they are expanded or template nodes. This only works if the traversal
43  * starts with a node of the instance LazyDocument. Template LazyDocuments
44  * will not work with this class and should be traversed normally.
45  */

46 public class LazyDOMTraversal extends DOMTraversal {
47     /** Lazy document being traversed */
48     private LazyDocument fLazyDoc;
49
50     /**
51      * Constructor.
52      * @param handler The object that will be called to handle each
53      * node.
54      * @param options Bit set of the option flags.
55      */

56     public LazyDOMTraversal(DOMTraversal.Handler handler,
57                             int options) {
58         super(handler, options);
59     }
60     
61     /**
62      * Default constructor, set handler later.
63      */

64     public LazyDOMTraversal(int options) {
65         super(options);
66     }
67
68     /**
69      * Traverse a DOM tree or subtree.
70      *
71      * @param root The root of the DOM tree or subtree that is to
72      * be traversed.
73      */

74     public void traverse(Node JavaDoc root) {
75         // Don't let XMLObject cause confusion
76
Node JavaDoc lazyRoot = DOMOps.getActualNode(root);
77         fLazyDoc = (LazyDocument)lazyRoot.getOwnerDocument();
78         if (fLazyDoc == null) {
79             fLazyDoc = (LazyDocument)lazyRoot;
80         }
81         if (fLazyDoc.isTemplateNode()) {
82             throw new LazyDOMError("LazyDOMTraversal used on template document");
83         }
84
85         fDepth = 0;
86         fProcessingAttribute = false;
87         processNode(lazyRoot);
88     }
89
90     /**
91      * Process the children of a node. This overrides the DOMTraversal method
92      * and handles switching from instance to template nodes.
93      */

94     public void processChildren(Node JavaDoc node) {
95         if (node instanceof LazyParent) {
96             LazyParent lazyNode = (LazyParent)node;
97             if (lazyNode.isTemplateNode()) {
98                 // templates may switch back to instance in processNode()
99
super.processChildren(lazyNode);
100             } else if (lazyNode.areChildrenExpanded()) {
101                 // expanded instance children, handle as normal
102
super.processChildren(lazyNode);
103             } else {
104                 // switch to template
105
super.processChildren(lazyNode.getTemplateNode());
106             }
107         }
108     }
109
110     /**
111      * Process the attributes of an element. This overrides the DOMTraversal
112      * method and handles switching from instance to template nodes.
113      */

114     public void processAttributes(Element JavaDoc element) {
115         LazyElement lazyElement = (LazyElement)element;
116         if (lazyElement.isTemplateNode()) {
117             // attribute are all template if node is template
118
super.processAttributes(lazyElement);
119         } else if (lazyElement.areAttributesExpanded()) {
120             // expanded instance attributes, handle as normal
121
super.processAttributes(lazyElement);
122         } else {
123             // switch to template
124
super.processAttributes(lazyElement.getTemplateElement());
125         }
126     }
127
128     /**
129      * Process a DocumentType attribute of a Document node, if it exists. This
130      * overrides the DOMTraversal method and handles switching from instance
131      * to template nodes.
132      */

133     public void processDocumentType(Document JavaDoc document) {
134         LazyDocument lazyDoc = (LazyDocument)document;
135         if (lazyDoc.isTemplateNode()) {
136             // if document is template, DocumentType is template
137
super.processDocumentType(document);
138         } else if (lazyDoc.isDocTypeExpanded()) {
139             // expanded instance DocumentType, handle as normal
140
super.processDocumentType(document);
141         } else {
142             // switch to template
143
super.processDocumentType(lazyDoc.getTemplateDocument());
144         }
145     }
146
147     /**
148      * Process the contents of a DocumentType node. This overrides the
149      * DOMTraversal method and handles switching from instance to template
150      * nodes.
151      */

152     public void processDocumentTypeContents(DocumentType JavaDoc documentType) {
153         LazyDocumentType lazyDocType = (LazyDocumentType)documentType;
154         if (lazyDocType.isTemplateNode()) {
155             // contents are all template if doctype is template
156
super.processDocumentTypeContents(lazyDocType);
157         } else if (lazyDocType.isContentsExpanded()) {
158             // expanded instance content, handle as normal
159
super.processDocumentTypeContents(lazyDocType);
160         } else {
161             // switch to template
162
super.processDocumentTypeContents(lazyDocType.getTemplateDocumentType());
163         }
164     }
165
166     /**
167      * Processing based on node type. All nodes go through here. This
168      * overrides the DOMTraversal method and handles switching from template
169      * back to instance nodes.
170      */

171     protected void processNode(Node JavaDoc node) {
172         LazyNode lazyNode = (LazyNode)node;
173         if (lazyNode.isTemplateNode()) {
174             LazyNode instanceNode = fLazyDoc.getExpandedNode(lazyNode.getNodeId());
175             if (instanceNode != null) {
176                 // switch back from template to instance
177
super.processNode(instanceNode);
178             } else {
179                 // not expanded, just process template
180
super.processNode(lazyNode);
181             }
182         } else {
183             // process instance node
184
super.processNode(lazyNode);
185         }
186     }
187 }
188
Popular Tags