You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by il...@apache.org on 2011/08/03 16:42:37 UTC
svn commit: r1153515 - in /cocoon/cocoon3/trunk: cocoon-docs/src/changes/
cocoon-sample/src/main/resources/COB-INF/
cocoon-sample/src/main/resources/COB-INF/string-template/
cocoon-sax/src/main/java/org/apache/cocoon/sax/component/
cocoon-stringtemplat...
Author: ilgrosso
Date: Wed Aug 3 14:42:31 2011
New Revision: 1153515
URL: http://svn.apache.org/viewvc?rev=1153515&view=rev
Log:
COCOON3-45 #resolve The StringTemplate has beed upgraded to latest v4, with some modifications in the source code of related components. The org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator is now referred as generator:controller-aware-string-template, because generator:string-template is now used for org.apache.cocoon.stringtemplate.StringTemplateGenerator.
Added:
cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/
cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/template.xml
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml
Modified:
cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/overview.html
cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateReader.java
cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/resources/META-INF/cocoon/spring/cocoon-stringtemplate-sitemap-components.xml
cocoon/cocoon3/trunk/parent/pom.xml
Modified: cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml Wed Aug 3 14:42:31 2011
@@ -27,6 +27,13 @@
<body>
<release version="3.0.0-beta-1" date="2011-00-00" description="unreleased">
+ <action dev="ilgrosso" type="update">
+ [cocoon-stringtemplate] The StringTemplate has beed upgraded to latest v4, with some modifications in the source
+ code of related components. The org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator is now
+ referred as "generator:controller-aware-string-template", because "generator:string-template" is now used for
+ org.apache.cocoon.stringtemplate.StringTemplateGenerator.
+ </action>
+
<action dev="anathaniel" type="update">
[cocoon-stax] Parametrise generic collections to avoid rawtype warnings and minizes scope of
unavoidable @SuppressWarnings("unchecked").
Modified: cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/overview.html
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/overview.html?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/overview.html (original)
+++ cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/overview.html Wed Aug 3 14:42:31 2011
@@ -127,5 +127,9 @@
<ul>
<li><a href="wicket/">Wicket Reader</a>: A reader defers redirects a request to a Wicket application.</li>
</ul>
+ <h2>StringTemplate</h2>
+ <ul>
+ <li><a href="string-template/generator">StringTemplateGenerator</a>: A generator using <a href="http://www.stringtemplate.org/">StringTemplate</a> template engine.</li>
+ </ul>
</body>
</html>
Modified: cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap (original)
+++ cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap Wed Aug 3 14:42:31 2011
@@ -441,7 +441,7 @@
</map:pipeline>
<map:pipeline type="noncaching">
<map:match pattern="controller/screen">
- <map:generate src="controller/demo.html" type="string-template" />
+ <map:generate src="controller/demo.html" type="controller-aware-string-template" />
<map:serialize type="xml" status-code="202" />
</map:match>
</map:pipeline>
@@ -519,6 +519,16 @@
</map:match>
</map:pipeline>
+ <!-- StringTemplate -->
+ <map:pipeline type="noncaching">
+ <map:match wildcard="string-template/generator">
+ <map:generate type="string-template" src="string-template/template.xml">
+ <map:parameter name="parameter" value="A value"/>
+ </map:generate>
+ <map:serialize/>
+ </map:match>
+ </map:pipeline>
+
<!-- Global error handling -->
<map:handle-errors>
<map:select value="{jexl:cocoon.exception.class.name}">
Added: cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/template.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/template.xml?rev=1153515&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/template.xml (added)
+++ cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/string-template/template.xml Wed Aug 3 14:42:31 2011
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+<html>
+ <head>
+ <title>StringTemplate demo</title>
+ </head>
+ <body>
+ <h3>StringTemplate demo</h3>
+ <p>parameter=$parameter$</p>
+ </body>
+</html>
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java Wed Aug 3 14:42:31 2011
@@ -96,13 +96,13 @@ public final class LogAsXMLTransformer e
return;
}
- String logFileName = String.valueOf(parameters.get(LOG_FILE));
+ final String logFileName = String.valueOf(parameters.get(LOG_FILE));
if (logFileName != null) {
- File logFile = new File(logFileName);
- OutputStream os;
+ final File logFile = new File(logFileName);
+ OutputStream outputSTream;
try {
- os = new FileOutputStream(logFile);
- this.init(os);
+ outputSTream = new FileOutputStream(logFile);
+ this.init(outputSTream);
} catch (FileNotFoundException e) {
throw new SetupException("Impossible to open XML log file '"
+ logFile
Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/pom.xml Wed Aug 3 14:42:31 2011
@@ -20,58 +20,63 @@
<!-- $Id$ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <packaging>jar</packaging>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>jar</packaging>
- <parent>
- <groupId>org.apache.cocoon.parent</groupId>
- <artifactId>cocoon-parent</artifactId>
- <version>3.0.0-beta-1-SNAPSHOT</version>
- <relativePath>../parent/pom.xml</relativePath>
- </parent>
+ <parent>
+ <groupId>org.apache.cocoon.parent</groupId>
+ <artifactId>cocoon-parent</artifactId>
+ <version>3.0.0-beta-1-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
- <groupId>org.apache.cocoon.stringtemplate</groupId>
- <artifactId>cocoon-stringtemplate</artifactId>
- <version>3.0.0-beta-1-SNAPSHOT</version>
+ <groupId>org.apache.cocoon.stringtemplate</groupId>
+ <artifactId>cocoon-stringtemplate</artifactId>
+ <version>3.0.0-beta-1-SNAPSHOT</version>
- <name>Apache Cocoon 3: String Templates</name>
- <description>String-Template support for Cocoon 3.</description>
+ <name>Apache Cocoon 3: String Templates</name>
+ <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>
+ <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>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- </dependency>
+ <!-- Other dependencies -->
+ <dependency>
+ <groupId>org.antlr</groupId>
+ <artifactId>stringtemplate</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
- <!-- Test dependencies -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
+ <!-- Test dependencies -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>xmlunit</groupId>
+ <artifactId>xmlunit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
Modified: 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=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/ControllerContextAwareStringTemplateGenerator.java Wed Aug 3 14:42:31 2011
@@ -19,52 +19,52 @@ 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.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.stringtemplate.v4.ST;
public class ControllerContextAwareStringTemplateGenerator extends StringTemplateGenerator {
- private final Log logger = LogFactory.getLog(this.getClass());
+ private static final Log LOG = LogFactory.getLog(
+ ControllerContextAwareStringTemplateGenerator.class);
public ControllerContextAwareStringTemplateGenerator() {
super();
}
+ public ControllerContextAwareStringTemplateGenerator(final URL source) {
+ super(source);
+ }
+
@Override
- protected void addTemplateAttributes(StringTemplate stringTemplate) {
+ protected void addTemplateAttributes(final ST stringTemplate) {
super.addTemplateAttributes(stringTemplate);
// put all objects that are passed by the controller context
- Map<String, Object> controllerContext = ControllerContextHelper.getContext(this.parameters);
+ final Map<String, Object> controllerContext =
+ ControllerContextHelper.getContext(this.parameters);
+ String key;
+ Object attribute;
for (Entry<String, Object> eachEntry : controllerContext.entrySet()) {
- String key = eachEntry.getKey().replace(".", "_");
+ 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);
+ // remove already set parameters (otherwise there's some strange
+ // behavior in some cases, at least with StringTemplate 3.0
+ attribute = stringTemplate.getAttribute(key);
if (attribute != null) {
- stringTemplate.removeAttribute(key);
+ stringTemplate.remove(key);
}
- stringTemplate.setAttribute(key, eachEntry.getValue());
+ stringTemplate.add(key,
+ StringEscapeUtils.escapeXml(eachEntry.getValue().toString()));
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Passing controller context parameter as attribute: key=" + eachEntry.getKey()
+ if (LOG.isDebugEnabled()) {
+ LOG.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"));
- }
}
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=1153515&r1=1153514&r2=1153515&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 Wed Aug 3 14:42:31 2011
@@ -16,6 +16,8 @@
*/
package org.apache.cocoon.stringtemplate;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
@@ -23,10 +25,6 @@ import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
-
-import org.antlr.stringtemplate.AttributeRenderer;
-import org.antlr.stringtemplate.StringTemplate;
-import org.apache.cocoon.pipeline.PipelineException;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.SetupException;
import org.apache.cocoon.pipeline.caching.CacheKey;
@@ -41,22 +39,47 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.stringtemplate.v4.ST;
import org.xml.sax.InputSource;
-public class StringTemplateGenerator extends AbstractSAXProducer implements Starter, CachingPipelineComponent {
+public class StringTemplateGenerator extends AbstractSAXProducer
+ implements Starter, CachingPipelineComponent {
- private final Log logger = LogFactory.getLog(this.getClass());
+ private static final Log LOG =
+ LogFactory.getLog(StringTemplateGenerator.class);
- private URL source;
protected Map<String, Object> parameters = new HashMap<String, Object>();
+ private URL url;
+
+ private File file;
+
+ private InputStream stream;
+
+ private String string;
+
public StringTemplateGenerator() {
super();
}
- public StringTemplateGenerator(URL source) {
+ public StringTemplateGenerator(final URL source) {
super();
- this.setSource(source);
+ this.url = source;
+ }
+
+ public StringTemplateGenerator(final File file) {
+ super();
+ this.file = file;
+ }
+
+ public StringTemplateGenerator(final InputStream stream) {
+ super();
+ this.stream = stream;
+ }
+
+ public StringTemplateGenerator(final String string) {
+ super();
+ this.string = string;
}
/**
@@ -65,11 +88,13 @@ public class StringTemplateGenerator ext
* @see org.apache.cocoon.pipeline.component.CachingPipelineComponent#constructCacheKey()
*/
public CacheKey constructCacheKey() {
- CompoundCacheKey cacheKey = new CompoundCacheKey();
+ final CompoundCacheKey cacheKey = new CompoundCacheKey();
try {
- cacheKey.addCacheKey(new TimestampCacheKey(this.source, this.source.openConnection().getLastModified()));
- cacheKey.addCacheKey(new ParameterCacheKey("contextParameters", this.parameters));
+ cacheKey.addCacheKey(new TimestampCacheKey(this.url, this.url.
+ openConnection().getLastModified()));
+ cacheKey.addCacheKey(new ParameterCacheKey("contextParameters",
+ this.parameters));
} catch (IOException e) {
throw new SetupException(e);
}
@@ -77,55 +102,86 @@ public class StringTemplateGenerator ext
return cacheKey;
}
+ private String getTemplate()
+ throws IOException {
+
+ String template = null;
+ InputStream localStream = null;
+ String templateLocation = null;
+ try {
+ if (this.url != null) {
+ localStream = this.url.openStream();
+ template = IOUtils.toString(localStream, "UTF-8");
+ templateLocation = this.url.toExternalForm();
+ } else if (this.file != null) {
+ localStream = new FileInputStream(this.file);
+ template = IOUtils.toString(localStream, "UTF-8");
+ templateLocation = this.file.toURI().toASCIIString();
+ } else if (this.stream != null) {
+ localStream = this.stream;
+ template = IOUtils.toString(this.stream, "UTF-8");
+ templateLocation = this.stream.toString();
+ } else if (this.string != null) {
+ template = this.string;
+ templateLocation = String.class.getName();
+ }
+ } finally {
+ if (localStream != null) {
+ IOUtils.closeQuietly(localStream);
+ }
+ }
+
+ if (LOG.isDebugEnabled() && templateLocation != null) {
+ LOG.debug("Using template: " + templateLocation);
+ }
+
+ return template;
+ }
+
/**
* {@inheritDoc}
*
* @see org.apache.cocoon.pipeline.component.Starter#execute()
*/
public void execute() {
- if (this.source == null) {
- throw new IllegalArgumentException("StringTemplateGenerator has no source.");
+ if (this.url == null && this.file == null && this.stream == null
+ && this.string == null) {
+
+ throw new IllegalArgumentException(
+ "StringTemplateGenerator has no source.");
}
try {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Using template: " + this.source.toURI().toASCIIString());
- }
+ final ST stringTemplate = new ST(getTemplate(), '$', '$');
+ this.addTemplateAttributes(stringTemplate);
- InputStream inputStream = this.source.openStream();
- try {
- StringTemplate stringTemplate = new StringTemplate(IOUtils.toString(inputStream, "UTF-8"));
- stringTemplate.registerRenderer(String.class, new AttributeRenderer() {
- public String toString(Object object) {
- return StringEscapeUtils.escapeXml(object.toString());
- }
-
- public String toString(Object o, String string) {
- return this.toString(o);
- }
- });
-
- this.addTemplateAttributes(stringTemplate);
-
- XMLUtils.createXMLReader(this.getSAXConsumer()).parse(
- new InputSource(new StringReader(stringTemplate.toString())));
- } finally {
- IOUtils.closeQuietly(inputStream);
- }
- } catch (PipelineException e) {
- throw e;
+ XMLUtils.createXMLReader(this.getSAXConsumer()).parse(
+ new InputSource(new StringReader(
+ stringTemplate.render())));
} catch (Exception e) {
- throw new ProcessingException("Can't parse url connection " + this.source, e);
+ throw new ProcessingException("Can't parse url connection "
+ + this.url, e);
}
}
- public void setSource(URL source) {
- this.source = source;
+ @Override
+ public void setup(final Map<String, Object> parameters) {
+ if (parameters != null && !parameters.isEmpty()) {
+ this.parameters.putAll(parameters);
+ }
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.pipeline.component.AbstractSAXProducer#setConfiguration(java.util.Map)
+ */
@Override
- public void setup(Map<String, Object> parameters) {
- this.parameters = parameters;
+ public void setConfiguration(
+ final Map<String, ? extends Object> parameters) {
+
+ this.url = (URL) parameters.get("source");
+ setup((Map<String, Object>) parameters);
}
/**
@@ -134,17 +190,19 @@ public class StringTemplateGenerator ext
*
* @param stringTemplate The template where the attributes are added to.
*/
- protected void addTemplateAttributes(StringTemplate stringTemplate) {
- if(this.parameters == null) {
- this.logger.warn("There are not any parameters passed to the template.");
+ protected void addTemplateAttributes(final ST stringTemplate) {
+ if (this.parameters == null) {
+ LOG.warn("There are not any parameters passed to the template.");
return;
}
for (Entry<String, Object> eachEntry : this.parameters.entrySet()) {
- stringTemplate.setAttribute(eachEntry.getKey().replace(".", "_"), eachEntry.getValue());
+ stringTemplate.add(eachEntry.getKey().replace(".", "_"),
+ StringEscapeUtils.escapeXml(eachEntry.getValue().toString()));
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Passing pipeline parameter as attribute: key=" + eachEntry.getKey() + ", value="
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Passing pipeline parameter as attribute: key="
+ + eachEntry.getKey() + ", value="
+ eachEntry.getValue());
}
}
Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateReader.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateReader.java?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateReader.java (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateReader.java Wed Aug 3 14:42:31 2011
@@ -24,9 +24,6 @@ import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
-
-import org.antlr.stringtemplate.AttributeRenderer;
-import org.antlr.stringtemplate.StringTemplate;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.SetupException;
import org.apache.cocoon.pipeline.caching.CacheKey;
@@ -40,20 +37,24 @@ import org.apache.cocoon.pipeline.compon
import org.apache.cocoon.servlet.controller.ControllerContextHelper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
+import org.stringtemplate.v4.ST;
-public class StringTemplateReader extends AbstractPipelineComponent implements CachingPipelineComponent, Starter,
- Finisher {
+public class StringTemplateReader extends AbstractPipelineComponent
+ implements CachingPipelineComponent, Starter, Finisher {
private String mimeType;
+
private OutputStream outputStream;
+
private Map<String, Object> parameters = new HashMap<String, Object>();
+
private URL source;
public StringTemplateReader() {
this(null);
}
- public StringTemplateReader(URL source) {
+ public StringTemplateReader(final URL source) {
super();
this.setSource(source);
@@ -66,14 +67,16 @@ public class StringTemplateReader extend
*/
public CacheKey constructCacheKey() {
if (this.source == null) {
- throw new IllegalArgumentException(this.getClass().getSimpleName() + " has no source.");
+ throw new IllegalArgumentException(this.getClass().getSimpleName()
+ + " has no source.");
}
- CompoundCacheKey cacheKey = new CompoundCacheKey();
-
+ final CompoundCacheKey cacheKey = new CompoundCacheKey();
try {
- cacheKey.addCacheKey(new TimestampCacheKey(this.source, this.source.openConnection().getLastModified()));
- cacheKey.addCacheKey(new ParameterCacheKey("contextParameters", this.parameters));
+ cacheKey.addCacheKey(new TimestampCacheKey(this.source, this.source.
+ openConnection().getLastModified()));
+ cacheKey.addCacheKey(new ParameterCacheKey("contextParameters",
+ this.parameters));
} catch (IOException e) {
throw new SetupException("Could not create cache key.", e);
}
@@ -88,32 +91,30 @@ public class StringTemplateReader extend
*/
public void execute() {
if (this.source == null) {
- throw new IllegalArgumentException(this.getClass().getSimpleName() + " has no source.");
+ throw new IllegalArgumentException(this.getClass().getSimpleName()
+ + " has no source.");
}
try {
- InputStream inputStream = this.source.openStream();
+ final InputStream inputStream = this.source.openStream();
try {
- StringTemplate stringTemplate = new StringTemplate(IOUtils.toString(inputStream, "UTF-8"));
- stringTemplate.registerRenderer(String.class, new AttributeRenderer() {
+ final ST stringTemplate = new ST(
+ IOUtils.toString(inputStream, "UTF-8"), '$', '$');
- public String toString(Object object) {
- return StringEscapeUtils.escapeXml(object.toString());
- }
-
- public String toString(Object o, String string) {
- return toString(o);
- }
- });
-
- Map<String, Object> controllerContext = ControllerContextHelper.getContext(this.parameters);
- for (Entry<String, Object> eachEntry : controllerContext.entrySet()) {
- stringTemplate.setAttribute(eachEntry.getKey(), eachEntry.getValue());
+ final Map<String, Object> controllerContext =
+ ControllerContextHelper.getContext(this.parameters);
+ for (Entry<String, Object> eachEntry :
+ controllerContext.entrySet()) {
+
+ stringTemplate.add(eachEntry.getKey(),
+ StringEscapeUtils.escapeXml(
+ eachEntry.getValue().toString()));
}
- OutputStreamWriter writer = new OutputStreamWriter(this.outputStream);
- writer.write(stringTemplate.toString());
+ final OutputStreamWriter writer = new OutputStreamWriter(
+ this.outputStream);
+ writer.write(stringTemplate.render());
writer.flush();
} finally {
IOUtils.closeQuietly(inputStream);
@@ -138,7 +139,9 @@ public class StringTemplateReader extend
* @see org.apache.cocoon.pipeline.component.PipelineComponent#setConfiguration(java.util.Map)
*/
@Override
- public void setConfiguration(Map<String, ? extends Object> configuration) {
+ public void setConfiguration(
+ final Map<String, ? extends Object> configuration) {
+
this.setSource((URL) configuration.get("source"));
this.setMimeType((String) configuration.get("mime-type"));
}
@@ -149,7 +152,7 @@ public class StringTemplateReader extend
* @param mimeType
* The mime-type that belongs to the content that is produced by this component.
*/
- public void setMimeType(String mimeType) {
+ public void setMimeType(final String mimeType) {
this.mimeType = mimeType;
}
@@ -158,7 +161,8 @@ public class StringTemplateReader extend
*
* @see org.apache.cocoon.pipeline.component.Finisher#setOutputStream(java.io.OutputStream)
*/
- public void setOutputStream(OutputStream outputStream) {
+ @Override
+ public void setOutputStream(final OutputStream outputStream) {
this.outputStream = outputStream;
}
@@ -168,7 +172,7 @@ public class StringTemplateReader extend
* @param source
* A {@link URL} that will be used by this component.
*/
- public void setSource(URL source) {
+ public final void setSource(final URL source) {
this.source = source;
}
@@ -178,9 +182,9 @@ public class StringTemplateReader extend
* @see org.apache.cocoon.pipeline.component.AbstractPipelineComponent#setup(java.util.Map)
*/
@Override
- public void setup(Map<String, Object> parameters) {
- super.setup(parameters);
-
- this.parameters = parameters;
+ public void setup(final Map<String, Object> parameters) {
+ if (parameters != null && !parameters.isEmpty()) {
+ this.parameters.putAll(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=1153515&r1=1153514&r2=1153515&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 Wed Aug 3 14:42:31 2011
@@ -16,14 +16,15 @@
limitations under the License.
-->
<!-- $Id$ -->
-<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- 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">
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean name="generator:string-template" class="org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator" scope="prototype" />
+ <bean name="generator:string-template" class="org.apache.cocoon.stringtemplate.StringTemplateGenerator" scope="prototype" />
- <bean name="reader:string-template" class="org.apache.cocoon.stringtemplate.StringTemplateReader" scope="prototype" />
+ <bean name="generator:controller-aware-string-template" class="org.apache.cocoon.stringtemplate.ControllerContextAwareStringTemplateGenerator" scope="prototype" />
+
+ <bean name="reader:string-template" class="org.apache.cocoon.stringtemplate.StringTemplateReader" scope="prototype" />
</beans>
Added: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java?rev=1153515&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java (added)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java Wed Aug 3 14:42:31 2011
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2011 The Apache Software Foundation.
+ *
+ * Licensed 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 static junit.framework.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.util.Collections;
+import junit.framework.Assert;
+import org.apache.cocoon.pipeline.NonCachingPipeline;
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.sax.SAXPipelineComponent;
+import org.apache.cocoon.sax.component.XMLSerializer;
+import org.apache.commons.io.FileUtils;
+import org.custommonkey.xmlunit.Diff;
+import org.junit.Test;
+
+public class StringTemplateGeneratorTest {
+
+ private boolean doTest(final StringTemplateGenerator generator)
+ throws Exception {
+
+ generator.setup(Collections.singletonMap("param", (Object) "value"));
+
+ final Pipeline<SAXPipelineComponent> pipeline =
+ new NonCachingPipeline<SAXPipelineComponent>();
+ pipeline.addComponent(generator);
+ pipeline.addComponent(new XMLSerializer());
+
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ pipeline.setup(baos);
+ pipeline.execute();
+
+ final String actualDocument = new String(baos.toByteArray(), "UTF-8");
+ Assert.assertNotNull(actualDocument);
+
+ final String expectedDocument = FileUtils.readFileToString(
+ new File(getClass().getResource("/test.xml").toURI())).
+ replaceAll("\\$param\\$", "value");
+
+ final Diff diff = new Diff(expectedDocument, actualDocument);
+ assertTrue("StringTemplate generation didn't work as expected " + diff,
+ diff.identical());
+
+ return true;
+ }
+
+ @Test
+ public final void testURL()
+ throws Exception {
+
+ assertTrue(doTest(new StringTemplateGenerator(
+ getClass().getResource("/test.xml"))));
+ }
+
+ @Test
+ public final void testFile()
+ throws Exception {
+
+ assertTrue(doTest(new StringTemplateGenerator(
+ new File(getClass().getResource("/test.xml").toURI()))));
+ }
+
+ @Test
+ public final void testStream()
+ throws Exception {
+
+ assertTrue(doTest(new StringTemplateGenerator(
+ getClass().getResourceAsStream("/test.xml"))));
+ }
+
+ @Test
+ public final void testString()
+ throws Exception {
+
+ assertTrue(doTest(new StringTemplateGenerator(FileUtils.readFileToString(
+ new File(getClass().getResource("/test.xml").toURI())))));
+ }
+}
Added: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml?rev=1153515&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml (added)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml Wed Aug 3 14:42:31 2011
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+<test>
+ <element>$param$</element>
+</test>
Modified: cocoon/cocoon3/trunk/parent/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/parent/pom.xml?rev=1153515&r1=1153514&r2=1153515&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/parent/pom.xml (original)
+++ cocoon/cocoon3/trunk/parent/pom.xml Wed Aug 3 14:42:31 2011
@@ -272,24 +272,24 @@
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
- <version>1.6.1</version>
+ <version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
- <version>1.6.1</version>
+ <version>1.6.11</version>
</dependency>
<!-- Other libraries -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
- <version>1.0.3</version>
+ <version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
- <version>1.0.3</version>
+ <version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
@@ -364,7 +364,7 @@
<dependency>
<groupId>org.antlr</groupId>
<artifactId>stringtemplate</artifactId>
- <version>3.2.1</version>
+ <version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>