Author: apetrelli
Date: Wed Feb 25 10:23:35 2009
New Revision: 747726

URL: http://svn.apache.org/viewvc?rev=747726&view=rev
Log:
TILESSB-7
Removed compilation problems.
Started initial example with "getAsString".
Removed some methods that are not useful anymore, because I "tiles-template" 
already has almost anything that a Velocity tool needs.

Added:
    
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
   (with props)
Modified:
    
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/Tiles2Tool.java
    
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityContextFactory.java
    
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityTiles2RequestContext.java

Modified: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/Tiles2Tool.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/Tiles2Tool.java?rev=747726&r1=747725&r2=747726&view=diff
==============================================================================
--- 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/Tiles2Tool.java
 (original)
+++ 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/Tiles2Tool.java
 Wed Feb 25 10:23:35 2009
@@ -21,165 +21,93 @@
 package com.anydoby.tiles2.velocity;
 
 import java.io.IOException;
+import java.util.Map;
 
 import org.apache.tiles.Attribute;
 import org.apache.tiles.AttributeContext;
 import org.apache.tiles.TilesContainer;
-import org.apache.tiles.TilesException;
-import org.apache.velocity.tools.view.context.ChainedContext;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.apache.tiles.template.AttributeResolver;
+import org.apache.tiles.template.DefaultAttributeResolver;
+import org.apache.tiles.template.GetAsStringModel;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.tools.view.ImportSupport;
 
 /**
  * 
  * @author SergeyZ
  * 
  */
