KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jaxen > function > NumberFunction


1 /*
2  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/function/NumberFunction.java,v 1.16 2005/06/26 16:07:22 elharo Exp $
3  * $Revision: 1.16 $
4  * $Date: 2005/06/26 16:07:22 $
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: NumberFunction.java,v 1.16 2005/06/26 16:07:22 elharo Exp $
60  */

61
62 package org.jaxen.function;
63
64 import java.util.Iterator JavaDoc;
65 import java.util.List JavaDoc;
66
67 import org.jaxen.Context;
68 import org.jaxen.Function;
69 import org.jaxen.FunctionCallException;
70 import org.jaxen.Navigator;
71
72 /**
73  * <p>
74  * <b>4.4</b> <code><i>number</i> number(<i>object</i>)</code>
75  *
76  *
77  * <blockquote SRC="http://www.w3.org/TR/xpath#function-number">
78  * <p>
79  * The <b>number</b> function converts
80  * its argument to a number as follows:
81  * </p>
82  *
83  * <ul>
84  *
85  * <li>
86  * <p>
87  * a string that consists of optional whitespace followed by an optional
88  * minus sign followed by a <a HREF="#NT-Number">Number</a> followed by
89  * whitespace is converted to the IEEE 754 number that is nearest
90  * (according to the IEEE 754 round-to-nearest rule) to the mathematical
91  * value represented by the string; any other string is converted to NaN
92  * </p>
93  * </li>
94  *
95  * <li>
96  * <p>
97  * boolean true is converted to 1; boolean false is converted to 0
98  * </p>
99  * </li>
100  *
101  * <li>
102  *
103  * <p>
104  * a node-set is first converted to a string as if by a call to the <b><a
105  * HREF="http://www.w3.org/TR/xpath#function-string" target="_top">string</a></b> function and then converted
106  * in the same way as a string argument
107  * </p>
108  *
109  * </li>
110  *
111  * <li>
112  * <p>
113  * an object of a type other than the four basic types is converted to a
114  * number in a way that is dependent on that type
115  * </p>
116  * </li>
117  *
118  * </ul>
119  *
120  * <p>
121  * If the argument is omitted, it defaults to a node-set with the
122  * context node as its only member.
123  * </p>
124  *
125  * <blockquote> <b>NOTE: </b>The <b>number</b>
126  * function should not be used for conversion of numeric data occurring
127  * in an element in an XML document unless the element is of a type that
128  * represents numeric data in a language-neutral format (which would
129  * typically be transformed into a language-specific format for
130  * presentation to a user). In addition, the <b>number</b> function cannot be used
131  * unless the language-neutral format used by the element is consistent
132  * with the XPath syntax for a <a HREF="http://www.w3.org/TR/xpath#NT-Number">Number</a>.</blockquote>
133  *
134  * </blockquote>
135  *
136  * @author bob mcwhirter (bob @ werken.com)
137  *
138  * @see <a HREF="http://www.w3.org/TR/xpath#function-number"
139  * target="_top">Section 4.4 of the XPath Specification</a>
140  */

141 public class NumberFunction implements Function
142 {
143
144     private final static Double JavaDoc NaN = new Double JavaDoc( Double.NaN );
145     
146     
147     /**
148      * Create a new <code>NumberFunction</code> object.
149      */

150     public NumberFunction() {}
151
152     /**
153      * Returns the number value of <code>args.get(0)</code>,
154      * or the number value of the context node if <code>args</code>
155      * is empty.
156      *
157      * @param context the context at the point in the
158      * expression when the function is called
159      * @param args a list containing the single item to be converted to a
160      * <code>Double</code>
161      *
162      * @return a <code>Double</code>
163      *
164      * @throws FunctionCallException if <code>args</code> has more than one item
165      */

166     public Object JavaDoc call(Context context, List JavaDoc args) throws FunctionCallException
167     {
168         if (args.size() == 1)
169         {
170             return evaluate( args.get(0), context.getNavigator() );
171         }
172         else if (args.size() == 0)
173         {
174             return evaluate( context.getNodeSet(), context.getNavigator() );
175         }
176     
177         throw new FunctionCallException( "number() takes at most one argument." );
178     }
179
180     /**
181      * Returns the number value of <code>obj</code>.
182      *
183      * @param obj the object to be converted to a number
184      * @param nav the <code>Navigator</code> used to calculate the string-value
185      * of node-sets
186      *
187      * @return a <code>Double</code>
188      */

189     public static Double JavaDoc evaluate(Object JavaDoc obj, Navigator nav)
190     {
191         if( obj instanceof Double JavaDoc )
192         {
193             return (Double JavaDoc) obj;
194         }
195         else if ( obj instanceof String JavaDoc )
196         {
197             String JavaDoc str = (String JavaDoc) obj;
198             try
199             {
200                 Double JavaDoc doubleValue = new Double JavaDoc( str );
201                 return doubleValue;
202             }
203             catch (NumberFormatException JavaDoc e)
204             {
205                 return NaN;
206             }
207         }
208         else if ( obj instanceof List JavaDoc || obj instanceof Iterator JavaDoc )
209         {
210           return evaluate( StringFunction.evaluate( obj, nav ), nav );
211         }
212         else if ( nav.isElement( obj ) || nav.isAttribute( obj ) )
213         {
214             return evaluate( StringFunction.evaluate( obj, nav ), nav );
215         }
216         else if ( obj instanceof Boolean JavaDoc )
217           {
218           if ( obj == Boolean.TRUE )
219           {
220               return new Double JavaDoc( 1 );
221           }
222           else
223           {
224               return new Double JavaDoc( 0 );
225           }
226         }
227         return NaN;
228     }
229   
230   /**
231    * Determines whether the argument is not a number (NaN) as defined
232    * by IEEE 754.
233    *
234    * @param val the double to test
235    * @return true if the value is NaN, false otherwise
236    */

237     public static boolean isNaN( double val )
238     {
239         return Double.isNaN(val);
240     }
241   
242   /**
243    * Determines whether the argument is not a number (NaN) as defined
244    * by IEEE 754.
245    *
246    * @param val the <code>Double</code> to test
247    * @return true if the value is NaN, false otherwise
248    */

249     public static boolean isNaN( Double JavaDoc val )
250     {
251         return val.equals( NaN );
252     }
253   
254 }
255
Popular Tags