You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Norris Boyd <nb...@atg.com> on 2001/02/26 21:40:38 UTC

Re: Xalan-J 2 Bug org.apache.xpath.XPathAPI.selectNodeList class casting error

There's already a bug filed on this; see
http://nagoya.betaversion.org/bugzilla/show_bug.cgi?id=702

I'll add your test case.

--N

"Atkins, Nathan" wrote:

> The selectNodeList method in XPathAPI throws a ClassCastException when it
> tries to cast the results of list.nodeset() to a NodeList.  The results of
> list.nodeset() is a NodeIterator.  Below is a test case to cause the
> exception.
>
> Test case output:
>
> Test1: Failed
> java.lang.ClassCastException: org.apache.xpath.axes.UnionPathIterator
>         at org.apache.xpath.XPathAPI.selectNodeList(XPathAPI.java:201)
>         at org.apache.xpath.XPathAPI.selectNodeList(XPathAPI.java:178)
>         at OrXPath.main(OrXPath.java:41)
> Test2: Passed
>   Node: a
>   Node: b
>   Node: a
>
> XPathAPI source code
>
>   /**
>    *  Use an XPath string to select a nodelist.
>    *  XPath namespace prefixes are resolved from the namespaceNode.
>    *
>    *  @param contextNode The node to start searching from.
>    *  @param str A valid XPath string.
>    *  @param namespaceNode The node from which prefixes in the XPath will be
> resolved to namespaces.
>    *  @return A NodeIterator, should never be null.
>    *
>    * @throws TransformerException
>    */
>   public static NodeList selectNodeList(
>           Node contextNode, String str, Node namespaceNode)
>             throws TransformerException
>   {
>
>     // Execute the XPath, and have it return the result
>     XObject list = eval(contextNode, str, namespaceNode);
>
>     // Have the XObject return its result as a NodeSet.
>     return (NodeList) list.nodeset();
>   }
>
> Test case:
>
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.io.StringReader;
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
> import javax.xml.parsers.ParserConfigurationException;
> import javax.xml.transform.TransformerException;
>
> import org.apache.xpath.XPathAPI;
> import org.w3c.dom.Document;
> import org.w3c.dom.Node;
> import org.w3c.dom.NodeList;
> import org.w3c.dom.traversal.NodeIterator;
> import org.xml.sax.SAXException;
> import org.xml.sax.InputSource;
>
> public class OrXPath
>         {
>         public static void main
>                 (
>                 String[] args
>                 )
>                 throws ParserConfigurationException,
>                         SAXException,
>                         IOException,
>                         FileNotFoundException,
>                         TransformerException
>                 {
>                 final String OR_XPATH = "//a|//b";
>                 DocumentBuilderFactory docBuilderFactory =
> DocumentBuilderFactory.newInstance();
>                 DocumentBuilder docBuilder =
> docBuilderFactory.newDocumentBuilder();
>
>                 String testString1 =
> "<container><a>data1</a><b>data2</b><a>data3</a></container>";
>
>                 InputSource inputSource = new InputSource( new StringReader(
> testString1 ) );
>                 Document testDoc1 = docBuilder.parse( inputSource );
>
>                 String testCaseName = "Test1";
>                 try
>                         {
>                         NodeList nodes = XPathAPI.selectNodeList( testDoc1,
> OR_XPATH );
>                         System.out.println( testCaseName + ": Passed" );
>                         }
>                 catch( Exception e )
>                         {
>                         System.out.println( testCaseName + ": Failed" );
>                         e.printStackTrace( System.out );
>                         }
>
>                 testCaseName = "Test2";
>                 try
>                         {
>                         NodeIterator nodes = XPathAPI.selectNodeIterator(
> testDoc1, OR_XPATH );
>                         Node node = null;
>                         System.out.println( testCaseName + ": Passed" );
>                         while( ( node = nodes.nextNode() ) != null )
>                                 {
>                                 System.out.println( "  Node: " +
> node.getNodeName() );
>                                 }
>
>                         }
>                 catch( Exception e )
>                         {
>                         System.out.println( testCaseName + ": Failed" );
>                         }
>                 }
>         }