Author: danielf Date: Tue Jan 4 16:41:09 2005 New Revision: 124175 URL: http://svn.apache.org/viewcvs?view=rev&rev=124175 Log: Support for JXPath Added: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathCompiler.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/jxpath/ cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/jxpath/JXPathTestCase.java Modified: cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest
Added: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathCompiler.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathCompiler.java?view=auto&rev=124175 ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathCompiler.java Tue Jan 4 16:41:09 2005 @@ -0,0 +1,28 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.cocoon.components.expression.jxpath; + +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.components.expression.Expression; +import org.apache.cocoon.components.expression.ExpressionCompiler; +import org.apache.cocoon.components.expression.ExpressionException; + +public class JXPathCompiler implements ExpressionCompiler, ThreadSafe { + public Expression compile(String language, String expression) + throws ExpressionException{ + return new JXPathExpression(language, expression); + } +} Added: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java?view=auto&rev=124175 ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java Tue Jan 4 16:41:09 2005 @@ -0,0 +1,96 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.cocoon.components.expression.jxpath; + +import java.util.Iterator; +import java.util.Map; + +import org.apache.cocoon.components.expression.Expression; +import org.apache.cocoon.components.expression.ExpressionCompiler; +import org.apache.cocoon.components.expression.ExpressionContext; +import org.apache.cocoon.components.expression.ExpressionException; +import org.apache.commons.jxpath.CompiledExpression; +import org.apache.commons.jxpath.JXPathContext; +import org.apache.commons.jxpath.Variables; + +public class JXPathExpression implements Expression { + + private final String language; + private final String expression; + private final CompiledExpression compiledExpression; + + public JXPathExpression(String language, String expression) + throws ExpressionException { + this.language = language; + this.expression = expression; + this.compiledExpression = JXPathContext.compile(expression); + } + + public Object evaluate(ExpressionContext context) + throws ExpressionException{ + return this.compiledExpression.getValue(getContext(context)); + } + + public Iterator iterate(ExpressionContext context) + throws ExpressionException { + return this.compiledExpression.iterate(getContext(context)); + } + + public void assign(ExpressionContext context, Object value) + throws ExpressionException { + this.compiledExpression.setValue(getContext(context), value); + } + + public String getExpression() { + return this.expression; + } + + public String getLanguage() { + return this.language; + } + + private JXPathContext getContext(ExpressionContext context) { + // This could be made more efficient by caching the + // JXPathContext within the Context object. + JXPathContext jxcontext = JXPathContext.newContext(null, context.getContextBean()); + jxcontext.setVariables(new VariableAdapter(context)); + return jxcontext; + } + + private static class VariableAdapter implements Variables { + private ExpressionContext context; + + public VariableAdapter(ExpressionContext context) { + this.context = context; + } + + public void declareVariable(String name, Object value) { + this.context.put(name, value); + } + + public Object getVariable(String name) { + return this.context.get(name); + } + + public boolean isDeclaredVariable(String name) { + return this.context.containsKey(name); + } + + public void undeclareVariable(String name) { + throw new UnsupportedOperationException("Operation undeclareVariable is not supported"); + } + } +} Modified: cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java?view=diff&rev=124175&p1=cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java&r1=124174&p2=cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java&r2=124175 ============================================================================== --- cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java (original) +++ cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.java Tue Jan 4 16:41:09 2005 @@ -45,7 +45,7 @@ assertEquals("bar", context.getContextBean()); } - public void testFactory() throws ExpressionException, ServiceException { + public void testFactoryJexl() throws ExpressionException, ServiceException { ExpressionFactory factory = (ExpressionFactory)this.lookup(ExpressionFactory.ROLE); assertNotNull("Test lookup of expression factory", factory); @@ -54,4 +54,15 @@ assertEquals(new Long(3), expression.evaluate(new ExpressionContext())); } + + public void testFactoryJXPath() throws ExpressionException, ServiceException { + ExpressionFactory factory = (ExpressionFactory)this.lookup(ExpressionFactory.ROLE); + assertNotNull("Test lookup of expression factory", factory); + + Expression expression = factory.getExpression("jxpath", "1+2"); + assertNotNull("Test expression compilation", expression); + + assertEquals(new Double(3), expression.evaluate(new ExpressionContext())); + } } + Modified: cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest?view=diff&rev=124175&p1=cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest&r1=124174&p2=cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest&r2=124175 ============================================================================== --- cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest (original) +++ cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/ExpressionTestCase.xtest Tue Jan 4 16:41:09 2005 @@ -28,6 +28,7 @@ <components> <expression-compilers> <component-instance class="org.apache.cocoon.components.expression.jexl.JexlCompiler" name="jexl"/> + <component-instance class="org.apache.cocoon.components.expression.jxpath.JXPathCompiler" name="jxpath"/> </expression-compilers> </components> Added: cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/jxpath/JXPathTestCase.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/jxpath/JXPathTestCase.java?view=auto&rev=124175 ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/template/test/org/apache/cocoon/components/expression/jxpath/JXPathTestCase.java Tue Jan 4 16:41:09 2005 @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.cocoon.components.expression.jxpath; + +import java.util.Iterator; + +import junit.framework.TestCase; +import org.apache.cocoon.components.expression.Expression; +import org.apache.cocoon.components.expression.ExpressionCompiler; +import org.apache.cocoon.components.expression.ExpressionContext; +import org.apache.cocoon.components.expression.ExpressionException; + +public class JXPathTestCase extends TestCase { + + public void testExpression() throws ExpressionException { + ExpressionCompiler compiler = new JXPathCompiler(); + Expression expression = compiler.compile("jxpath", "1+2"); + assertEquals(new Double(3), expression.evaluate(new ExpressionContext())); + } + + public void testContextExpression() throws ExpressionException { + ExpressionCompiler compiler = new JXPathCompiler(); + ExpressionContext context = new ExpressionContext(); + context.put("a", new Long(1)); + context.put("b", new Long(2)); + Expression expression = compiler.compile("jxpath", "$a+$b"); + assertEquals(new Double(3), expression.evaluate(context)); + } + + public void testIterator() throws ExpressionException { + ExpressionCompiler compiler = new JXPathCompiler(); + ExpressionContext context = new ExpressionContext(); + String[] arr = {"foo"}; + context.setContextBean(arr); + Expression expression = compiler.compile("jxpath", "."); + Iterator iter = expression.iterate(context); + assertTrue("hasNext", iter.hasNext()); + assertEquals("foo", iter.next()); + assertFalse("!hasNext", iter.hasNext()); + } +}