exsltdynamic.java

来自「java jdk 1.4的源码」· Java 代码 · 共 644 行 · 第 1/2 页

JAVA
644
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.lib;import org.w3c.dom.*;import org.apache.xpath.objects.XObject;import org.apache.xpath.objects.XBoolean;import org.apache.xpath.objects.XNumber;import org.apache.xpath.objects.XNodeSet;import org.apache.xpath.XPath;import org.apache.xpath.XPathContext;import org.apache.xpath.NodeSet;import org.apache.xpath.NodeSetDTM;import org.xml.sax.SAXNotSupportedException;import org.apache.xalan.extensions.ExpressionContext;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.res.XSLTErrorResources;import javax.xml.transform.*;import javax.xml.parsers.*;/** * <meta name="usage" content="general"/> * This class contains EXSLT dynamic extension functions. * * It is accessed by specifying a namespace URI as follows: * <pre> *    xmlns:math="http://exslt.org/dynamic" * </pre> * The documentation for each function has been copied from the relevant * EXSLT Implementer page. *  * @see <a href="http://www.exslt.org/">EXSLT</a> */public class ExsltDynamic extends ExsltBase{   public static final String EXSL_URI = "http://exslt.org/common";     /**   * The dyn:max function calculates the maximum value for the nodes passed as    * the first argument, where the value of each node is calculated dynamically    * using an XPath expression passed as a string as the second argument.    * <p>   * The expressions are evaluated relative to the nodes passed as the first argument.   * In other words, the value for each node is calculated by evaluating the XPath    * expression with all context information being the same as that for the call to    * the dyn:max function itself, except for the following:   * <p>   * <ul>   *  <li>the context node is the node whose value is being calculated.</li>   *  <li>the context position is the position of the node within the node set passed as    *   the first argument to the dyn:max function, arranged in document order.</li>   *  <li>the context size is the number of nodes passed as the first argument to the    *   dyn:max function.</li>   * </ul>   * <p>   * The dyn:max function returns the maximum of these values, calculated in exactly    * the same way as for math:max.    * <p>   * If the expression string passed as the second argument is an invalid XPath    * expression (including an empty string), this function returns NaN.    * <p>   * This function must take a second argument. To calculate the maximum of a set of    * nodes based on their string values, you should use the math:max function.   *   * @param myContext The ExpressionContext passed by the extension processor   * @param nl The node set   * @param expr The expression string   *   * @return The maximum evaluation value   */  public static double max(ExpressionContext myContext, NodeList nl, String expr)    throws SAXNotSupportedException  {    XPathContext xctxt = null;    if (myContext instanceof XPathContext.XPathExpressionContext)      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();    else      throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));    if (expr == null || expr.length() == 0)      return Double.NaN;          NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);    xctxt.pushContextNodeList(contextNodes);        double maxValue = Double.MIN_VALUE;    for (int i = 0; i < contextNodes.getLength(); i++)    {      int contextNode = contextNodes.item(i);      xctxt.pushCurrentNode(contextNode);            double result = 0;      try      {        XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),                                       xctxt.getNamespaceContext(),                                       XPath.SELECT);        result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();      }      catch (TransformerException e)      {        xctxt.popCurrentNode();        xctxt.popContextNodeList();        return Double.NaN;      }            xctxt.popCurrentNode();                    if (result > maxValue)          maxValue = result;    }                xctxt.popContextNodeList();    return maxValue;          }    /**   * The dyn:min function calculates the minimum value for the nodes passed as the    * first argument, where the value of each node is calculated dynamically using    * an XPath expression passed as a string as the second argument.    * <p>   * The expressions are evaluated relative to the nodes passed as the first argument.    * In other words, the value for each node is calculated by evaluating the XPath    * expression with all context information being the same as that for the call to    * the dyn:min function itself, except for the following:    * <p>   * <ul>   *  <li>the context node is the node whose value is being calculated.</li>   *  <li>the context position is the position of the node within the node set passed    *    as the first argument to the dyn:min function, arranged in document order.</li>   *  <li>the context size is the number of nodes passed as the first argument to the    *    dyn:min function.</li>   * </ul>   * <p>   * The dyn:min function returns the minimum of these values, calculated in exactly    * the same way as for math:min.    * <p>   * If the expression string passed as the second argument is an invalid XPath expression    * (including an empty string), this function returns NaN.    * <p>   * This function must take a second argument. To calculate the minimum of a set of    * nodes based on their string values, you should use the math:min function.   *   * @param myContext The ExpressionContext passed by the extension processor   * @param nl The node set   * @param expr The expression string   *   * @return The minimum evaluation value   */  public static double min(ExpressionContext myContext, NodeList nl, String expr)    throws SAXNotSupportedException  {        XPathContext xctxt = null;    if (myContext instanceof XPathContext.XPathExpressionContext)      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();    else      throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));    if (expr == null || expr.length() == 0)      return Double.NaN;          NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);    xctxt.pushContextNodeList(contextNodes);        double minValue = Double.MAX_VALUE;    for (int i = 0; i < nl.getLength(); i++)    {      int contextNode = contextNodes.item(i);      xctxt.pushCurrentNode(contextNode);            double result = 0;      try      {        XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),                                       xctxt.getNamespaceContext(),                                       XPath.SELECT);        result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();      }      catch (TransformerException e)      {        xctxt.popCurrentNode();        xctxt.popContextNodeList();        return Double.NaN;      }            xctxt.popCurrentNode();                    if (result < minValue)          minValue = result;    }                xctxt.popContextNodeList();    return minValue;    }  /**   * The dyn:sum function calculates the sum for the nodes passed as the first argument,    * where the value of each node is calculated dynamically using an XPath expression    * passed as a string as the second argument.    * <p>   * The expressions are evaluated relative to the nodes passed as the first argument.    * In other words, the value for each node is calculated by evaluating the XPath    * expression with all context information being the same as that for the call to    * the dyn:sum function itself, except for the following:    * <p>   * <ul>   *  <li>the context node is the node whose value is being calculated.</li>   *  <li>the context position is the position of the node within the node set passed as    *    the first argument to the dyn:sum function, arranged in document order.</li>   *  <li>the context size is the number of nodes passed as the first argument to the    *    dyn:sum function.</li>   * </ul>   * <p>   * The dyn:sum function returns the sumimum of these values, calculated in exactly    * the same way as for sum.    * <p>   * If the expression string passed as the second argument is an invalid XPath    * expression (including an empty string), this function returns NaN.    * <p>   * This function must take a second argument. To calculate the sumimum of a set of    * nodes based on their string values, you should use the sum function.   *   * @param myContext The ExpressionContext passed by the extension processor   * @param nl The node set   * @param expr The expression string   *   * @return The sum of the evaluation value on each node   */  public static double sum(ExpressionContext myContext, NodeList nl, String expr)    throws SAXNotSupportedException  {    XPathContext xctxt = null;    if (myContext instanceof XPathContext.XPathExpressionContext)      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();    else      throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));    if (expr == null || expr.length() == 0)      return Double.NaN;          NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);    xctxt.pushContextNodeList(contextNodes);        double sum = 0;    for (int i = 0; i < nl.getLength(); i++)    {      int contextNode = contextNodes.item(i);      xctxt.pushCurrentNode(contextNode);            double result = 0;      try      {        XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),                                       xctxt.getNamespaceContext(),                                       XPath.SELECT);        result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();      }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?