KickJava   Java API By Example, From Geeks To Geeks.

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


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: TemplateDOM.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.SimpleDOMTraversal;
27 import org.w3c.dom.Node JavaDoc;
28
29 //FIXME: need to add support for defaultAttrs and elementDefinitons
30

31 /**
32  * Class used to contain hold a Template DOM that is shared by all instance
33  * of the document. This assign node ids to all nodes in the template,
34  * mark them as template nodes and makes them read-only.
35  */

36 public final class TemplateDOM {
37     /** The template document, marked readonly */
38     private LazyDocument fTemplateDocument;
39
40     /** The maximum node id in the document */
41     private int fMaxNodeId = LazyNode.NULL_NODE_ID;
42
43     /** Table, indexed by node id, of nodes in the template doc */
44     private LazyNode[] fTemplateNodes;
45
46     /**
47      * Constructor.
48      */

49     public TemplateDOM(LazyDocument templateDocument) {
50         fTemplateDocument = templateDocument;
51         // Only assigned ids if they don't already exist.
52
if (fTemplateDocument.getNodeId() == LazyNode.NULL_NODE_ID) {
53             assignNodeIds();
54         } else {
55             findMaxNodeId();
56         }
57         initNodeTable();
58         templateDocument.setReadOnly(true, true);
59     }
60
61     /**
62      * Assign node ids to all nodes.
63      */

64     private void assignNodeIds() {
65         fMaxNodeId = LazyNode.DOCUMENT_NODE_ID;
66         SimpleDOMTraversal traverser
67             = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() {
68                     public void handleNode(Node JavaDoc node) {
69                         ((LazyNode)node).makeTemplateNode(fMaxNodeId++);
70                     }
71                 });
72         traverser.traverse(fTemplateDocument);
73     }
74     
75     /**
76      * Find the maximum node id; used when ids are already assigned
77      */

78     private void findMaxNodeId() {
79         SimpleDOMTraversal traverser
80             = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() {
81                     public void handleNode(Node JavaDoc node) {
82                         int nodeId = ((LazyNode)node).getNodeId();
83                         if (nodeId > fMaxNodeId) {
84                             fMaxNodeId = nodeId;
85                         }
86                     }
87                 });
88         traverser.traverse(fTemplateDocument);
89     }
90     
91     /**
92      * Initialize the node table.
93      */

94     private void initNodeTable() {
95         fTemplateNodes = new LazyNode[fMaxNodeId+1];
96
97         SimpleDOMTraversal traverser
98             = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() {
99                     public void handleNode(Node JavaDoc node) {
100                         LazyNode lazyNode = (LazyNode)node;
101                         fTemplateNodes[lazyNode.getNodeId()] = lazyNode;
102                     }
103                 });
104         traverser.traverse(fTemplateDocument);
105     }
106
107     /**
108      * Get the maximum node id.
109      */

110     public final int getMaxNodeId() {
111         return fMaxNodeId;
112     }
113
114     /**
115      * Get a node, given an id.
116      * @param nodeId A valid node id for this document, or NULL_NODE_ID.
117      * @return The template node, or null if NULL_NODE_ID was specified.
118      */

119     public final LazyNode getNode(int nodeId) {
120         if (nodeId == LazyNode.NULL_NODE_ID) {
121             return null;
122         } else {
123             return fTemplateNodes[nodeId];
124         }
125     }
126 }
127
Popular Tags