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 23:47:01 UTC
svn commit: r279130 - in /cocoon/blocks/template/trunk: WEB-INF/xconf/
java/org/apache/cocoon/template/ java/org/apache/cocoon/template/environment/
java/org/apache/cocoon/template/expression/
java/org/apache/cocoon/template/script/ test/org/apache/coc...
Author: lgawron
Date: Tue Sep 6 14:44:39 2005
New Revision: 279130
URL: http://svn.apache.org/viewcvs?rev=279130&view=rev
Log:
pluggable JXTExpressionCompiler allows to easily replace ${expr} #{expr} syntax with {jexl:expr} {jxpath:expr}
Added:
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java (with props)
Modified:
cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template-expression.xconf
cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template.xconf
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/TransformerAdapter.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ParsingContext.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTExpressionCompiler.java
cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java
cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.xtest
Modified: cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template-expression.xconf
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template-expression.xconf?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template-expression.xconf (original)
+++ cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template-expression.xconf Tue Sep 6 14:44:39 2005
@@ -23,6 +23,7 @@
<!-- Include roles -->
<include src="resource://org/apache/cocoon/components/expression/expression.roles"/>
+ <expression-factory/>
<expression-compilers>
<component-instance class="org.apache.cocoon.components.expression.jxpath.JXPathCompiler" name="default"/>
<component-instance class="org.apache.cocoon.components.expression.jexl.JexlCompiler" name="jexl"/>
Modified: cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template.xconf
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template.xconf?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template.xconf (original)
+++ cocoon/blocks/template/trunk/WEB-INF/xconf/cocoon-template.xconf Tue Sep 6 14:44:39 2005
@@ -23,4 +23,5 @@
<include src="context://WEB-INF/xconf/cocoon-template-expression.xconf"/>
<component role="org.apache.cocoon.template.script.ScriptManager" class="org.apache.cocoon.template.script.DefaultScriptManager"/>
<component role="org.apache.cocoon.template.script.InstructionFactory" class="org.apache.cocoon.template.script.DefaultInstructionFactory"/>
+ <component role="org.apache.cocoon.template.expression.JXTExpressionCompiler" class="org.apache.cocoon.template.expression.DefaultJXTExpressionCompiler"/>
</components>
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/TransformerAdapter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/TransformerAdapter.java?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/TransformerAdapter.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/TransformerAdapter.java Tue Sep 6 14:44:39 2005
@@ -24,9 +24,9 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.ProcessingException;
-import org.apache.cocoon.components.expression.ExpressionFactory;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.template.environment.ParsingContext;
+import org.apache.cocoon.template.expression.JXTExpressionCompiler;
import org.apache.cocoon.template.script.InstructionFactory;
import org.apache.cocoon.template.script.Parser;
import org.apache.cocoon.transformation.ServiceableTransformer;
@@ -56,14 +56,14 @@
public void service(ServiceManager manager) throws ServiceException {
this.manager = manager;
this.gen.service(manager);
- setParsingContext(new ParsingContext((ExpressionFactory) this.manager.lookup(ExpressionFactory.ROLE),
+ setParsingContext(new ParsingContext((JXTExpressionCompiler) this.manager.lookup(JXTExpressionCompiler.ROLE),
(InstructionFactory) this.manager.lookup(InstructionFactory.ROLE)));
}
public void dispose() {
setParsingContext( null );
this.manager.release(this.parsingContext.getInstructionFactory());
- this.manager.release(this.parsingContext.getExpressionFactory());
+ this.manager.release(this.parsingContext.getJxtExpressionCompiler());
}
public void endDocument() throws SAXException {
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ParsingContext.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ParsingContext.java?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ParsingContext.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ParsingContext.java Tue Sep 6 14:44:39 2005
@@ -15,7 +15,6 @@
*/
package org.apache.cocoon.template.environment;
-import org.apache.cocoon.components.expression.ExpressionFactory;
import org.apache.cocoon.template.expression.JXTExpressionCompiler;
import org.apache.cocoon.template.script.InstructionFactory;
@@ -23,18 +22,12 @@
* @version SVN $Id$
*/
public class ParsingContext {
- private ExpressionFactory expressionFactory;
private InstructionFactory instructionFactory;
private JXTExpressionCompiler jxtExpressionCompiler;
- public ParsingContext(ExpressionFactory expressionFactory, InstructionFactory instructionFactory) {
- this.expressionFactory = expressionFactory;
+ public ParsingContext(JXTExpressionCompiler jxtExpressionCompiler, InstructionFactory instructionFactory) {
this.instructionFactory = instructionFactory;
- this.jxtExpressionCompiler = new JXTExpressionCompiler(this.expressionFactory);
- }
-
- public ExpressionFactory getExpressionFactory() {
- return expressionFactory;
+ this.jxtExpressionCompiler = jxtExpressionCompiler;
}
public InstructionFactory getInstructionFactory() {
Added: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java?rev=279130&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java (added)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java Tue Sep 6 14:44:39 2005
@@ -0,0 +1,77 @@
+/*
+ * Copyright 1999-2005 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.template.expression;
+
+import java.io.Reader;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @version SVN $Id$
+ */
+public class DefaultJXTExpressionCompiler extends JXTExpressionCompiler {
+ public List compileSubstitutions(Reader in) throws Exception {
+ 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;
+ }
+
+}
Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/DefaultJXTExpressionCompiler.java
------------------------------------------------------------------------------
svn:keywords = Id
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=279130&r1=279129&r2=279130&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 14:44:39 2005
@@ -22,9 +22,14 @@
import java.io.Reader;
import java.io.StringReader;
-import java.util.LinkedList;
import java.util.List;
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.components.expression.Expression;
import org.apache.cocoon.components.expression.ExpressionFactory;
import org.apache.cocoon.template.environment.ErrorHolder;
@@ -32,16 +37,25 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
-public class JXTExpressionCompiler {
+public abstract class JXTExpressionCompiler extends AbstractLogEnabled implements Serviceable, Disposable, ThreadSafe {
+ public static String ROLE = JXTExpressionCompiler.class.getName();
+
+ private ServiceManager manager;
private ExpressionFactory expressionFactory;
+
public static String JXPATH = "jxpath";
public static String JEXL = "jexl";
- public JXTExpressionCompiler(ExpressionFactory expressionFactory) {
- this.expressionFactory = expressionFactory;
+ public void service(ServiceManager manager) throws ServiceException {
+ this.manager = manager;
+ this.expressionFactory = (ExpressionFactory) this.manager.lookup(ExpressionFactory.ROLE);
}
- private JXTExpression compile(final String expression, String language) throws Exception {
+ public void dispose() {
+ this.manager.release(this.expressionFactory);
+ }
+
+ protected JXTExpression compile(final String expression, String language) throws Exception {
Expression compiled = this.expressionFactory.getExpression(language, expression);
return new JXTExpression(expression, compiled);
}
@@ -91,58 +105,13 @@
*/
public List compileSubstitutions(Reader in, String errorPrefix, Locator location) throws SAXParseException {
try {
- 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;
+ return compileSubstitutions(in);
} catch (Exception exc) {
throw new SAXParseException(errorPrefix + exc.getMessage(), location, exc);
} catch (Error err) {
throw new SAXParseException(errorPrefix + err.getMessage(), location, new ErrorHolder(err));
}
}
+
+ public abstract List compileSubstitutions(Reader in) throws Exception;
}
Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java Tue Sep 6 14:44:39 2005
@@ -21,9 +21,9 @@
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.ProcessingException;
-import org.apache.cocoon.components.expression.ExpressionFactory;
import org.apache.cocoon.components.source.SourceUtil;
import org.apache.cocoon.template.environment.ParsingContext;
+import org.apache.cocoon.template.expression.JXTExpressionCompiler;
import org.apache.cocoon.template.script.event.StartDocument;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
@@ -42,7 +42,7 @@
private final static String JX_STORE_PREFIX = "jxtg:";
private Store store;
private InstructionFactory instructionFactory;
- private ExpressionFactory expressionFactory;
+ private JXTExpressionCompiler jxtExpressionCompiler;
public DefaultScriptManager() {
}
@@ -51,13 +51,13 @@
this.manager = manager;
this.store = (Store) this.manager.lookup(Store.TRANSIENT_STORE);
this.instructionFactory = (InstructionFactory) this.manager.lookup(InstructionFactory.ROLE);
- this.expressionFactory = (ExpressionFactory) this.manager.lookup(ExpressionFactory.ROLE);
+ this.jxtExpressionCompiler = (JXTExpressionCompiler) this.manager.lookup(JXTExpressionCompiler.ROLE);
}
public void dispose() {
this.manager.release(this.store);
this.manager.release(this.instructionFactory);
- this.manager.release(this.expressionFactory);
+ this.manager.release(this.jxtExpressionCompiler);
}
private Store getStore() {
@@ -101,7 +101,7 @@
}
if (doc == null) {
- Parser parser = new Parser(new ParsingContext(this.expressionFactory, this.instructionFactory));
+ Parser parser = new Parser(new ParsingContext(this.jxtExpressionCompiler, this.instructionFactory));
// call getValidity before using the stream is faster if
// the source is a SitemapSource
if (validity == null) {
Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.xtest
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.xtest?rev=279130&r1=279129&r2=279130&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.xtest (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.xtest Tue Sep 6 14:44:39 2005
@@ -38,6 +38,7 @@
<store logger="test"/>
<component role="org.apache.cocoon.template.script.ScriptManager" class="org.apache.cocoon.template.script.DefaultScriptManager"/>
<component role="org.apache.cocoon.template.script.InstructionFactory" class="org.apache.cocoon.template.script.DefaultInstructionFactory"/>
+ <component role="org.apache.cocoon.template.expression.JXTExpressionCompiler" class="org.apache.cocoon.template.expression.DefaultJXTExpressionCompiler"/>
<expression-factory/>
<expression-compilers>
<component-instance class="org.apache.cocoon.components.expression.jxpath.JXPathCompiler" name="default"/>