You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2009/11/22 16:58:48 UTC
svn commit: r883092 - in /cocoon/cocoon3/trunk/cocoon-stringtemplate: ./
src/main/java/org/apache/cocoon/stringtemplate/
src/main/resources/META-INF/cocoon/spring/
Author: reinhard
Date: Sun Nov 22 15:58:47 2009
New Revision: 883092
URL: http://svn.apache.org/viewvc?rev=883092&view=rev
Log:
<action dev="reinhard" type="add">
[cocoon-stringtemplate] The StringTemplateGenerator doesn't use controller context parameters as attributes anymore.
This allows its usage outside of servlet environments. For the use with a Cocoon controller implementation
org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator was added. This generator is also
used as the bean 'generator:string-template' to be referred in sitemaps
(e.g. <map:generator src="string-template" src="..." />);
</action>
Added:
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java (with props)
Modified:
cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml
Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml?rev=883092&r1=883091&r2=883092&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml Sun Nov 22 15:58:47 2009
@@ -38,10 +38,22 @@
<description>String-Template support for Cocoon 3.</description>
<dependencies>
+ <!-- Cocoon dependencies -->
+ <dependency>
+ <groupId>org.apache.cocoon.sax</groupId>
+ <artifactId>cocoon-sax</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon.pipeline</groupId>
+ <artifactId>cocoon-pipeline</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.cocoon.servlet</groupId>
<artifactId>cocoon-servlet</artifactId>
+ <optional>true</optional>
</dependency>
+
+ <!-- Other dependencies -->
<dependency>
<groupId>org.antlr</groupId>
<artifactId>stringtemplate</artifactId>
@@ -54,18 +66,12 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
-
+
+ <!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
-
-
- <scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/cocoon/cocoon3/tags/cocoon-root/3.0.0-alpha-1</connection>
- <developerConnection>scm:svn:https://svn.apache.org/repos/asf/cocoon/cocoon3/tags/cocoon-root/3.0.0-alpha-1</developerConnection>
- <url>http://svn.apache.org/repos/asf/cocoon/cocoon3/tags/cocoon-root/3.0.0-alpha-1</url>
- </scm>
</project>
Added: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java?rev=883092&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java (added)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java Sun Nov 22 15:58:47 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.cocoon.stringtemplate;
+
+import java.net.URL;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.antlr.stringtemplate.StringTemplate;
+import org.apache.cocoon.servlet.controller.ControllerContextHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class ControllerContextAwareStringTemplateGenerator extends StringTemplateGenerator {
+
+ private final Log logger = LogFactory.getLog(this.getClass());
+
+ public ControllerContextAwareStringTemplateGenerator() {
+ super();
+ }
+
+ @Override
+ protected void addTemplateAttributes(StringTemplate stringTemplate) {
+ super.addTemplateAttributes(stringTemplate);
+
+ // put all objects that are passed by the controller context
+ Map<String, Object> controllerContext = ControllerContextHelper.getContext(this.parameters);
+ for (Entry<String, Object> eachEntry : controllerContext.entrySet()) {
+ String key = eachEntry.getKey().replace(".", "_");
+
+ // remove already set parameters (otherwise there's some strange behavior in some cases
+ // at least with StringTempalte 3.0
+ Object attribute = stringTemplate.getAttribute(key);
+ if (attribute != null) {
+ stringTemplate.removeAttribute(key);
+ }
+
+ stringTemplate.setAttribute(key, eachEntry.getValue());
+
+ if (this.logger.isDebugEnabled()) {
+ this.logger.debug("Passing controller context parameter as attribute: key=" + eachEntry.getKey()
+ + ", value=" + eachEntry.getValue());
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.pipeline.component.AbstractSAXProducer#setConfiguration(java.util.Map)
+ */
+ @Override
+ public void setConfiguration(Map<String, ? extends Object> parameters) {
+ super.setSource((URL) parameters.get("source"));
+ }
+}
Propchange: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java?rev=883092&r1=883091&r2=883092&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java Sun Nov 22 15:58:47 2009
@@ -37,7 +37,6 @@
import org.apache.cocoon.pipeline.component.Starter;
import org.apache.cocoon.sax.AbstractSAXProducer;
import org.apache.cocoon.sax.util.XMLUtils;
-import org.apache.cocoon.servlet.controller.ControllerContextHelper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
@@ -49,7 +48,7 @@
private final Log logger = LogFactory.getLog(this.getClass());
private URL source;
- private Map<String, Object> parameters = new HashMap<String, Object>();
+ protected Map<String, Object> parameters = new HashMap<String, Object>();
public StringTemplateGenerator() {
super();
@@ -57,12 +56,12 @@
public StringTemplateGenerator(URL source) {
super();
- this.source = source;
+ this.setSource(source);
}
/**
* {@inheritDoc}
- *
+ *
* @see org.apache.cocoon.pipeline.component.CachingPipelineComponent#constructCacheKey()
*/
public CacheKey constructCacheKey() {
@@ -80,7 +79,7 @@
/**
* {@inheritDoc}
- *
+ *
* @see org.apache.cocoon.pipeline.component.Starter#execute()
*/
public void execute() {
@@ -102,15 +101,7 @@
}
});
- Map<String, Object> controllerContext = ControllerContextHelper.getContext(this.parameters);
- for (Entry<String, Object> eachEntry : controllerContext.entrySet()) {
- stringTemplate.setAttribute(eachEntry.getKey(), eachEntry.getValue());
-
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Passing parameter: key=" + eachEntry.getKey() + ", value="
- + eachEntry.getValue());
- }
- }
+ this.addTemplateAttributes(stringTemplate);
XMLUtils.createXMLReader(this.getSAXConsumer()).parse(
new InputSource(new StringReader(stringTemplate.toString())));
@@ -125,15 +116,23 @@
}
/**
- * {@inheritDoc}
- *
- * @see org.apache.cocoon.pipeline.component.AbstractSAXProducer#setConfiguration(java.util.Map)
+ * Add attributes to the StringTemplate. Be careful to keep the constructCacheKey() method
+ * align.
+ *
+ * @param stringTemplate The template where the attributes are added to.
*/
- @Override
- public void setConfiguration(Map<String, ? extends Object> parameters) {
- this.setSource((URL) parameters.get("source"));
+ protected void addTemplateAttributes(StringTemplate stringTemplate) {
+ for (Entry<String, Object> eachEntry : this.parameters.entrySet()) {
+ stringTemplate.setAttribute(eachEntry.getKey().replace(".", "_"), eachEntry.getValue());
+
+ if (this.logger.isDebugEnabled()) {
+ this.logger.debug("Passing pipeline parameter as attribute: key=" + eachEntry.getKey() + ", value="
+ + eachEntry.getValue());
+ }
+ }
}
+
@Override
public void setup(Map<String, Object> parameters) {
this.parameters = parameters;
Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml?rev=883092&r1=883091&r2=883092&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml Sun Nov 22 15:58:47 2009
@@ -22,7 +22,7 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean name="generator:string-template" class="org.apache.cocoon.stringtemplate.StringTemplateGenerator" scope="prototype" />
+ <bean name="generator:string-template" class="org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator" scope="prototype" />
<bean name="reader:string-template" class="org.apache.cocoon.stringtemplate.StringTemplateReader" scope="prototype" />