This is a duplicate of bug 20439.

Please attach your test program to this bug report so the developer can reproduce the error.

Thanks.

Joanne Tong



Fr�d�ric Laurent <[EMAIL PROTECTED]>

05/03/2004 03:13 PM

Please respond to
xalan-dev

To
[EMAIL PROTECTED]
cc
Subject
XPathNSResolver : ClassCastException exception





Hello

I previously posted this topic on xalan-user, but with no response. So
I'm posting here with hope :)

I'am trying to make XPathNSResolver work...
I made 2 versions for resolving prefixes, one works :

XPathNSResolver nsResolver =
   evaluator.createNSResolver(buildNSDoc().getDocumentElement());

buildNSDoc() is a simple method which builds a new DOM document with
only a root element and namespaces declaration. When I create my
resolver by giving the document, it works fine.

The other method doesn't :

I made a class that implements XPathNSResolver and gives the
good namespace for a given prefix. Then I set these resolver
to the XPath evaluator

  // !!!!! XPathNSResolver subclassing : doesn't work
  //XPathNSResolver nsResolver = new NamespacesRes();


but an exception is raised :


test:
     [java] java.lang.ClassCastException
     [java] at
org.apache.xpath.domapi.XPathEvaluatorImpl.createExpression(XPathEvaluatorImpl.java:165)
     [java] at
org.apache.xpath.domapi.XPathEvaluatorImpl.evaluate(XPathEvaluatorImpl.java:257)
     [java] at test.XPathNSResolverTest.evalXpath(Unknown Source)
     [java] at test.XPathNSResolverTest.main(Unknown Source)
     [java] Exception in thread "main"
     [java] Java Result: 1


any idea ?


I've got these XML buffer

<?xml version="1.0"?>
<ab:foo xmlns="http://ns.test.org/"
        xmlns:ab="http://ns.test.org/ab/"
        xmlns:xy="http://ns.test.org/xy/">
  <list><xy:item>item1</xy:item><xy:item>item2</xy:item></list>
</ab:foo>

------------------------ 8< ------------------------------------------
package test;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xpath.domapi.XPathEvaluatorImpl;
import org.w3c.dom.*;
import org.w3c.dom.xpath.*;
import org.xml.sax.InputSource;

class NamespacesRes implements XPathNSResolver {
  public String lookupNamespaceURI(String prefix) {
    System.out.println("lookupNamespaceURI " + prefix);
    if (prefix.equals("ab")) {
       return "http://ns.test.org/ab/";
    } else if (prefix.equals("xy")) {
       return "http://ns.test.org/xy/";
    } else if (prefix.equals("def")) {
       return "http://ns.test.org/";
    } else {
       return null;
    }
  }
}

public class XPathNSResolverTest {
  private Document doc = null;
  public XPathNSResolverTest() {
  InputSource is;
  try {
    is = new InputSource(new StringReader(
           "<?xml version=\"1.0\"?>"
           + "<ab:foo xmlns=\"http://ns.test.org/\" "
           + "     xmlns:ab=\"http://ns.test.org/ab/\" "
           + "     xmlns:xy=\"http://ns.test.org/xy/\">"
           + "<list><xy:item>item1</xy:item>"
           + "<xy:item>item2</xy:item></list>"
           + "</ab:foo>"));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    this.doc = dbf.newDocumentBuilder().parse(is);
  } catch (Exception e) {
    System.err.println(e.getMessage());
    e.printStackTrace();
  }
}


public Document buildNSDoc(){
  Document nsdoc=null;
  try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DOMImplementation impl =
      dbf.newDocumentBuilder().getDOMImplementation();
    nsdoc = impl.createDocument("http://ns.test.org/",
                                "def:nsdoc", null);
    Element root = nsdoc.getDocumentElement();
    root.setAttributeNS("http://www.w3.org/2000/xmlns/",
        "xmlns:ab", "http://ns.test.org/ab/");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/",
        "xmlns:xy", "http://ns.test.org/xy/");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/",
        "xmlns:def", "http://ns.test.org/");
  } catch (ParserConfigurationException e) {
    e.printStackTrace();
  }
  return nsdoc;
}

public void evalXpath(String xpath) {
  XPathEvaluator evaluator = new XPathEvaluatorImpl(doc);

  // !!!!! XPathNSResolver subclassing : doesn't work
  //XPathNSResolver nsResolver = new NamespacesRes();

  // works !
  XPathNSResolver nsResolver =
   evaluator.createNSResolver(buildNSDoc().getDocumentElement());
                                 
   XPathResult result = (XPathResult) evaluator.evaluate(xpath, doc,
      nsResolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

   System.out.println("Result >>");
   Node next = result.iterateNext();
   while (next != null) {
     XPathResult stringValue = (XPathResult) evaluator
         .evaluate("string()", next, nsResolver,
                   XPathResult.STRING_TYPE, null);

     System.out.println(stringValue.getStringValue());
     next = result.iterateNext();
   }
}

public static void main(String[] args) {
   XPathNSResolverTest test = new XPathNSResolverTest();
   test.evalXpath("/ab:foo/def:list/xy:item");
}

}
------------------------ 8< ------------------------------------------


regards

--
XPath free testing software :  http://lantern.sourceforge.net
Fr�d�ric Laurent                     http://www.opikanoba.org



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to