cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lgaw...@apache.org
Subject svn commit: r325986 - in /cocoon/blocks/template/trunk/java/org/apache/cocoon: components/expression/ components/expression/javascript/ components/expression/jexl/ components/expression/jxpath/ template/expression/ template/script/
Date Mon, 17 Oct 2005 21:36:31 GMT
Author: lgawron
Date: Mon Oct 17 14:35:18 2005
New Revision: 325986

URL: http://svn.apache.org/viewcvs?rev=325986&view=rev
Log:
initial version of javascript expression support in JXTG
use @{expression} for standard JXTG and {js:expression} for CTemplate

Added:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
  (with props)
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
  (with props)
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
  (with props)
Modified:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/expression.roles
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTGStringTemplateParser.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/DefaultScriptManager.java

Added: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java?rev=325986&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
(added)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
Mon Oct 17 14:35:18 2005
@@ -0,0 +1,58 @@
+/*
+ * 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.components.expression;
+
+import java.util.Iterator;
+
+/**
+ * @version $Id$
+ */
+public abstract class AbstractExpression implements Expression {
+    private String language;
+    private String expression;
+
+    public AbstractExpression(String language, String expression) {
+        this.language = language;
+        this.expression = expression;
+    }
+
+    public String getExpression() {
+        return this.expression;
+    }
+
+    public String getLanguage() {
+        return this.language;
+    }
+
+    public void setProperty(String property, Object value) {
+        // has no properties
+    }
+
+    protected static final Iterator EMPTY_ITER = new Iterator() {
+        public boolean hasNext() {
+            return false;
+        }
+
+        public Object next() {
+            return null;
+        }
+
+        public void remove() {
+            // EMPTY
+        }
+    };
+
+}

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/AbstractExpression.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/expression.roles
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/expression.roles?rev=325986&r1=325985&r2=325986&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/expression.roles
(original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/expression.roles
Mon Oct 17 14:35:18 2005
@@ -22,4 +22,9 @@
   <role name="org.apache.cocoon.components.expression.ExpressionFactory"
         shorthand="expression-factory"
         default-class="org.apache.cocoon.components.expression.ExpressionFactory"/>
+  <!-- TODO move it somewhere else! -->
+  <role name="org.apache.cocoon.template.expression.StringTemplateParserSelector" 
+        shorthand="string-template-parsers" 
+        default-class="org.apache.cocoon.core.container.DefaultServiceSelector"/>
+  
 </role-list>

Added: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java?rev=325986&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
(added)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
Mon Oct 17 14:35:18 2005
@@ -0,0 +1,30 @@
+/*
+ * 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.components.expression.javascript;
+
+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;
+
+/**
+ * @version SVN $Id$
+ */
+public class JavaScriptCompiler implements ExpressionCompiler, ThreadSafe {
+    public Expression compile(String language, String expression) throws ExpressionException
{
+        return new JavaScriptExpression(language, expression);
+    }
+}

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptCompiler.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java?rev=325986&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
(added)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
Mon Oct 17 14:35:18 2005
@@ -0,0 +1,102 @@
+/*
+ * $Id$
+ *
+ * Created on 2005-10-17
+ *
+ * Copyright (c) 2005, MobileBox sp. z o.o.
+ * All rights reserved.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR
+ * 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.
+ */
+package org.apache.cocoon.components.expression.javascript;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.cocoon.components.expression.AbstractExpression;
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.components.expression.ExpressionException;
+import org.apache.cocoon.components.expression.jexl.JSIntrospector;
+import org.apache.cocoon.components.flow.FlowHelper;
+import org.apache.cocoon.environment.TemplateObjectModelHelper;
+import org.apache.commons.jexl.util.introspection.Info;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Script;
+import org.mozilla.javascript.Scriptable;
+
+public class JavaScriptExpression extends AbstractExpression {
+    private Script script;
+    private JSIntrospector introspector;
+
+    public JavaScriptExpression(String language, String expression) {
+        super(language, expression);
+        compile();
+    }
+
+    private void compile() {
+        Context ctx = Context.enter();
+        try {
+            this.script = ctx.compileString(getExpression(), getExpression(), 1, null);
+        } finally {
+            Context.exit();
+        }
+    }
+
+    public Object evaluate(ExpressionContext context) throws ExpressionException {
+        Context ctx = Context.enter();
+        try {
+            Scriptable scope = ctx.newObject(TemplateObjectModelHelper.getScope());
+            // Populate the scope
+            Iterator iter = context.entrySet().iterator();
+            while (iter.hasNext()) {
+                Map.Entry entry = (Map.Entry) iter.next();
+                String key = (String) entry.getKey();
+                Object value = entry.getValue();
+                scope.put(key, scope, Context.toObject(value, scope));
+            }
+
+            Object result = this.script.exec(ctx, scope);
+            return FlowHelper.unwrap(result);
+        } finally {
+            Context.exit();
+        }
+    }
+
+    public Iterator iterate(ExpressionContext context) throws ExpressionException {
+        Object result = evaluate(context);
+        if (result == null)
+            return EMPTY_ITER;
+
+        if (this.introspector == null)
+            introspector = new JSIntrospector();
+
+        Iterator iter = null;
+        try {
+            iter = introspector.getIterator(result, new Info("Unknown", 0, 0));
+        } catch (Exception e) {
+            throw new ExpressionException("Couldn't get an iterator from expression " + getExpression(),
e);
+        }
+
+        if (iter == null)
+            iter = EMPTY_ITER;
+        return iter;
+    }
+
+    public void assign(ExpressionContext context, Object value) throws ExpressionException
{
+        throw new UnsupportedOperationException("assignment not implemented for javascript
expressions");
+    }
+
+    public Object getNode(ExpressionContext context) throws ExpressionException {
+        return evaluate(context);
+    }
+}

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/javascript/JavaScriptExpression.java
------------------------------------------------------------------------------
    svn:keywords = Id

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=325986&r1=325985&r2=325986&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
Mon Oct 17 14:35:18 2005
@@ -19,7 +19,7 @@
 import java.util.Iterator;
 import java.util.Map;
 
-import org.apache.cocoon.components.expression.Expression;
+import org.apache.cocoon.components.expression.AbstractExpression;
 import org.apache.cocoon.components.expression.ExpressionContext;
 import org.apache.cocoon.components.expression.ExpressionException;
 import org.apache.commons.jexl.JexlContext;
@@ -29,13 +29,12 @@
 /**
  * @version SVN $Id$
  */
-public class JexlExpression implements Expression {
+public class JexlExpression extends AbstractExpression {
 
-    private final String language;
     private final org.apache.commons.jexl.Expression compiledExpression;
 
     public JexlExpression(String language, String expression) throws ExpressionException
{
-        this.language = language;
+        super(language, expression);
         try {
             this.compiledExpression = org.apache.commons.jexl.ExpressionFactory.createExpression(expression);
         } catch (Exception e) {
@@ -78,18 +77,6 @@
         throw new UnsupportedOperationException("Assign is not yet implemented for Jexl");
     }
 
-    public String getExpression() {
-        return this.compiledExpression.getExpression();
-    }
-
-    public String getLanguage() {
-        return this.language;
-    }
-
-    public void setProperty(String property, Object value) {
-        // Has no properties
-    }
-
     public Object getNode(ExpressionContext context) throws ExpressionException {
         return evaluate(context);
     }
@@ -109,20 +96,6 @@
             this.context.setVars(map);
         }
     }
-
-    private static final Iterator EMPTY_ITER = new Iterator() {
-        public boolean hasNext() {
-            return false;
-        }
-
-        public Object next() {
-            return null;
-        }
-
-        public void remove() {
-            // EMPTY
-        }
-    };
 
     static {
         // Hack: there's no _nice_ way to add my introspector to Jexl right now

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java?rev=325986&r1=325985&r2=325986&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java
(original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java
Mon Oct 17 14:35:18 2005
@@ -35,7 +35,6 @@
  * @version SVN $Id$
  */
 public class JXPathExpression implements Expression {
-
     private final String language;
     private final String expression;
     private final CompiledExpression compiledExpression;

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTGStringTemplateParser.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTGStringTemplateParser.java?rev=325986&r1=325985&r2=325986&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTGStringTemplateParser.java
(original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/expression/JXTGStringTemplateParser.java
Mon Oct 17 14:35:18 2005
@@ -20,11 +20,12 @@
 import java.util.List;
 
 /**
- * @version SVN $Id$ 
+ * @version SVN $Id$
  */
 public class JXTGStringTemplateParser extends StringTemplateParser {
-    public static String JXPATH = "jxpath";
-    public static String JEXL = "jexl";
+    public final static String JXPATH = "jxpath";
+    public final static String JEXL = "jexl";
+    public final static String JAVASCRIPT = "js";
 
     public List parseSubstitutions(Reader in) throws Exception {
         LinkedList substitutions = new LinkedList();
@@ -32,7 +33,7 @@
         buf.setLength(0);
         int ch;
         boolean inExpr = false;
-        boolean xpath = false;
+        String lang = null;
         top: while ((ch = in.read()) != -1) {
             // column++;
             char c = (char) ch;
@@ -43,16 +44,17 @@
                         buf.append(ch == -1 ? '\\' : (char) ch);
                     } else if (c == '}') {
                         String str = buf.toString();
-                        substitutions.add(compile(str, xpath ? JXPATH : JEXL));
+                        substitutions.add(compile(str, lang));
                         buf.setLength(0);
                         inExpr = false;
                     } else {
                         buf.append(c);
                     }
-                } else if (c == '$' || c == '#') {
+                } else if (c == '$' || c == '#' || c == '@') {
                     ch = in.read();
                     if (ch == '{') {
-                        xpath = c == '#';
+
+                        lang = (c == '#') ? JXPATH : ((c == '$') ? JEXL : JAVASCRIPT);
                         inExpr = true;
                         if (buf.length() > 0) {
                             substitutions.add(new Literal(buf.toString()));
@@ -72,7 +74,7 @@
             }
         }
         if (inExpr)
-            throw new Exception("Unterminated " + (xpath ? "#" : "$") + "{");
+            throw new Exception("Unterminated {");
         substitutions.add(new Literal(buf.toString()));
         return substitutions;
     }

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=325986&r1=325985&r2=325986&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
Mon Oct 17 14:35:18 2005
@@ -19,6 +19,7 @@
 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.ServiceSelector;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.components.source.SourceUtil;
@@ -42,7 +43,9 @@
     private final static String JX_STORE_PREFIX = "jxtg:";
     private Store store;
     private InstructionFactory instructionFactory;
-    private StringTemplateParser jxtExpressionCompiler;
+    private ServiceSelector stringTemplateParserSelector;
+    private StringTemplateParser stringTemplateParser;
+    private String stringTemplateParserName = "jxtg";
 
     public DefaultScriptManager() {
     }
@@ -51,13 +54,17 @@
         this.manager = manager;
         this.store = (Store) this.manager.lookup(Store.TRANSIENT_STORE);
         this.instructionFactory = (InstructionFactory) this.manager.lookup(InstructionFactory.ROLE);
-        this.jxtExpressionCompiler = (StringTemplateParser) this.manager.lookup(StringTemplateParser.ROLE);
+        this.stringTemplateParserSelector = (ServiceSelector) this.manager.lookup(StringTemplateParser.ROLE
+                + "Selector");
+        this.stringTemplateParser = (StringTemplateParser) this.stringTemplateParserSelector
+                .select(this.stringTemplateParserName);
     }
 
     public void dispose() {
         this.manager.release(this.store);
         this.manager.release(this.instructionFactory);
-        this.manager.release(this.jxtExpressionCompiler);
+        this.stringTemplateParserSelector.release(this.stringTemplateParser);
+        this.manager.release(this.stringTemplateParserSelector);
     }
 
     private Store getStore() {
@@ -101,7 +108,7 @@
             }
 
             if (doc == null) {
-                Parser parser = new Parser(new ParsingContext(this.jxtExpressionCompiler,
this.instructionFactory));
+                Parser parser = new Parser(new ParsingContext(this.stringTemplateParser,
this.instructionFactory));
                 // call getValidity before using the stream is faster if
                 // the source is a SitemapSource
                 if (validity == null) {



Mime
View raw message