jstrachan 2002/06/06 00:13:41
Modified: jelly/src/java/org/apache/commons/jelly/parser
XMLParser.java
jelly/src/java/org/apache/commons/jelly TagLibrary.java
JellyContext.java
jelly/src/java/org/apache/commons/jelly/expression/jexl
JexlExpressionFactory.java
jelly build.xml
Added: jelly/src/test/org/apache/commons/jelly/expression
TestExpressions.java
jelly/src/java/org/apache/commons/jelly/expression
CompositeExpression.java
Log:
Added support for composite expressions in attribute values so that tags can be used
as
<foo "some text ${expr1} some more text ${expr2} more text">
Along with test cases to check this works. Ultimately it might makes sense to push
this down into Jexl. Currently there's no support for $ escaping like Velocity, so
$${foo} won't escape the $.
Revision Changes Path
1.20 +9 -9
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java
Index: XMLParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- XMLParser.java 30 May 2002 14:27:07 -0000 1.19
+++ XMLParser.java 6 Jun 2002 07:13:41 -0000 1.20
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
1.19 2002/05/30 14:27:07 jstrachan Exp $
- * $Revision: 1.19 $
- * $Date: 2002/05/30 14:27:07 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
1.20 2002/06/06 07:13:41 jstrachan Exp $
+ * $Revision: 1.20 $
+ * $Date: 2002/06/06 07:13:41 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: XMLParser.java,v 1.19 2002/05/30 14:27:07 jstrachan Exp $
+ * $Id: XMLParser.java,v 1.20 2002/06/06 07:13:41 jstrachan Exp $
*/
package org.apache.commons.jelly.parser;
import java.io.File;
@@ -87,6 +87,7 @@
import org.apache.commons.jelly.impl.DynaTagScript;
import org.apache.commons.jelly.impl.TagScript;
import org.apache.commons.jelly.impl.TextScript;
+import org.apache.commons.jelly.expression.CompositeExpression;
import org.apache.commons.jelly.expression.ConstantExpression;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.ExpressionFactory;
@@ -109,7 +110,7 @@
* The SAXParser and XMLReader portions of this code come from Digester.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.19 $
+ * @version $Revision: 1.20 $
*/
public class XMLParser extends DefaultHandler {
@@ -985,10 +986,9 @@
for (int i = 0; i < size; i++) {
String attributeName = list.getLocalName(i);
String attributeValue = list.getValue(i);
- Expression expression =
getExpressionFactory().createExpression(attributeValue);
- if (expression == null) {
- expression = createConstantExpression(localName, attributeName,
attributeValue);
- }
+ Expression expression = CompositeExpression.parse(
+ attributeValue, getExpressionFactory()
+ );
script.addAttribute(attributeName, expression);
}
return script;
1.10 +11 -8
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagLibrary.java
Index: TagLibrary.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagLibrary.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TagLibrary.java 30 May 2002 08:11:55 -0000 1.9
+++ TagLibrary.java 6 Jun 2002 07:13:41 -0000 1.10
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagLibrary.java,v
1.9 2002/05/30 08:11:55 jstrachan Exp $
- * $Revision: 1.9 $
- * $Date: 2002/05/30 08:11:55 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagLibrary.java,v
1.10 2002/06/06 07:13:41 jstrachan Exp $
+ * $Revision: 1.10 $
+ * $Date: 2002/06/06 07:13:41 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: TagLibrary.java,v 1.9 2002/05/30 08:11:55 jstrachan Exp $
+ * $Id: TagLibrary.java,v 1.10 2002/06/06 07:13:41 jstrachan Exp $
*/
package org.apache.commons.jelly;
@@ -65,6 +65,8 @@
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.jelly.expression.CompositeExpression;
+import org.apache.commons.jelly.expression.ConstantExpression;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.ExpressionFactory;
import org.apache.commons.jelly.impl.TagScript;
@@ -74,7 +76,7 @@
/** <p><code>Taglib</code> represents the metadata for a Jelly custom tag
library.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
public abstract class TagLibrary {
@@ -111,10 +113,11 @@
myFactory = factory;
}
if (myFactory != null) {
- return myFactory.createExpression(attributeValue);
+ return CompositeExpression.parse(attributeValue, myFactory);
}
- // will use the default expression instead
- return null;
+
+ // will use a constant expression instead
+ return new ConstantExpression(attributeValue);;
}
1.10 +3 -1
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java
Index: JellyContext.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- JellyContext.java 5 Jun 2002 07:00:58 -0000 1.9
+++ JellyContext.java 6 Jun 2002 07:13:41 -0000 1.10
@@ -560,7 +560,9 @@
else {
urlText = rootURL.toString() + relativeURI;
}
- log.info("Attempting to open url: " + urlText);
+ if ( log.isDebugEnabled() ) {
+ log.debug("Attempting to open url: " + urlText);
+ }
return new URL(urlText);
}
1.5 +8 -22
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java
Index: JexlExpressionFactory.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- JexlExpressionFactory.java 17 May 2002 15:18:14 -0000 1.4
+++ JexlExpressionFactory.java 6 Jun 2002 07:13:41 -0000 1.5
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java,v
1.4 2002/05/17 15:18:14 jstrachan Exp $
- * $Revision: 1.4 $
- * $Date: 2002/05/17 15:18:14 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/jexl/JexlExpressionFactory.java,v
1.5 2002/06/06 07:13:41 jstrachan Exp $
+ * $Revision: 1.5 $
+ * $Date: 2002/06/06 07:13:41 $
*
* ====================================================================
*
@@ -57,13 +57,12 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: JexlExpressionFactory.java,v 1.4 2002/05/17 15:18:14 jstrachan Exp $
+ * $Id: JexlExpressionFactory.java,v 1.5 2002/06/06 07:13:41 jstrachan Exp $
*/
package org.apache.commons.jelly.expression.jexl;
import org.apache.commons.jelly.expression.Expression;
-
import org.apache.commons.jelly.expression.ExpressionFactory;
/**
@@ -71,30 +70,17 @@
* expression which fully supports the Expression Language in JSTL and JSP.
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
public class JexlExpressionFactory implements ExpressionFactory {
// ExpressionFactory interface
-
//-------------------------------------------------------------------------
-
public Expression createExpression(String text) throws Exception {
-
- int length = text.length();
-
- if (length > 3 && text.startsWith("${") && text.charAt(length - 1) == '}') {
-
- text = text.substring(2, length - 1);
-
- return new JexlExpression(
- org.apache.commons.jexl.ExpressionFactory.createExpression(text));
-
- }
-
- return null;
-
+ return new JexlExpression(
+ org.apache.commons.jexl.ExpressionFactory.createExpression(text)
+ );
}
}
1.1
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/expression/TestExpressions.java
Index: TestExpressions.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/TestCoreTags.java,v
1.8 2002/05/28 07:20:06 jstrachan Exp $
* $Revision: 1.8 $
* $Date: 2002/05/28 07:20:06 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: TestCoreTags.java,v 1.8 2002/05/28 07:20:06 jstrachan Exp $
*/
package org.apache.commons.jelly.expression;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Tests the use of Expression parsing
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @version $Revision: 1.8 $
*/
public class TestExpressions extends TestCase {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog(TestExpressions.class);
protected JellyContext context = new JellyContext();
protected ExpressionFactory factory = new JexlExpressionFactory();
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(TestExpressions.class);
}
public TestExpressions(String testName) {
super(testName);
}
public void testExpresssions() throws Exception {
context.setVariable("topping", "cheese");
context.setVariable("type", "deepPan");
assertExpression("foo", "foo");
assertExpression("${topping}", "cheese");
assertExpression("some${topping}", "somecheese");
assertExpression("${topping}y", "cheesey");
assertExpression("A ${topping} ${type} pizza", "A cheese deepPan pizza");
assertExpression("${topping}-${type}", "cheese-deepPan");
}
protected void assertExpression(String expressionText, Object expectedValue)
throws Exception {
Expression expression = CompositeExpression.parse(expressionText, factory);
assertTrue( "Created a valid expression for: " + expressionText, expression
!= null );
Object value = expression.evaluate(context);
//assertEquals( "Expression for: " + expressionText + " is: " + expression,
expectedValue, value );
assertEquals( "Wrong result for expression: " + expressionText,
expectedValue, value );
}
}
1.1
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/CompositeExpression.java
Index: CompositeExpression.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/ConstantExpression.java,v
1.4 2002/05/17 15:18:15 jstrachan Exp $
* $Revision: 1.4 $
* $Date: 2002/05/17 15:18:15 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: ConstantExpression.java,v 1.4 2002/05/17 15:18:15 jstrachan Exp $
*/
package org.apache.commons.jelly.expression;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.SingletonIterator;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
/**
* <p><code>CompositeExpression</code> is a Composite expression made up of several
* Expression objects which are concatenated into a single String.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @version $Revision: 1.4 $
*/
public class CompositeExpression extends ExpressionSupport {
/** The expressions */
private List expressions;
public CompositeExpression() {
this.expressions = new ArrayList();
}
public CompositeExpression(List expressions) {
this.expressions = expressions;
}
public String toString() {
return super.toString() + "[expressions=" + expressions +"]";
}
/**
* Parses the given String to be either a ConstantExpresssion, an Expression
denoted as
* "${foo}" or some String with embedded expresssions such as
"abc${something}def${else}xyz"
* which results in a CompositeExpression being returned.
*
* @param text is the String to parse into expressions
* @param factory is the Factory of Expression objects used to create
expresssions for the contents
* of the String "foo" inside expressions such as "${foo}"
*
* @return the Expresssion for the given String.
* @throws JellyException if the text is invalid (such as missing '}' character).
* @throws Exception if there was some problem creating the underlying
Expression object
* from the ExpressionFactory
*/
public static Expression parse(String text, ExpressionFactory factory) throws
Exception {
int length = text.length();
int startIndex = text.indexOf( "${" );
if (startIndex < 0 ) {
return new ConstantExpression(text);
}
int endIndex = text.indexOf( "}", startIndex+2 );
if ( endIndex < 0 ) {
throw new JellyException( "Missing '}' character at the end of
expression: " + text );
}
if ( startIndex == 0 && endIndex == length - 1 ) {
return factory.createExpression(text.substring(2, endIndex));
}
else {
CompositeExpression answer = new CompositeExpression();
if ( startIndex > 0 ) {
String prefix = text.substring(0, startIndex);
answer.addTextExpression(prefix);
}
String middle = text.substring(startIndex+2, endIndex);
answer.addExpression(factory.createExpression(middle));
// now lets iterate through the rest of the string.
while (++endIndex < length) {
startIndex = text.indexOf( "${", endIndex );
if ( startIndex < 0 ) {
String postfix = text.substring(endIndex);
answer.addTextExpression(postfix);
break;
}
// add text in between expresssions
if (startIndex > endIndex ) {
answer.addTextExpression(text.substring(endIndex, startIndex));
}
endIndex = text.indexOf( "}", startIndex+2 );
if ( endIndex < 0 ) {
throw new JellyException( "Missing '}' character at the end of
expression: " + text );
}
middle = text.substring(startIndex+2, endIndex);
answer.addExpression(factory.createExpression(middle));
}
return answer;
}
}
// Properties
//-------------------------------------------------------------------------
/**
* @return the Expression objects that make up this
* composite expression
*/
public List getExpressions() {
return expressions;
}
/**
* Sets the Expression objects that make up this
* composite expression
*/
public void setExpressions(List expressions) {
this.expressions = expressions;
}
/**
* Adds a new expression to the end of the expression list
*/
public void addExpression(Expression expression) {
expressions.add(expression);
}
/**
* A helper method to add a new constant text expression
*/
public void addTextExpression(String text) {
addExpression(new ConstantExpression(text));
}
// Expression interface
//-------------------------------------------------------------------------
// inherit javadoc from interface
public Object evaluate(JellyContext context) {
return evaluateAsString(context);
}
// inherit javadoc from interface
public String evaluateAsString(JellyContext context) {
StringBuffer buffer = new StringBuffer();
for (Iterator iter = expressions.iterator(); iter.hasNext(); ) {
Expression expression = (Expression) iter.next();
String value = expression.evaluateAsString(context);
if ( value != null ) {
buffer.append( value );
}
}
return buffer.toString();
}
// inherit javadoc from interface
public Iterator evaluateAsIterator(JellyContext context) {
String value = evaluateAsString(context);
if ( value == null ) {
return Collections.EMPTY_LIST.iterator();
}
else {
return new SingletonIterator( value );
}
}
}
1.36 +10 -0 jakarta-commons-sandbox/jelly/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/build.xml,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- build.xml 4 Jun 2002 18:34:33 -0000 1.35
+++ build.xml 6 Jun 2002 07:13:41 -0000 1.36
@@ -202,7 +202,17 @@
<target name="dist" depends="maven:dist-build"/>
<target name="site" depends="maven:site"/>
+<!-- ========== Specific Test cases ======================================= -->
+
+ <target name="test.expr"
+ description="Runs the Expression unit test">
+ <property name="maven.testcase"
value="org.apache.commons.jelly.expression.TestExpressions"/>
+ <maven-ant antfile="${maven.home}/plugins/test/build.xml"
target="run-singletest"/>
+ </target>
+
+
<!-- ========== Sample Program Targets ==================================== -->
+
<target name="demo.hw" depends="compile"
description="Runs the Hello World demo">
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>