Author: apetrelli
Date: Wed Apr 29 22:10:10 2009
New Revision: 769964

URL: http://svn.apache.org/viewvc?rev=769964&view=rev
Log:
-- Second phase, added files --

TILES-400
FreeMarker attribute renderer created

TILES-408
A Velocity-styled tool has been created.

TILES-406
initContainer deprecated and test removed.

TILES-398
Velocity attribute renderer created.

TILES-403
Now TilesRequestContext can set content type and write response headers.

TILES-405
templateExpression and templateType now supported.

Added:
    
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/CompareUtil.java
    
tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/
    
tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/FreeMarkerAttributeRenderer.java
    
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ExternalWriterHttpServletResponse.java
      - copied, changed from r768810, 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/ExternalWriterHttpServletResponse.java
    
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/NotAServletEnvironmentException.java
    tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/
    tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/MANIFEST.MF
    
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/
    
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/VelocityAttributeRenderer.java
    
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/VelocityStyleTilesTool.java
    
tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/VelocityStyleTilesToolTest.java

Added: 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/CompareUtil.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/CompareUtil.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/CompareUtil.java 
(added)
+++ 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/CompareUtil.java 
Wed Apr 29 22:10:10 2009
@@ -0,0 +1,13 @@
+package org.apache.tiles;
+
+public final class CompareUtil {
+
+    private CompareUtil() {}
+
+    public static boolean nullSafeEquals(Object obj1, Object obj2) {
+        if (obj1 != null) {
+            return obj1.equals(obj2);
+        }
+        return obj2 == null;
+    }
+}

Added: 
tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/FreeMarkerAttributeRenderer.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/FreeMarkerAttributeRenderer.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/FreeMarkerAttributeRenderer.java
 (added)
+++ 
tiles/framework/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/renderer/FreeMarkerAttributeRenderer.java
 Wed Apr 29 22:10:10 2009