-public class Tiles2Tool {
+public class Tiles2Tool extends ImportSupport {
 
-    private final TilesContainer container;
-    private final VelocityTiles2RequestContext context;
-
-    public Tiles2Tool(TilesContainer container, VelocityTiles2RequestContext 
context) {
-        this.container = container;
-        this.context = context;
-    }
-
-    /**
-     * Returns a string representation of attribute value. If the attribute is
-     * <code>null</code> or if the attribute value is <code>null</code>,
-     * <code>null</code> is returned
-     * 
-     * @param attributeName
-     */
-    public String getAsString(String attributeName) {
-        Attribute attribute = getAttribute(attributeName);
-        String value = null;
-        if (attribute != null) {
-            Object value2 = attribute.getValue();
-            if (value2 != null) {
-                value = value2.toString();
-            }
-        }
-        return value;
-    }
-
-    public Attribute getAttribute(String key) {
-        AttributeContext attributeContext = 
container.getAttributeContext(context);
-        Attribute attribute = attributeContext.getAttribute(key);
-        return attribute;
+    private Context velocityContext;
+    
+    private GetAsStringModel getAsStringModel;
+    
+    public Tiles2Tool() {
+        AttributeResolver attributeResolver = new DefaultAttributeResolver();
+        getAsStringModel = new GetAsStringModel(attributeResolver);
     }
 
     /**
-     * Imports attribute to current velocity context using the optional toName
-     * as the destination name in context
-     * 
-     * @param attributeName
-     * @param toName
-     */
-    public void importAttribute(String attributeName, String toName) {
-        Object attribute = getAttribute(attributeName);
-        if (toName == null) {
-            toName = attributeName;
+     * Initializes this tool.
+     *
+     * @param context the current {...@link Context}
+     * @throws IllegalArgumentException if the param is not a {...@link 
Context}
+     */
+    public void setVelocityContext(Context context)
+    {
+        if (context == null)
+        {
+            throw new NullPointerException("velocity context should not be 
null");
         }
-        context.put(toName, attribute);
-    }
-
-    /**
-     * Invokes {...@link #insertAttribute(String, true)}
-     * 
-     * @param attributeName
-     * @throws TilesException
-     * @throws IOException
-     */
-    public void insertAttribute(String attributeName) throws TilesException, 
IOException {
-        insertAttribute(attributeName, true);
+        this.velocityContext = context;
     }
-
+    
     /**
-     * <p>
-     * <strong>Inserts the value of an attribute into the page.</strong>
-     * </p>
-     * <p>
-     * This tag can be flexibly used to insert the value of an attribute into a
-     * page. As in other usages in Tiles, every attribute can be determined to
-     * have a "type", either set explicitly when it was defined, or "computed".
-     * If the type is not explicit, then if the attribute value is a valid
-     * definition, it will be inserted as such. Otherwise, if it begins with a
-     * "/" character, it will be treated as a "template". Finally, if it has 
not
-     * otherwise been assigned a type, it will be treated as a String and
-     * included without any special handling.
-     * </p>
-     * 
-     * @param attributeName
-     * @throws TilesException
-     * @throws IOException
-     */
-    public void insertAttribute(String attributeName, boolean ownContext) 
throws TilesException, IOException {
-        Attribute attribute = getAttribute(attributeName);
-        if (attribute == null) {
-            throw new TilesException("Attribute '" + attributeName + "' is 
null");
-        }
-        if (ownContext) {
-            ChainedContext chainedContext = new 
ChainedContext(context.getContext(), context.getEngine(), context
-                    .getRequest(), context.getResponse(), 
context.getServletContext());
-            container.startContext(chainedContext);
-            try {
-                render(attribute);
-            } finally {
-                container.endContext(chainedContext);
-            }
-        } else {
-            render(attribute);
-        }
-    }
-
-    /**
-     * Invokes {...@link #insertDefinition(String, true)}.
-     * 
-     * @param definitionName
-     * @throws TilesException
-     */
-    public void insertDefinition(String definitionName) throws TilesException {
-        insertDefinition(definitionName, true);
-    }
-
-    /**
-     * Inserts a named definition from the tiles definitions set.
+     * Returns a string representation of attribute value. If the attribute is
+     * <code>null</code> or if the attribute value is <code>null</code>,
+     * <code>null</code> is returned
      * 
-     * @param definitionName
-     * @param ownContext
-     *            if <code>true</code> a separate request context will be
-     *            created for the definition rendering. Can be used to avoid
-     *            name conflicts if the definition being included contains the
-     *            same attribute names as the invoking tile
-     * @throws TilesException
+     * @throws IOException If something goes wrong.
      */
-    public void insertDefinition(String definitionName, boolean ownContext) 
throws TilesException {
-        if (ownContext) {
-            ChainedContext chainedContext = new 
ChainedContext(context.getContext(), context.getEngine(), context
-                    .getRequest(), context.getResponse(), 
context.getServletContext());
-            container.startContext(chainedContext);
-            try {
-                container.render(definitionName, chainedContext);
-            } finally {
-                container.endContext(chainedContext);
-            }
-        } else {
-            container.render(definitionName, context);
-        }
+    public void getAsString_start(Map<String, Object> params) throws 
IOException {
+        getAsStringModel.start(ServletUtil.getComposeStack(request),
+                ServletUtil.getCurrentContainer(request, application),
+                VelocityUtil.toSimpleBoolean((Boolean) params.get("ignore"),
+                        false), (String) params.get("preparer"),
+                (String) params.get("role"), params.get("defaultValue"),
+                (String) params.get("defaultValueRole"), (String) params
+                        .get("defaultValueType"), (String) params.get("name"),
+                (Attribute) params.get("value"), velocityContext, request,
+                response);
     }
-
+    
     /**
-     * Includes the specified page.
+     * Returns a string representation of attribute value. If the attribute is
+     * <code>null</code> or if the attribute value is <code>null</code>,
+     * <code>null</code> is returned
      * 
-     * @param template
-     * @throws IOException
+     * @throws IOException If something goes wrong.
      */
-    public void insertTemplate(String template) throws IOException {
-        context.include(template);
+    public void getAsString(Map<String, Object> params) throws IOException {
+        TilesContainer container = ServletUtil.getCurrentContainer(request, 
application);
+        getAsStringModel.execute(container, response.getWriter(), VelocityUtil
+                .toSimpleBoolean((Boolean) params.get("ignore"), false),
+                (String) params.get("preparer"), (String) params.get("role"),
+                params.get("defaultValue"), (String) params
+                        .get("defaultValueRole"), (String) params
+                        .get("defaultValueType"), (String) params.get("name"),
+                (Attribute) params.get("value"), velocityContext, request,
+                response);
     }
 
-    private void render(Attribute attribute) throws TilesException, 
IOException {
-        container.render(attribute, context.getResponse().getWriter(), 
context);
+    public Attribute getAttribute(String key) {
+        TilesContainer container = ServletUtil.getCurrentContainer(request,
+                application);
+        AttributeContext attributeContext = container.getAttributeContext(
+                velocityContext, request, response);
+        Attribute attribute = attributeContext.getAttribute(key);
+        return attribute;
     }
-
 }

Modified: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityContextFactory.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityContextFactory.java?rev=747726&r1=747725&r2=747726&view=diff
==============================================================================
--- 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityContextFactory.java
 (original)
+++ 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityContextFactory.java
 Wed Feb 25 10:23:35 2009
@@ -22,14 +22,15 @@
 
 import java.util.Map;
 
-import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.tiles.TilesApplicationContext;
-import org.apache.tiles.context.TilesContextFactory;
+import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
 import org.apache.tiles.context.TilesRequestContext;
-import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
+import org.apache.tiles.context.TilesRequestContextFactory;
+import org.apache.tiles.servlet.context.ServletTilesRequestContext;
 import org.apache.velocity.context.Context;
-import org.apache.velocity.tools.view.context.ChainedContext;
 
 /**
  * 
@@ -37,25 +38,33 @@
  * 
  * @since Mar 15, 2008
  */
-public class VelocityContextFactory implements TilesContextFactory {
+public class VelocityContextFactory implements TilesRequestContextFactory, 
TilesRequestContextFactoryAware {
 
-    public TilesApplicationContext createApplicationContext(Object context) {
-        if (context instanceof ServletContext) {
-            ServletContext servletContext = (ServletContext) context;
-            return new ServletTilesApplicationContext(servletContext);
-        }
-        return null;
-    }
+    /**
+     * Parent Tiles context factory.
+     */
+    private TilesRequestContextFactory parent;
 
     public TilesRequestContext createRequestContext(TilesApplicationContext 
context, Object... requestItems) {
-        if (requestItems.length == 1) {
-            if (requestItems[0] instanceof Context) {
-                ChainedContext ctx = (ChainedContext) requestItems[0];
-                return new VelocityTiles2RequestContext(ctx);
-            } else if (requestItems[0] instanceof 
VelocityTiles2RequestContext) {
-                VelocityTiles2RequestContext ctx = 
(VelocityTiles2RequestContext) requestItems[0];
-                return ctx;
+        if (requestItems.length == 3 && requestItems[0] instanceof Context
+                && requestItems[1] instanceof HttpServletRequest
+                && requestItems[2] instanceof HttpServletResponse) {
+            Context ctx = (Context) requestItems[0];
+            HttpServletRequest request = (HttpServletRequest) requestItems[1];
+            HttpServletResponse response = (HttpServletResponse) 
requestItems[2];
+            TilesRequestContext enclosedRequest;
+            if (parent != null) {
+                enclosedRequest = parent.createRequestContext(context, 
request, response);
+            } else {
+                enclosedRequest = new ServletTilesRequestContext(context, 
request, response);
             }
+            return new VelocityTiles2RequestContext(enclosedRequest, ctx);
+        } else if (requestItems.length == 1
+            && requestItems[0] instanceof VelocityTiles2RequestContext) {
+            // FIXME is it necessary?
+            
+            VelocityTiles2RequestContext ctx = (VelocityTiles2RequestContext) 
requestItems[0];
+            return ctx;
         }
         return null;
     }
@@ -65,4 +74,8 @@
 
     }
 
+    public void setRequestContextFactory(
+            TilesRequestContextFactory contextFactory) {
+        this.parent = contextFactory;
+    }
 }

Modified: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityTiles2RequestContext.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityTiles2RequestContext.java?rev=747726&r1=747725&r2=747726&view=diff
==============================================================================
--- 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityTiles2RequestContext.java
 (original)
+++ 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityTiles2RequestContext.java
 Wed Feb 25 10:23:35 2009
@@ -22,53 +22,42 @@
 
 import java.io.IOException;
 
-import org.apache.tiles.servlet.context.ServletTilesRequestContext;
-import org.apache.velocity.Template;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.tools.view.context.ChainedContext;
-
-import static org.apache.tiles.access.TilesAccess.getContainer;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.context.TilesRequestContextWrapper;
+import org.apache.velocity.context.Context;
 
 /**
  * 
  * @author SergeyZ
  * 
  */
-public class VelocityTiles2RequestContext extends ServletTilesRequestContext {
+public class VelocityTiles2RequestContext extends TilesRequestContextWrapper {
 
-    private final ChainedContext ctx;
+    private final Context ctx;
+    
+    private Object[] requestObjects;
 
-    public VelocityTiles2RequestContext(ChainedContext ctx) {
-        super(ctx.getServletContext(), ctx.getRequest(), ctx.getResponse());
+    public VelocityTiles2RequestContext(TilesRequestContext enclosedRequest, 
Context ctx) {
+        super(enclosedRequest);
         this.ctx = ctx;
-        ctx.put("tiles", new Tiles2Tool(getContainer(ctx.getServletContext()), 
this));
+        // FIXME This should go into a renderer
+        //ctx.put("tiles", new 
Tiles2Tool(getContainer(ctx.getServletContext()), this));
     }
 
     public void dispatch(String path) throws IOException {
         include(path);
     }
 
-    public ChainedContext getContext() {
-        return ctx;
-    }
-
-    public VelocityEngine getEngine() {
-        VelocityEngine velocityEngine = ctx.getVelocityEngine();
-        return velocityEngine;
-    }
-
     @Override
-    public void include(String path) throws IOException {
-        try {
-            Template template = ctx.getVelocityEngine().getTemplate(path);
-            template.merge(ctx, getResponse().getWriter());
-        } catch (Exception e) {
-            throw new IOException(e.getMessage(), e);
+    public Object[] getRequestObjects() {
+        if (requestObjects == null) {
+            Object[] parentRequestObjects = super.getRequestObjects();
+            requestObjects = new Object[parentRequestObjects.length + 1];
+            requestObjects[0] = ctx;
+            for (int i = 0; i < parentRequestObjects.length; i++) {
+                requestObjects[i+1] = parentRequestObjects[i];
+            }
         }
+        return requestObjects;
     }
-
-    public void put(String toName, Object attribute) {
-        ctx.put(toName, attribute);
-    }
-
 }

Added: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java?rev=747726&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
 (added)
+++ 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
 Wed Feb 25 10:23:35 2009
@@ -0,0 +1,14 @@
+package com.anydoby.tiles2.velocity;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.template.GetAsStringModel;
+
+public class VelocityUtil {
+
+    private final static String TEMPLATE_PREFIX="org.apache.tiles.template.";
+    
+    public static boolean toSimpleBoolean(Boolean obj, boolean defaultValue) {
+        return obj != null ? obj : defaultValue;
+    }
+}

Propchange: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/sandbox/trunk/tiles-velocity/src/main/java/com/anydoby/tiles2/velocity/VelocityUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL


Reply via email to