KickJava   Java API By Example, From Geeks To Geeks.

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


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: SimpleDOMTraversal.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.LazyDOMSimpleTraversal;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.DocumentType JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 //FIXME: Makes so it doesn't expand, but LazyDOM counts on this.
34

35 /**
36  * Simple DOM traverser that calls a handler for every node in the DOM tree.
37  */

38 public class SimpleDOMTraversal {
39     /**
40      * Interface for node callback object.
41      */

42     public interface Handler {
43         /**
44          * Handler called for each Node.
45          */

46         public void handleNode(Node JavaDoc node);
47     }
48
49     /** Handler object for the traversal */
50     protected Handler fHandler;
51
52     /**
53      * Constructor.
54      * @param handler The object that will be called to handle each
55      * node.
56      */

57     public SimpleDOMTraversal(SimpleDOMTraversal.Handler handler) {
58         fHandler = handler;
59     }
60
61     /**
62      * Traverse a DOM tree or subtree.
63      *
64      * @param root The root of the DOM tree or subtree that is to
65      * be traversed.
66      */

67     public void traverse(Node JavaDoc root) {
68         processNode(root);
69     }
70
71     /**
72      * Process the children of a node.
73      */

74     protected void processChildren(Node JavaDoc node) {
75         //FIXME: Xerces sometimes has DocumentType objects as children (bug?)
76
for (Node JavaDoc child = node.getFirstChild(); child != null;
77              child = child.getNextSibling()) {
78             if (!(child instanceof DocumentType JavaDoc)) {
79                 processNode(child);
80             }
81         }
82     }
83     
84     /**
85      * Process the attributes of an element.
86      */

87     protected void processAttributes(Node JavaDoc node) {
88         NamedNodeMap JavaDoc attrMap = node.getAttributes();
89         int len = attrMap.getLength();
90         for (int i = 0; i < len; i++) {
91             processNode(attrMap.item(i));
92         }
93     }
94
95     /**
96      * Process a DocumentType attribute of a Document node, if it exists.
97      */

98     public void processDocumentType(Document JavaDoc document) {
99         DocumentType JavaDoc docType = document.getDoctype();
100         if (docType != null) {
101             processNode(docType);
102         }
103     }
104
105     /**
106      * Process the contents of a DocumentType node,
107      */

108     protected void processDocumentTypeContents(DocumentType JavaDoc documentType) {
109         processNamedNodeMap(documentType.getEntities());
110         processNamedNodeMap(documentType.getNotations());
111     }
112
113     /**
114      * Process contents of a NamedNodeMap.
115      */

116     private void processNamedNodeMap(NamedNodeMap JavaDoc nodeMap) {
117         if (nodeMap != null) {
118             int len = nodeMap.getLength();
119             for (int i = 0; i < len; i++) {
120                 processNode(nodeMap.item(i));
121             }
122         }
123     }
124
125     /**
126      * Processing based on node type. All nodes go through here.
127      */

128     protected void processNode(Node JavaDoc node) {
129         fHandler.handleNode(node);
130         if (node instanceof Document JavaDoc) {
131             processDocumentType((Document JavaDoc)node);
132         } else if (node instanceof DocumentType JavaDoc) {
133             DocumentType JavaDoc docType = (DocumentType JavaDoc)node;
134             processNamedNodeMap(docType.getEntities());
135             processNamedNodeMap(docType.getNotations());
136         } else if (node instanceof Element JavaDoc) {
137             processAttributes(node);
138         }
139
140         processChildren(node);
141     }
142
143     /**
144      * Factory method to create a traverser based on the type of
145      * a document.
146      */

147     public static SimpleDOMTraversal getTraverser(SimpleDOMTraversal.Handler handler,
148                                                   Node JavaDoc node) {
149         /*
150          * Find the document to determine type. The LazyDOM traverser is only
151          * used on instance documents. If a template DOM node is specified,
152          * the template LazyDocument is traverse as any other done.
153          */

154         if (DOMOps.isLazyDOMInstance(DOMOps.getDocument(node))) {
155             return new LazyDOMSimpleTraversal(handler);
156         } else {
157             return new SimpleDOMTraversal(handler);
158         }
159     }
160 }
161
Popular Tags