1 /*
2 * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/saxpath/Axis.java,v 1.6 2005/06/28 13:44:46 elharo Exp $
3 * $Revision: 1.6 $
4 * $Date: 2005/06/28 13:44:46 $
5 *
6 * ====================================================================
7 *
8 * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions, and the disclaimer that follows
20 * these conditions in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * 3. The name "Jaxen" must not be used to endorse or promote products
24 * derived from this software without prior written permission. For
25 * written permission, please contact license@jaxen.org.
26 *
27 * 4. Products derived from this software may not be called "Jaxen", nor
28 * may "Jaxen" appear in their name, without prior written permission
29 * from the Jaxen Project Management (pm@jaxen.org).
30 *
31 * In addition, we request (but do not require) that you include in the
32 * end-user documentation provided with the redistribution and/or in the
33 * software itself an acknowledgement equivalent to the following:
34 * "This product includes software developed by the
35 * Jaxen Project <http://www.jaxen.org/>."
36 * Alternatively, the acknowledgment may be graphical using the logos
37 * available at http://www.jaxen.org/
38 *
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * ====================================================================
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Jaxen Project and was originally
55 * created by bob mcwhirter <bob@werken.com> and
56 * James Strachan <jstrachan@apache.org>. For more information on the
57 * Jaxen Project, please see <http://www.jaxen.org/>.
58 *
59 * $Id: Axis.java,v 1.6 2005/06/28 13:44:46 elharo Exp $
60 */
61
62
63
64 package org.jaxen.saxpath;
65
66 import org.jaxen.JaxenRuntimeException;
67
68
69
70 /***
71 *
72 * Internal SAXPath class that contains constants representing
73 * XPath operators to avoid a lot of string comparisons.
74 */
75 public class Axis
76 {
77
78 private Axis() {}
79
80 // XXX Ultimately these should use the type-safe enum pattern instead
81 /*** Marker for an invalid axis */
82 public final static int INVALID_AXIS = 0;
83
84 /*** The <code>child</code> axis */
85 public final static int CHILD = 1;
86
87 /*** The <code>descendant</code> axis */
88 public final static int DESCENDANT = 2;
89
90 /*** The <code>parent</code> axis */
91 public final static int PARENT = 3;
92
93 /*** The <code>ancestor</code> axis */
94 public final static int ANCESTOR = 4;
95
96 /*** The <code>following-sibling</code> axis */
97 public final static int FOLLOWING_SIBLING = 5;
98
99 /*** The <code>preceding-sibling</code> axis */
100 public final static int PRECEDING_SIBLING = 6;
101
102 /*** The <code>following</code> axis */
103 public final static int FOLLOWING = 7;
104
105 /*** The <code>preceding</code> axis */
106 public final static int PRECEDING = 8;
107
108 /*** The <code>attribute</code> axis */
109 public final static int ATTRIBUTE = 9;
110
111 /*** The <code>namespace</code> axis */
112 public final static int NAMESPACE = 10;
113
114 /*** The <code>self</code> axis */
115 public final static int SELF = 11;
116
117 /*** The <code>descendant-or-self</code> axis */
118 public final static int DESCENDANT_OR_SELF = 12;
119
120 /*** The <code>ancestor-or-self</code> axis */
121 public final static int ANCESTOR_OR_SELF = 13;
122
123 /***
124 * <p>
125 * Returns the name of the axis.
126 * </p>
127 *
128 * @param axisNum the axis code
129 * @return the name of the axis such as might be used in an XPath expression
130 * @throws JaxenRuntimeException if the number does not represent one of the 13
131 * XPath axes
132 */
133 public static String lookup(int axisNum)
134 {
135 switch ( axisNum )
136 {
137 case CHILD:
138 return "child";
139
140 case DESCENDANT:
141 return "descendant";
142
143 case PARENT:
144 return "parent";
145
146 case ANCESTOR:
147 return "ancestor";
148
149 case FOLLOWING_SIBLING:
150 return "following-sibling";
151
152 case PRECEDING_SIBLING:
153 return "preceding-sibling";
154
155 case FOLLOWING:
156 return "following";
157
158 case PRECEDING:
159 return "preceding";
160
161 case ATTRIBUTE:
162 return "attribute";
163
164 case NAMESPACE:
165 return "namespace";
166
167 case SELF:
168 return "self";
169
170 case DESCENDANT_OR_SELF:
171 return "descendant-or-self";
172
173 case ANCESTOR_OR_SELF:
174 return "ancestor-or-self";
175 }
176
177 throw new JaxenRuntimeException("Illegal Axis Number");
178 }
179
180 /***
181 * <p>
182 * Returns the code for an axis given its name.
183 * </p>
184 *
185 * @param axisName the name of the axis: child, parent, descendant, descendant-or-self, etc.
186 * @return the axis code
187 */
188 public static int lookup(String axisName)
189 {
190 if ( "child".equals( axisName ) )
191 {
192 return CHILD;
193 }
194
195 if ( "descendant".equals( axisName ) )
196 {
197 return DESCENDANT;
198 }
199
200 if ( "parent".equals( axisName ) )
201 {
202 return PARENT;
203 }
204
205 if ( "ancestor".equals( axisName ) )
206 {
207 return ANCESTOR;
208 }
209
210 if ( "following-sibling".equals( axisName ) )
211 {
212 return FOLLOWING_SIBLING;
213 }
214
215 if ( "preceding-sibling".equals( axisName ) )
216 {
217 return PRECEDING_SIBLING;
218 }
219
220 if ( "following".equals( axisName ) )
221 {
222 return FOLLOWING;
223 }
224
225 if ( "preceding".equals( axisName ) )
226 {
227 return PRECEDING;
228 }
229
230 if ( "attribute".equals( axisName ) )
231 {
232 return ATTRIBUTE;
233 }
234
235 if ( "namespace".equals( axisName ) )
236 {
237 return NAMESPACE;
238 }
239
240 if ( "self".equals( axisName ) )
241 {
242 return SELF;
243 }
244
245 if ( "descendant-or-self".equals( axisName ) )
246 {
247 return DESCENDANT_OR_SELF;
248 }
249
250 if ( "ancestor-or-self".equals( axisName ) )
251 {
252 return ANCESTOR_OR_SELF;
253 }
254
255 return INVALID_AXIS;
256 }
257 }