@@ -0,0 +1,184 @@
+/*
+ * $Id: TilesFreemarkerServlet.java 765386 2009-04-15 21:56:54Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.freemarker.renderer;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.collections.iterators.IteratorEnumeration;
+import org.apache.tiles.Attribute;
+import org.apache.tiles.Initializable;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.servlet.TilesFreemarkerServlet;
+import org.apache.tiles.impl.InvalidTemplateException;
+import org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer;
+import org.apache.tiles.servlet.context.ExternalWriterHttpServletResponse;
+import org.apache.tiles.servlet.context.ServletTilesRequestContext;
+import org.apache.tiles.servlet.context.ServletUtil;
+
+/**
+ * FreeMarker renderer for rendering FreeMarker templates as Tiles attributes.
+ * It is only usable under a Servlet environment, because it uses
+ * {...@link TilesFreemarkerServlet} internally to forward the request.<br/>
+ * To initialize it correctly, call {...@link #setParameter(String, String)} 
for all the
+ * parameters that you want to set, and then call {...@link #commit()}.
+ *
+ * @version $Rev: 765386 $ $Date: 2009-04-15 23:56:54 +0200 (mer, 15 apr 2009) 
$
+ * @since 2.2.0
+ */
+public class FreeMarkerAttributeRenderer extends AbstractBaseAttributeRenderer 
implements Initializable {
+
+    /**
+     * The servlet that is used to forward the request to.
+     */
+    private AttributeValueFreemarkerServlet servlet;
+
+    /**
+     * The initialization parameters.
+     */
+    private Map<String, String> params = new HashMap<String, String>();
+
+    /** {...@inheritdoc} */
+    public void init(Map<String, String> params) {
+        this.params.putAll(params);
+        commit();
+    }
+
+    /**
+     * Sets a parameter for the internal servlet.
+     *
+     * @param key The name of the parameter.
+     * @param value The value of the parameter.
+     * @since 2.2.0
+     */
+    public void setParameter(String key, String value) {
+        params.put(key, value);
+    }
+
+    /**
+     * Commits the parameters and makes this renderer ready for the use.
+     *
+     * @since 2.2.0
+     */
+    public void commit() {
+        servlet = new AttributeValueFreemarkerServlet();
+        try {
+            servlet.init(new InitParamsServletConfig());
+        } catch (ServletException e) {
+            throw new FreeMarkerTilesException("Cannot initialize internal 
servlet");
+        }
+    }
+
+    /** {...@inheritdoc} */
+    @Override
+    public void write(Object value, Attribute attribute,
+            TilesRequestContext request) throws IOException {
+        if (value != null) {
+            if (value instanceof String) {
+                ServletTilesRequestContext servletRequest = 
ServletUtil.getServletRequest(request);
+                HttpServletRequest httpRequest = servletRequest.getRequest();
+                HttpServletResponse httpResponse = 
servletRequest.getResponse();
+                servlet.setValue((String) value);
+                try {
+                    servlet.doGet(httpRequest, new 
ExternalWriterHttpServletResponse(httpResponse, request.getPrintWriter()));
+                } catch (ServletException e) {
+                    throw new FreeMarkerTilesException("Exception when 
rendering a FreeMarker attribute", e);
+                }
+            } else {
+                throw new InvalidTemplateException(
+                        "Cannot render a template that is not an object: "
+                                + value.toString());
+            }
+        } else {
+            throw new InvalidTemplateException("Cannot render a null 
template");
+        }
+    }
+
+    /**
+     * Extends {...@link TilesFreemarkerServlet} to use the attribute value as 
the template name.
+     *
+     * @since 2.2.0
+     */
+    private static class AttributeValueFreemarkerServlet extends 
TilesFreemarkerServlet {
+
+        /**
+         * Holds the value that should be used as the template name.
+         */
+        private ThreadLocal<String> valueHolder = new ThreadLocal<String>();
+
+        /**
+         * Sets the value to use as the template name.
+         *
+         * @param value The template name.
+         * @since 2.2.0
+         */
+        public void setValue(String value) {
+            valueHolder.set(value);
+        }
+
+        /** {...@inheritdoc} */
+        @Override
+        protected String requestUrlToTemplatePath(HttpServletRequest request) {
+            return valueHolder.get();
+        }
+    }
+
+    /**
+     * Implements {...@link ServletConfig} to initialize the internal servlet 
using parameters
+     * set through {...@link FreeMarkerAttributeRenderer#setParameter(String, 
String)}.
+     *
+     * @version $Rev$ $Date$
+     * @since 2.2.0
+     */
+    private class InitParamsServletConfig implements ServletConfig {
+
+        /** {...@inheritdoc} */
+        public String getInitParameter(String name) {
+            return params.get(name);
+        }
+
+        /** {...@inheritdoc} */
+        @SuppressWarnings("unchecked")
+        public Enumeration getInitParameterNames() {
+            return new IteratorEnumeration(params.keySet().iterator());
+        }
+
+        /** {...@inheritdoc} */
+        public ServletContext getServletContext() {
+            return ServletUtil.getServletContext(applicationContext);
+        }
+
+        /** {...@inheritdoc} */
+        public String getServletName() {
+            return "FreeMarker Attribute Renderer";
+        }
+    }
+}

Copied: 
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ExternalWriterHttpServletResponse.java
 (from r768810, 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/ExternalWriterHttpServletResponse.java)
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ExternalWriterHttpServletResponse.java?p2=tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ExternalWriterHttpServletResponse.java&p1=tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/ExternalWriterHttpServletResponse.java&r1=768810&r2=769964&rev=769964&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/ExternalWriterHttpServletResponse.java
 (original)
+++ 
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ExternalWriterHttpServletResponse.java
 Wed Apr 29 22:10:10 2009
@@ -19,7 +19,7 @@
  * under the License.
  */
 
-package org.apache.tiles.velocity.context;
+package org.apache.tiles.servlet.context;
 
 import java.io.IOException;
 import java.io.PrintWriter;

Added: 
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/NotAServletEnvironmentException.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/NotAServletEnvironmentException.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/NotAServletEnvironmentException.java
 (added)
+++ 
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/NotAServletEnvironmentException.java
 Wed Apr 29 22:10:10 2009
@@ -0,0 +1,74 @@
+/*
+ * $Id: PortletTilesRequestContext.java 736275 2009-01-21 09:58:20Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.servlet.context;
+
+import org.apache.tiles.TilesException;
+
+/**
+ * Exception that indicates that a resource could not be used because it is not
+ * in a servlet environment.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.2.0
+ */
+public class NotAServletEnvironmentException extends TilesException {
+
+    /**
+     * Constructor.
+     *
+     * @since 2.2.0
+     */
+       public NotAServletEnvironmentException() {
+       }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @since 2.2.0
+     */
+       public NotAServletEnvironmentException(String message) {
+               super(message);
+       }
+
+    /**
+     * Constructor.
+     *
+     * @param e The exception to be wrapped.
+     * @since 2.2.0
+     */
+       public NotAServletEnvironmentException(Exception e) {
+               super(e);
+       }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @param e The exception to be wrapped.
+     * @since 2.2.0
+     */
+       public NotAServletEnvironmentException(String message, Exception e) {
+               super(message, e);
+       }
+
+}

Added: tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/MANIFEST.MF?rev=769964&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/MANIFEST.MF 
(added)
+++ tiles/framework/trunk/tiles-test/src/main/webapp/META-INF/MANIFEST.MF Wed 
Apr 29 22:10:10 2009
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path: 
+

Added: 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/VelocityAttributeRenderer.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/VelocityAttributeRenderer.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/VelocityAttributeRenderer.java
 (added)
+++ 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/renderer/VelocityAttributeRenderer.java
 Wed Apr 29 22:10:10 2009
@@ -0,0 +1,149 @@
+/*
+ * $Id: BasicRendererFactoryTest.java 711885 2008-11-06 16:06:38Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.velocity.renderer;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.commons.collections.iterators.IteratorEnumeration;
+import org.apache.tiles.Attribute;
+import org.apache.tiles.Initializable;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.impl.InvalidTemplateException;
+import org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer;
+import org.apache.tiles.servlet.context.ServletTilesRequestContext;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.apache.velocity.Template;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.tools.view.AbstractJeeConfigImpl;
+import org.apache.velocity.tools.view.VelocityView;
+
+/**
+ * Attribute renderer for rendering Velocity templates as attributes. <br>
+ * It is available only to Servlet-based environment.<br>
+ * It uses {...@link VelocityView} to render the response.<br>
+ * To initialize it correctly, call {...@link #setParameter(String, String)} 
for
+ * all the parameters that you want to set, and then call {...@link #commit()}.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.2.0
+ */
+public class VelocityAttributeRenderer extends AbstractBaseAttributeRenderer
+        implements Initializable {
+
+    /**
+     * The VelocityView object to use.
+     */
+    private VelocityView velocityView;
+
+    /**
+     * The initialization parameters for VelocityView.
+     */
+    private Map<String, String> params = new HashMap<String, String>();
+
+    /** {...@inheritdoc} */
+    public void init(Map<String, String> params) {
+        this.params.putAll(params);
+        commit();
+    }
+
+    /**
+     * Sets a parameter for the internal servlet.
+     *
+     * @param key The name of the parameter.
+     * @param value The value of the parameter.
+     * @since 2.2.0
+     */
+    public void setParameter(String key, String value) {
+        params.put(key, value);
+    }
+
+    /**
+     * Commits the parameters and makes this renderer ready for the use.
+     *
+     * @since 2.2.0
+     */
+    public void commit() {
+       velocityView = new VelocityView(new TilesApplicationContextJeeConfig());
+    }
+
+    /** {...@inheritdoc} */
+    @Override
+    public void write(Object value, Attribute attribute,
+            TilesRequestContext request) throws IOException {
+        if (value != null) {
+            if (value instanceof String) {
+                ServletTilesRequestContext servletRequest = 
ServletUtil.getServletRequest(request);
+                // then get a context
+                Context context = velocityView.createContext(servletRequest
+                        .getRequest(), servletRequest.getResponse());
+
+                // get the template
+                Template template = velocityView.getTemplate((String) value);
+
+                // merge the template and context into the writer
+                velocityView.merge(template, context, request.getWriter());
+            } else {
+                throw new InvalidTemplateException(
+                        "Cannot render a template that is not an object: "
+                                + value.toString());
+            }
+        } else {
+            throw new InvalidTemplateException("Cannot render a null 
template");
+        }
+    }
+
+    /**
+     * Implements JeeConfig to use parameters set through
+     * {...@link VelocityAttributeRenderer#setParameter(String, String)}.
+     *
+     * @version $Rev$ $Date$
+     * @since 2.2.0
+     */
+    private class TilesApplicationContextJeeConfig extends 
AbstractJeeConfigImpl {
+
+        /** {...@inheritdoc} */
+        public String getInitParameter(String name) {
+            return params.get("name");
+        }
+
+        /** {...@inheritdoc} */
+        @SuppressWarnings("unchecked")
+        public Enumeration getInitParameterNames() {
+            return new IteratorEnumeration(params.keySet().iterator());
+        }
+
+        /** {...@inheritdoc} */
+        public String getName() {
+            return "Tiles Application Context JEE Config";
+        }
+
+        /** {...@inheritdoc} */
+        public ServletContext getServletContext() {
+            return ServletUtil.getServletContext(applicationContext);
+        }
+    }
+}

Added: 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/VelocityStyleTilesTool.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/VelocityStyleTilesTool.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/VelocityStyleTilesTool.java
 (added)
+++ 
tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/VelocityStyleTilesTool.java
 Wed Apr 29 22:10:10 2009
@@ -0,0 +1,212 @@
+/*
+ * $Id: Tiles2Tool.java 765774 2009-04-16 21:43:00Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.velocity.template;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.tiles.Attribute;
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.Renderable;
+
+/**
+ * Tiles Tool to be used "the classic way".
+ *
+ * @version $Rev$ $Date$
+ * @since 2.2.0
+ */
+public class VelocityStyleTilesTool extends ContextHolder {
+
+    /**
+     * Returns an attribute.
+     *
+     * @param key The name of the attribute to get.
+     * @return The Attribute.
+     * @since 2.2.0
+     */
+    public Attribute getAttribute(String key) {
+        TilesContainer container = ServletUtil.getCurrentContainer(
+                getRequest(), getServletContext());
+        AttributeContext attributeContext = container.getAttributeContext(
+                getVelocityContext(), getRequest(), getResponse());
+        Attribute attribute = attributeContext.getAttribute(key);
+        return attribute;
+    }
+
+    /**
+     * Creates an attribute that is a copy of the one passed as a parameter.
+     *
+     * @param attribute The attribute to copy.
+     * @return The copied attribute.
+     * @since 2.2.0
+     */
+    public Attribute cloneAttribute(Attribute attribute) {
+        return new Attribute(attribute);
+    }
+
+    /**
+     * Creates an attribute that represents a template.
+     *
+     * @param template The template.
+     * @return The attribute.
+     * @since 2.2.0
+     */
+    public Attribute createTemplateAttribute(String template) {
+        return Attribute.createTemplateAttribute(template);
+    }
+
+    /**
+     * Renders an attribute.
+     *
+     * @param attribute The attribute to render.
+     * @return The renderable object, ready to be rendered.
+     * @since 2.2.0
+     */
+    public Renderable renderAttribute(final Attribute attribute) {
+        return new AbstractDefaultToStringRenderable(getVelocityContext(),
+                null, getResponse(), getRequest()) {
+
+            public boolean render(InternalContextAdapter context, Writer 
writer)
+                    throws IOException, MethodInvocationException,
+                    ParseErrorException, ResourceNotFoundException {
+                TilesContainer container = 
ServletUtil.getCurrentContainer(request, getServletContext());
+                container.render(attribute, velocityContext, request, 
response, writer);
+                return true;
+            }
+
+        };
+    }
+
+    /**
+     * Renders a definition. It can be used in conjunction with
+     * {...@link #startAttributeContext()} and {...@link 
#endAttributeContext()} to
+     * customize appearance.
+     *
+     * @param definitionName The name of the definition to render.
+     * @return The renderable that renders the definition.
+     * @since 2.2.0
+     */
+    public Renderable renderDefinition(final String definitionName) {
+        return new AbstractDefaultToStringRenderable(getVelocityContext(),
+                null, getResponse(), getRequest()) {
+
+            public boolean render(InternalContextAdapter context, Writer 
writer)
+                    throws IOException, MethodInvocationException,
+                    ParseErrorException, ResourceNotFoundException {
+                TilesContainer container = 
ServletUtil.getCurrentContainer(request, getServletContext());
+                container.render(definitionName, velocityContext, request, 
response, writer);
+                return true;
+            }
+
+        };
+    }
+
+    /**
+     * Renders the current attribute context. It can be used in conjunction 
with
+     * {...@link #startAttributeContext()} and {...@link 
#endAttributeContext()} to
+     * customize appearance.
+     *
+     * @return The renderable that renders the current attribute context.
+     * @since 2.2.0
+     */
+    public Renderable renderAttributeContext() {
+        return new AbstractDefaultToStringRenderable(getVelocityContext(),
+                null, getResponse(), getRequest()) {
+
+            public boolean render(InternalContextAdapter context, Writer 
writer)
+                    throws IOException, MethodInvocationException,
+                    ParseErrorException, ResourceNotFoundException {
+                TilesContainer container = 
ServletUtil.getCurrentContainer(request, getServletContext());
+                container.renderContext(velocityContext, request, response, 
writer);
+                return true;
+            }
+
+        };
+    }
+
+    /**
+     * Starts the attribute context. Remember to call
+     * {...@link #endAttributeContext()} when finished!
+     *
+     * @return The started attribute context, ready to be customized.
+     * @since 2.2.0
+     */
+    public AttributeContext startAttributeContext() {
+        TilesContainer container = ServletUtil.getCurrentContainer(
+                getRequest(), getServletContext());
+        return container.startContext(getVelocityContext(), getRequest(),
+                getResponse());
+    }
+
+    /**
+     * Ends the current attribute context. To be called after
+     * {...@link #startAttributeContext()}.
+     *
+     * @return The tool itself.
+     * @since 2.2.0
+     */
+    public VelocityStyleTilesTool endAttributeContext() {
+        TilesContainer container = ServletUtil.getCurrentContainer(
+                getRequest(), getServletContext());
+        container.endContext(getVelocityContext(), getRequest(),
+                getResponse());
+        return this;
+    }
+
+    /**
+     * Returns the current attribute context.
+     *
+     * @return The current attribute context.
+     * @since 2.2.0
+     */
+    public AttributeContext getAttributeContext() {
+        TilesContainer container = ServletUtil.getCurrentContainer(
+                getRequest(), getServletContext());
+        return container.getAttributeContext(getVelocityContext(), 
getRequest(),
+                getResponse());
+    }
+
+    /**
+     * Sets the current container for the current request.
+     *
+     * @param containerKey The key of the container to set as "current" for 
the current request.
+     * @return The tool itself.
+     * @since 2.2.0
+     */
+    public VelocityStyleTilesTool setCurrentContainer(String containerKey) {
+        ServletUtil.setCurrentContainer(getRequest(), getServletContext(),
+                containerKey);
+        return this;
+    }
+
+    /** {...@inheritdoc} */
+    @Override
+    public String toString() {
+        return "";
+    }
+}

Added: 
tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/VelocityStyleTilesToolTest.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/VelocityStyleTilesToolTest.java?rev=769964&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/VelocityStyleTilesToolTest.java
 (added)
+++ 
tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/VelocityStyleTilesToolTest.java
 Wed Apr 29 22:10:10 2009
@@ -0,0 +1,179 @@
+/**
+ *
+ */
+package org.apache.tiles.velocity.template;
+
+import static org.junit.Assert.*;
+import static org.easymock.classextension.EasyMock.*;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.tiles.Attribute;
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.apache.velocity.context.Context;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * TODO
+ *
+ * @version $Rev$ $Date$
+ * @since 2.2.0
+ */
+public class VelocityStyleTilesToolTest {
+
+    /**
+     * The tool to test.
+     */
+    private VelocityStyleTilesTool tool;
+
+    /**
+     * The request object.
+     */
+    private HttpServletRequest request;
+
+    /**
+     * The response object.
+     */
+    private HttpServletResponse response;
+
+    /**
+     * The servlet context.
+     */
+    private ServletContext servletContext;
+
+    /**
+     * The current velocity context.
+     */
+    private Context velocityContext;
+
+    /**
+     * Sets up the tool to test.
+     *
+     * @throws java.lang.Exception
+     * @since 2.2.0
+     */
+    @Before
+    public void setUp() throws Exception {
+        tool = new VelocityStyleTilesTool();
+        request = createMock(HttpServletRequest.class);
+        response = createMock(HttpServletResponse.class);
+        velocityContext = createMock(Context.class);
+        servletContext = createMock(ServletContext.class);
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#getAttribute(java.lang.String)}.
+     */
+    @Test
+    public void testGetAttribute() {
+        TilesContainer container = createMock(TilesContainer.class);
+        AttributeContext attributeContext = createMock(AttributeContext.class);
+        Attribute attribute = new Attribute("myValue");
+
+        
expect(request.getAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME))
+                .andReturn(container);
+        expect(container.getAttributeContext(velocityContext, request, 
response))
+                .andReturn(attributeContext);
+        
expect(attributeContext.getAttribute("myAttribute")).andReturn(attribute);
+
+        replay(velocityContext, request, response, servletContext, container, 
attributeContext);
+        initializeTool();
+        assertEquals(attribute, tool.getAttribute("myAttribute"));
+        verify(velocityContext, request, response, servletContext, container, 
attributeContext);
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#cloneAttribute(org.apache.tiles.Attribute)}.
+     */
+    @Test
+    public void testCloneAttribute() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#createTemplateAttribute(java.lang.String)}.
+     */
+    @Test
+    public void testCreateTemplateAttribute() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#renderAttribute(org.apache.tiles.Attribute)}.
+     */
+    @Test
+    public void testRenderAttribute() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#renderDefinition(java.lang.String)}.
+     */
+    @Test
+    public void testRenderDefinition() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#renderAttributeContext()}.
+     */
+    @Test
+    public void testRenderAttributeContext() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#startAttributeContext()}.
+     */
+    @Test
+    public void testStartAttributeContext() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#endAttributeContext()}.
+     */
+    @Test
+    public void testEndAttributeContext() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#getAttributeContext()}.
+     */
+    @Test
+    public void testGetAttributeContext() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#setCurrentContainer(java.lang.String)}.
+     */
+    @Test
+    public void testSetCurrentContainer() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Test method for {...@link 
org.apache.tiles.velocity.template.VelocityStyleTilesTool#toString()}.
+     */
+    @Test
+    public void testToString() {
+        fail("Not yet implemented");
+    }
+
+    /**
+     * Initializes the tool for the test.
+     */
+    private void initializeTool() {
+        tool.setRequest(request);
+        tool.setResponse(response);
+        tool.setServletContext(servletContext);
+        tool.setVelocityContext(velocityContext);
+    }
+}


Reply via email to