You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2007/01/11 00:15:37 UTC

svn commit: r495033 - in /jakarta/commons/proper/jxpath/trunk/src: java/org/apache/commons/jxpath/util/ test/org/apache/commons/jxpath/ri/compiler/

Author: mbenson
Date: Wed Jan 10 15:15:33 2007
New Revision: 495033

URL: http://svn.apache.org/viewvc?view=rev&rev=495033
Log:
[JXPATH-10] Provide 1.1 backward-compatibility workaround with 1.3

Added:
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java   (with props)
Modified:
    jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java
    jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java

Added: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java?view=auto&rev=495033
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java (added)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java Wed Jan 10 15:15:33 2007
@@ -0,0 +1,42 @@
+/*
+ * 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.util;
+
+import org.apache.commons.jxpath.NodeSet;
+
+/**
+ * TypeConverter implementation to circumvent automagic NodeSet decoding. Suggested by JIRA issue JXPATH-10.
+ *
+ * @since JXPath 1.3
+ * @author Matt Benson
+ * @version $Revision$ $Date$
+ */
+public class JXPath11CompatibleTypeConverter extends BasicTypeConverter {
+    /**
+     * {@inheritDoc}
+     */
+    public boolean canConvert(Object object, Class toType) {
+        return object instanceof NodeSet ? toType.isInstance(object) : super.canConvert(object, toType);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object convert(Object object, Class toType) {
+        return object instanceof NodeSet && toType.isInstance(object) ? object : super.convert(object, toType);
+    }
+}

Propchange: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/util/JXPath11CompatibleTypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java?view=diff&rev=495033&r1=495032&r2=495033
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java (original)
+++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java Wed Jan 10 15:15:33 2007
@@ -30,11 +30,15 @@
 import org.apache.commons.jxpath.Functions;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.JXPathTestCase;
+import org.apache.commons.jxpath.NodeSet;
 import org.apache.commons.jxpath.PackageFunctions;
 import org.apache.commons.jxpath.Pointer;
 import org.apache.commons.jxpath.TestBean;
 import org.apache.commons.jxpath.Variables;
 import org.apache.commons.jxpath.ri.model.NodePointer;
+import org.apache.commons.jxpath.util.JXPath11CompatibleTypeConverter;
+import org.apache.commons.jxpath.util.TypeConverter;
+import org.apache.commons.jxpath.util.TypeUtils;
 
 /**
  * Test extension functions.
@@ -47,6 +51,7 @@
     private Functions functions;
     private JXPathContext context;
     private TestBean testBean;
+    private TypeConverter typeConverter;
 
     public static void main(String[] args) {
         TestRunner.run(ExtensionFunctionTest.class);
@@ -78,8 +83,15 @@
                     "jxpathtest"));
             lib.addFunctions(new PackageFunctions("", null));
             context.setFunctions(lib);
+            context.getVariables().declareVariable("List.class", List.class);
+            context.getVariables().declareVariable("NodeSet.class", NodeSet.class);
         }
         functions = new ClassFunctions(TestFunctions.class, "test");
+        typeConverter = TypeUtils.getTypeConverter();
+    }
+
+    public void tearDown() {
+        TypeUtils.setTypeConverter(typeConverter);
     }
 
     public void testConstructorLookup() {
@@ -362,6 +374,29 @@
             "/beans[1]/@name");
     }
 
+    public void testEstablishNodeSetBaseline() {
+        assertXPathValue(
+            context,
+            "test:isInstance(//strings, $List.class)",
+            Boolean.TRUE);
+        assertXPathValue(
+            context,
+            "test:isInstance(//strings, $NodeSet.class)",
+            Boolean.FALSE);
+    }
+
+    public void testBCNodeSetHack() {
+        TypeUtils.setTypeConverter(new JXPath11CompatibleTypeConverter());
+        assertXPathValue(
+            context,
+            "test:isInstance(//strings, $List.class)",
+            Boolean.FALSE);
+        assertXPathValue(
+            context,
+            "test:isInstance(//strings, $NodeSet.class)",
+            Boolean.TRUE);
+    }
+
     private static class Context implements ExpressionContext {
         private Object object;
 
@@ -386,4 +421,4 @@
             return 0;
         }
     }
-}
+}
\ No newline at end of file

Modified: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java?view=diff&rev=495033&r1=495032&r2=495033
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java (original)
+++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java Wed Jan 10 15:15:33 2007
@@ -149,4 +149,9 @@
     public static Collection items(Collection arg) {
         return arg;
     }
+
+    public static Boolean isInstance(Object o, Class c) {
+        return c.isInstance(o) ? Boolean.TRUE : Boolean.FALSE;
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org