Commons JXPath
  1. Commons JXPath
  2. JXPATH-89

Cannot compare pointers that do not belong to the same tree

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2 Final, Nightly Builds
    • Fix Version/s: 1.3
    • Labels:
      None
    • Environment:

      codebase: 1.2 or latest version from SVN, JDK 1.4

      Description

      For XPath "$var | /MAIN/A" exception is thrown:

      org.apache.commons.jxpath.JXPathException: Cannot compare pointers that do not belong to the same tree: '$var' and ''
      at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:665)
      at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:649)
      at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:649)
      at org.apache.commons.jxpath.ri.model.NodePointer.compareTo(NodePointer.java:639)
      at java.util.Arrays.mergeSort(Arrays.java:1152)
      at java.util.Arrays.sort(Arrays.java:1079)
      at java.util.Collections.sort(Collections.java:113)
      at org.apache.commons.jxpath.ri.EvalContext.constructIterator(EvalContext.java:176)
      at org.apache.commons.jxpath.ri.EvalContext.hasNext(EvalContext.java:100)
      at org.apache.commons.jxpath.JXPathContext.selectNodes(JXPathContext.java:648)
      at org.apache.commons.jxpath.ri.model.VariablePointerTestCase.testUnionOfVariableAndNode(VariablePointerTestCase.java:76)

      1. VariablePointerTestCase.java
        4 kB
        Sergey Vladimirov
      2. hack-patch.txt
        3 kB
        Sergey Vladimirov
      3. VariablePointerTestCase.java
        3 kB
        Sergey Vladimirov

        Activity

        Hide
        Sergey Vladimirov added a comment -

        Test case

        Show
        Sergey Vladimirov added a comment - Test case
        Hide
        Matt Benson added a comment -

        fixed in trunk, rev. 552591. Thanks for the report!

        Show
        Matt Benson added a comment - fixed in trunk, rev. 552591. Thanks for the report!
        Hide
        Sergey Vladimirov added a comment -

        Matt,

        Is it possible to fix it without relaxing restriction?

        I.e. is there a mechanism to put some part of tree to variable, and use this variable later as part of the tree, not as part of another tree.

        Removing exceptions looks like just a workaround. Sorting is not working anyway.

        Show
        Sergey Vladimirov added a comment - Matt, Is it possible to fix it without relaxing restriction? I.e. is there a mechanism to put some part of tree to variable, and use this variable later as part of the tree, not as part of another tree. Removing exceptions looks like just a workaround. Sorting is not working anyway.
        Hide
        Matt Benson added a comment -

        Pretending a variable is a child of some random parent seems like the workaround to me. I removed the exception because I saw the problem differently: if two nodes have no common parents, then their comparison is undefined and thus irrelevant. I'm not sure what you're referring to when you say sorting is not working. The existing testcases that relied on the sort did not break, so I can only assume you're talking about sorting things that formerly broke in the manner of your original report. Can you provide an example and explain what expectations you have that are not being met?

        Show
        Matt Benson added a comment - Pretending a variable is a child of some random parent seems like the workaround to me. I removed the exception because I saw the problem differently: if two nodes have no common parents, then their comparison is undefined and thus irrelevant. I'm not sure what you're referring to when you say sorting is not working. The existing testcases that relied on the sort did not break, so I can only assume you're talking about sorting things that formerly broke in the manner of your original report. Can you provide an example and explain what expectations you have that are not being met?
        Hide
        Sergey Vladimirov added a comment -

        Matt,

        The example is in original test case. I extract several variables from original tree and want to select them (and sort) in one XPath expression.

        I can't do this right now, because variables are from different trees... but is it possible to "remember" that variables are from original tree?

        Show
        Sergey Vladimirov added a comment - Matt, The example is in original test case. I extract several variables from original tree and want to select them (and sort) in one XPath expression. I can't do this right now, because variables are from different trees... but is it possible to "remember" that variables are from original tree?
        Hide
        Matt Benson added a comment -

        But that's just the problem: a variable isn't part of the object graph represented by the JXPathContext; it exists parallel to that graph. So how would you expect the results of such a query to sort?

        Show
        Matt Benson added a comment - But that's just the problem: a variable isn't part of the object graph represented by the JXPathContext; it exists parallel to that graph. So how would you expect the results of such a query to sort?
        Hide
        Sergey Vladimirov added a comment -

        For example, Variables interface should allow to return not the object itself, but node pointer, so value will be part of original tree.

        Then at "sorting stage" variable is unpacked and original node pointer (if such exists) is used.

        Show
        Sergey Vladimirov added a comment - For example, Variables interface should allow to return not the object itself, but node pointer, so value will be part of original tree. Then at "sorting stage" variable is unpacked and original node pointer (if such exists) is used.
        Hide
        Matt Benson added a comment -

        I'm afraid I still don't follow you. The variables exist as an externally-injected dataset to use in conjunction with the JXPathContext. If in your own code you know that the variable's value is somewhere in the graph accessible from a given parent object, you may be able to do some custom work to search it out. My current opinion is that the route by which a value is reached defines its location, because multiple routes may lead to the same object. This means that a variable rightly has no contextual information by which it can correctly be compared to true path nodes.

        Show
        Matt Benson added a comment - I'm afraid I still don't follow you. The variables exist as an externally-injected dataset to use in conjunction with the JXPathContext. If in your own code you know that the variable's value is somewhere in the graph accessible from a given parent object, you may be able to do some custom work to search it out. My current opinion is that the route by which a value is reached defines its location, because multiple routes may lead to the same object. This means that a variable rightly has no contextual information by which it can correctly be compared to true path nodes.
        Hide
        Sergey Vladimirov added a comment -

        Let me try to implement my idea and discuss this later

        Show
        Sergey Vladimirov added a comment - Let me try to implement my idea and discuss this later
        Hide
        Sergey Vladimirov added a comment -

        "Hack" patch is included. This patch only illustates the idea, it should not be included in project without validation.

        Show
        Sergey Vladimirov added a comment - "Hack" patch is included. This patch only illustates the idea, it should not be included in project without validation.
        Hide
        Sergey Vladimirov added a comment -

        Updates test case. See the second testUnionOfVariableByPointerAndNode() test.

        Main idea - to return nodePointer instead of variable value in getVariable() method.

        Show
        Sergey Vladimirov added a comment - Updates test case. See the second testUnionOfVariableByPointerAndNode() test. Main idea - to return nodePointer instead of variable value in getVariable() method.
        Hide
        Sergey Vladimirov added a comment -

        Matt,

        please, look at the updated test case testUnionOfVariableByPointerAndNode(). "Hach" patch should be applied to jxpath/src for test case to work.

        Show
        Sergey Vladimirov added a comment - Matt, please, look at the updated test case testUnionOfVariableByPointerAndNode(). "Hach" patch should be applied to jxpath/src for test case to work.

          People

          • Assignee:
            Unassigned
            Reporter:
            Sergey Vladimirov
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development