cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r565655 - in /cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables: PreparedVariableResolver.java VariableExpressionTokenizer.java
Date Tue, 14 Aug 2007 08:23:41 GMT
Author: gkossakowski
Date: Tue Aug 14 01:23:40 2007
New Revision: 565655

URL: http://svn.apache.org/viewvc?view=rev&rev=565655
Log:
COCOON-2110: First attempt to evaluate new expressions in sitemap. Expressions like ${...}
will be evaluated as expressions from cocoon-expression-language-impl module.

Modified:
    cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java
    cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableExpressionTokenizer.java

Modified: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java?view=diff&rev=565655&r1=565654&r2=565655
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java
(original)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/PreparedVariableResolver.java
Tue Aug 14 01:23:40 2007
@@ -16,22 +16,25 @@
  */
 package org.apache.cocoon.components.treeprocessor.variables;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.thread.ThreadSafe;
-
+import org.apache.cocoon.components.expression.Expression;
+import org.apache.cocoon.components.expression.ExpressionException;
+import org.apache.cocoon.components.expression.ExpressionFactory;
 import org.apache.cocoon.components.modules.input.InputModule;
 import org.apache.cocoon.components.treeprocessor.InvokeContext;
+import org.apache.cocoon.objectmodel.ObjectModel;
 import org.apache.cocoon.sitemap.PatternException;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Stack;
-
 /**
  * Prepared implementation of {@link VariableResolver} for fast evaluation.
  *
@@ -79,6 +82,9 @@
                     case VariableExpressionTokenizer.TokenReciever.TEXT:
                         tokens.add(new Token(value));
                         break;
+                    case VariableExpressionTokenizer.TokenReciever.NEW_EXPRESSION:
+                        tokens.add(new Token(NEW_EXPRESSION, value));
+                        break;
                     case VariableExpressionTokenizer.TokenReciever.MODULE:
                         Token token;
                         if (value.equals("sitemap")) {
@@ -184,6 +190,23 @@
                         Token module = (Token)stack.pop();
                         stack.pop(); // Pop the OPEN
                         result = processModule(module, expr, objectModel, context, mapStack,
stackSize);
+                    } else if (lastButOne.hasType(VariableExpressionTokenizer.TokenReciever.NEW_EXPRESSION))
{
+                        stack.pop(); // Pop the OPEN
+                        ExpressionFactory expressionFactory = null;
+                        ObjectModel newObjectModel = null;
+                        try {
+                            expressionFactory = (ExpressionFactory)manager.lookup(ExpressionFactory.ROLE);
+                            newObjectModel = (ObjectModel)manager.lookup(ObjectModel.ROLE);
+                            result = processNewExpression(lastButOne, expressionFactory,
newObjectModel);
+                        } catch (ServiceException e) {
+                            throw new PatternException("Cannot obtain necessary components
to evaluate new expression '"
+                                    + lastButOne.getStringValue() + "' in expression '" +
this.originalExpr + "'", e);
+                        } finally {
+                            if (expressionFactory != null)
+                                manager.release(expressionFactory);
+                            if (newObjectModel != null)
+                                manager.release(newObjectModel);
+                        }
                     } else {
                         result = processVariable(expr, mapStack, stackSize);
                     }
@@ -297,6 +320,18 @@
 
         Object result = ((Map)mapStack.get(stackSize - type)).get(value);
         return new Token(EXPR, result==null ? "" : result.toString());
+    }
+    
+    private Token processNewExpression(Token expr, ExpressionFactory expressionFactory, ObjectModel
newObjectModel) throws PatternException {
+        Object result;
+        try {
+            Expression newExpression = expressionFactory.getExpression(expr.getStringValue());
+            result = newExpression.evaluate(newObjectModel);
+        } catch (ExpressionException e) {
+            throw new PatternException("Cannot evaluate new expression '" + expr.getStringValue()
+ "' in expression "
+                                       + "'" + this.originalExpr + "'", e);
+        }
+        return new Token(EXPR, result == null ? "" : result.toString());
     }
 
     /**

Modified: cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableExpressionTokenizer.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableExpressionTokenizer.java?view=diff&rev=565655&r1=565654&r2=565655
==============================================================================
--- cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableExpressionTokenizer.java
(original)
+++ cocoon/trunk/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/variables/VariableExpressionTokenizer.java
Tue Aug 14 01:23:40 2007
@@ -37,6 +37,7 @@
         int TEXT = -5;
         int MODULE = -6;
         int VARIABLE = -8;
+        int NEW_EXPRESSION = -12;
 
         /**
          * Reports parsed tokens.
@@ -68,11 +69,31 @@
                 escape = false;
             } else if (c == '\\' && i < expression.length()) {
                 char nextChar = expression.charAt(i + 1);
-                if (nextChar == '{' || nextChar == '}') {
+                if (nextChar == '{' || nextChar == '}' || nextChar == '$') {
                     expression = expression.substring(0, i) + expression.substring(i + 1);
                     escape = true;
                     i--;
                 }
+            } else if (c == '$') {
+                if (expression.charAt(i+1) != '{')
+                    //it's not an expression like ${cocoon.request}, skipping
+                    continue;
+                
+                if (i > pos) {
+                    reciever.addToken(lastTokenType = TokenReciever.TEXT, expression.substring(pos,
i));
+                }
+                
+                i++;
+                openCount++;
+                reciever.addToken(lastTokenType = TokenReciever.OPEN, null);
+                
+                int closePos = indexOf(expression, '}', i);
+
+                //expression conforming cocoon-expression-language
+                String newExpression = expression.substring(i+1, closePos);
+                reciever.addToken(lastTokenType = TokenReciever.NEW_EXPRESSION, newExpression);
+                i = closePos - 1;
+                
             } else if (c == '{') {
                 if (i > pos) {
                     reciever.addToken(lastTokenType = TokenReciever.TEXT, expression.substring(pos,
i));



Mime
View raw message