Coverage Report - org.jaxen.function.NameFunction
 
Classes in this File Line Coverage Branch Coverage Complexity
NameFunction
100%
26/26
100%
11/11
8.667
 
 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/function/NameFunction.java,v 1.11 2006/02/05 21:47:41 elharo Exp $
 3  
  * $Revision: 1.11 $
 4  
  * $Date: 2006/02/05 21:47:41 $
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright 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 are
 13  
  * met:
 14  
  * 
 15  
  *   * Redistributions of source code must retain the above copyright
 16  
  *     notice, this list of conditions and the following disclaimer.
 17  
  * 
 18  
  *   * Redistributions in binary form must reproduce the above copyright
 19  
  *     notice, this list of conditions and the following disclaimer in the
 20  
  *     documentation and/or other materials provided with the distribution.
 21  
  * 
 22  
  *   * Neither the name of the Jaxen Project nor the names of its
 23  
  *     contributors may be used to endorse or promote products derived 
 24  
  *     from this software without specific prior written permission.
 25  
  * 
 26  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 27  
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 28  
  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 29  
  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 30  
  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 31  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 32  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 33  
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 34  
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 35  
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 36  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 37  
  *
 38  
  * ====================================================================
 39  
  * This software consists of voluntary contributions made by many 
 40  
  * individuals on behalf of the Jaxen Project and was originally 
 41  
  * created by bob mcwhirter <bob@werken.com> and 
 42  
  * James Strachan <jstrachan@apache.org>.  For more information on the 
 43  
  * Jaxen Project, please see <http://www.jaxen.org/>.
 44  
  * 
 45  
  * $Id: NameFunction.java,v 1.11 2006/02/05 21:47:41 elharo Exp $
 46  
  */
 47  
 
 48  
 
 49  
 package org.jaxen.function;
 50  
 
 51  
 import java.util.List;
 52  
 
 53  
 import org.jaxen.Context;
 54  
 import org.jaxen.Function;
 55  
 import org.jaxen.FunctionCallException;
 56  
 import org.jaxen.Navigator;
 57  
 
 58  
 /**
 59  
  * <p><b>4.1</b> <code><i>string</i> name(<i>node-set?</i>)</code> </p>
 60  
  *  
 61  
  * 
 62  
  * <blockquote src="http://www.w3.org/TR/xpath">
 63  
  * The <b>name</b> function returns a string containing a QName representing 
 64  
  * the expanded-name of the node in the argument node-set that is first in document order. 
 65  
  * The QName must represent the expanded-name with respect to the namespace declarations 
 66  
  * in effect on the node whose expanded-name is being represented. Typically, this will 
 67  
  * be the QName that occurred in the XML source....If 
 68  
  * the argument node-set is empty or the first node has 
 69  
  * no expanded-name, an empty string is returned. 
 70  
  * If the argument it omitted, it defaults to a node-set with the context node as its only member.
 71  
  * </blockquote>
 72  
  * 
 73  
  * @author bob mcwhirter (bob @ werken.com)
 74  
  * 
 75  
  * @see <a href="http://www.w3.org/TR/xpath#function-name" target="_top">Section 4.1 of the XPath Specification</a>
 76  
  */
 77  
 public class NameFunction implements Function
 78  
 {
 79  
     
 80  
     
 81  
     /**
 82  
      * Create a new <code>NameFunction</code> object.
 83  
      */
 84  106
     public NameFunction() {}
 85  
     
 86  
     /**
 87  
      * Returns the name of the specified node or the name of the context node if 
 88  
      * no arguments are provided.
 89  
      * 
 90  
      * @param context the context at the point in the
 91  
      *         expression where the function is called
 92  
      * @param args a <code>List</code> containing zero or one items
 93  
      * 
 94  
      * @return a <code>String</code> containing the name
 95  
      * 
 96  
      * @throws FunctionCallException if <code>args</code> has more than one item
 97  
      */
 98  
     public Object call(Context context,
 99  
                        List args) throws FunctionCallException
 100  
     {
 101  316
         if ( args.size() == 0 )
 102  
         {
 103  78
             return evaluate( context.getNodeSet(),
 104  
                              context.getNavigator() );
 105  
         }
 106  
 
 107  238
         if ( args.size() == 1 )
 108  
         {
 109  236
             return evaluate( args,
 110  
                              context.getNavigator() );
 111  
         }
 112  
 
 113  2
         throw new FunctionCallException( "name() requires zero or one argument." );
 114  
     }
 115  
 
 116  
     /**
 117  
      * Returns the name of <code>list.get(0)</code>
 118  
      * 
 119  
      * @param list a list of nodes
 120  
      * @param nav the <code>Navigator</code> used to retrieve the name
 121  
      * 
 122  
      * @return the name of <code>list.get(0)</code>
 123  
      * 
 124  
      * @throws FunctionCallException if <code>list.get(0)</code> is not a node
 125  
      */
 126  
     public static String evaluate(List list,
 127  
                                   Navigator nav) throws FunctionCallException
 128  
     {
 129  548
         if ( ! list.isEmpty() )
 130  
         {            
 131  532
             Object first = list.get(0);
 132  
 
 133  532
             if (first instanceof List)
 134  
             {
 135  234
                 return evaluate( (List) first,
 136  
                                  nav );
 137  
             }
 138  298
             else if ( nav.isElement( first ) )
 139  
             {
 140  214
                 return nav.getElementQName( first );
 141  
             }
 142  84
             else if ( nav.isAttribute( first ) )
 143  
             {
 144  2
                 return nav.getAttributeQName( first );
 145  
             }
 146  82
             else if ( nav.isProcessingInstruction( first ) )
 147  
             {
 148  2
                 return nav.getProcessingInstructionTarget( first );
 149  
             }
 150  80
             else if ( nav.isNamespace( first ) )
 151  
             {
 152  16
                 return nav.getNamespacePrefix( first );
 153  
             }
 154  64
             else if ( nav.isDocument( first ) )
 155  
             {
 156  58
                 return "";
 157  
             }
 158  6
             else if ( nav.isComment( first ) )
 159  
             {
 160  2
                 return "";
 161  
             }
 162  4
             else if ( nav.isText( first ) )
 163  
             {
 164  2
                 return "";
 165  
             }
 166  
             else {
 167  2
                 throw new FunctionCallException("The argument to the name function must be a node-set");   
 168  
             }
 169  
         }
 170  
 
 171  16
         return "";
 172  
 
 173  
     }
 174  
     
 175  
 }