com.sun.org.apache.xpath.internal.XPath Java Examples

The following examples show how to use com.sun.org.apache.xpath.internal.XPath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExsltDynamic.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #2
Source File: ExsltDynamic.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #3
Source File: XPathEvaluatorImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #4
Source File: XPathEvaluatorImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #5
Source File: ExsltDynamic.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #6
Source File: ExsltDynamic.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #7
Source File: XPathEvaluatorImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #8
Source File: ExsltDynamic.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #9
Source File: XPathEvaluatorImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #10
Source File: ExsltDynamic.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #11
Source File: XPathEvaluatorImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #12
Source File: ExsltDynamic.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #13
Source File: XPathEvaluatorImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #14
Source File: ExsltDynamic.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #15
Source File: XPathEvaluatorImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #16
Source File: ExsltDynamic.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #17
Source File: XPathEvaluatorImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #18
Source File: XPathEvaluatorImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
* Creates a parsed XPath expression with resolved namespaces. This is
* useful when an expression will be reused in an application since it
* makes it possible to compile the expression string into a more
* efficient internal form and preresolve all namespace prefixes which
* occur within the expression.
*
* @param expression The XPath expression string to be parsed.
* @param resolver The <code>resolver</code> permits translation of
*   prefixes within the XPath expression into appropriate namespace URIs
*   . If this is specified as <code>null</code>, any namespace prefix
*   within the expression will result in <code>DOMException</code>
*   being thrown with the code <code>NAMESPACE_ERR</code>.
* @return The compiled form of the XPath expression.
* @exception XPathException
*   INVALID_EXPRESSION_ERR: Raised if the expression is not legal
*   according to the rules of the <code>XPathEvaluator</code>i
* @exception DOMException
*   NAMESPACE_ERR: Raised if the expression contains namespace prefixes
*   which cannot be resolved by the specified
*   <code>XPathNSResolver</code>.
*
    * @see org.w3c.dom.xpath.XPathEvaluator#createExpression(String, XPathNSResolver)
    */
   public XPathExpression createExpression(
           String expression,
           XPathNSResolver resolver)
           throws XPathException, DOMException {

           try {

                   // If the resolver is null, create a dummy prefix resolver
                   XPath xpath =  new XPath(expression,null,
                        ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)),
                         XPath.SELECT);

       return new XPathExpressionImpl(xpath, m_doc);

           } catch (TransformerException e) {
                   // Need to pass back exception code DOMException.NAMESPACE_ERR also.
                   // Error found in DOM Level 3 XPath Test Suite.
                   if(e instanceof XPathStylesheetDOM3Exception)
                           throw new DOMException(DOMException.NAMESPACE_ERR,e.getMessageAndLocation());
                   else
                           throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation());

           }
   }
 
Example #19
Source File: ExsltDynamic.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #20
Source File: ExsltDynamic.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * The dyn:evaluate function evaluates a string as an XPath expression and returns
 * the resulting value, which might be a boolean, number, string, node set, result
 * tree fragment or external object. The sole argument is the string to be evaluated.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath
 * expression (including an empty string), this function returns an empty node set.
 * <p>
 * You should only use this function if the expression must be constructed dynamically,
 * otherwise it is much more efficient to use the expression literally.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param xpathExpr The XPath expression string
 *
 * @return The evaluation result
 */
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
  throws SAXNotSupportedException
{
  if (myContext instanceof XPathContext.XPathExpressionContext)
  {
    XPathContext xctxt = null;
    try
    {
      xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
      XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);

      return dynamicXPath.execute(xctxt, myContext.getContextNode(),
                                  xctxt.getNamespaceContext());
    }
    catch (TransformerException e)
    {
      return new XNodeSet(xctxt.getDTMManager());
    }
  }
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
 
Example #21
Source File: SqlXmlHelperSun5.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Take the received string, which is an XML query expression, compile it, and
 * store the compiled query locally. Note that for now, we only support XPath
 * because that's what Xalan supports.
 * 
 * @param queryExpr
 *          The XPath expression to compile
 */
public void compileXQExpr(final String queryExpr, final String opName,
    final DocumentBuilder dBuilder) throws StandardException {
  try {

    /* The following XPath constructor compiles the expression
     * as part of the construction process.  We have to pass
     * in a PrefixResolver object in order to avoid NPEs when
     * invalid/unknown functions are used, so we just create
     * a dummy one, which means prefixes will not be resolved
     * in the query (Xalan will just throw an error if a prefix
     * is used).  In the future we may want to revisit this
     * to make it easier for users to query based on namespaces.
     */
    query = new XPath(queryExpr, null, new PrefixResolverDefault(
        dBuilder.newDocument()), XPath.SELECT);

  } catch (Throwable te) {

    /* Something went wrong during compilation of the
     * expression; wrap the error and re-throw it.
     * Note: we catch "Throwable" here to catch as many
     * Xalan-produced errors as possible in order to
     * minimize the chance of an uncaught Xalan error
     * (such as a NullPointerException) causing Derby
     * to fail in a more serious way.  In particular, an
     * uncaught Java exception like NPE can result in
     * Derby throwing "ERROR 40XT0: An internal error was
     * identified by RawStore module" for all statements on
     * the connection after the failure--which we clearly
     * don't want.  If we catch the error and wrap it,
     * though, the statement will fail but Derby will
     * continue to run as normal. 
     */
    throw StandardException.newException(SQLState.LANG_XML_QUERY_ERROR, te,
        opName, te.getMessage());

  }
}
 
Example #22
Source File: SqlXmlHelperSun5.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Take the received string, which is an XML query expression, compile it, and
 * store the compiled query locally. Note that for now, we only support XPath
 * because that's what Xalan supports.
 * 
 * @param queryExpr
 *          The XPath expression to compile
 */
public void compileXQExpr(final String queryExpr, final String opName,
    final DocumentBuilder dBuilder) throws StandardException {
  try {

    /* The following XPath constructor compiles the expression
     * as part of the construction process.  We have to pass
     * in a PrefixResolver object in order to avoid NPEs when
     * invalid/unknown functions are used, so we just create
     * a dummy one, which means prefixes will not be resolved
     * in the query (Xalan will just throw an error if a prefix
     * is used).  In the future we may want to revisit this
     * to make it easier for users to query based on namespaces.
     */
    query = new XPath(queryExpr, null, new PrefixResolverDefault(
        dBuilder.newDocument()), XPath.SELECT);

  } catch (Throwable te) {

    /* Something went wrong during compilation of the
     * expression; wrap the error and re-throw it.
     * Note: we catch "Throwable" here to catch as many
     * Xalan-produced errors as possible in order to
     * minimize the chance of an uncaught Xalan error
     * (such as a NullPointerException) causing Derby
     * to fail in a more serious way.  In particular, an
     * uncaught Java exception like NPE can result in
     * Derby throwing "ERROR 40XT0: An internal error was
     * identified by RawStore module" for all statements on
     * the connection after the failure--which we clearly
     * don't want.  If we catch the error and wrap it,
     * though, the statement will fail but Derby will
     * continue to run as normal. 
     */
    throw StandardException.newException(SQLState.LANG_XML_QUERY_ERROR, te,
        opName, te.getMessage());

  }
}
 
Example #23
Source File: ExsltDynamic.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #24
Source File: ExsltDynamic.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #25
Source File: ExsltDynamic.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #26
Source File: ExsltDynamic.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #27
Source File: ExsltDynamic.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #28
Source File: ExsltDynamic.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #29
Source File: ExsltDynamic.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example #30
Source File: ExsltDynamic.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;

}