KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyDOMSimpleTraversal.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.SimpleDOMTraversal;
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 /**
34  * Simple DOM traverser that calls a handler for every node in the DOM tree.
35  * Avoids expansion of LazyDOM tree.
36  */

37 public class LazyDOMSimpleTraversal extends SimpleDOMTraversal {
38     /** Lazy document being traversed */
39     private LazyDocument fLazyDoc;
40
41     /**
42      * Constructor.
43      * @param handler The object that will be called to handle each
44      * node.
45      */

46     public LazyDOMSimpleTraversal(SimpleDOMTraversal.Handler handler) {
47         super(handler);
48     }
49     
50     /**
51      * Traverse a DOM tree or subtree.
52      *
53      * @param root The root of the DOM tree or subtree that is to
54      * be traversed.
55      */

56     public void traverse(Node JavaDoc root) {
57         // Don't let XMLObject cause confusion
58
Node JavaDoc lazyRoot = DOMOps.getActualNode(root);
59         fLazyDoc = (LazyDocument)lazyRoot.getOwnerDocument();
60         if (fLazyDoc == null) {
61             fLazyDoc = (LazyDocument)lazyRoot;
62         }
63         if (fLazyDoc.isTemplateNode()) {
64             throw new LazyDOMError("LazyDOMSimpleTraversal used on template document");
65         }
66         processNode(lazyRoot);
67     }
68
69     /**
70      * Process the children of a node. This overrides the SimpleDOMTraversal
71      * method and handles switching from instance to template nodes.
72      */

73     public void processChildren(Node JavaDoc node) {
74         if (node instanceof LazyParent) {
75             LazyParent lazyNode = (LazyParent)node;
76             if (lazyNode.isTemplateNode()) {
77                 // templates may switch back to instance in processNode()
78
super.processChildren(lazyNode);
79             } else if (lazyNode.areChildrenExpanded()) {
80                 // expand instance children, handle as normal
81
super.processChildren(lazyNode);
82             } else {
83                 // switch to template
84
super.processChildren(lazyNode.getTemplateNode());
85             }
86         }
87     }
88
89     /**
90      * Process the attributes of an element. This overrides the
91      * SimpleDOMTraversal method and handles switching from instance to
92      * template nodes.
93      */

94     public void processAttributes(Element JavaDoc element) {
95         LazyElement lazyElement = (LazyElement)element;
96         if (lazyElement.isTemplateNode()) {
97             // attribute are all template if node is template
98
super.processAttributes(lazyElement);
99         } else if (lazyElement.areAttributesExpanded()) {
100             // expand instance attributes, handle as normal
101
super.processAttributes(lazyElement);
102         } else {
103             // switch to template
104
super.processAttributes(lazyElement.getTemplateElement());
105         }
106     }
107
108     /**
109      * Process a DocumentType attribute of a Document node, if it exists. This
110      * overrides the DOMTraversal method and handles switching from instance
111      * to template nodes.
112      */

113     public void processDocumentType(Document JavaDoc document) {
114         LazyDocument lazyDoc = (LazyDocument)document;
115         if (lazyDoc.isTemplateNode()) {
116             // if document is template, DocumentType is template
117
super.processDocumentType(document);
118         } else if (lazyDoc.isDocTypeExpanded()) {
119             // expanded instance document, handle as normal
120
super.processDocumentType(document);
121         } else {
122             // switch to template
123
super.processDocumentType(lazyDoc.getTemplateDocument());
124         }
125     }
126
127     /**
128      * Process the contents of a DocumentType node, This overrides the
129      * SimpleDOMTraversal method and handles switching from instance to
130      * template nodes.
131      */

132     public void processDocumentTypeContents(DocumentType JavaDoc documentType) {
133         LazyDocumentType lazyDocType = (LazyDocumentType)documentType;
134         if (lazyDocType.isTemplateNode()) {
135             // contents are all template if doctype is template
136
super.processDocumentTypeContents(lazyDocType);
137         } else if (lazyDocType.isContentsExpanded()) {
138             // expand instance content, handle as normal
139
super.processDocumentTypeContents(lazyDocType);
140         } else {
141             // switch to template
142
super.processDocumentTypeContents(lazyDocType.getTemplateDocumentType());
143         }
144     }
145
146     /**
147      * Processing based on node type. All nodes go through here. This
148      * overrides the DOMTraversal method and handles switching from template
149      * back to instance nodes.
150      */

151     protected void processNode(Node JavaDoc node) {
152         LazyNode lazyNode = (LazyNode)node;
153         if (lazyNode.isTemplateNode()) {
154             LazyNode instanceNode = fLazyDoc.getExpandedNode(lazyNode.getNodeId());
155             if (instanceNode != null) {
156                 // switch back from template to instance
157
super.processNode(instanceNode);
158             } else {
159                 // not expanded, just process template
160
super.processNode(lazyNode);
161             }
162         } else {
163             // process instance node
164
super.processNode(lazyNode);
165         }
166     }
167 }
168
Popular Tags