⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exsltdynamic.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: ExsltDynamic.java,v 1.1.2.1 2005/08/01 02:08:51 jeffsuttor Exp $ */package com.sun.org.apache.xalan.internal.lib;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.TransformerException;import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;import com.sun.org.apache.xalan.internal.res.XSLMessages;import com.sun.org.apache.xalan.internal.res.XSLTErrorResources;import com.sun.org.apache.xpath.internal.NodeSet;import com.sun.org.apache.xpath.internal.NodeSetDTM;import com.sun.org.apache.xpath.internal.XPath;import com.sun.org.apache.xpath.internal.XPathContext;import com.sun.org.apache.xpath.internal.objects.XBoolean;import com.sun.org.apache.xpath.internal.objects.XNodeSet;import com.sun.org.apache.xpath.internal.objects.XNumber;import com.sun.org.apache.xpath.internal.objects.XObject;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.xml.sax.SAXNotSupportedException;/** * This class contains EXSLT dynamic extension functions. * * It is accessed by specifying a namespace URI as follows: * <pre> *    xmlns:dyn="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> * @xsl.usage general */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.MAX_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();      }      catch (TransformerException e)      {        xctxt.popCurrentNode();        xctxt.popContextNodeList();        return Double.NaN;      }            xctxt.popCurrentNode();            sum = sum + result;                  }                xctxt.popContextNodeList();    return sum;  }  /**   * The dyn:map function evaluates the expression passed as the second argument for    * each of the nodes passed as the first argument, and returns a node set of those values. 

⌨️ 快捷键说明

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