Author: apetrelli
Date: Tue Dec 23 06:04:45 2008
New Revision: 728958
URL: http://svn.apache.org/viewvc?rev=728958&view=rev
Log:
TILESSB-4
Added first bunch of directive models.
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
(with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
(with props)
Modified:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContextFactory.java
Modified:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContextFactory.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContextFactory.java?rev=728958&r1=728957&r2=728958&view=diff
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContextFactory.java
(original)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContextFactory.java
Tue Dec 23 06:04:45 2008
@@ -5,6 +5,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
import org.apache.tiles.context.TilesRequestContext;
@@ -14,12 +16,12 @@
import freemarker.core.Environment;
import freemarker.ext.servlet.HttpRequestHashModel;
-import freemarker.template.TemplateHashModel;
-import freemarker.template.TemplateModelException;
public class FreeMarkerTilesRequestContextFactory implements
TilesRequestContextFactory, TilesRequestContextFactoryAware {
+ private static final Log LOG =
LogFactory.getLog(FreeMarkerTilesRequestContextFactory.class);
+
/**
* Parent Tiles context factory.
*/
@@ -35,29 +37,25 @@
TilesApplicationContext context, Object... requestItems) {
if (requestItems.length == 1 && requestItems[0] instanceof
Environment) {
Environment env = (Environment) requestItems[0];
- TemplateHashModel dataModel = env.getDataModel();
+ HttpRequestHashModel requestModel;
try {
- Object requestObj = dataModel.get("Request");
- if (requestObj instanceof HttpRequestHashModel) {
- HttpRequestHashModel requestModel = (HttpRequestHashModel)
requestObj;
- HttpServletRequest request = requestModel.getRequest();
- HttpServletResponse response = requestModel.getResponse();
- TilesRequestContext enclosedRequest;
- if (parent != null) {
- enclosedRequest = parent.createRequestContext(context,
request,
- response);
- } else {
- enclosedRequest = new
ServletTilesRequestContext(context,
- (HttpServletRequest) request,
- (HttpServletResponse) response);
- }
- return new FreeMarkerTilesRequestContext(enclosedRequest,
env);
- } else {
- throw new FreeMarkerTilesException("The request hash model
is not present");
- }
- } catch (TemplateModelException e) {
- throw new FreeMarkerTilesException("Cannot complete the
FreeMarker request", e);
+ requestModel = FreeMarkerUtil.getRequestHashModel(env);
+ } catch (FreeMarkerTilesException e) {
+ LOG.warn("Cannot evaluate as a FreeMarker in Servlet
Environment, skipping", e);
+ return null;
}
+ HttpServletRequest request = requestModel.getRequest();
+ HttpServletResponse response = requestModel.getResponse();
+ TilesRequestContext enclosedRequest;
+ if (parent != null) {
+ enclosedRequest = parent.createRequestContext(context, request,
+ response);
+ } else {
+ enclosedRequest = new ServletTilesRequestContext(context,
+ (HttpServletRequest) request,
+ (HttpServletResponse) response);
+ }
+ return new FreeMarkerTilesRequestContext(enclosedRequest, env);
}
return null;
}
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,132 @@
+package org.apache.tiles.freemarker.context;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.servlet.context.ServletUtil;
+
+import freemarker.core.Environment;
+import freemarker.ext.servlet.FreemarkerServlet;
+import freemarker.ext.servlet.HttpRequestHashModel;
+import freemarker.ext.servlet.ServletContextHashModel;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+import freemarker.template.utility.DeepUnwrap;
+
+public class FreeMarkerUtil {
+
+ private FreeMarkerUtil() {
+ }
+
+ /**
+ * Returns true if forced include of the result is needed.
+ *
+ * @param request The HTTP request.
+ * @return If <code>true</code> the include operation must be forced.
+ * @since 2.0.6
+ */
+ public static boolean isForceInclude(Environment env) {
+ return ServletUtil
+ .isForceInclude(getRequestHashModel(env).getRequest());
+ }
+
+ /**
+ * Sets the option that enables the forced include of the response.
+ *
+ * @param request The HTTP request.
+ * @param forceInclude If <code>true</code> the include operation must be
+ * forced.
+ */
+ public static void setForceInclude(Environment env, boolean forceInclude) {
+ ServletUtil.setForceInclude(getRequestHashModel(env).getRequest(),
+ forceInclude);
+ }
+
+ /**
+ * Sets the current container to use in web pages.
+ *
+ * @param request The request to use.
+ * @param context The servlet context to use.
+ * @param key The key under which the container is stored.
+ */
+ public static void setCurrentContainer(Environment env,
+ ServletContext context, String key) {
+ ServletUtil.setCurrentContainer(getRequestHashModel(env).getRequest(),
+ context, key);
+ }
+
+ /**
+ * Sets the current container to use in web pages.
+ *
+ * @param request The request to use.
+ * @param context The servlet context to use.
+ * @param container The container to use as the current container.
+ */
+ public static void setCurrentContainer(Environment env,
+ TilesContainer container) {
+ ServletUtil.setCurrentContainer(getRequestHashModel(env).getRequest(),
+ getServletContextHashModel(env).getServlet()
+ .getServletContext(), container);
+ }
+
+ /**
+ * Returns the current container that has been set, or the default one.
+ *
+ * @param request The request to use.
+ * @param context The servlet context to use.
+ * @return The current Tiles container to use in web pages.
+ */
+ public static TilesContainer getCurrentContainer(Environment env) {
+ return ServletUtil.getCurrentContainer(getRequestHashModel(env)
+ .getRequest(), getServletContextHashModel(env).getServlet()
+ .getServletContext());
+ }
+
+ public static HttpRequestHashModel getRequestHashModel(Environment env) {
+ try {
+ return (HttpRequestHashModel) env.getDataModel().get(
+ FreemarkerServlet.KEY_REQUEST);
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException(
+ "Exception got when obtaining the request hash model", e);
+ }
+ }
+
+ public static ServletContextHashModel getServletContextHashModel(
+ Environment env) {
+ try {
+ return (ServletContextHashModel) env.getDataModel().get(
+ FreemarkerServlet.KEY_APPLICATION);
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException(
+ "Exception got when obtaining the application hash model",
+ e);
+ }
+ }
+
+ public static String getAsString(TemplateModel model) {
+ try {
+ return (String) DeepUnwrap.unwrap(model);
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException("Cannot unwrap a model", e);
+ }
+ }
+
+ public static boolean getAsBoolean(TemplateModel model, boolean
defaultValue) {
+ try {
+ Boolean retValue = (Boolean) DeepUnwrap.unwrap(model);
+ return retValue != null ? retValue : defaultValue;
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException("Cannot unwrap a model", e);
+ }
+ }
+
+ public static Object getAsObject(TemplateModel model) {
+ try {
+ return DeepUnwrap.unwrap(model);
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException("Cannot unwrap a model", e);
+ }
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerUtil.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,164 @@
+/*
+ * $Id$
+ *
+ * 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.template;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.tiles.Attribute;
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.ListAttribute;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateModel;
+
+/**
+ * <p>
+ * Support for all tags which render (an attribute, a template, or definition).
+ * </p>
+ * <p>
+ * Properly invokes the defined preparer and invokes the abstract render method
+ * upon completion.
+ * </p>
+ * This tag takes special care to ensure that the attribute context is reset to
+ * it's original state after the execution of the tag is complete. This ensures
+ * that all all included attributes in subsequent tiles are scoped properly and
+ * do not bleed outside their intended scope.
+ *
+ * @since Tiles 2.0
+ * @version $Rev$ $Date$
+ */
+public abstract class AbstractRenderModel extends
NestableTemplateDirectiveModel implements
+ PutAttributeModelParent, PutListAttributeModelParent {
+
+ /**
+ * The log instance for this tag.
+ */
+ private static final Log LOG =
LogFactory.getLog(AbstractRenderModel.class);
+
+ protected TilesContainer container;
+
+ private AttributeContext attributeContext;
+
+ /** {...@inheritdoc} */
+ public void doStart(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ container = FreeMarkerUtil.getCurrentContainer(env);
+ if (container != null) {
+ startContext(env, params, loopVars, body);
+ } else {
+ throw new FreeMarkerTilesException("TilesContainer not
initialized");
+ }
+ }
+
+ /** {...@inheritdoc} */
+ public void doEnd(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ try {
+ render(env, params, loopVars, body);
+ if (FreeMarkerUtil.getAsBoolean(params.get("flush"), false)) {
+ env.getOut().flush();
+ }
+ } catch (IOException io) {
+ String message = "IO Error executing tag: " + io.getMessage();
+ LOG.error(message, io);
+ throw new FreeMarkerTilesException(message, io);
+ } finally {
+ endContext(env, params, loopVars, body);
+ }
+ }
+
+ /**
+ * Render the specified content.
+ *
+ * @throws TilesJspException if a jsp exception occurs.
+ * @throws IOException if an io exception occurs.
+ */
+ protected abstract void render(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body);
+
+ /**
+ * Starts the context when entering the tag.
+ *
+ * @param env The page context to use.
+ */
+ protected void startContext(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ if (container != null) {
+ attributeContext = container.startContext(env);
+ }
+ }
+
+ /**
+ * Ends the context when exiting the tag.
+ *
+ * @param context The page context to use.
+ */
+ protected void endContext(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ if (attributeContext != null && container != null) {
+ container.endContext(env);
+ }
+ }
+
+ /**
+ * <p>
+ * Process nested ≶put> tag.
+ * <p/>
+ * <p>
+ * Places the value of the nested tag within the
+ * {...@link org.apache.tiles.AttributeContext}.It is the responsibility
+ * of the descendent to check security. Security will be managed by
+ * called tags.
+ * </p>
+ *
+ * @param nestedTag the put tag desciendent.
+ */
+ public void processNestedModel(PutAttributeModel nestedTag) {
+ Attribute attribute = new Attribute(
+ nestedTag.getValue(), nestedTag.getRole(),
+ nestedTag.getType());
+
+ attributeContext.putAttribute(nestedTag.getName(), attribute, nestedTag
+ .isCascade());
+ }
+
+ /** {...@inheritdoc} */
+ public void processNestedModel(PutListAttributeModel nestedTag) {
+ ListAttribute attribute = new ListAttribute(nestedTag.getAttributes());
+ attribute.setRole(nestedTag.getRole());
+ attribute.setInherit(nestedTag.getInherit());
+
+ attributeContext.putAttribute(nestedTag.getName(), attribute, nestedTag
+ .isCascade());
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AbstractRenderModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,186 @@
+/*
+ * $Id$
+ *
+ * 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.template;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+
+/**
+ * <p><strong>Adds an attribute in enclosing attribute container
tag.</strong></p>
+ * <p>Enclosing attribute container tag can be :
+ * <ul>
+ * <li><putListAttribute></li>
+ * <li><putAttribute></li>
+ * </ul>
+ * (or any other tag which implements the <code>{...@link
AddAttributeModelParent}</code> interface.
+ * Exception is thrown if no appropriate tag can be found.</p>
+ * <p>Put tag can have following atributes :
+ * <ul>
+ * <li>name : Name of the attribute</li>
+ * <li>value : value to put as attribute</li>
+ * <li>type : value type. Only valid if value is a String and is set by
+ * value="something" or by a bean.
+ * Possible type are : string (value is used as direct string),
+ * template (value is used as a page url to insert),
+ * definition (value is used as a definition name to insert)</li>
+ * <li>role : Role to check when 'insert' will be called. If enclosing tag is
+ * <insert>, role is checked immediately. If enclosing tag is
+ * <definition>, role will be checked when this definition will be
+ * inserted.</li>
+ * </ul></p>
+ * <p>Value can also come from tag body. Tag body is taken into account only if
+ * value is not set by one of the tag attributes. In this case Attribute type
is
+ * "string", unless tag body define another type.</p>
+ *
+ * @version $Rev$ $Date$
+ */
+public class AddAttributeModel extends NestableTemplateDirectiveModel
implements DefinitionModelParent {
+
+ protected Map<String, TemplateModel> currentParams;
+
+ protected Object value;
+
+ private String type;
+
+ /**
+ * Returns the role to check. If the user is in the specified role, the
tag is
+ * taken into account; otherwise, the tag is ignored (skipped).
+ *
+ * @return The role to check.
+ */
+ public String getRole() {
+ return FreeMarkerUtil.getAsString(currentParams.get("role"));
+ }
+
+ /**
+ * Returns the attribute value.
+ *
+ * @return Attribute value. Can be a String or Object.
+ */
+ public Object getValue() {
+ return value;
+ }
+
+ /**
+ * <p>
+ * Returns content type: string, template or definition.
+ * </p>
+ * <ul>
+ * <li>String : Content is printed directly.</li>
+ * <li>template : Content is included from specified URL. Value is used as
+ * an URL.</li>
+ * <li>definition : Value denote a definition defined in factory (xml
+ * file). Definition will be searched in the inserted tile, in a
+ * <code><insert attribute="attributeName"></code> tag, where
+ * 'attributeName' is the name used for this tag.</li>
+ * </ul>
+ *
+ * @return The attribute type.
+ */
+ public String getType() {
+ return type;
+ }
+
+ @Override
+ protected void evaluateBody(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ if (value == null && body != null) {
+ StringWriter writer = new StringWriter();
+ try {
+ body.render(writer);
+ writer.close();
+ } catch (TemplateException e) {
+ throw new FreeMarkerTilesException(
+ "Exception during rendition of the body", e);
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException(
+ "I/O Exception during rendition of the body", e);
+ }
+ value = writer.toString();
+ type = "string";
+ }
+ }
+
+ @Override
+ protected void doEnd(Environment env, Map<String, TemplateModel> params,
+ TemplateModel[] loopVars, TemplateDirectiveBody body) {
+ if (isAccessAllowed(env)) {
+ execute(env);
+ }
+ }
+
+ @Override
+ protected void doStart(Environment env, Map<String, TemplateModel> params,
+ TemplateModel[] loopVars, TemplateDirectiveBody body) {
+ currentParams = params;
+ value = FreeMarkerUtil.getAsObject(params.get("value"));
+ type = FreeMarkerUtil.getAsString(currentParams.get("type"));
+ }
+
+ /** {...@inheritdoc} */
+ public void processNestedDefinitionName(String definitionName) {
+ value = definitionName;
+ if (type == null) {
+ type = "definition";
+ }
+ }
+
+ /**
+ * Executes the processing of this tag, calling its parent tag.
+ *
+ * @throws TilesJspException If something goes wrong during execution.
+ */
+ protected void execute(Environment env) {
+ AddAttributeModelParent parent = (AddAttributeModelParent)
findAncestorWithClass(
+ env, AddAttributeModelParent.class);
+ if (parent == null) {
+ throw new FreeMarkerTilesException("Error: cannot find an
AddAttributeModelParent ancestor'");
+ }
+
+ parent.processNestedTag(this);
+ }
+
+ /**
+ * Checks if the user is inside the specified role.
+ *
+ * @return <code>true</code> if the user is allowed to have the tag
+ * rendered.
+ */
+ protected boolean isAccessAllowed(Environment env) {
+ HttpServletRequest req = FreeMarkerUtil.getRequestHashModel(env)
+ .getRequest();
+ String role = getRole();
+ return (role == null || req.isUserInRole(role));
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,39 @@
+/*
+ * $Id$
+ *
+ * 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.template;
+
+/**
+ * Tag classes implementing this interface can contain nested {...@link
AddAttributeTag}.
+ * This interface defines a method called by nested tags.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface AddAttributeModelParent {
+ /**
+ * Process the nested tag.
+ *
+ * @param nestedTag Nested tag to process.
+ * @throws TilesJspException If something goes wrong during processing.
+ */
+ void processNestedTag(AddAttributeModel nestedTag);
+
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,41 @@
+/*
+ * $Id$
+ *
+ * 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.template;
+
+/**
+ * Tag classes implementing this interface can contain nested
+ * {...@link DefinitionTag}. This interface defines a method called by nested
+ * tags.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public interface DefinitionModelParent {
+
+ /**
+ * Process the nested <tiles:definition> tag.
+ *
+ * @param definitionName Nested definition name.
+ * @throws TilesJspException If something goes wrong during the processing.
+ * @since 2.1.0
+ */
+ void processNestedDefinitionName(String definitionName);
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/DefinitionModelParent.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,95 @@
+package org.apache.tiles.freemarker.template;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.tiles.Attribute;
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateModel;
+
+public class InsertAttributeModel extends AbstractRenderModel {
+
+ /**
+ * The evaluated attribute.
+ *
+ * @since 2.1.0
+ */
+ protected Attribute attribute;
+
+ @Override
+ public void doStart(Environment env, Map<String, TemplateModel> params,
+ TemplateModel[] loopVars, TemplateDirectiveBody body) {
+ if (FreeMarkerUtil.getAsObject(params.get("value")) == null
+ && FreeMarkerUtil.getAsString(params.get("name")) == null) {
+ throw new FreeMarkerTilesException(
+ "No attribute name or value has been provided.");
+ }
+ super.doStart(env, params, loopVars, body);
+ }
+
+ /** {...@inheritdoc} */
+ protected void render(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ HttpServletRequest req = FreeMarkerUtil.getRequestHashModel(env)
+ .getRequest();
+
+ String role = FreeMarkerUtil.getAsString(params.get("role"));
+ boolean ignore = FreeMarkerUtil.getAsBoolean(params.get("ignore"),
false);
+ // Checks if the attribute can be rendered with the current user.
+ if ((role != null && !req.isUserInRole(role))
+ || (attribute == null && ignore)) {
+ return;
+ }
+ render(attribute, env);
+ }
+
+ /** {...@inheritdoc} */
+ @Override
+ protected void startContext(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ String preparer = FreeMarkerUtil.getAsString(params.get("preparer"));
+ if (preparer != null) {
+ container.prepare(preparer, env);
+ }
+
+ attribute = (Attribute)
FreeMarkerUtil.getAsObject(params.get("value"));
+ boolean ignore = FreeMarkerUtil.getAsBoolean(params.get("ignore"),
false);
+ if (attribute == null) {
+ AttributeContext evaluatingContext = container
+ .getAttributeContext(env);
+ String name = FreeMarkerUtil.getAsString(params.get("name"));
+ attribute = evaluatingContext.getAttribute(name);
+ if (attribute == null && !ignore) {
+ throw new FreeMarkerTilesException("Attribute '" + name
+ + "' not found.");
+ }
+ }
+
+ super.startContext(env, params, loopVars, body);
+ }
+
+ /**
+ * Renders an attribute for real.
+ *
+ * @param attr The attribute to render.
+ * @throws IOException If something goes wrong during the reading of
+ * definition files.
+ */
+ protected void render(Attribute attr, Environment env) {
+ try {
+ container.render(attr, env.getOut(), env);
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException(
+ "I/O Exception during rendition of the attribute", e);
+ }
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,85 @@
+package org.apache.tiles.freemarker.template;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Stack;
+
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateDirectiveModel;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+
+public abstract class NestableTemplateDirectiveModel implements
+ TemplateDirectiveModel {
+
+ private static final String MODEL_STACK_ATTRIBUTE_NAME =
"org.apache.tiles.freemarker.template.NestableTemplateDirectiveModel.MODEL_STACK";
+
+ @SuppressWarnings("unchecked")
+ public void execute(Environment env, Map params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) throws TemplateException, IOException {
+ Stack<TemplateDirectiveModel> modelStack;
+ modelStack = getModelStack(env);
+ doStart(env, params, loopVars, body);
+ modelStack.push(this);
+ evaluateBody(env, params, loopVars, body);
+ modelStack.pop();
+ doEnd(env, params, loopVars, body);
+ }
+
+ protected abstract void doStart(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body);
+
+ protected abstract void doEnd(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body);
+
+ protected void evaluateBody(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ try {
+ body.render(env.getOut());
+ } catch (TemplateException e) {
+ throw new FreeMarkerTilesException(
+ "Error during evaluation of the body of the template
directive",
+ e);
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException(
+ "Error during writing the body the template directive", e);
+ }
+ }
+
+ protected TemplateDirectiveModel findAncestorWithClass(Environment env,
+ Class<?> clazz) {
+ TemplateDirectiveModel retValue = null;
+ Stack<TemplateDirectiveModel> modelStack = getModelStack(env);
+ for (int i=modelStack.size() - 1; i >= 0 && retValue == null; i++) {
+ TemplateDirectiveModel ancestor = modelStack.get(i);
+ if (clazz.isAssignableFrom(ancestor.getClass())) {
+ retValue = ancestor;
+ }
+ }
+ return retValue;
+ }
+
+ @SuppressWarnings("unchecked")
+ private Stack<TemplateDirectiveModel> getModelStack(Environment env) {
+ Stack<TemplateDirectiveModel> modelStack;
+ Environment.Namespace namespace = env.getCurrentNamespace();
+ try {
+ modelStack = (Stack<TemplateDirectiveModel>) namespace
+ .get(MODEL_STACK_ATTRIBUTE_NAME);
+ } catch (TemplateModelException e) {
+ throw new FreeMarkerTilesException("Cannot find the model stack",
e);
+ }
+ if (modelStack == null) {
+ modelStack = new Stack<TemplateDirectiveModel>();
+ namespace.put(MODEL_STACK_ATTRIBUTE_NAME, modelStack);
+ }
+ return modelStack;
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/NestableTemplateDirectiveModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,41 @@
+package org.apache.tiles.freemarker.template;
+
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+
+public class PutAttributeModel extends AddAttributeModel {
+
+ /**
+ * Returns the name of the attribute.
+ *
+ * @return The name of the attribute.
+ */
+ public String getName() {
+ return FreeMarkerUtil.getAsString(currentParams.get("name"));
+ }
+
+ /**
+ * Checks if the attribute should be cascaded to nested definitions.
+ *
+ * @return <code>true</code> if the attribute will be cascaded.
+ * @since 2.1.0
+ */
+ public boolean isCascade() {
+ return FreeMarkerUtil.getAsBoolean(currentParams.get("cascade"),
false);
+ }
+
+ /** {...@inheritdoc} */
+ @Override
+ protected void execute(Environment env) {
+ PutAttributeModelParent parent = (PutAttributeModelParent)
+ findAncestorWithClass(env, PutAttributeModelParent.class);
+
+ if (parent == null) {
+ throw new FreeMarkerTilesException("Error: cannot find an
PutAttributeModelParent ancestor'");
+ }
+
+ parent.processNestedModel(this);
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,5 @@
+package org.apache.tiles.freemarker.template;
+
+public interface PutAttributeModelParent {
+ void processNestedModel(PutAttributeModel nestedTag);
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutAttributeModelParent.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,125 @@
+package org.apache.tiles.freemarker.template;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tiles.Attribute;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+
+public class PutListAttributeModel extends PutAttributeModel {
+
+ /**
+ * If true, the attribute will put the elements of the attribute with the
+ * same name of the parent definition before the ones specified here. By
+ * default, it is 'false'
+ *
+ * @return The "inherit" value.
+ * @since 2.1.0
+ */
+ public boolean getInherit() {
+ return FreeMarkerUtil.getAsBoolean(currentParams.get("inherit"),
false);
+ }
+
+ /**
+ * Get list defined in tag.
+ *
+ * @return The value of this list attribute.
+ */
+ @SuppressWarnings("unchecked")
+ public List<Attribute> getAttributes() {
+ return (List<Attribute>) super.getValue();
+ }
+
+ @Override
+ protected void doStart(Environment env, Map<String, TemplateModel> params,
+ TemplateModel[] loopVars, TemplateDirectiveBody body) {
+ super.doStart(env, params, loopVars, body);
+ value = new ArrayList<Attribute>();
+ }
+
+ @Override
+ protected void evaluateBody(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ if (body != null) {
+ Writer writer = new NullWriter();
+ try {
+ body.render(writer);
+ } catch (TemplateException e) {
+ throw new FreeMarkerTilesException(
+ "Exception during rendition of the body", e);
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException(
+ "I/O Exception during rendition of the body", e);
+ }
+ }
+ }
+
+ /**
+ * Process nested ≶putAttribute> tag.
+ * <p/>
+ * Places the value of the nested tag within the
+ * {...@link org.apache.tiles.AttributeContext}.It is the responsibility
+ * of the descendent to check security. Security will be managed by called
+ * tags.
+ *
+ * @param nestedTag the put tag desciendent.
+ */
+ public void processNestedModel(AddAttributeModel nestedTag) {
+ Attribute attribute = new Attribute(nestedTag.getValue(), nestedTag
+ .getRole(), nestedTag.getType());
+
+ this.addValue(attribute);
+ }
+
+ /**
+ * Adds an attribute value to the list.
+ *
+ * @param attribute The attribute to add.
+ */
+ private void addValue(Attribute attribute) {
+ this.getAttributes().add(attribute);
+ }
+
+ /** {...@inheritdoc} */
+ @Override
+ protected void execute(Environment env) {
+ PutListAttributeModelParent parent = (PutListAttributeModelParent)
findAncestorWithClass(
+ env, PutListAttributeModelParent.class);
+
+ if (parent == null) {
+ // Try with the old method.
+ super.execute(env);
+ }
+
+ parent.processNestedModel(this);
+ }
+
+ private static class NullWriter extends Writer {
+
+ @Override
+ public void close() throws IOException {
+ // Does nothing
+ }
+
+ @Override
+ public void flush() throws IOException {
+ // Does nothing
+ }
+
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException {
+ // Does nothing
+ }
+
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java?rev=728958&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
(added)
+++
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
Tue Dec 23 06:04:45 2008
@@ -0,0 +1,5 @@
+package org.apache.tiles.freemarker.template;
+
+public interface PutListAttributeModelParent {
+ void processNestedModel(PutListAttributeModel nestedTag);
+}
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModelParent.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL