286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: ExsltMath.java,v 1.1.2.1 2005/08/01 02:08:50 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xalan.internal.lib;
286N/A
286N/Aimport com.sun.org.apache.xpath.internal.NodeSet;
286N/A
286N/Aimport org.w3c.dom.Node;
286N/Aimport org.w3c.dom.NodeList;
286N/A
286N/A/**
286N/A * This class contains EXSLT math extension functions.
286N/A * It is accessed by specifying a namespace URI as follows:
286N/A * <pre>
286N/A * xmlns:math="http://exslt.org/math"
286N/A * </pre>
286N/A *
286N/A * The documentation for each function has been copied from the relevant
286N/A * EXSLT Implementer page.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A
286N/A * @xsl.usage general
286N/A */
286N/Apublic class ExsltMath extends ExsltBase
286N/A{
286N/A // Constants
286N/A private static String PI = "3.1415926535897932384626433832795028841971693993751";
286N/A private static String E = "2.71828182845904523536028747135266249775724709369996";
286N/A private static String SQRRT2 = "1.41421356237309504880168872420969807856967187537694";
286N/A private static String LN2 = "0.69314718055994530941723212145817656807550013436025";
286N/A private static String LN10 = "2.302585092994046";
286N/A private static String LOG2E = "1.4426950408889633";
286N/A private static String SQRT1_2 = "0.7071067811865476";
286N/A
286N/A /**
286N/A * The math:max function returns the maximum value of the nodes passed as the argument.
286N/A * The maximum value is defined as follows. The node set passed as an argument is sorted
286N/A * in descending order as it would be by xsl:sort with a data type of number. The maximum
286N/A * is the result of converting the string value of the first node in this sorted list to
286N/A * a number using the number function.
286N/A * <p>
286N/A * If the node set is empty, or if the result of converting the string values of any of the
286N/A * nodes to a number is NaN, then NaN is returned.
286N/A *
286N/A * @param nl The NodeList for the node-set to be evaluated.
286N/A *
286N/A * @return the maximum value found, NaN if any node cannot be converted to a number.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static double max (NodeList nl)
286N/A {
286N/A if (nl == null || nl.getLength() == 0)
286N/A return Double.NaN;
286N/A
286N/A double m = - Double.MAX_VALUE;
286N/A for (int i = 0; i < nl.getLength(); i++)
286N/A {
286N/A Node n = nl.item(i);
286N/A double d = toNumber(n);
286N/A if (Double.isNaN(d))
286N/A return Double.NaN;
286N/A else if (d > m)
286N/A m = d;
286N/A }
286N/A
286N/A return m;
286N/A }
286N/A
286N/A /**
286N/A * The math:min function returns the minimum value of the nodes passed as the argument.
286N/A * The minimum value is defined as follows. The node set passed as an argument is sorted
286N/A * in ascending order as it would be by xsl:sort with a data type of number. The minimum
286N/A * is the result of converting the string value of the first node in this sorted list to
286N/A * a number using the number function.
286N/A * <p>
286N/A * If the node set is empty, or if the result of converting the string values of any of
286N/A * the nodes to a number is NaN, then NaN is returned.
286N/A *
286N/A * @param nl The NodeList for the node-set to be evaluated.
286N/A *
286N/A * @return the minimum value found, NaN if any node cannot be converted to a number.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static double min (NodeList nl)
286N/A {
286N/A if (nl == null || nl.getLength() == 0)
286N/A return Double.NaN;
286N/A
286N/A double m = Double.MAX_VALUE;
286N/A for (int i = 0; i < nl.getLength(); i++)
286N/A {
286N/A Node n = nl.item(i);
286N/A double d = toNumber(n);
286N/A if (Double.isNaN(d))
286N/A return Double.NaN;
286N/A else if (d < m)
286N/A m = d;
286N/A }
286N/A
286N/A return m;
286N/A }
286N/A
286N/A /**
286N/A * The math:highest function returns the nodes in the node set whose value is the maximum
286N/A * value for the node set. The maximum value for the node set is the same as the value as
286N/A * calculated by math:max. A node has this maximum value if the result of converting its
286N/A * string value to a number as if by the number function is equal to the maximum value,
286N/A * where the equality comparison is defined as a numerical comparison using the = operator.
286N/A * <p>
286N/A * If any of the nodes in the node set has a non-numeric value, the math:max function will
286N/A * return NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any
286N/A * of the nodes in the node set has a non-numeric value, math:highest will return an empty
286N/A * node set.
286N/A *
286N/A * @param nl The NodeList for the node-set to be evaluated.
286N/A *
286N/A * @return node-set with nodes containing the maximum value found, an empty node-set
286N/A * if any node cannot be converted to a number.
286N/A */
286N/A public static NodeList highest (NodeList nl)
286N/A {
286N/A double maxValue = max(nl);
286N/A
286N/A NodeSet highNodes = new NodeSet();
286N/A highNodes.setShouldCacheNodes(true);
286N/A
286N/A if (Double.isNaN(maxValue))
286N/A return highNodes; // empty Nodeset
286N/A
286N/A for (int i = 0; i < nl.getLength(); i++)
286N/A {
286N/A Node n = nl.item(i);
286N/A double d = toNumber(n);
286N/A if (d == maxValue)
286N/A highNodes.addElement(n);
286N/A }
286N/A return highNodes;
286N/A }
286N/A
286N/A /**
286N/A * The math:lowest function returns the nodes in the node set whose value is the minimum value
286N/A * for the node set. The minimum value for the node set is the same as the value as calculated
286N/A * by math:min. A node has this minimum value if the result of converting its string value to
286N/A * a number as if by the number function is equal to the minimum value, where the equality
286N/A * comparison is defined as a numerical comparison using the = operator.
286N/A * <p>
286N/A * If any of the nodes in the node set has a non-numeric value, the math:min function will return
286N/A * NaN. The definition numeric comparisons entails that NaN != NaN. Therefore if any of the nodes
286N/A * in the node set has a non-numeric value, math:lowest will return an empty node set.
286N/A *
286N/A * @param nl The NodeList for the node-set to be evaluated.
286N/A *
286N/A * @return node-set with nodes containing the minimum value found, an empty node-set
286N/A * if any node cannot be converted to a number.
286N/A *
286N/A */
286N/A public static NodeList lowest (NodeList nl)
286N/A {
286N/A double minValue = min(nl);
286N/A
286N/A NodeSet lowNodes = new NodeSet();
286N/A lowNodes.setShouldCacheNodes(true);
286N/A
286N/A if (Double.isNaN(minValue))
286N/A return lowNodes; // empty Nodeset
286N/A
286N/A for (int i = 0; i < nl.getLength(); i++)
286N/A {
286N/A Node n = nl.item(i);
286N/A double d = toNumber(n);
286N/A if (d == minValue)
286N/A lowNodes.addElement(n);
286N/A }
286N/A return lowNodes;
286N/A }
286N/A
286N/A /**
286N/A * The math:abs function returns the absolute value of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The absolute value of the number
286N/A */
286N/A public static double abs(double num)
286N/A {
286N/A return Math.abs(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:acos function returns the arccosine value of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The arccosine value of the number
286N/A */
286N/A public static double acos(double num)
286N/A {
286N/A return Math.acos(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:asin function returns the arcsine value of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The arcsine value of the number
286N/A */
286N/A public static double asin(double num)
286N/A {
286N/A return Math.asin(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:atan function returns the arctangent value of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The arctangent value of the number
286N/A */
286N/A public static double atan(double num)
286N/A {
286N/A return Math.atan(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:atan2 function returns the angle ( in radians ) from the X axis to a point (y,x).
286N/A *
286N/A * @param num1 The X axis value
286N/A * @param num2 The Y axis value
286N/A * @return The angle (in radians) from the X axis to a point (y,x)
286N/A */
286N/A public static double atan2(double num1, double num2)
286N/A {
286N/A return Math.atan2(num1, num2);
286N/A }
286N/A
286N/A /**
286N/A * The math:cos function returns cosine of the passed argument.
286N/A *
286N/A * @param num A number
286N/A * @return The cosine value of the number
286N/A */
286N/A public static double cos(double num)
286N/A {
286N/A return Math.cos(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:exp function returns e (the base of natural logarithms) raised to a power.
286N/A *
286N/A * @param num A number
286N/A * @return The value of e raised to the given power
286N/A */
286N/A public static double exp(double num)
286N/A {
286N/A return Math.exp(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:log function returns the natural logarithm of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The natural logarithm of the number
286N/A */
286N/A public static double log(double num)
286N/A {
286N/A return Math.log(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:power function returns the value of a base expression taken to a specified power.
286N/A *
286N/A * @param num1 The base
286N/A * @param num2 The power
286N/A * @return The value of the base expression taken to the specified power
286N/A */
286N/A public static double power(double num1, double num2)
286N/A {
286N/A return Math.pow(num1, num2);
286N/A }
286N/A
286N/A /**
286N/A * The math:random function returns a random number from 0 to 1.
286N/A *
286N/A * @return A random double from 0 to 1
286N/A */
286N/A public static double random()
286N/A {
286N/A return Math.random();
286N/A }
286N/A
286N/A /**
286N/A * The math:sin function returns the sine of the number.
286N/A *
286N/A * @param num A number
286N/A * @return The sine value of the number
286N/A */
286N/A public static double sin(double num)
286N/A {
286N/A return Math.sin(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:sqrt function returns the square root of a number.
286N/A *
286N/A * @param num A number
286N/A * @return The square root of the number
286N/A */
286N/A public static double sqrt(double num)
286N/A {
286N/A return Math.sqrt(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:tan function returns the tangent of the number passed as an argument.
286N/A *
286N/A * @param num A number
286N/A * @return The tangent value of the number
286N/A */
286N/A public static double tan(double num)
286N/A {
286N/A return Math.tan(num);
286N/A }
286N/A
286N/A /**
286N/A * The math:constant function returns the specified constant to a set precision.
286N/A * The possible constants are:
286N/A * <pre>
286N/A * PI
286N/A * E
286N/A * SQRRT2
286N/A * LN2
286N/A * LN10
286N/A * LOG2E
286N/A * SQRT1_2
286N/A * </pre>
286N/A * @param name The name of the constant
286N/A * @param precision The precision
286N/A * @return The value of the specified constant to the given precision
286N/A */
286N/A public static double constant(String name, double precision)
286N/A {
286N/A String value = null;
286N/A if (name.equals("PI"))
286N/A value = PI;
286N/A else if (name.equals("E"))
286N/A value = E;
286N/A else if (name.equals("SQRRT2"))
286N/A value = SQRRT2;
286N/A else if (name.equals("LN2"))
286N/A value = LN2;
286N/A else if (name.equals("LN10"))
286N/A value = LN10;
286N/A else if (name.equals("LOG2E"))
286N/A value = LOG2E;
286N/A else if (name.equals("SQRT1_2"))
286N/A value = SQRT1_2;
286N/A
286N/A if (value != null)
286N/A {
286N/A int bits = new Double(precision).intValue();
286N/A
286N/A if (bits <= value.length())
286N/A value = value.substring(0, bits);
286N/A
293N/A return Double.parseDouble(value);
286N/A }
286N/A else
286N/A return Double.NaN;
286N/A
286N/A }
286N/A
286N/A}