KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.enhydra.xml.lazydom;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import org.enhydra.xml.dom.SimpleDOMTraversal;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.DocumentType JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 /**
35  * Class to assign and map node ids to a node. Use in the construction of
36  * template DOMs.
37  */

38 public class NodeIdMap {
39     /**
40      * Map of node to node id.
41      */

42     private HashMap JavaDoc fNodeIdMap = new HashMap JavaDoc();
43     
44     /**
45      * Map of node id to node. Used to assign node ids.
46      */

47     private ArrayList JavaDoc fIdNodeMap = new ArrayList JavaDoc();
48
49     /**
50      * Constructor.
51      */

52     public NodeIdMap(Document JavaDoc document) {
53         buildNodeIdMap(document);
54     }
55
56     /**
57      * Traverse document and assign node ids.
58      */

59     private void buildNodeIdMap(Document JavaDoc document) {
60         SimpleDOMTraversal traverser
61             = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() {
62                     public void handleNode(Node JavaDoc node) {
63                         put(node);
64                     }
65                 });
66         traverser.traverse(document);
67         if (fIdNodeMap.get(LazyNode.DOCUMENT_NODE_ID) != document) {
68            throw new LazyDOMError("document assigned wrong id");
69         }
70     }
71
72     /**
73      * Add a node to the map, assigning it id.
74      */

75     private void put(Node JavaDoc node) {
76         int nodeId = fIdNodeMap.size();
77         fNodeIdMap.put(node, new Integer JavaDoc(nodeId));
78         fIdNodeMap.add(node);
79     }
80
81     /**
82      * Get a node, given its id.
83      */

84     public Node JavaDoc getNode(int id) {
85         return (Node JavaDoc)fIdNodeMap.get(id);
86     }
87
88     /**
89      * Get the node id for a node, generating an error if not found.
90      * If null is passed in, NULL_NODE_ID is returned.
91      */

92     public int getId(Node JavaDoc node) {
93         if (node == null) {
94             return LazyNode.NULL_NODE_ID;
95         }
96         Integer JavaDoc id = (Integer JavaDoc)fNodeIdMap.get(node);
97         if (id == null) {
98             throw new LazyDOMError("Id not found for node: " + node
99                                    + ": " + node.getClass());
100         }
101         return id.intValue();
102     }
103     
104     /**
105      * Get an id as a string.
106      */

107     public String JavaDoc getIdStr(Node JavaDoc node) {
108         return Integer.toString(getId(node));
109     }
110
111     /**
112      * Get the maximum node id assigned.
113      */

114     public int getMaxNodeId() {
115         return fIdNodeMap.size()-1;
116     }
117
118     /**
119      * Get the size of the node map.
120      */

121     public int size() {
122         return fIdNodeMap.size();
123     }
124
125     /**
126      * Get the document type node id.
127      */

128     public int getDocumentTypeNodeId(Node JavaDoc node) {
129         Document JavaDoc document = node.getOwnerDocument();
130         if (document == null) {
131             document = (Document JavaDoc)node;
132         }
133         DocumentType JavaDoc documentType = document.getDoctype();
134         if (documentType == null) {
135             return LazyNode.NULL_NODE_ID;
136         } else {
137             return getId(documentType);
138         }
139     }
140 }
141
Popular Tags