KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > lib > ExsltMath


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ExsltMath.java,v 1.12 2004/02/11 17:56:36 minchau Exp $
18  */

19 package org.apache.xalan.lib;
20
21 import org.apache.xpath.NodeSet;
22
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25
26 /**
27  * This class contains EXSLT math extension functions.
28  * It is accessed by specifying a namespace URI as follows:
29  * <pre>
30  * xmlns:math="http://exslt.org/math"
31  * </pre>
32  *
33  * The documentation for each function has been copied from the relevant
34  * EXSLT Implementer page.
35  *
36  * @see <a HREF="http://www.exslt.org/">EXSLT</a>
37
38  * @xsl.usage general
39  */

40 public class ExsltMath extends ExsltBase
41 {
42   // Constants
43
private static String JavaDoc PI = "3.1415926535897932384626433832795028841971693993751";
44   private static String JavaDoc E = "2.71828182845904523536028747135266249775724709369996";
45   private static String JavaDoc SQRRT2 = "1.41421356237309504880168872420969807856967187537694";
46   private static String JavaDoc LN2 = "0.69314718055994530941723212145817656807550013436025";
47   private static String JavaDoc LN10 = "2.302585092994046";
48   private static String JavaDoc LOG2E = "1.4426950408889633";
49   private static String JavaDoc SQRT1_2 = "0.7071067811865476";
50     
51   /**
52    * The math:max function returns the maximum value of the nodes passed as the argument.
53    * The maximum value is defined as follows. The node set passed as an argument is sorted
54    * in descending order as it would be by xsl:sort with a data type of number. The maximum
55    * is the result of converting the string value of the first node in this sorted list to
56    * a number using the number function.
57    * <p>
58    * If the node set is empty, or if the result of converting the string values of any of the
59    * nodes to a number is NaN, then NaN is returned.
60    *
61    * @param nl The NodeList for the node-set to be evaluated.
62    *
63    * @return the maximum value found, NaN if any node cannot be converted to a number.
64    *
65    * @see <a HREF="http://www.exslt.org/">EXSLT</a>
66    */

67   public static double max (NodeList JavaDoc nl)
68   {
69     if (nl == null || nl.getLength() == 0)
70       return Double.NaN;
71       
72     double m = - Double.MAX_VALUE;
73     for (int i = 0; i < nl.getLength(); i++)
74     {
75       Node JavaDoc n = nl.item(i);
76       double d = toNumber(n);
77       if (Double.isNaN(d))
78         return Double.NaN;
79       else if (d > m)
80         m = d;
81     }
82     
83     return m;
84   }
85
86   /**
87    * The math:min function returns the minimum value of the nodes passed as the argument.
88    * The minimum value is defined as follows. The node set passed as an argument is sorted
89    * in ascending order as it would be by xsl:sort with a data type of number. The minimum
90    * is the result of converting the string value of the first node in this sorted list to
91    * a number using the number function.
92    * <p>
93    * If the node set is empty, or if the result of converting the string values of any of
94    * the nodes to a number is NaN, then NaN is returned.
95    *
96    * @param nl The NodeList for the node-set to be evaluated.
97    *
98    * @return the minimum value found, NaN if any node cannot be converted to a number.
99    *
100    * @see <a HREF="http://www.exslt.org/">EXSLT</a>
101    */

102   public static double min (NodeList JavaDoc nl)
103   {
104     if (nl == null || nl.getLength() == 0)
105       return Double.NaN;
106
107     double m = Double.MAX_VALUE;
108     for (int i = 0; i < nl.getLength(); i++)
109     {
110       Node JavaDoc n = nl.item(i);
111       double d = toNumber(n);
112       if (Double.isNaN(d))
113         return Double.NaN;
114       else if (d < m)
115         m = d;
116     }
117     
118     return m;
119   }
120   
121   /**
122    * The math:highest function returns the nodes in the node set whose value is the maximum
123    * value for the node set. The maximum value for the node set is the same as the value as
124    * calculated by math:max. A node has this maximum value if the result of converting its
125    * string value to a number as if by the number function is equal to the maximum value,
126    * where the equality comparison is defined as a numerical comparison using the = operator.
127    * <p>
128    * If any of the nodes in the node set has a non-numeric value, the math:max function will
129    * return NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any
130    * of the nodes in the node set has a non-numeric value, math:highest will return an empty
131    * node set.
132    *
133    * @param nl The NodeList for the node-set to be evaluated.
134    *
135    * @return node-set with nodes containing the maximum value found, an empty node-set
136    * if any node cannot be converted to a number.
137    */

138   public static NodeList JavaDoc highest (NodeList JavaDoc nl)
139   {
140     double maxValue = max(nl);
141
142     NodeSet highNodes = new NodeSet();
143     highNodes.setShouldCacheNodes(true);
144     
145     if (Double.isNaN(maxValue))
146       return highNodes; // empty Nodeset
147

148     for (int i = 0; i < nl.getLength(); i++)
149     {
150       Node JavaDoc n = nl.item(i);
151       double d = toNumber(n);
152       if (d == maxValue)
153         highNodes.addElement(n);
154     }
155     return highNodes;
156   }
157   
158   /**
159    * The math:lowest function returns the nodes in the node set whose value is the minimum value
160    * for the node set. The minimum value for the node set is the same as the value as calculated
161    * by math:min. A node has this minimum value if the result of converting its string value to
162    * a number as if by the number function is equal to the minimum value, where the equality
163    * comparison is defined as a numerical comparison using the = operator.
164    * <p>
165    * If any of the nodes in the node set has a non-numeric value, the math:min function will return
166    * NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any of the nodes
167    * in the node set has a non-numeric value, math:lowest will return an empty node set.
168    *
169    * @param nl The NodeList for the node-set to be evaluated.
170    *
171    * @return node-set with nodes containing the minimum value found, an empty node-set
172    * if any node cannot be converted to a number.
173    *
174    */

175   public static NodeList JavaDoc lowest (NodeList JavaDoc nl)
176   {
177     double minValue = min(nl);
178
179     NodeSet lowNodes = new NodeSet();
180     lowNodes.setShouldCacheNodes(true);
181     
182     if (Double.isNaN(minValue))
183       return lowNodes; // empty Nodeset
184

185     for (int i = 0; i < nl.getLength(); i++)
186     {
187       Node JavaDoc n = nl.item(i);
188       double d = toNumber(n);
189       if (d == minValue)
190         lowNodes.addElement(n);
191     }
192     return lowNodes;
193   }
194   
195   /**
196    * The math:abs function returns the absolute value of a number.
197    *
198    * @param num A number
199    * @return The absolute value of the number
200    */

201    public static double abs(double num)
202    {
203      return Math.abs(num);
204    }
205
206   /**
207    * The math:acos function returns the arccosine value of a number.
208    *
209    * @param num A number
210    * @return The arccosine value of the number
211    */

212    public static double acos(double num)
213    {
214      return Math.acos(num);
215    }
216
217   /**
218    * The math:asin function returns the arcsine value of a number.
219    *
220    * @param num A number
221    * @return The arcsine value of the number
222    */

223    public static double asin(double num)
224    {
225      return Math.asin(num);
226    }
227
228   /**
229    * The math:atan function returns the arctangent value of a number.
230    *
231    * @param num A number
232    * @return The arctangent value of the number
233    */

234    public static double atan(double num)
235    {
236      return Math.atan(num);
237    }
238   
239   /**
240    * The math:atan2 function returns the angle ( in radians ) from the X axis to a point (y,x).
241    *
242    * @param num1 The X axis value
243    * @param num2 The Y axis value
244    * @return The angle (in radians) from the X axis to a point (y,x)
245    */

246    public static double atan2(double num1, double num2)
247    {
248      return Math.atan2(num1, num2);
249    }
250
251   /**
252    * The math:cos function returns cosine of the passed argument.
253    *
254    * @param num A number
255    * @return The cosine value of the number
256    */

257    public static double cos(double num)
258    {
259      return Math.cos(num);
260    }
261
262   /**
263    * The math:exp function returns e (the base of natural logarithms) raised to a power.
264    *
265    * @param num A number
266    * @return The value of e raised to the given power
267    */

268    public static double exp(double num)
269    {
270      return Math.exp(num);
271    }
272
273   /**
274    * The math:log function returns the natural logarithm of a number.
275    *
276    * @param num A number
277    * @return The natural logarithm of the number
278    */

279    public static double log(double num)
280    {
281      return Math.log(num);
282    }
283
284   /**
285    * The math:power function returns the value of a base expression taken to a specified power.
286    *
287    * @param num1 The base
288    * @param num2 The power
289    * @return The value of the base expression taken to the specified power
290    */

291    public static double power(double num1, double num2)
292    {
293      return Math.pow(num1, num2);
294    }
295
296   /**
297    * The math:random function returns a random number from 0 to 1.
298    *
299    * @return A random double from 0 to 1
300    */

301    public static double random()
302    {
303      return Math.random();
304    }
305
306   /**
307    * The math:sin function returns the sine of the number.
308    *
309    * @param num A number
310    * @return The sine value of the number
311    */

312    public static double sin(double num)
313    {
314      return Math.sin(num);
315    }
316
317   /**
318    * The math:sqrt function returns the square root of a number.
319    *
320    * @param num A number
321    * @return The square root of the number
322    */

323    public static double sqrt(double num)
324    {
325      return Math.sqrt(num);
326    }
327
328   /**
329    * The math:tan function returns the tangent of the number passed as an argument.
330    *
331    * @param num A number
332    * @return The tangent value of the number
333    */

334    public static double tan(double num)
335    {
336      return Math.tan(num);
337    }
338
339   /**
340    * The math:constant function returns the specified constant to a set precision.
341    * The possible constants are:
342    * <pre>
343    * PI
344    * E
345    * SQRRT2
346    * LN2
347    * LN10
348    * LOG2E
349    * SQRT1_2
350    * </pre>
351    * @param name The name of the constant
352    * @param precision The precision
353    * @return The value of the specified constant to the given precision
354    */

355    public static double constant(String JavaDoc name, double precision)
356    {
357      String JavaDoc value = null;
358      if (name.equals("PI"))
359        value = PI;
360      else if (name.equals("E"))
361        value = E;
362      else if (name.equals("SQRRT2"))
363        value = SQRRT2;
364      else if (name.equals("LN2"))
365        value = LN2;
366      else if (name.equals("LN10"))
367        value = LN10;
368      else if (name.equals("LOG2E"))
369        value = LOG2E;
370      else if (name.equals("SQRT1_2"))
371        value = SQRT1_2;
372      
373      if (value != null)
374      {
375        int bits = new Double JavaDoc(precision).intValue();
376        
377        if (bits <= value.length())
378          value = value.substring(0, bits);
379          
380        return new Double JavaDoc(value).doubleValue();
381      }
382      else
383        return Double.NaN;
384             
385    }
386       
387 }
388
Popular Tags