KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
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 org.xquark.schema.Type;
26
27 public class StepExprImpl implements StepExpr {
28
29     /**
30      *
31      */

32     private boolean hasSeparator = false;
33
34     /**
35      *
36      */

37     private byte axis = Axis.NONE;
38
39     /**
40      *
41      */

42     private String JavaDoc localName = null;
43
44     /**
45      *
46      */

47     private String JavaDoc nameSpace = null;
48
49     /**
50      *
51      */

52     private byte wild = StepExpr.NO_WILDCARD;
53
54     /**
55      *
56      */

57     private byte kindtest = NodeKind.NONE;
58
59     /**
60      *
61      */

62     private Type type = null;
63     /**
64      *
65      */

66     private boolean isTypeNillable = false;
67
68     public StepExprImpl(byte axis, String JavaDoc nameSpace, String JavaDoc localName) {
69         this.axis = axis;
70         this.nameSpace = nameSpace;
71         this.localName = localName;
72     }
73
74     /**
75      *
76      * @return true if there is a separator in front of the step
77      */

78     public boolean hasSeparator() {
79         return hasSeparator;
80     }
81
82     /**
83      *
84      * @return the constant representing the axis
85      */

86     public byte getAxis() {
87         return axis;
88     }
89
90     /**
91      *
92      * @return the namespace if any null otherwise
93      */

94     public String JavaDoc getNameSpace() {
95         return nameSpace;
96     }
97
98     /**
99      *
100      * @return the localname if any null otherwise
101      */

102     public String JavaDoc getLocalName() {
103         return localName;
104     }
105
106     /**
107      *
108      * @return the constant representing the wilcard
109      */

110     public byte getWildCardType() {
111         return wild;
112     }
113
114     /**
115      *
116      * @return false if no wildcard is present
117      */

118     public boolean hasWildcard() {
119         return wild != StepExpr.NO_WILDCARD;
120     }
121
122     /**
123      *
124      * @return true if step if *
125      */

126     public boolean isWildcard() {
127         return wild == StepExpr.WILDCARD;
128     }
129
130     /**
131      *
132      * @return true if step is qname with * as namespace
133      */

134     public boolean isNameSpaceWildcard() {
135         return wild == StepExpr.NAMESPACE_WILDCARD;
136     }
137
138     /**
139      *
140      * @return true if step is qname with * as localname
141      */

142     public boolean isLocalNameWildcard() {
143         return wild == StepExpr.LOCALNAME_WILDCARD;
144     }
145
146     /**
147      *
148      * @return the constant representing the kind of the step
149      */

150     public byte getKindTest() {
151         return kindtest;
152     }
153
154     /**
155      *
156      * @return the (schema) type used in element or attribute test
157      */

158     public org.xquark.schema.Type getType() {
159         return type;
160     }
161
162     /**
163      *
164      * @return true if the type used in element test is nillable
165      */

166     public boolean isTypeNillable() {
167         return isTypeNillable;
168     }
169
170     /**
171      * @return the string value of the step
172      */

173     public String JavaDoc toHashCodeString() {
174         return toString();
175     }
176     public String JavaDoc toString() {
177         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
178         if (axis != Axis.NONE)
179             buf.append('/');
180         buf.append(Axis.AXISSTRINGS[axis]);
181         switch (kindtest) {
182             case NodeKind.ATTRIBUTE :
183 // buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
184
// buf.append('(');
185
if (isWildcard())
186                     buf.append('*');
187                 else if (localName != null) {
188 // buf.append('@');
189
if (nameSpace != null) {
190                         buf.append('{');
191                         buf.append(nameSpace);
192                         buf.append('}');
193                     }
194                     buf.append(localName);
195 // if (type != null) {
196
// buf.append(" ,");
197
// buf.append(type.toString());
198
// }
199
// } else if (type != null) {
200
// buf.append(type.toString());
201
}
202 // buf.append(')');
203
break;
204             case NodeKind.COMMENT :
205                 buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
206                 buf.append('(');
207                 buf.append(')');
208                 break;
209             case NodeKind.DOCUMENT :
210                 buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
211                 buf.append('(');
212                 if (localName != null || type != null) {
213 // buf.append(NodeKind.NODEKINDSTRINGS[NodeKind.ELEMENT]);
214
// buf.append('(');
215
if (isWildcard())
216                         buf.append('*');
217                     else if (localName != null) {
218                         if (nameSpace != null) {
219                             buf.append('{');
220                             buf.append(nameSpace);
221                             buf.append('}');
222                         }
223                         buf.append(localName);
224 // if (type != null) {
225
// buf.append(" ,");
226
// buf.append(type.toString());
227
// if (isTypeNillable) {
228
// buf.append(" nillable");
229
// }
230
// }
231
// } else if (type != null) {
232
// buf.append(type.toString());
233
}
234 // buf.append(')');
235
}
236                 buf.append(')');
237                 break;
238             case NodeKind.ELEMENT :
239 // buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
240
// buf.append('(');
241
if (isWildcard())
242                     buf.append('*');
243                 else if (localName != null) {
244                     if (nameSpace != null) {
245                         buf.append('{');
246                         buf.append(nameSpace);
247                         buf.append('}');
248                     }
249                     buf.append(localName);
250 // if (type != null) {
251
// buf.append(" ,");
252
// buf.append(type.toString());
253
// if (isTypeNillable) {
254
// buf.append(" nillable");
255
// }
256
// }
257
// } else if (type != null) {
258
// buf.append(type.toString());
259
}
260 // buf.append(')');
261
break;
262                 //case NodeKind.NAMESPACE :
263
case NodeKind.NODE :
264                 buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
265                 buf.append('(');
266                 buf.append(')');
267                 break;
268             case NodeKind.PI :
269                 buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
270                 buf.append('(');
271                 if (localName != null)
272                     buf.append(localName);
273                 buf.append(')');
274                 break;
275             case NodeKind.TEXT :
276                 buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
277                 buf.append('(');
278                 buf.append(')');
279                 break;
280         }
281         return buf.toString();
282     }
283
284     // methods for path resolving
285

286     public boolean equals(Object JavaDoc o)
287     {
288         return (o instanceof StepExpr)
289             && (nameSpace == null || nameSpace.equals(((StepExpr)o).getNameSpace()))
290             && localName.equals(((StepExpr)o).getLocalName())
291             && kindtest == ((StepExpr)o).getKindTest()
292 // && type == ((StepExpr)o).getType()
293
// && wild == ((StepExpr)o).getWildCardType()
294
&& (axis == ((StepExpr)o).getAxis());
295     }
296
297     public static final String JavaDoc NAME_WILDCARD = "*";
298     public XNode xnode = null;
299
300     public StepExprImpl(byte axis, XNode xnode) {
301         this.axis = axis;
302         this.xnode = xnode;
303         this.localName = xnode.getLocalName();
304         this.nameSpace = xnode.getNamespace();
305         this.kindtest = xnode.getType();
306     }
307
308     public StepExprImpl(XNode xnode) {
309         this.axis = Axis.CHILD;
310         this.xnode = xnode;
311         this.localName = xnode.getLocalName();
312         this.nameSpace = xnode.getNamespace();
313         this.kindtest = xnode.getType();
314     }
315
316     public XNode getXNode() {
317         return xnode;
318     }
319
320     public boolean match(XNode node) {
321         if (node == null)
322             return false;
323         if (isTypeTest()) { // true if local name == null
324
return typeMatch(node);
325         } else // namespace matching
326
{
327             if (typeMatch(node) == false) {
328                 return false;
329             }
330             if (getNamespace() == null) {
331                 if (node.getNamespace() == null) {
332                     // namespace match
333
} else {
334                     return false;
335                 }
336             } else if (isNamespaceWildcard()) {
337                 // namespace match
338
} else if (node.getNamespace() == null) {
339                 return false;
340             } else if (getNamespace().equals(node.getNamespace())) {
341                 // namespace match
342
} else {
343                 return false;
344             }
345
346             // matching local name
347
if (this.getLocalName() == null) {
348                 if (node.getLocalName() == null) {
349                     // local name match
350
} else {
351                     return false;
352                 }
353             } else if (this.isLocalNameWildcard()) {
354                 // local name match
355
} else if (node.getLocalName() == null) {
356                 return false;
357             } else if (this.getLocalName().equals(node.getLocalName())) {
358                 // local name match
359
} else {
360                 return false;
361             }
362             return true;
363         }
364     }
365     public boolean isTypeTest()
366     {
367         return xnode.getLocalName() == null;
368     }
369     private boolean typeMatch(XNode node)
370     {
371         return ((getTypeTest() == node.getType()) || (getTypeTest() == NodeKind.NODE));
372     }
373     public byte getTypeTest()
374     {
375         return xnode.getType();
376     }
377     public String JavaDoc getNamespace()
378     {
379         return xnode.getNamespace();
380     }
381     public boolean isNamespaceWildcard()
382     {
383         return isNameTest() && (xnode.getNamespace() != null) && xnode.getNamespace().equals(NAME_WILDCARD);
384     }
385     public boolean isNameTest()
386     {
387         return xnode.getLocalName() != null;
388     }
389     
390 }
391
Popular Tags