KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26
27 /**
28  * This tree structure implements the XPath data model.
29  *
30  * Each <code>Node</code> in the <code>Tree</code> is identified by a
31  * <code>org.xquark.xpath.LocationExpression</code> representing the full
32  * access path.
33  */

34 public class XTree
35 {
36     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     protected XTreeNode root;
40
41     protected HashMap locationNodeMap = new HashMap();
42
43     /**
44      * Accessor to the tree root node.
45      * @return an XTreeNode of type ROOT
46      * @see Type
47      */

48     public XTreeNode getRoot()
49     {
50         return root;
51     }
52
53     /**
54      * Setter for the tree root node.
55      * @param root an XTreeNode of type ROOT
56      * @see Type
57      */

58     void setRoot(XTreeNode root)
59     {
60         this.root = root;
61     }
62
63     /**
64      * Remove a branch from the tree.
65      * @param location a location expression identifying the root node of the
66      * subtree to prune.
67      */

68     public void remove(PathExpr location)
69     {
70         throw new UnsupportedOperationException JavaDoc("Not implemented yet.");
71     }
72
73     /**
74      * Returns the node corresponding to the path location passed as a
75      * parameter.
76      *
77      * <p><B>The path expression must not contains wildcards.</B> Once you
78      * get an XTreeNode (you can also use getRoot), use navigate(Step step)</p>
79      * @param location the location expression used for node retrieval.
80      */

81     public XTreeNode getNode(PathExpr location)
82     {
83         int hash = location.hashCode();
84         return (XTreeNode) locationNodeMap.get(location);
85     }
86
87     /**
88      * Returns the nodes corresponding to an odd path location.
89      * @param location the location expression used for nodes search.
90      */

91     public Collection getNodeSet(ArrayList location)
92     {
93         return prune(location);
94     }
95
96     /**
97      * Compute the LocationExpressions matching given regular location
98      * expression.
99      * @param location the location expression used for nodes search.
100      * @return a collection of XTreeNode objects
101      */

102     public final Collection prune(ArrayList location)
103     {
104         return root.prune(location);
105     }
106
107     /**
108      * Compute the LocationExpressions matching given regular location
109      * expression building a collection of collections in order to
110      * regroup same level (for using in PathResolver and next for typing))
111      * @param location a location expression
112      * @return a collection collection of collections containing XTreeNode
113      * objects matching the regular expression. First collection are results
114      * for the level of the root node.
115      */

116     public final Collection pruneGroup(ArrayList location)
117     {
118         return root.pruneGroup(location);
119     }
120
121     /**
122      * Returns an iterator browsing the tree using a depth-first algorithm.
123      * @return a standard java iterator.
124      */

125     public Iterator iterator()
126     {
127         return new XTreeIterator(this);
128     }
129
130     /**
131      * Add an XTree node to the tree index. Should be called for every node
132      * added to the tree
133      * @param node the new attached node.
134      * @return XTreeNode the new attached node.
135      */

136     public XTreeNode register(XTreeNode node)
137     {
138         PathExpr path = node.getLocation();
139         if (path != null)
140             locationNodeMap.put(path, node);
141         return node;
142     }
143
144     /**
145      * Return a string representation of this tree.
146      * @return the string representation of the tree.
147      */

148     public String JavaDoc toString()
149     {
150         return getRoot().toString(0);
151     }
152 }
153
Popular Tags