6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: ExsltMath.java,v 1.1.2.1 2005/08/01 02:08:50 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xalan.internal.lib;
|
|
24 |
|
|
25 |
import com.sun.org.apache.xpath.internal.NodeSet;
|
|
26 |
|
|
27 |
import org.w3c.dom.Node;
|
|
28 |
import org.w3c.dom.NodeList;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* This class contains EXSLT math extension functions.
|
|
32 |
* It is accessed by specifying a namespace URI as follows:
|
|
33 |
* <pre>
|
|
34 |
* xmlns:math="http://exslt.org/math"
|
|
35 |
* </pre>
|
|
36 |
*
|
|
37 |
* The documentation for each function has been copied from the relevant
|
|
38 |
* EXSLT Implementer page.
|
|
39 |
*
|
|
40 |
* @see <a href="http://www.exslt.org/">EXSLT</a>
|
|
41 |
|
|
42 |
* @xsl.usage general
|
|
43 |
*/
|
|
44 |
public class ExsltMath extends ExsltBase
|
|
45 |
{
|
|
46 |
// Constants
|
|
47 |
private static String PI = "3.1415926535897932384626433832795028841971693993751";
|
|
48 |
private static String E = "2.71828182845904523536028747135266249775724709369996";
|
|
49 |
private static String SQRRT2 = "1.41421356237309504880168872420969807856967187537694";
|
|
50 |
private static String LN2 = "0.69314718055994530941723212145817656807550013436025";
|
|
51 |
private static String LN10 = "2.302585092994046";
|
|
52 |
private static String LOG2E = "1.4426950408889633";
|
|
53 |
private static String SQRT1_2 = "0.7071067811865476";
|
|
54 |
|
|
55 |
/**
|
|
56 |
* The math:max function returns the maximum value of the nodes passed as the argument.
|
|
57 |
* The maximum value is defined as follows. The node set passed as an argument is sorted
|
|
58 |
* in descending order as it would be by xsl:sort with a data type of number. The maximum
|
|
59 |
* is the result of converting the string value of the first node in this sorted list to
|
|
60 |
* a number using the number function.
|
|
61 |
* <p>
|
|
62 |
* If the node set is empty, or if the result of converting the string values of any of the
|
|
63 |
* nodes to a number is NaN, then NaN is returned.
|
|
64 |
*
|
|
65 |
* @param nl The NodeList for the node-set to be evaluated.
|
|
66 |
*
|
|
67 |
* @return the maximum value found, NaN if any node cannot be converted to a number.
|
|
68 |
*
|
|
69 |
* @see <a href="http://www.exslt.org/">EXSLT</a>
|
|
70 |
*/
|
|
71 |
public static double max (NodeList nl)
|
|
72 |
{
|
|
73 |
if (nl == null || nl.getLength() == 0)
|
|
74 |
return Double.NaN;
|
|
75 |
|
|
76 |
double m = - Double.MAX_VALUE;
|
|
77 |
for (int i = 0; i < nl.getLength(); i++)
|
|
78 |
{
|
|
79 |
Node n = nl.item(i);
|
|
80 |
double d = toNumber(n);
|
|
81 |
if (Double.isNaN(d))
|
|
82 |
return Double.NaN;
|
|
83 |
else if (d > m)
|
|
84 |
m = d;
|
|
85 |
}
|
|
86 |
|
|
87 |
return m;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* The math:min function returns the minimum value of the nodes passed as the argument.
|
|
92 |
* The minimum value is defined as follows. The node set passed as an argument is sorted
|
|
93 |
* in ascending order as it would be by xsl:sort with a data type of number. The minimum
|
|
94 |
* is the result of converting the string value of the first node in this sorted list to
|
|
95 |
* a number using the number function.
|
|
96 |
* <p>
|
|
97 |
* If the node set is empty, or if the result of converting the string values of any of
|
|
98 |
* the nodes to a number is NaN, then NaN is returned.
|
|
99 |
*
|
|
100 |
* @param nl The NodeList for the node-set to be evaluated.
|
|
101 |
*
|
|
102 |
* @return the minimum value found, NaN if any node cannot be converted to a number.
|
|
103 |
*
|
|
104 |
* @see <a href="http://www.exslt.org/">EXSLT</a>
|
|
105 |
*/
|
|
106 |
public static double min (NodeList nl)
|
|
107 |
{
|
|
108 |
if (nl == null || nl.getLength() == 0)
|
|
109 |
return Double.NaN;
|
|
110 |
|
|
111 |
double m = Double.MAX_VALUE;
|
|
112 |
for (int i = 0; i < nl.getLength(); i++)
|
|
113 |
{
|
|
114 |
Node n = nl.item(i);
|
|
115 |
double d = toNumber(n);
|
|
116 |
if (Double.isNaN(d))
|
|
117 |
return Double.NaN;
|
|
118 |
else if (d < m)
|
|
119 |
m = d;
|
|
120 |
}
|
|
121 |
|
|
122 |
return m;
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* The math:highest function returns the nodes in the node set whose value is the maximum
|
|
127 |
* value for the node set. The maximum value for the node set is the same as the value as
|
|
128 |
* calculated by math:max. A node has this maximum value if the result of converting its
|
|
129 |
* string value to a number as if by the number function is equal to the maximum value,
|
|
130 |
* where the equality comparison is defined as a numerical comparison using the = operator.
|
|
131 |
* <p>
|
|
132 |
* If any of the nodes in the node set has a non-numeric value, the math:max function will
|
|
133 |
* return NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any
|
|
134 |
* of the nodes in the node set has a non-numeric value, math:highest will return an empty
|
|
135 |
* node set.
|
|
136 |
*
|
|
137 |
* @param nl The NodeList for the node-set to be evaluated.
|
|
138 |
*
|
|
139 |
* @return node-set with nodes containing the maximum value found, an empty node-set
|
|
140 |
* if any node cannot be converted to a number.
|
|
141 |
*/
|
|
142 |
public static NodeList highest (NodeList nl)
|
|
143 |
{
|
|
144 |
double maxValue = max(nl);
|
|
145 |
|
|
146 |
NodeSet highNodes = new NodeSet();
|
|
147 |
highNodes.setShouldCacheNodes(true);
|
|
148 |
|
|
149 |
if (Double.isNaN(maxValue))
|
|
150 |
return highNodes; // empty Nodeset
|
|
151 |
|
|
152 |
for (int i = 0; i < nl.getLength(); i++)
|
|
153 |
{
|
|
154 |
Node n = nl.item(i);
|
|
155 |
double d = toNumber(n);
|
|
156 |
if (d == maxValue)
|
|
157 |
highNodes.addElement(n);
|
|
158 |
}
|
|
159 |
return highNodes;
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* The math:lowest function returns the nodes in the node set whose value is the minimum value
|
|
164 |
* for the node set. The minimum value for the node set is the same as the value as calculated
|
|
165 |
* by math:min. A node has this minimum value if the result of converting its string value to
|
|
166 |
* a number as if by the number function is equal to the minimum value, where the equality
|
|
167 |
* comparison is defined as a numerical comparison using the = operator.
|
|
168 |
* <p>
|
|
169 |
* If any of the nodes in the node set has a non-numeric value, the math:min function will return
|
|
170 |
* NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any of the nodes
|
|
171 |
* in the node set has a non-numeric value, math:lowest will return an empty node set.
|
|
172 |
*
|
|
173 |
* @param nl The NodeList for the node-set to be evaluated.
|
|
174 |
*
|
|
175 |
* @return node-set with nodes containing the minimum value found, an empty node-set
|
|
176 |
* if any node cannot be converted to a number.
|
|
177 |
*
|
|
178 |
*/
|
|
179 |
public static NodeList lowest (NodeList nl)
|
|
180 |
{
|
|
181 |
double minValue = min(nl);
|
|
182 |
|
|
183 |
NodeSet lowNodes = new NodeSet();
|
|
184 |
lowNodes.setShouldCacheNodes(true);
|
|
185 |
|
|
186 |
if (Double.isNaN(minValue))
|
|
187 |
return lowNodes; // empty Nodeset
|
|
188 |
|
|
189 |
for (int i = 0; i < nl.getLength(); i++)
|
|
190 |
{
|
|
191 |
Node n = nl.item(i);
|
|
192 |
double d = toNumber(n);
|
|
193 |
if (d == minValue)
|
|
194 |
lowNodes.addElement(n);
|
|
195 |
}
|
|
196 |
return lowNodes;
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* The math:abs function returns the absolute value of a number.
|
|
201 |
*
|
|
202 |
* @param num A number
|
|
203 |
* @return The absolute value of the number
|
|
204 |
*/
|
|
205 |
public static double abs(double num)
|
|
206 |
{
|
|
207 |
return Math.abs(num);
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* The math:acos function returns the arccosine value of a number.
|
|
212 |
*
|
|
213 |
* @param num A number
|
|
214 |
* @return The arccosine value of the number
|
|
215 |
*/
|
|
216 |
public static double acos(double num)
|
|
217 |
{
|
|
218 |
return Math.acos(num);
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* The math:asin function returns the arcsine value of a number.
|
|
223 |
*
|
|
224 |
* @param num A number
|
|
225 |
* @return The arcsine value of the number
|
|
226 |
*/
|
|
227 |
public static double asin(double num)
|
|
228 |
{
|
|
229 |
return Math.asin(num);
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* The math:atan function returns the arctangent value of a number.
|
|
234 |
*
|
|
235 |
* @param num A number
|
|
236 |
* @return The arctangent value of the number
|
|
237 |
*/
|
|
238 |
public static double atan(double num)
|
|
239 |
{
|
|
240 |
return Math.atan(num);
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* The math:atan2 function returns the angle ( in radians ) from the X axis to a point (y,x).
|
|
245 |
*
|
|
246 |
* @param num1 The X axis value
|
|
247 |
* @param num2 The Y axis value
|
|
248 |
* @return The angle (in radians) from the X axis to a point (y,x)
|
|
249 |
*/
|
|
250 |
public static double atan2(double num1, double num2)
|
|
251 |
{
|
|
252 |
return Math.atan2(num1, num2);
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* The math:cos function returns cosine of the passed argument.
|
|
257 |
*
|
|
258 |
* @param num A number
|
|
259 |
* @return The cosine value of the number
|
|
260 |
*/
|
|
261 |
public static double cos(double num)
|
|
262 |
{
|
|
263 |
return Math.cos(num);
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* The math:exp function returns e (the base of natural logarithms) raised to a power.
|
|
268 |
*
|
|
269 |
* @param num A number
|
|
270 |
* @return The value of e raised to the given power
|
|
271 |
*/
|
|
272 |
public static double exp(double num)
|
|
273 |
{
|
|
274 |
return Math.exp(num);
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* The math:log function returns the natural logarithm of a number.
|
|
279 |
*
|
|
280 |
* @param num A number
|
|
281 |
* @return The natural logarithm of the number
|
|
282 |
*/
|
|
283 |
public static double log(double num)
|
|
284 |
{
|
|
285 |
return Math.log(num);
|
|
286 |
}
|
|
287 |
|
|
288 |
/**
|
|
289 |
* The math:power function returns the value of a base expression taken to a specified power.
|
|
290 |
*
|
|
291 |
* @param num1 The base
|
|
292 |
* @param num2 The power
|
|
293 |
* @return The value of the base expression taken to the specified power
|
|
294 |
*/
|
|
295 |
public static double power(double num1, double num2)
|
|
296 |
{
|
|
297 |
return Math.pow(num1, num2);
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* The math:random function returns a random number from 0 to 1.
|
|
302 |
*
|
|
303 |
* @return A random double from 0 to 1
|
|
304 |
*/
|
|
305 |
public static double random()
|
|
306 |
{
|
|
307 |
return Math.random();
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* The math:sin function returns the sine of the number.
|
|
312 |
*
|
|
313 |
* @param num A number
|
|
314 |
* @return The sine value of the number
|
|
315 |
*/
|
|
316 |
public static double sin(double num)
|
|
317 |
{
|
|
318 |
return Math.sin(num);
|
|
319 |
}
|
|
320 |
|
|
321 |
/**
|
|
322 |
* The math:sqrt function returns the square root of a number.
|
|
323 |
*
|
|
324 |
* @param num A number
|
|
325 |
* @return The square root of the number
|
|
326 |
*/
|
|
327 |
public static double sqrt(double num)
|
|
328 |
{
|
|
329 |
return Math.sqrt(num);
|
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
|
333 |
* The math:tan function returns the tangent of the number passed as an argument.
|
|
334 |
*
|
|
335 |
* @param num A number
|
|
336 |
* @return The tangent value of the number
|
|
337 |
*/
|
|
338 |
public static double tan(double num)
|
|
339 |
{
|
|
340 |
return Math.tan(num);
|
|
341 |
}
|
|
342 |
|
|
343 |
/**
|
|
344 |
* The math:constant function returns the specified constant to a set precision.
|
|
345 |
* The possible constants are:
|
|
346 |
* <pre>
|
|
347 |
* PI
|
|
348 |
* E
|
|
349 |
* SQRRT2
|
|
350 |
* LN2
|
|
351 |
* LN10
|
|
352 |
* LOG2E
|
|
353 |
* SQRT1_2
|
|
354 |
* </pre>
|
|
355 |
* @param name The name of the constant
|
|
356 |
* @param precision The precision
|
|
357 |
* @return The value of the specified constant to the given precision
|
|
358 |
*/
|
|
359 |
public static double constant(String name, double precision)
|
|
360 |
{
|
|
361 |
String value = null;
|
|
362 |
if (name.equals("PI"))
|
|
363 |
value = PI;
|
|
364 |
else if (name.equals("E"))
|
|
365 |
value = E;
|
|
366 |
else if (name.equals("SQRRT2"))
|
|
367 |
value = SQRRT2;
|
|
368 |
else if (name.equals("LN2"))
|
|
369 |
value = LN2;
|
|
370 |
else if (name.equals("LN10"))
|
|
371 |
value = LN10;
|
|
372 |
else if (name.equals("LOG2E"))
|
|
373 |
value = LOG2E;
|
|
374 |
else if (name.equals("SQRT1_2"))
|
|
375 |
value = SQRT1_2;
|
|
376 |
|
|
377 |
if (value != null)
|
|
378 |
{
|
|
379 |
int bits = new Double(precision).intValue();
|
|
380 |
|
|
381 |
if (bits <= value.length())
|
|
382 |
value = value.substring(0, bits);
|
|
383 |
|
12458
|
384 |
return Double.parseDouble(value);
|
6
|
385 |
}
|
|
386 |
else
|
|
387 |
return Double.NaN;
|
|
388 |
|
|
389 |
}
|
|
390 |
|
|
391 |
}
|