KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xpath > XTreeBuilder


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xpath;
24
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * This class allows the construction of specific implementations of XTree.
29  *
30  * <p>Adding data to XTreeNode implementations is made possible by derivation
31  * of this object.</p>
32  * <p></B>Some methods in this base class may be overriden but they must absolutely
33  * be called by derived implementations because it performs registration.</p>
34  * <p>Methods in this base class are synchronized because there is only one
35  * instance of XTreeBuilder per tree so in case of thread shared building.</p>
36  *
37  */

38 public abstract class XTreeBuilder
39 {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42     
43     protected XTree tree = null;
44
45     protected XNode pattern = new XNode();
46
47     /**
48      * Default constructor.
49      */

50     public XTreeBuilder()
51     {}
52
53     /**
54      * Constructor for adding nodes to an existing tree.
55      * @param tree an existing tree to enrich.
56      */

57     public XTreeBuilder(XTree tree)
58     {
59         this.tree = tree;
60     }
61
62     /**
63      * XTree factory. Calls {@link #allocateTree()};
64      * @return a new tree or the builder's tree if already initialized.
65      */

66     public XTree createTree()
67     {
68         if (tree == null)
69         {
70             this.tree = allocateTree();
71             tree.setRoot(createNode(null, NodeKind.NONE));
72         }
73         return tree;
74     }
75
76     /**
77      * Accessor to the builder's tree.
78      * @return XTree
79      */

80     public XTree getTree()
81     {
82         return tree;
83     }
84     
85     /**
86      * Creates an instance of <code>XTreeNode</code> or of its sub-class
87      * for qualified element and attributes nodes.
88      * @param parent the parent node of the new node
89      * @param namespace the namespace uri of the new node.
90      * @param localName the local name of the new node.
91      * @param type the type of the new node
92      */

93     public synchronized XTreeNode createNamedNode(
94         XTreeNode parent,
95         String JavaDoc namespace,
96         String JavaDoc localName,
97         byte type)
98     {
99         return tree.register(allocateNode(parent, namespace, localName, type));
100     }
101
102     /**
103      * Creates an instance of <code>XTreeNode</code> or of its sub-class
104      * for unqualified element and attributes nodes.
105      * @param parent the parent node of the new node
106      * @param localName the local name of the new node.
107      * @param type the type of the new node
108      */

109     public synchronized XTreeNode createNamedNode(
110         XTreeNode parent,
111         String JavaDoc localName,
112         byte type)
113     {
114         return createNamedNode(parent, null, localName, type);
115     }
116
117     /**
118      * Creates an instance of <code>XTreeNode</code> or of its sub-class
119      * for unqualified element and attributes nodes if the node does not
120      * already exist.
121      * @param parent the parent node of the new node
122      * @param localName the local name of the new node.
123      * @param type the type of the new node
124      */

125     public synchronized XTreeNode createNamedNodeIfNotExist(
126         XTreeNode parent,
127         String JavaDoc localName,
128         byte type)
129     {
130         pattern.set(null, localName, type);
131         XTreeNode node = parent.getChild(pattern);
132         if (node == null)
133             return createNamedNode(parent, localName, type);
134         else
135             return node;
136     }
137
138     /**
139      * Creates an instance of <code>XTreeNode</code> or of its sub-class
140      * for qualified element and attributes nodes if the node does not
141      * already exist.
142      * @param parent the parent node of the new node
143      * @param namespace the namespace uri of the new node.
144      * @param localName the local name of the new node.
145      * @param type the type of the new node
146      */

147     public synchronized XTreeNode createNamedNodeIfNotExist(
148         XTreeNode parent,
149         String JavaDoc namespace,
150         String JavaDoc localName,
151         byte type)
152     {
153         pattern.set(namespace, localName, type);
154         XTreeNode node = parent.getChild(pattern);
155         if (node == null)
156             return createNamedNode(parent, namespace, localName, type);
157         else
158             return node;
159     }
160
161     /**
162      * Creates an instance of <code>XTreeNode</code> or of its sub-class
163      * for root, comment & text nodes.
164      * @param parent the parent node of the new node
165      * @param type ROOT, COMMENT or TEXT
166      */

167     public synchronized XTreeNode createNode(XTreeNode parent, byte type)
168     {
169         return tree.register(allocateNode(parent, null, null, type));
170     }
171
172     /**
173      * Creates an instance of <code>XTreeNode</code> or of its sub-class
174      * for Processing instructions nodes.
175      * @param parent the parent node of the new node
176      * @param target the PI target name or null
177      */

178     public synchronized XTreeNode createPI(XTreeNode parent, String JavaDoc target)
179     {
180         return tree.register(allocateNode(parent, target, null, NodeKind.PI));
181     }
182
183     /**
184      * Creates an instance of <code>XTreeNode</code> or of its sub-class
185      * for Processing instructions nodes if a node does not already exist
186      * for this target.
187      * @param parent the parent node of the new node
188      * @param target the PI target name or null
189      */

190     public synchronized XTreeNode createPIIfNotExist(
191         XTreeNode parent,
192         String JavaDoc target)
193     {
194         pattern.set(target);
195         XTreeNode node = parent.getChild(pattern);
196         if (node == null)
197             return createPI(parent, target);
198         else
199             return node;
200     }
201
202     /**
203      * XTree factory to implement. Implementation should create a new XTree
204      * derived from XTree base implementation and calling the "super"
205      * constructor.
206      * @return XTreeNode
207      */

208     public abstract XTree allocateTree();
209     
210     /**
211      * XTreeNode factory to implement. Implementation should create a new node
212      * derived from the base XTreeNode implementation and calling the "super"
213      * constructor that performs parent/child registration.
214      * @param parent parent node of the new node
215      * @param decl the XML schema declaration that will be used to create
216      * @return the new TypedXTreeNode (upcasted)
217      */

218     public abstract XTreeNode allocateNode(
219                                             XTreeNode parent,
220                                             String JavaDoc namespace,
221                                             String JavaDoc localName,
222                                             byte type
223                                             );
224
225     /**
226      * Creates if necessary all the nodes of a given location expression.
227      * Location expression is normalized during process.
228      * @param location any location expression
229      */

230     public void add(PathExpr location)
231     {
232         int index = 0;
233         StepExpr step;
234         XTreeNode parent = tree.getRoot();
235         //Iterator it = location.normalize().iterator();
236
Iterator JavaDoc it = location.getSteps().iterator();
237
238         while (it.hasNext())
239         {
240             step = (StepExpr) it.next();
241             parent =
242                 createNamedNodeIfNotExist(
243                     parent,
244                     step.getNameSpace(),
245                     step.getLocalName(),
246                     step.getTypeTest());
247         }
248     }
249
250     /**
251      * Create an homothetic XTree using a distinct builder.
252      * @param builder the buider for homothetic XTree
253      */

254     public void duplicate(XTreeBuilder builder)
255     {
256         tree.getRoot().duplicate(
257             builder.getTree().getRoot().customizeDuplicate(tree.getRoot()),
258             builder);
259     }
260
261 }
262
Popular Tags