KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 /**
27  * This class implements the model node defined in the W3C Recommendation
28  * on 16 Nov, 1999 <A HREF="http://www.w3.org/TR/1999/REC-xpath-19991116">
29  * XML Path Language (XPath) Version 1.0</A>
30  * <p><B>Only the subset of the recommendation useful for indexing
31  * metatada model is used.</B> e.g. No string-value is considered for the nodes.</p>
32  */

33 public class XNode implements Cloneable JavaDoc
34 {
35     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37
38     /** The two following attributes make up the "expanded localName" defined in the
39      * recommendation.
40      */

41     private String JavaDoc namespace = null;
42     // used also as target for Processing Instructions
43
private String JavaDoc localName = null;
44
45     /** the node type */
46     protected byte type = NodeKind.NODE;
47
48     /**
49      * Default constructor.
50      */

51     public XNode()
52     {}
53
54     /**
55      * Constructor used for element and attributes nodes (when XNode is used as
56      * a step node test). Type is the default value: NODE.
57      * @param namespace namespace URI of the node.
58      * @param localName local name of the node
59      */

60     public XNode(String JavaDoc namespace, String JavaDoc localName)
61     {
62         this(namespace, localName, NodeKind.NODE);
63     }
64
65     /**
66      * Constructor used for root, comment & text nodes
67      * @param the node type as a constant defined in {@link Type}.
68      */

69     public XNode(byte type)
70     {
71         this(null, null, type);
72     }
73
74     /**
75      * Constructor used for Processing instructions nodes.
76      * @param target processing instruction target.
77      */

78     public XNode(String JavaDoc target)
79     {
80         this(target, null, NodeKind.PI);
81     }
82
83     /**
84      * Constructor used for elements, attributes & namespaces nodes
85      * @param namespace namespace URI of the node.
86      * @param localName local name of the node
87      * @param the node type as a constant defined in {@link Type}.
88      */

89     public XNode(String JavaDoc namespace, String JavaDoc localName, byte type)
90     {
91         set(namespace, localName, type);
92     }
93
94     /**
95      * Setter used for qualified elements, attributes & namespaces nodes
96      * @param namespace namespace URI of the node.
97      * @param localName local name of the node
98      * @param the node type as a constant defined in {@link Type}.
99      */

100     public void set(String JavaDoc namespace, String JavaDoc localName, byte type)
101     {
102         if ((namespace == null) || (namespace.length() == 0))
103             this.namespace = null;
104         else
105             this.namespace = namespace;
106         this.localName = localName;
107         set(type);
108     }
109
110     /**
111      * Setter for node type.
112      * @param the node type as a constant defined in {@link Type}.
113      */

114     public void set(byte type)
115     {
116         this.type = type;
117     }
118
119     /**
120      * Parse an abbreviated string representation for attributes & elements
121      * only. Syntax is of the form : '@'? ('{' namespace '}')? localName
122      * @param exp the node abbreviated string representation.
123      */

124     public void set(String JavaDoc exp)
125     {
126         int i, j = 0;
127
128         // string step parsing
129
i = 0;
130         if (exp.charAt(i) == '@')
131         {
132             i++;
133             type = NodeKind.ATTRIBUTE;
134         }
135         else
136             type = NodeKind.ELEMENT;
137         if (exp.charAt(i) == '{')
138         {
139             j = exp.indexOf('}');
140             namespace = exp.substring(i + 1, j);
141             i = j + 1;
142         }
143         else
144             namespace = null;
145
146         localName = exp.substring(i);
147     }
148
149     /**
150      * Accessor to the namespace uri.
151      * @return the namespace uri of the node.
152      */

153     public String JavaDoc getNamespace()
154     {
155         return namespace;
156     }
157
158     /**
159      * Accessor to the local name.
160      * @return the local name of the node.
161      */

162     public final String JavaDoc getLocalName()
163     {
164         return localName;
165     }
166
167     /**
168      * Returns the canonical "expanded" name of this step. The returned string
169      * matches the following formatting rules:
170      * <code>{namespace}localName</code> if the namespace is not null and
171      * <code>localName</code> if the namespace is null.
172      * @return the expanded name of the node.
173      */

174     public final String JavaDoc getExpandedName()
175     {
176         if ((namespace == null) || namespace.equals(""))
177             return localName;
178         else
179             return '{' + namespace + '}' + localName;
180     }
181
182     /**
183      * Accessor to the node type.
184      * @return the type of the node.
185      * @see Type
186      */

187     public byte getType()
188     {
189         return type;
190     }
191
192     /**
193      * Test XNodes for equality. XNode are considered equals if their names and
194      * types are equals.
195      * @param o the XNode to compare to this object.
196      */

197     public final boolean equals(Object JavaDoc o)
198     {
199         XNode node = null;
200         if (o instanceof XNode)
201             node = (XNode) o;
202         else
203             return false;
204         return (
205             ((getNamespace() == null) && (node.getNamespace() == null))
206                 || ((getNamespace() != null)
207                     && getNamespace().equals(node.getNamespace())))
208             && (((getLocalName() == null) && (node.getLocalName() == null))
209                 || ((getLocalName() != null)
210                     && getLocalName().equals(node.getLocalName())))
211             && (type == node.getType());
212     }
213
214     /**
215      * Return the node hash code.
216      * This method is overriden because {@link XTreeNode} use XNode
217      * as hash keys of the children map.
218      * @return the hash code for node.
219      */

220     public int hashCode()
221     {
222         // local name hashcode is enough because it is the more likely to vary.
223
if (localName != null)
224             return localName.hashCode();
225         else
226             return toString().hashCode();
227     }
228
229     /**
230      * Return a string representation of this node using the abbreviated
231      * syntax.
232      * @return the string representation of the node.
233      */

234     public String JavaDoc toString()
235     {
236         return getAbbreviatedSyntax();
237     }
238     
239     /**
240      * Return a string representation of this node using the abbreviated
241      * syntax.
242      * @return the string representation of the node.
243      */

244     public final String JavaDoc getAbbreviatedSyntax()
245     {
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
247
248         switch (type)
249         {
250             case NodeKind.NODE :
251                 sb.append("node()");
252                 break;
253
254             case NodeKind.COMMENT :
255                 sb.append("comment()");
256                 break;
257
258             case NodeKind.TEXT :
259                 sb.append("text()");
260                 break;
261
262             case NodeKind.PI :
263                 if (localName == null)
264                     sb.append("processing-instruction()");
265                 else
266                     sb.append("processing-instruction('" + localName + "')");
267                 break;
268
269             case NodeKind.ATTRIBUTE :
270                 sb.append("@" + getExpandedName());
271                 break;
272
273             case NodeKind.ELEMENT :
274                 sb.append(getExpandedName());
275                 break;
276         }
277         return sb.toString();
278     }
279
280     /**
281      * Whether this node matches the given step expression (used for
282      * navigation).
283      * @param step the XPath step to check for.
284      * @return true if this node matches the step.
285      */

286     boolean match(StepExpr step)
287     {
288         return step.match(this);
289     }
290
291     /**
292      * Clone the current object.
293      * @return a new XNode object with the same contents.
294      */

295     public Object JavaDoc clone()
296     {
297         try
298         {
299             return super.clone();
300         }
301         catch (CloneNotSupportedException JavaDoc e)
302         {
303             // sure that Object is cloneable !
304
return null;
305         }
306     }
307
308 }
309
Popular Tags