Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4946192

XPathFunctionResolver is not used to resolve function name

    XMLWordPrintable

Details

    • b46
    • generic
    • generic
    • Verified

    Description



      Name: eaR10174 Date: 10/30/2003


        The method

            public XPathFunction resolveFunction(QName functionName, int arity)

      of the javax.xml.xpath.XPathFunctionResolver implementation is not invoked during
      evaluation of xpath expression in case when an expression contains a user defined function
      (see test.java below). According to the method XPath.evaluate() javadoc:
        
        public Object evaluate(String expression,
                             Object item,
                             QName returnType)
                      throws XPathException
        ...
        If the expression contains a function reference, the function will be found through the
        function resolver. An exception is raised if the function resolver is undefined or the
        resolver returns null for the function.
                      
      the method should be invoked.

        The bug appears in jdk1.5.0beta-b25 and affects a new JCK1.5 test:

          api/javax_xml/xpath/XPath/index.html#Evaluate[Evaluate028]


      ------------------------------------test.java-----------------------------
      import java.util.List;
      import java.util.Iterator;
      import javax.xml.namespace.QName;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathFactory;
      import javax.xml.xpath.XPathConstants;
      import javax.xml.xpath.XPathException;
      import javax.xml.xpath.XPathFunction;
      import javax.xml.xpath.XPathFunctionResolver;

      public class test {

          public static void main (String[] args) {
              try {
                  String expr = "join(1, 2, 3)";
                  XPathFactory xpf = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
                  XPath xpath = xpf.newXPath();
                  XPathFunctionResolver funcResolver = new XPathFunctionResolver() {
                      public XPathFunction resolveFunction(QName functionName, int arity) {
                          System.out.println("resolveFunction: INVOKED.");
                          if (!"join".equals(functionName.getLocalPart()) || (arity != 3)) {
                              return null;
                          }
                          return new XPathFunction() {
                              public Object evaluate(List args) throws XPathException {
                                  if (args.isEmpty()) {
                                      return "";
                                  }
                                  StringBuffer sb = new StringBuffer();
                                  for (Iterator i = args.iterator(); i.hasNext();) {
                                      sb.append(i).append(",");
                                  }
                                  return sb.substring(0, sb.length() - 1);
                              }
                          };
                      }
                  };
                  xpath.setFunctionResolver(funcResolver);

                  String result = (String)xpath.evaluate(expr, (Object)null,
                                                         XPathConstants.STRING);

                  if ("1,2,3".equals(result)) {
                      System.out.println("OK");
                  } else {
                      System.out.println("Unexpected result is returned.");
                  }
              } catch (XPathException e) {
                  e.printStackTrace();
              }
          }
      }
      --------------------------------------------------------------------------
      % java -showversion test
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b25)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b25, mixed mode)

      javax.xml.xpath.XPathException: javax.xml.transform.TransformerException: Could not find
      function: join
              at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:184)
              at test.main(test.java:40)
      Caused by: javax.xml.transform.TransformerException: Could not find function: join
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.error(XPathParser.java:640)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.FunctionCall(XPathParser.java:1487)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.PrimaryExpr(XPathParser.java:1426)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.FilterExpr(XPathParser.java:1325)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(XPathParser.java:1258)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(XPathParser.java:1216)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(XPathParser.java:1122)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(XPathParser.java
      :1043)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(XPathParser.java:985)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(XPathParser.java:910
      )
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(XPathParser.java:850)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(XPathParser.java:814)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(XPathParser.java:787)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(XPathParser.java:770)
              at
      com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:163)
              at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:200)
              at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:236)
              at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:124)
              at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:181)
              ... 1 more
      --------------------------------------------------------------------------

      ======================================================================

      Attachments

        Activity

          People

            rmandavasunw Ramesh Mandava (Inactive)
            evgsunw Evg Evg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: