KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > extra > spath > Step


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.extra.spath;
18
19 import java.util.List JavaDoc;
20
21 /**
22  * <p>Represents a 'step' in an SPath expression.</p>
23  *
24  * @author Shawn Bayern
25  */

26 public class Step {
27
28     private boolean depthUnlimited;
29     private String JavaDoc name;
30     private List JavaDoc predicates;
31
32     // record a few things for for efficiency...
33
private String JavaDoc uri, localPart;
34
35     /**
36      * Constructs a new Step object, given a name and a (possibly null)
37      * list of predicates. A boolean is also passed, indicating
38      * whether this particular Step is relative to the 'descendent-or-self'
39      * axis of the node courrently under consideration. If true, it is;
40      * if false, then this Step is rooted as a direct child of the node
41      * under consideration.
42      */

43     public Step(boolean depthUnlimited, String JavaDoc name, List JavaDoc predicates) {
44     if (name == null)
45         throw new IllegalArgumentException JavaDoc("non-null name required");
46     this.depthUnlimited = depthUnlimited;
47     this.name = name;
48     this.predicates = predicates;
49     }
50
51     /**
52      * Returns true if the given name matches the Step object's
53      * name, taking into account the Step object's wildcards; returns
54      * false otherwise.
55      */

56     public boolean isMatchingName(String JavaDoc uri, String JavaDoc localPart) {
57     // check and normalize arguments
58
if (localPart == null)
59         throw new IllegalArgumentException JavaDoc("need non-null localPart");
60     if (uri != null && uri.equals(""))
61         uri = null;
62
63     // split name into uri/localPart if we haven't done so already
64
if (this.localPart == null && this.uri == null)
65         parseStepName();
66
67     // generic wildcard
68
if (this.uri == null && this.localPart.equals("*"))
69         return true;
70
71     // match will null namespace
72
if (uri == null && this.uri == null
73         && localPart.equals(this.localPart))
74         return true;
75
76     if (uri != null && this.uri != null && uri.equals(this.uri)) {
77         // exact match
78
if (localPart.equals(this.localPart))
79         return true;
80
81         // namespace-specific wildcard
82
if (this.localPart.equals("*"))
83         return true;
84     }
85
86     // no match
87
return false;
88     }
89
90     /** Returns true if the Step's depth is unlimited, false otherwise. */
91     public boolean isDepthUnlimited() {
92     return depthUnlimited;
93     }
94
95     /** Returns the Step's node name. */
96     public String JavaDoc getName() {
97     return name;
98     }
99
100     /** Returns a list of this Step object's predicates. */
101     public List JavaDoc getPredicates() {
102     return predicates;
103     }
104
105     /** Lazily computes some information about our name. */
106     private void parseStepName() {
107     String JavaDoc prefix;
108     int colonIndex = name.indexOf(":");
109
110     if (colonIndex == -1) {
111         // no colon, so localpart is simply name (even if it's "*")
112
prefix = null;
113         localPart = name;
114     } else {
115         prefix = name.substring(0, colonIndex);
116         localPart = name.substring(colonIndex + 1);
117     }
118
119     uri = mapPrefix(prefix);
120     }
121
122     /** Returns a URI for the given prefix, given our mappings. */
123     private String JavaDoc mapPrefix(String JavaDoc prefix) {
124     // ability to specify a mapping is, as of yet, unimplemented
125
if (prefix == null)
126         return null;
127     else
128         throw new IllegalArgumentException JavaDoc(
129         "unknown prefix '" + prefix + "'");
130     }
131 }
132
Popular Tags