View Javadoc

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