Author: mbenson
Date: Tue Jul 10 14:59:45 2007
New Revision: 555094
URL: http://svn.apache.org/viewvc?view=rev&rev=555094
Log:
[JXPATH-93] relational operators did not conform to XPath spec
Added:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
(with props)
Modified:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThan.java
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThanOrEqual.java
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThan.java
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThanOrEqual.java
jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
Modified:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThan.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThan.java?view=diff&rev=555094&r1=555093&r2=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThan.java
(original)
+++
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThan.java
Tue Jul 10 14:59:45 2007
@@ -16,35 +16,22 @@
*/
package org.apache.commons.jxpath.ri.compiler;
-import org.apache.commons.jxpath.ri.EvalContext;
-import org.apache.commons.jxpath.ri.InfoSetUtil;
-
/**
* Implementation of Expression for the operation ">".
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class CoreOperationGreaterThan extends CoreOperation {
+public class CoreOperationGreaterThan extends
CoreOperationRelationalExpression {
public CoreOperationGreaterThan(Expression arg1, Expression arg2) {
super(new Expression[] { arg1, arg2 });
}
- public Object computeValue(EvalContext context) {
- double l = InfoSetUtil.doubleValue(args[0].computeValue(context));
- double r = InfoSetUtil.doubleValue(args[1].computeValue(context));
- return l > r ? Boolean.TRUE : Boolean.FALSE;
- }
-
- protected int getPrecedence() {
- return 3;
+ protected boolean evaluateCompare(int compare) {
+ return compare > 0;
}
- protected boolean isSymmetric() {
- return false;
- }
-
public String getSymbol() {
return ">";
}
Modified:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThanOrEqual.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThanOrEqual.java?view=diff&rev=555094&r1=555093&r2=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThanOrEqual.java
(original)
+++
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationGreaterThanOrEqual.java
Tue Jul 10 14:59:45 2007
@@ -16,35 +16,23 @@
*/
package org.apache.commons.jxpath.ri.compiler;
-import org.apache.commons.jxpath.ri.EvalContext;
-import org.apache.commons.jxpath.ri.InfoSetUtil;
-
/**
* Implementation of Expression for the operation ">=".
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class CoreOperationGreaterThanOrEqual extends CoreOperation {
+public class CoreOperationGreaterThanOrEqual extends
+ CoreOperationRelationalExpression {
public CoreOperationGreaterThanOrEqual(Expression arg1, Expression arg2) {
super(new Expression[] { arg1, arg2 });
}
- public Object computeValue(EvalContext context) {
- double l = InfoSetUtil.doubleValue(args[0].computeValue(context));
- double r = InfoSetUtil.doubleValue(args[1].computeValue(context));
- return l >= r ? Boolean.TRUE : Boolean.FALSE;
- }
-
- protected int getPrecedence() {
- return 3;
+ protected boolean evaluateCompare(int compare) {
+ return compare >= 0;
}
- protected boolean isSymmetric() {
- return false;
- }
-
public String getSymbol() {
return ">=";
}
Modified:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThan.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThan.java?view=diff&rev=555094&r1=555093&r2=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThan.java
(original)
+++
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThan.java
Tue Jul 10 14:59:45 2007
@@ -16,35 +16,22 @@
*/
package org.apache.commons.jxpath.ri.compiler;
-import org.apache.commons.jxpath.ri.EvalContext;
-import org.apache.commons.jxpath.ri.InfoSetUtil;
-
/**
* Implementation of Expression for the operation "<".
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class CoreOperationLessThan extends CoreOperation {
+public class CoreOperationLessThan extends CoreOperationRelationalExpression {
public CoreOperationLessThan(Expression arg1, Expression arg2) {
super(new Expression[] { arg1, arg2 });
}
- public Object computeValue(EvalContext context) {
- double l = InfoSetUtil.doubleValue(args[0].computeValue(context));
- double r = InfoSetUtil.doubleValue(args[1].computeValue(context));
- return l < r ? Boolean.TRUE : Boolean.FALSE;
- }
-
- protected int getPrecedence() {
- return 3;
+ protected boolean evaluateCompare(int compare) {
+ return compare < 0;
}
- protected boolean isSymmetric() {
- return false;
- }
-
public String getSymbol() {
return "<";
}
Modified:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThanOrEqual.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThanOrEqual.java?view=diff&rev=555094&r1=555093&r2=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThanOrEqual.java
(original)
+++
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationLessThanOrEqual.java
Tue Jul 10 14:59:45 2007
@@ -16,35 +16,23 @@
*/
package org.apache.commons.jxpath.ri.compiler;
-import org.apache.commons.jxpath.ri.EvalContext;
-import org.apache.commons.jxpath.ri.InfoSetUtil;
-
/**
* Implementation of Expression for the operation "<=".
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class CoreOperationLessThanOrEqual extends CoreOperation {
+public class CoreOperationLessThanOrEqual extends
+ CoreOperationRelationalExpression {
public CoreOperationLessThanOrEqual(Expression arg1, Expression arg2) {
super(new Expression[] { arg1, arg2 });
}
- public Object computeValue(EvalContext context) {
- double l = InfoSetUtil.doubleValue(args[0].computeValue(context));
- double r = InfoSetUtil.doubleValue(args[1].computeValue(context));
- return l <= r ? Boolean.TRUE : Boolean.FALSE;
- }
-
- protected int getPrecedence() {
- return 3;
+ protected boolean evaluateCompare(int compare) {
+ return compare <= 0;
}
- protected boolean isSymmetric() {
- return false;
- }
-
public String getSymbol() {
return "<=";
}
Added:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java?view=auto&rev=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
(added)
+++
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
Tue Jul 10 14:59:45 2007
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jxpath.ri.compiler;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import org.apache.commons.jxpath.ri.EvalContext;
+import org.apache.commons.jxpath.ri.InfoSetUtil;
+import org.apache.commons.jxpath.ri.axes.InitialContext;
+import org.apache.commons.jxpath.ri.axes.SelfContext;
+
+/**
+ * Base implementation of Expression for the operations ">", ">=",
"<", "<=".
+ *
+ * @author Matt Benson
+ * @version $Revision:$ $Date:$
+ */
+public abstract class CoreOperationRelationalExpression extends CoreOperation {
+
+ protected CoreOperationRelationalExpression(Expression[] args) {
+ super(args);
+ }
+
+ public final Object computeValue(EvalContext context) {
+ return compute(args[0].computeValue(context), args[1]
+ .computeValue(context)) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ protected final int getPrecedence() {
+ return 3;
+ }
+
+ protected final boolean isSymmetric() {
+ return false;
+ }
+
+ protected abstract boolean evaluateCompare(int compare);
+
+ private boolean compute(Object left, Object right) {
+ left = reduce(left);
+ right = reduce(right);
+
+ if (left instanceof InitialContext) {
+ ((InitialContext) left).reset();
+ }
+ if (right instanceof InitialContext) {
+ ((InitialContext) right).reset();
+ }
+ if (left instanceof Iterator && right instanceof Iterator) {
+ return findMatch((Iterator) left, (Iterator) right);
+ }
+ if (left instanceof Iterator) {
+ return containsMatch((Iterator) left, right);
+ }
+ if (right instanceof Iterator) {
+ return containsMatch((Iterator) right, left);
+ }
+ return evaluateCompare(compare(left, right));
+ }
+
+ private Object reduce(Object o) {
+ if (o instanceof SelfContext) {
+ o = ((EvalContext) o).getSingleNodePointer();
+ }
+ if (o instanceof Collection) {
+ o = ((Collection) o).iterator();
+ }
+ return o;
+ }
+
+ private boolean containsMatch(Iterator it, Object value) {
+ while (it.hasNext()) {
+ Object element = it.next();
+ if (evaluateCompare(compare(element, value))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean findMatch(Iterator lit, Iterator rit) {
+ HashSet left = new HashSet();
+ while (lit.hasNext()) {
+ left.add(lit.next());
+ }
+ while (rit.hasNext()) {
+ if (containsMatch(left.iterator(), rit.next())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private int compare(Object l, Object r) {
+ double ld = InfoSetUtil.doubleValue(l);
+ double rd = InfoSetUtil.doubleValue(r);
+ return ld == rd ? 0 : ld < rd ? -1 : 1;
+ }
+
+}
Propchange:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified:
jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java?view=diff&rev=555094&r1=555093&r2=555094
==============================================================================
---
jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
(original)
+++
jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
Tue Jul 10 14:59:45 2007
@@ -45,6 +45,7 @@
context = JXPathContext.newContext(null);
Variables vars = context.getVariables();
vars.declareVariable("integer", new Integer(1));
+ vars.declareVariable("array", new double[] { 0.25, 0.5, 0.75 });
}
}
@@ -93,5 +94,20 @@
assertXPathValue(context, "2 + 3", Boolean.TRUE, boolean.class);
assertXPathValue(context, "'true'", Boolean.TRUE, Boolean.class);
+ }
+
+ public void testNodeSetOperations() {
+ assertXPathValue(context, "$array > 0", Boolean.TRUE, Boolean.class);
+ assertXPathValue(context, "$array >= 0", Boolean.TRUE, Boolean.class);
+ assertXPathValue(context, "$array = 0", Boolean.FALSE, Boolean.class);
+ assertXPathValue(context, "$array = 0.25", Boolean.TRUE,
Boolean.class);
+ assertXPathValue(context, "$array = 0.5", Boolean.TRUE, Boolean.class);
+ assertXPathValue(context, "$array = 0.50000", Boolean.TRUE,
Boolean.class);
+ assertXPathValue(context, "$array = 0.75", Boolean.TRUE,
Boolean.class);
+ assertXPathValue(context, "$array < 1", Boolean.TRUE, Boolean.class);
+ assertXPathValue(context, "$array <= 1", Boolean.TRUE, Boolean.class);
+ assertXPathValue(context, "$array = 1", Boolean.FALSE, Boolean.class);
+ assertXPathValue(context, "$array > 1", Boolean.FALSE, Boolean.class);
+ assertXPathValue(context, "$array < 0", Boolean.FALSE, Boolean.class);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]