Coverage Report - org.jaxen.saxpath.Axis

Classes in this Package Line Coverage Branch Coverage Complexity
Axis
72% 
100% 
27.5

 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/saxpath/Axis.java,v 1.4 2005/05/13 14:00:07 elharo Exp $
 3  
  * $Revision: 1.4 $
 4  
  * $Date: 2005/05/13 14:00:07 $
 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.4 2005/05/13 14:00:07 elharo Exp $
 60  
  */
 61  
 
 62  
 
 63  
 
 64  
 package org.jaxen.saxpath;
 65  
 
 66  
 import org.jaxen.JaxenRuntimeException;
 67  
 
 68  
 
 69  
 
 70  
 /**
 71  
  * Internal SAXPath class to avoid a lot of string comparisons. 
 72  
  */
 73  0
 public class Axis
 74  
 {
 75  
     
 76  
     // XXX Ultimately these should use the type-safe enum pattern instead
 77  
     /** Marker for an invalid axis */
 78  
     public final static int INVALID_AXIS       =  0;
 79  
 
 80  
     /** The <code>child</code> axis */
 81  
     public final static int CHILD              =  1;
 82  
 
 83  
     /** The <code>descendant</code> axis */
 84  
     public final static int DESCENDANT         =  2;
 85  
 
 86  
     /** The <code>parent</code> axis */
 87  
     public final static int PARENT             =  3;
 88  
 
 89  
     /** The <code>ancestor</code> axis */
 90  
     public final static int ANCESTOR           =  4;
 91  
 
 92  
     /** The <code>following-sibling</code> axis */
 93  
     public final static int FOLLOWING_SIBLING  =  5;
 94  
 
 95  
     /** The <code>preceding-sibling</code> axis */
 96  
     public final static int PRECEDING_SIBLING  =  6;
 97  
 
 98  
     /** The <code>following</code> axis */
 99  
     public final static int FOLLOWING          =  7;
 100  
 
 101  
     /** The <code>preceding</code> axis */
 102  
     public final static int PRECEDING          =  8;
 103  
 
 104  
     /** The <code>attribute</code> axis */
 105  
     public final static int ATTRIBUTE          =  9;
 106  
 
 107  
     /** The <code>namespace</code> axis */
 108  
     public final static int NAMESPACE          = 10;
 109  
 
 110  
     /** The <code>self</code> axis */
 111  
     public final static int SELF               = 11;
 112  
 
 113  
     /** The <code>descendant-or-self</code> axis */
 114  
     public final static int DESCENDANT_OR_SELF = 12;
 115  
 
 116  
     /** The <code>ancestor-or-self</code> axis */
 117  
     public final static int ANCESTOR_OR_SELF   = 13;
 118  
 
 119  
     /**
 120  
      * <p>
 121  
      * Returns the name of the axis.
 122  
      * </p>
 123  
      * 
 124  
      * @param axisNum the axis code
 125  
      * @return the name of the axis such as might be used in an XPath expression
 126  
      * @throws JaxenRuntimeException if the number does not represent one of the 13
 127  
      *     XPath axes
 128  
      */
 129  
     public static String lookup(int axisNum)
 130  
     {
 131  15
         switch ( axisNum )
 132  
         {
 133  
             case CHILD:
 134  8
                 return "child";
 135  
 
 136  
             case DESCENDANT:
 137  0
                 return "descendant";
 138  
 
 139  
             case PARENT:
 140  0
                 return "parent";
 141  
 
 142  
             case ANCESTOR:
 143  0
                 return "ancestor";
 144  
 
 145  
             case FOLLOWING_SIBLING:
 146  0
                 return "following-sibling";
 147  
 
 148  
             case PRECEDING_SIBLING:
 149  0
                 return "preceding-sibling";
 150  
 
 151  
             case FOLLOWING:
 152  0
                 return "following";
 153  
 
 154  
             case PRECEDING:
 155  0
                 return "preceding";
 156  
 
 157  
             case ATTRIBUTE:
 158  1
                 return "attribute";
 159  
 
 160  
             case NAMESPACE:
 161  0
                 return "namespace";
 162  
 
 163  
             case SELF:
 164  0
                 return "self";
 165  
 
 166  
             case DESCENDANT_OR_SELF:
 167  6
                 return "descendant-or-self";
 168  
 
 169  
             case ANCESTOR_OR_SELF:
 170  0
                 return "ancestor-or-self";
 171  
         }
 172  
 
 173  0
         throw new JaxenRuntimeException("Illegal Axis Number");
 174  
     }
 175  
 
 176  
     /**
 177  
      * <p>
 178  
      * Returns the code for an axis given its name.
 179  
      * </p>
 180  
      * 
 181  
      * @param axisName the name of the axis: child, parent, descendant, descendant-or-self, etc.
 182  
      * @return the axis code
 183  
      */
 184  
     public static int lookup(String axisName)
 185  
     {
 186  424
         if ( "child".equals( axisName ) )
 187  
         {
 188  56
             return CHILD;
 189  
         }
 190  
 
 191  368
         if ( "descendant".equals( axisName ) )
 192  
         {
 193  89
             return DESCENDANT;
 194  
         }
 195  
 
 196  279
         if ( "parent".equals( axisName ) )
 197  
         {
 198  35
             return PARENT;
 199  
         }
 200  
 
 201  244
         if ( "ancestor".equals( axisName ) )
 202  
         {
 203  18
             return ANCESTOR;
 204  
         }
 205  
 
 206  226
         if ( "following-sibling".equals( axisName ) )
 207  
         {
 208  9
             return FOLLOWING_SIBLING;
 209  
         }
 210  
 
 211  217
         if ( "preceding-sibling".equals( axisName ) )
 212  
         {
 213  45
             return PRECEDING_SIBLING;
 214  
         }
 215  
 
 216  172
         if ( "following".equals( axisName ) )
 217  
         {
 218  16
             return FOLLOWING;
 219  
         }
 220  
 
 221  156
         if ( "preceding".equals( axisName ) )
 222  
         {
 223  37
             return PRECEDING;
 224  
         }
 225  
 
 226  119
         if ( "attribute".equals( axisName ) )
 227  
         {
 228  7
             return ATTRIBUTE;
 229  
         }
 230  
 
 231  112
         if ( "namespace".equals( axisName ) )
 232  
         {
 233  68
             return NAMESPACE;
 234  
         }
 235  
 
 236  44
         if ( "self".equals( axisName ) )
 237  
         {
 238  7
             return SELF;
 239  
         }
 240  
 
 241  37
         if ( "descendant-or-self".equals( axisName ) )
 242  
         {
 243  26
             return DESCENDANT_OR_SELF;
 244  
         }
 245  
 
 246  11
         if ( "ancestor-or-self".equals( axisName ) )
 247  
         {
 248  9
             return ANCESTOR_OR_SELF;
 249  
         }
 250  
 
 251  2
         return INVALID_AXIS;
 252  
     }
 253  
 }