View Javadoc
1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
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$
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>
60   * <b>4.3</b> <code><i>boolean</i> boolean(<i>object</i>)</code>
61   * </p>
62   * 
63   * <blockquote cite="http://www.w3.org/TR/xpath#section-Boolean-Functions">
64   * <p>
65   * The <b><a href="https://www.w3.org/TR/xpath#function-boolean" target="_top">boolean</a></b>
66   * function converts its argument to a boolean as follows:
67   * </p>
68   * 
69   * <ul>
70   * 
71   * <li>
72   * <p>
73   * a number is true if and only if it is neither positive or negative
74   * zero nor NaN
75   * </p>
76   * </li>
77   * 
78   * <li>
79   * <p>
80   * a node-set is true if and only if it is non-empty
81   * </p>
82   * </li>
83   * 
84   * <li>
85   * <p>
86   * a string is true if and only if its length is non-zero
87   * </p>
88   * </li>
89   * 
90   * <li>
91   * 
92   * <p>
93   * an object of a type other than the four basic types is converted to a
94   * boolean in a way that is dependent on that type
95   * </p></li></ul>
96   * </blockquote>
97   * 
98   * @author bob mcwhirter (bob @ werken.com)
99   * @see <a href="https://www.w3.org/TR/xpath#function-boolean">Section 4.3 of the XPath Specification</a>
100  */
101 public class BooleanFunction implements Function
102 {
103 
104 
105     /**
106      * Create a new <code>BooleanFunction</code> object.
107      */
108     public BooleanFunction() {}
109     
110     /** Convert the argument to a <code>Boolean</code>
111      *
112      * @param context the context at the point in the
113      *         expression when the function is called
114      * @param args a list with exactly one item which will be converted to a 
115      *     <code>Boolean</code>
116      * 
117      * @return the result of evaluating the function; 
118      *     <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
119      * 
120      * @throws FunctionCallException if <code>args</code> has more or less than one item
121      */
122     public Object call(Context context,
123                        List args) throws FunctionCallException
124     {
125         if ( args.size() == 1 )
126         {
127             return evaluate( args.get(0), context.getNavigator() );
128         }
129 
130         throw new FunctionCallException("boolean() requires one argument");
131     }
132 
133     /** 
134      * <p>Convert the argument <code>obj</code> to a <code>Boolean</code> 
135      * according to the following rules:</p>
136      *
137      * <ul>
138      * <li>Lists are false if they're empty; true if they're not.</li>
139      * <li>Booleans are false if they're false; true if they're true.</li>
140      * <li>Strings are false if they're empty; true if they're not.</li>
141      * <li>Numbers are false if they're 0 or NaN; true if they're not.</li>
142      * <li>All other objects are true.</li>
143      * </ul>
144      * 
145      * @param obj the object to convert to a boolean
146      * @param nav ignored
147      * 
148      * @return <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
149      */
150     public static Boolean evaluate(Object obj, Navigator nav)
151     {
152         if ( obj instanceof List )
153         {
154             List list = (List) obj;
155             
156             // if it's an empty list, then we have a null node-set -> false            
157             if (list.size() == 0)
158             {
159                 return Boolean.FALSE;
160             }
161      
162             // otherwise, unwrap the list and check the primitive
163             obj = list.get(0);
164         }
165         
166         // now check for primitive types
167         // otherwise a non-empty node-set is true
168 
169         // if it's a Boolean, let it decide
170         if ( obj instanceof Boolean )
171         {
172             return (Boolean) obj;
173         }
174         // if it's a Number, != 0 -> true
175         else if ( obj instanceof Number )
176         {
177             double d = ((Number) obj).doubleValue();
178             if ( d == 0 || Double.isNaN(d) )
179             {
180                 return Boolean.FALSE;
181             }
182             return Boolean.TRUE;
183         }
184         // if it's a String, "" -> false
185         else if ( obj instanceof String )
186         {
187             return ( ((String)obj).length() > 0
188                      ? Boolean.TRUE
189                      : Boolean.FALSE );
190         }
191         else 
192         {
193             // assume it's a node so that this node-set is non-empty 
194             // and so it's true
195             return ( obj != null ) ? Boolean.TRUE : Boolean.FALSE;
196         }
197 
198     }
199 }