Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 99779 invoked from network); 5 Jan 2005 00:41:17 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 5 Jan 2005 00:41:17 -0000 Received: (qmail 35594 invoked by uid 500); 5 Jan 2005 00:41:15 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 35539 invoked by uid 500); 5 Jan 2005 00:41:14 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 35507 invoked by uid 99); 5 Jan 2005 00:41:14 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Tue, 04 Jan 2005 16:41:12 -0800 Received: (qmail 99743 invoked by uid 65534); 5 Jan 2005 00:41:11 -0000 Date: 5 Jan 2005 00:41:11 -0000 Message-ID: <20050105004111.99736.qmail@minotaur.apache.org> From: danielf@apache.org To: cvs@cocoon.apache.org Subject: svn commit: r124175 - in cocoon/trunk/src/blocks/template: java/org/apache/cocoon/components/expression/jxpath test/org/apache/cocoon/components/expression test/org/apache/cocoon/components/expression/jxpath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 @@ + 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()); + } +}