You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2009/01/30 14:09:20 UTC
svn commit: r739266 - in
/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker:
context/ io/ template/
Author: apetrelli
Date: Fri Jan 30 13:09:19 2009
New Revision: 739266
URL: http://svn.apache.org/viewvc?rev=739266&view=rev
Log:
TILESSB-4
Fixed use of Tiles code.
Added a bunch of template models.
Added:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java (with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java (with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java (with props)
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java (with props)
Modified:
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContext.java
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java
tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java
Modified: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContext.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContext.java?rev=739266&r1=739265&r2=739266&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContext.java (original)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/context/FreeMarkerTilesRequestContext.java Fri Jan 30 13:09:19 2009
@@ -1,6 +1,8 @@
package org.apache.tiles.freemarker.context;
import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
import java.util.Locale;
import org.apache.tiles.context.TilesRequestContext;
@@ -12,6 +14,8 @@
private Environment env;
+ private transient Object[] requestObjects;
+
public FreeMarkerTilesRequestContext(
TilesRequestContext enclosedRequest, Environment env) {
super(enclosedRequest);
@@ -33,4 +37,28 @@
public Object getResponse() {
return env;
}
+
+ @Override
+ public PrintWriter getPrintWriter() throws IOException {
+ Writer writer = env.getOut();
+ if (writer instanceof PrintWriter) {
+ return (PrintWriter) writer;
+ } else {
+ return new PrintWriter(writer);
+ }
+ }
+
+ @Override
+ public Writer getWriter() throws IOException {
+ return env.getOut();
+ }
+
+ @Override
+ public Object[] getRequestObjects() {
+ if (requestObjects == null) {
+ requestObjects = new Object[1];
+ requestObjects[0] = env;
+ }
+ return requestObjects;
+ }
}
Added: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java?rev=739266&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java (added)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java Fri Jan 30 13:09:19 2009
@@ -0,0 +1,26 @@
+/**
+ *
+ */
+package org.apache.tiles.freemarker.io;
+
+import java.io.IOException;
+import java.io.Writer;
+
+public 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
+ }
+
+}
\ No newline at end of file
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/io/NullWriter.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified: 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=739266&r1=739265&r2=739266&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java (original)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModel.java Fri Jan 30 13:09:19 2009
@@ -168,7 +168,7 @@
throw new FreeMarkerTilesException("Error: cannot find an AddAttributeModelParent ancestor'");
}
- parent.processNestedTag(this);
+ parent.processNestedModel(this);
}
/**
Modified: 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=739266&r1=739265&r2=739266&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java (original)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddAttributeModelParent.java Fri Jan 30 13:09:19 2009
@@ -34,6 +34,6 @@
* @param nestedTag Nested tag to process.
* @throws TilesJspException If something goes wrong during processing.
*/
- void processNestedTag(AddAttributeModel nestedTag);
+ void processNestedModel(AddAttributeModel nestedTag);
}
Added: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java?rev=739266&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java (added)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java Fri Jan 30 13:09:19 2009
@@ -0,0 +1,93 @@
+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 org.apache.tiles.freemarker.io.NullWriter;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+
+public class AddListAttributeModel extends PutAttributeModel implements AddAttributeModelParent {
+
+ /**
+ * 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(), null,
+ 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);
+ }
+}
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AddListAttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java?rev=739266&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java (added)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java Fri Jan 30 13:09:19 2009
@@ -0,0 +1,131 @@
+/*
+ * $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.TilesContainer;
+import org.apache.tiles.TilesException;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+
+import freemarker.core.Environment;
+import freemarker.template.TemplateDirectiveBody;
+import freemarker.template.TemplateDirectiveModel;
+import freemarker.template.TemplateModel;
+
+/**
+ * Support for Scoped tags.
+ *
+ * @version $Rev$ $Date$
+ */
+public abstract class AttributeModel implements TemplateDirectiveModel {
+
+ /**
+ * The logging object.
+ */
+ private final Log log = LogFactory.getLog(AttributeModel.class);
+
+
+ /**
+ * The Tiles container to use.
+ */
+ protected TilesContainer container;
+
+ /**
+ * The current attribute context.
+ */
+ protected AttributeContext attributeContext;
+
+ /**
+ * The found attribute.
+ */
+ protected Attribute attribute;
+
+ /**
+ * The attribute value.
+ *
+ * @since 2.1.0
+ */
+ protected Object attributeValue;
+
+ /** {@inheritDoc} */
+ @SuppressWarnings("unchecked")
+ public void execute(Environment env,
+ Map params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) {
+ container = FreeMarkerUtil.getCurrentContainer(env);
+ attributeContext = container.getAttributeContext(env);
+
+ String name = FreeMarkerUtil.getAsString((TemplateModel) params.get("name"));
+ boolean ignore = FreeMarkerUtil.getAsBoolean((TemplateModel) params.get("ignore"), false);
+ // Some tags allow for unspecified attribues. This
+ // implies that the tag should use all of the attributes.
+ if (name != null) {
+ attribute = attributeContext.getAttribute(name);
+ if ((attribute == null || attribute.getValue() == null) && ignore) {
+ return;
+ }
+
+ if (attribute == null) {
+ throw new FreeMarkerTilesException("Attribute with name '" + name
+ + "' not found");
+ }
+
+ try {
+ attributeValue = container.evaluate(attribute, env);
+ } catch (TilesException e) {
+ if (!ignore) {
+ throw e;
+ } else if (log.isDebugEnabled()) {
+ log.debug("Ignoring Tiles Exception", e);
+ }
+ }
+
+ if (attributeValue == null) {
+ throw new FreeMarkerTilesException("Attribute with name '" + name
+ + "' has a null value.");
+ }
+ }
+
+ try {
+ evaluate(env, params, loopVars, body);
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException("io error while executing tag '"
+ + getClass().getName() + "'.", e);
+ }
+ }
+
+ /**
+ * Execute this tag. It is called inside {@link #doEndTag()}.
+ *
+ * @throws TilesJspException If something goes wrong during rendering.
+ * @throws IOException If something goes wrong during writing content.
+ */
+ public abstract void evaluate(Environment env,
+ Map<String, TemplateModel> params, TemplateModel[] loopVars,
+ TemplateDirectiveBody body) throws IOException;
+}
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/AttributeModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java?rev=739266&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java (added)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java Fri Jan 30 13:09:19 2009
@@ -0,0 +1,49 @@
+/*
+ * $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 org.apache.tiles.Attribute;
+import org.apache.tiles.freemarker.FreeMarkerTilesException;
+
+import freemarker.core.Environment;
+
+import java.io.IOException;
+
+/**
+ * Retrieve the value of the specified definition/template attribute property,
+ * and render it to the current JspWriter as a String.
+ * The usual toString() conversion is applied on the found value.
+ *
+ * @version $Rev$ $Date$
+ */
+public class GetAsStringModel extends InsertAttributeModel {
+
+ /** {@inheritDoc} */
+ @Override
+ protected void render(Attribute attr, Environment env) {
+ try {
+ env.getOut().write(attr.getValue().toString());
+ } catch (IOException e) {
+ throw new FreeMarkerTilesException("Cannot write the attribute: "
+ + attr.getValue(), e);
+ }
+ }
+}
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/GetAsStringModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: 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=739266&r1=739265&r2=739266&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java (original)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/InsertAttributeModel.java Fri Jan 30 13:09:19 2009
@@ -86,7 +86,7 @@
*/
protected void render(Attribute attr, Environment env) {
try {
- container.render(attr, env.getOut(), env);
+ container.render(attr, env);
} catch (IOException e) {
throw new FreeMarkerTilesException(
"I/O Exception during rendition of the attribute", e);
Modified: 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=739266&r1=739265&r2=739266&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java (original)
+++ tiles/sandbox/trunk/tiles-freemarker/src/main/java/org/apache/tiles/freemarker/template/PutListAttributeModel.java Fri Jan 30 13:09:19 2009
@@ -9,13 +9,14 @@
import org.apache.tiles.Attribute;
import org.apache.tiles.freemarker.FreeMarkerTilesException;
import org.apache.tiles.freemarker.context.FreeMarkerUtil;
+import org.apache.tiles.freemarker.io.NullWriter;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
-public class PutListAttributeModel extends PutAttributeModel {
+public class PutListAttributeModel extends PutAttributeModel implements PutAttributeModelParent {
/**
* If true, the attribute will put the elements of the attribute with the
@@ -74,7 +75,7 @@
*
* @param nestedTag the put tag desciendent.
*/
- public void processNestedModel(AddAttributeModel nestedTag) {
+ public void processNestedModel(PutAttributeModel nestedTag) {
Attribute attribute = new Attribute(nestedTag.getValue(), null,
nestedTag.getRole(), nestedTag.getType());
@@ -103,23 +104,4 @@
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
- }
-
- }
}