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

Arguments passed to user defined function have unexpected types

    XMLWordPrintable

Details

    • b51
    • generic
    • generic
    • Verified

    Description



      Name: eaR10174 Date: 04/23/2004



      Arguments passed by javax.xml.xpath.XPath implementation to the user defined function
      have unexpected types. According to the javadoc the types should be either classes or
      subclasses of

        org.w3c.dom.NodeList (an unordered collection of nodes without duplicates)
        java.lang.Boolean
        java.lang.Double
        java.lanf.String

      but the types of the passed arguments are neither classes nor subclasses of the
      classes listed above.

      These tests were in jck_api_javax_xml.kfl with the bug

        4946192 XPathFunctionResolver is not used to resolve function name

      The bug affects the new JCK1.5 tests:

      api/javax_xml/xpath/XPathFunction/index.html#Evaluate[Evaluate001]
      api/javax_xml/xpath/XPathFunctionResolver/index.html#ResolveFunction[ResolveFunction001]

      The sample is provided below; it fails with JDK 1.5.0-beta-b48.
      ------------------------------------test.java-----------------------------
      import java.util.List;
      import javax.xml.XMLConstants;
      import javax.xml.namespace.QName;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathFactory;
      import javax.xml.xpath.XPathConstants;
      import javax.xml.xpath.XPathFunction;
      import javax.xml.xpath.XPathFunctionException;
      import javax.xml.xpath.XPathFunctionResolver;
      import org.w3c.dom.Node;
      import org.w3c.dom.Document;
      import org.w3c.dom.NodeList;
      import javax.xml.parsers.DocumentBuilderFactory;
      import org.w3c.dom.DOMImplementation;
      import javax.xml.namespace.NamespaceContext;
      import java.util.Iterator;
      import java.util.ArrayList;


      public class test {
          XPathFunction testFunc = new XPathFunction() {
                      public Object evaluate(List args) throws XPathFunctionException {
                          if (args.get(0) instanceof String) {
                              System.out.println("1st argument: OK");
                          } else {
                              System.out.println("1st argument: Failed. Class "
                                  + args.get(0).getClass().getName()
                                  + " is not subclass of String.");
                          }
                          if (args.get(1) instanceof Number) {
                              System.out.println("2nd argument: OK");
                          } else {
                              System.out.println("2st argument: Failed. Class "
                                  + args.get(1).getClass().getName()
                                  + " is not subclass of Number.");
                          }
                          if (args.get(2) instanceof Boolean) {
                              System.out.println("3d argument: OK");
                          } else {
                              System.out.println("3d argument: Failed. Class "
                                  + args.get(2).getClass().getName()
                                  + " is not subclass of Boolean.");
                          }
                          if (args.get(3) instanceof NodeList) {
                              System.out.println("4th argument: OK");
                          } else {
                              System.out.println("4th argument: Failed. Class "
                                  + args.get(3).getClass().getName()
                                  + " is not subclass of NodeList.");
                          }
                          return "OK";
                      }
          };

          public static void main(String argv[]) {
              test t = new test();
              t.run();
          }

          public void run() {
              try {
                  String expr = "nsTest:test('1', 1, true(), /nsTest:root)";
                  XPath xpath = createXPath();
                  XPathFunctionResolver funcResolver = new XPathFunctionResolver() {
                      public XPathFunction resolveFunction(QName functionName, int arity) {
                          if (!"test".equals(functionName.getLocalPart())) {
                              return null;
                          }
                          return testFunc;
                      }
                  };
                  xpath.setXPathFunctionResolver(funcResolver);
                  Document doc = createDocument("test://jaxp/xpath", "nsTest:root");
                  xpath.setNamespaceContext(new DOMNSContext(doc));

                  xpath.evaluate(expr, doc, XPathConstants.STRING);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }

          public static Document createDocument(String namespaceURI, String qName)
                  throws Exception {
              DOMImplementation domImpl = null;
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

              dbf.setNamespaceAware(true);
              domImpl = dbf.newDocumentBuilder().getDOMImplementation();

              return domImpl.createDocument(namespaceURI, qName, null);
          }

          public static XPath createXPath() throws Exception {
              XPathFactory xpf = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);

              return xpf.newXPath();
          }
      }


      class DOMNSContext implements NamespaceContext {
          private Node node;

          public DOMNSContext(Node node) {
              this.node = node;
          }

          public String getNamespaceURI(String prefix) {
              if (prefix == null) {
                  throw new IllegalArgumentException("The specified prefix is null.");
              }

              String uri = XMLConstants.NULL_NS_URI;

              if (this.node == null) {
                  return uri;
              }

              if ("xml".equals(prefix)) {
                  uri = XMLConstants.XML_NS_URI;
              } else if ("xmlns".equals(prefix)) {
                  uri = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
              } else {
                  prefix = ("".equals(prefix)) ? null : prefix;
                  uri = node.lookupNamespaceURI(prefix);
                  uri = (uri == null) ? XMLConstants.NULL_NS_URI : uri;
              }

              return uri;
          }

          public String getPrefix(String namespaceURI) {
              if (namespaceURI == null) {
                  throw new IllegalArgumentException("null namespaceURI");
              }

              String prefix = null;
              if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
                  prefix = XMLConstants.XML_NS_PREFIX;
              } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
                  prefix = XMLConstants.XMLNS_ATTRIBUTE;
              } else {
                  prefix = getPrefix(namespaceURI, this.node);
              }

              return prefix;
          }

          private String getPrefix(String namespaceURI, Node node) {
              String prefix = null;

              if (node == null) {
                  return prefix;
              }

              if (node.isDefaultNamespace(namespaceURI)) {
                  prefix = XMLConstants.DEFAULT_NS_PREFIX;
              } else {
                  prefix = node.lookupPrefix(namespaceURI);
              }

              return prefix;
          }

          public Iterator getPrefixes(String namespaceURI) {
              if (namespaceURI == null) {
                  throw new IllegalArgumentException("null namespaceURI");
              }

              ArrayList prefixes = new ArrayList();

              if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
                  prefixes.add(XMLConstants.XML_NS_PREFIX);
              } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
                  prefixes.add(XMLConstants.XMLNS_ATTRIBUTE);
              } else {
                  lookupPrefixes(this.node, namespaceURI, prefixes);
              }

              return prefixes.iterator();
          }

          private void lookupPrefixes(Node node, String namespaceURI, ArrayList prefixes) {
              if (node == null) {
                  return;
              }

              if (node.hasChildNodes()) {
                  lookupPrefixes(node.getFirstChild(), namespaceURI, prefixes);
              }

              String prefix = getPrefix(namespaceURI, node);
              if (prefix != null && !prefixes.contains(prefix)) {
                  prefixes.add(prefix);
              }

              lookupPrefixes(node.getNextSibling(), namespaceURI, prefixes);
          }
      }
      --------------------------------------------------------------------------
      % java -showversion test
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b48)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b48, mixed mode)

      1st argument: Failed. Class com.sun.org.apache.xpath.internal.objects.XString is not
      subclass of String.
      2st argument: Failed. Class com.sun.org.apache.xpath.internal.objects.XNumber is not
      subclass of Number.
      3d argument: Failed. Class com.sun.org.apache.xpath.internal.objects.XBooleanStatic
      is not subclass of Boolean.
      4th argument: Failed. Class com.sun.org.apache.xpath.internal.objects.XNodeSet is not
      subclass of NodeList.
      --------------------------------------------------------------------------

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

      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: