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

throw XPathExpressionException when context is null and expression refers contex

    Details

    • Subcomponent:
    • Resolved In Build:
      1.4
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description

        XPath.evaluate(java.lang.String expression, java.lang.Object item, QName returnType) should throw XPathExpressionException when 'item' is null and expression refers to the context .

        According to the spec :
         In XPath.evaluate(java.lang.String expression,java.lang.Object item,QName returnType) , a null value for item indicates that the expression should be evaluated without a context . In the absence of a context item, simple expressions, such as "1+1", can be evaluated . Any expression that refers to the context will throw an XPathExpressionException

        -----------------------------------
        java -version
        java version "1.5.0-beta2"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b37)
        Java HotSpot(TM) Client VM (build 1.5.0-beta2-b37, mixed mode)
        -----------------------------------
        javac Test1.java
        java Test1
        Test1/checkXPath09() : OK : 3.0
        Test1/checkXPath09() : Failed - expected XPathExpressionException not thrown ::

        Testcase - Test1.java
        ------------------------------------------------------------------------------

        import javax.xml.xpath.XPath;
        import javax.xml.xpath.XPathFactory;
        import javax.xml.xpath.XPathConstants;
        import javax.xml.xpath.XPathExpression;
        import javax.xml.xpath.XPathFactoryConfigurationException;
        import javax.xml.xpath.XPathExpressionException;

        import org.w3c.dom.Document;

        import java.io.File;


        public class Test1 {
           
           Document document1 = null;
           
           Test1 () {}
            
            // main()
            public static void main(String[] argv) {
                Test1 test1 = new Test1();
                test1.checkXPath09();
            }
            // end main()

            // test for XPath.evaluate(java.lang.String expression, java.lang.Object item, QName returnType) , returnType is String
            // item is null , A null value for item indicates that the expression should be evaluated without a context.
            // In the absence of a context item, simple expressions, such as "1+1", can be evaluated.
            // Any expression that refers to the context will throw an XPathExpressionException
            private void checkXPath09() {
             try{
             XPathFactory xpathFactory = XPathFactory.newInstance();
             if( xpathFactory instanceof XPathFactory ){
             XPath xpath = xpathFactory.newXPath();
        if ( xpath instanceof XPath ){
        String expression1 = "1+2";
        Double result = (Double)xpath.evaluate(expression1, document1, XPathConstants.NUMBER);
        System.out.println("Test1/checkXPath09() : OK : "+result);
        String expression2 = "/widgets/widget[@name='a']/@quantity" ;
        String quantity = (String)xpath.evaluate(expression2, document1, XPathConstants.STRING);
        System.out.println("Test1/checkXPath09() : Failed - expected XPathExpressionException not thrown :"+quantity+":");
        }
             else{
             System.out.println("checkXPath09() failed - xpath not instance of XPath");
             }
             }else{
             System.out.println(" checkXPath09() failed - creating instance of XPathFactory ");
             }
             }catch(NullPointerException npe ){
             System.out.println(" checkXPathFactory09() - failed , NPE thrown ");
             }catch(XPathFactoryConfigurationException xpfce ){
             System.out.println(" checkXPathFactory09() - failed , Default object model not available ");
             }catch(XPathExpressionException xpee ){
             System.out.println(" checkXPathFactory09() - passed , expected XPathExpressionException thrown ");
             }catch(Exception e){
             System.out.println(" Exception thrown - checkXPath09() failed ");
             }
            }
        }


        -------------------------------------------------------------------------------
        This bug is mostly fixed , now there are only a few contexts
        which don't throw an exception (such as when the context is ".")

        lowering the priority of this bug to a P4 given
        that it currently is mostly fixed and this bug seems to deal
        with just a corner case .

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  duke J. Duke (Inactive)
                  Reporter:
                  saksunw Sreejith Ak (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  0 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: