You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by lg...@apache.org on 2005/09/06 22:53:45 UTC
svn commit: r279099 - in
/cocoon/blocks/template/trunk/java/org/apache/cocoon:
components/expression/jexl/ template/expression/
Author: lgawron
Date: Tue Sep 6 13:53:03 2005
New Revision: 279099
URL: http://svn.apache.org/viewcvs?rev=279099&view=rev
Log:
expression parsing in single place
Modified:
cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlCompiler.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/Substitutions.java
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlCompiler.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlCompiler.java?rev=279099&r1=279098&r2=279099&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlCompiler.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlCompiler.java Tue Sep 6 13:53:03 2005
@@ -24,8 +24,7 @@
* @version SVN $Id$
*/
public class JexlCompiler implements ExpressionCompiler, ThreadSafe {
- public Expression compile(String language, String expression)
- throws ExpressionException{
+ public Expression compile(String language, String expression) throws ExpressionException {
return new JexlExpression(language, expression);
}
}
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java?rev=279099&r1=279098&r2=279099&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java Tue Sep 6 13:53:03 2005
@@ -34,44 +34,38 @@
private final String language;
private final org.apache.commons.jexl.Expression compiledExpression;
- public JexlExpression(String language, String expression)
- throws ExpressionException {
+ public JexlExpression(String language, String expression) throws ExpressionException {
this.language = language;
try {
- this.compiledExpression =
- org.apache.commons.jexl.ExpressionFactory.createExpression(expression);
+ this.compiledExpression = org.apache.commons.jexl.ExpressionFactory.createExpression(expression);
} catch (Exception e) {
throw new ExpressionException("Couldn't create expression " + expression, e);
}
}
- public Object evaluate(ExpressionContext context)
- throws ExpressionException{
+ public Object evaluate(ExpressionContext context) throws ExpressionException {
try {
return this.compiledExpression.evaluate(new ContextAdapter(context));
} catch (Exception e) {
- throw new ExpressionException("Couldn't evaluate expression " +
- getExpression(), e);
+ throw new ExpressionException("Couldn't evaluate expression " + getExpression(), e);
}
}
- public Iterator iterate(ExpressionContext context)
- throws ExpressionException {
+ public Iterator iterate(ExpressionContext context) throws ExpressionException {
Iterator iter = null;
Object result = evaluate(context);
if (result != null) {
- /* The Info object is supposed to contain the script
- location where the expression is invoked and use that
- in a warning log message if no iterator can be
- generated. This info is not available in the expression
- object and might not be relevant either as it can be
- used from a non script situation.
- */
+ /*
+ * The Info object is supposed to contain the script location where
+ * the expression is invoked and use that in a warning log message
+ * if no iterator can be generated. This info is not available in
+ * the expression object and might not be relevant either as it can
+ * be used from a non script situation.
+ */
try {
iter = Introspector.getUberspect().getIterator(result, new Info("Unknown", 0, 0));
} catch (Exception e) {
- throw new ExpressionException("Couldn't get an iterator from expression " +
- getExpression(), e);
+ throw new ExpressionException("Couldn't get an iterator from expression " + getExpression(), e);
}
}
if (iter == null) {
@@ -80,8 +74,7 @@
return iter;
}
- public void assign(ExpressionContext context, Object value)
- throws ExpressionException {
+ public void assign(ExpressionContext context, Object value) throws ExpressionException {
throw new UnsupportedOperationException("Assign is not yet implemented for Jexl");
}
@@ -103,6 +96,7 @@
private static class ContextAdapter implements JexlContext {
private final ExpressionContext context;
+
public ContextAdapter(ExpressionContext context) {
this.context = context;
}
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java?rev=279099&r1=279098&r2=279099&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java Tue Sep 6 13:53:03 2005
@@ -21,7 +21,10 @@
package org.apache.cocoon.template.expression;
import java.io.IOException;
+import java.io.Reader;
import java.io.StringReader;
+import java.util.LinkedList;
+import java.util.List;
import org.apache.cocoon.components.expression.Expression;
import org.apache.cocoon.components.expression.ExpressionFactory;
@@ -34,42 +37,45 @@
private ExpressionFactory expressionFactory;
public static String JXPATH = "jxpath";
public static String JEXL = "jexl";
-
+
public JXTExpressionCompiler(ExpressionFactory expressionFactory) {
this.expressionFactory = expressionFactory;
}
-
- public JXTExpression compile(final String expression, boolean xpath)
- throws Exception {
- Expression compiled;
- if (xpath)
- compiled = this.expressionFactory.getExpression(JXPATH, expression);
- else
- compiled = this.expressionFactory.getExpression(JEXL, expression);
+
+ private JXTExpression compile(final String expression, String language) throws Exception {
+ Expression compiled = this.expressionFactory.getExpression(language, expression);
return new JXTExpression(expression, compiled);
}
-
- public JXTExpression compileBoolean(String val, String msg,
- Locator location) throws SAXException {
+
+ /*
+ * Compile a boolean expression (returns either a Compiled Expression or a
+ * Boolean literal)
+ */
+ public JXTExpression compileBoolean(String val, String msg, Locator location) throws SAXException {
JXTExpression res = compileExpr(val, msg, location);
if (res != null && res.getCompiledExpression() == null && res.getRaw() != null) {
res.setCompiledExpression(Boolean.valueOf(res.getRaw()));
}
return res;
}
+
/*
* Compile an integer expression (returns either a Compiled Expression or an
* Integer literal)
*/
- public JXTExpression compileInt(String val, String msg,
- Locator location) throws SAXException {
+ public JXTExpression compileInt(String val, String msg, Locator location) throws SAXException {
JXTExpression res = compileExpr(val, msg, location);
if (res != null && res.getCompiledExpression() == null && res.getRaw() != null) {
res.setCompiledExpression(Integer.valueOf(res.getRaw()));
}
return res;
}
- public JXTExpression compileExpr(String inStr) throws Exception {
+
+ /**
+ * Compile a single Jexl expr (contained in ${}) or XPath expression
+ * (contained in #{})
+ */
+ public JXTExpression compileExpr(String inStr, String errorPrefix, Locator location) throws SAXParseException {
try {
if (inStr == null) {
return null;
@@ -86,7 +92,8 @@
ch = in.read();
expr.append((ch == -1) ? '\\' : (char) ch);
} else if (c == '}') {
- return compile(expr.toString(), xpath);
+ return compile(expr.toString(), xpath ? JXTExpressionCompiler.JXPATH
+ : JXTExpressionCompiler.JEXL);
} else {
expr.append(c);
}
@@ -104,30 +111,73 @@
return new JXTExpression(inStr, null);
}
}
- if (inExpr) {
- // unclosed #{} or ${}
+ if (inExpr)
throw new Exception("Unterminated " + (xpath ? "#" : "$") + "{");
- }
- } catch (IOException ignored) {
- ignored.printStackTrace();
+ } catch (Exception exc) {
+ throw new SAXParseException(errorPrefix + exc.getMessage(), location, exc);
+ } catch (Error err) {
+ throw new SAXParseException(errorPrefix + err.getMessage(), location, new ErrorHolder(err));
}
return new JXTExpression(inStr, null);
}
+
/**
- * Compile a single Jexl expr (contained in ${}) or XPath expression
- * (contained in #{})
+ * Compile a set of expressions spaced with literals
*/
- public JXTExpression compileExpr(String expr, String errorPrefix,
- Locator location) throws SAXParseException {
+ public List compileSubstitutions(Reader in, String errorPrefix, Locator location) throws SAXParseException {
try {
- return compileExpr(expr);
+ LinkedList substitutions = new LinkedList();
+ StringBuffer buf = new StringBuffer();
+ buf.setLength(0);
+ int ch;
+ boolean inExpr = false;
+ boolean xpath = false;
+ top: while ((ch = in.read()) != -1) {
+ // column++;
+ char c = (char) ch;
+ processChar: while (true) {
+ if (inExpr) {
+ if (c == '\\') {
+ ch = in.read();
+ buf.append(ch == -1 ? '\\' : (char) ch);
+ } else if (c == '}') {
+ String str = buf.toString();
+ substitutions.add(compile(str, xpath ? JXPATH : JEXL));
+ buf.setLength(0);
+ inExpr = false;
+ } else {
+ buf.append(c);
+ }
+ } else if (c == '$' || c == '#') {
+ ch = in.read();
+ if (ch == '{') {
+ xpath = c == '#';
+ inExpr = true;
+ if (buf.length() > 0) {
+ substitutions.add(new Literal(buf.toString()));
+ buf.setLength(0);
+ }
+ continue top;
+ }
+ buf.append(c);
+ if (ch != -1) {
+ c = (char) ch;
+ continue processChar;
+ }
+ } else {
+ buf.append(c);
+ }
+ break;
+ }
+ }
+ if (inExpr)
+ throw new Exception("Unterminated " + (xpath ? "#" : "$") + "{");
+ substitutions.add(new Literal(buf.toString()));
+ return substitutions;
} catch (Exception exc) {
- throw new SAXParseException(errorPrefix + exc.getMessage(),
- location, exc);
+ throw new SAXParseException(errorPrefix + exc.getMessage(), location, exc);
} catch (Error err) {
- throw new SAXParseException(errorPrefix + err.getMessage(),
- location, new ErrorHolder(err));
+ throw new SAXParseException(errorPrefix + err.getMessage(), location, new ErrorHolder(err));
}
}
-
}
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/Substitutions.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/Substitutions.java?rev=279099&r1=279098&r2=279099&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/Substitutions.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/Substitutions.java Tue Sep 6 13:53:03 2005
@@ -16,11 +16,9 @@
package org.apache.cocoon.template.expression;
import java.io.CharArrayReader;
-import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import org.apache.cocoon.components.expression.ExpressionContext;
@@ -43,81 +41,12 @@
}
public Substitutions(ParsingContext parsingContext, Locator location, char[] chars, int start, int length)
- throws SAXException {
+ throws SAXException {
this(parsingContext, location, new CharArrayReader(chars, start, length));
}
private Substitutions(ParsingContext parsingContext, Locator location, Reader in) throws SAXException {
- LinkedList substitutions = new LinkedList();
- StringBuffer buf = new StringBuffer();
- buf.setLength(0);
- int ch;
- boolean inExpr = false;
- boolean xpath = false;
- try {
- top:
- while ((ch = in.read()) != -1) {
- // column++;
- char c = (char) ch;
- processChar:
- while (true) {
- if (inExpr) {
- if (c == '\\') {
- ch = in.read();
- buf.append(ch == -1 ? '\\' : (char) ch);
- } else if (c == '}') {
- String str = buf.toString();
- JXTExpression compiledExpression;
- try {
- compiledExpression = parsingContext.getJxtExpressionCompiler().compile(str, xpath);
- } catch (Exception exc) {
- throw new SAXParseException(exc.getMessage(),
- location, exc);
- } catch (Error err) {
- throw new SAXParseException(err.getMessage(),
- location,
- new ErrorHolder(err));
- }
- substitutions.add(compiledExpression);
- buf.setLength(0);
- inExpr = false;
- } else {
- buf.append(c);
- }
- } else if (c == '$' || c == '#') {
- ch = in.read();
- if (ch == '{') {
- xpath = c == '#';
- inExpr = true;
- if (buf.length() > 0) {
- substitutions.add(new Literal(buf.toString()));
- buf.setLength(0);
- }
- continue top;
- }
- buf.append(c);
- if (ch != -1) {
- c = (char) ch;
- continue processChar;
- }
- } else {
- buf.append(c);
- }
- break;
- }
- }
- } catch (IOException ignored) {
- // won't happen
- ignored.printStackTrace();
- }
- if (inExpr) {
- // unclosed #{} or ${}
- String msg = "Unterminated " + (xpath ? "#" : "$") + "{";
- throw new SAXParseException(msg, location, null);
- }
- substitutions.add(new Literal(buf.toString()));
-
- this.substitutions = substitutions;
+ this.substitutions = parsingContext.getJxtExpressionCompiler().compileSubstitutions( in, "", location );
this.hasSubstitutions = !substitutions.isEmpty();
}
@@ -137,8 +66,7 @@
return this.substitutions.get(pos);
}
- public String toString(Locator location, ExpressionContext expressionContext)
- throws SAXException {
+ public String toString(Locator location, ExpressionContext expressionContext) throws SAXException {
StringBuffer buf = new StringBuffer();
Iterator iterSubst = iterator();
while (iterSubst.hasNext()) {
@@ -154,8 +82,7 @@
} catch (Exception e) {
throw new SAXParseException(e.getMessage(), location, e);
} catch (Error err) {
- throw new SAXParseException(err.getMessage(), location,
- new ErrorHolder(err));
+ throw new SAXParseException(err.getMessage(), location, new ErrorHolder(err));
}
buf.append(val != null ? val.toString() : "");
}