cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject svn commit: r1195361 - in /cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate: ControllerContextAwareStringTemplateGenerator.java STRenderer.java StringTemplateGenerator.java StringTemplateTransformer.java
Date Mon, 31 Oct 2011 07:36:27 GMT
Author: ilgrosso
Date: Mon Oct 31 07:36:26 2011
New Revision: 1195361

URL: http://svn.apache.org/viewvc?rev=1195361&view=rev
Log:
Unifying ST rendering among all SAX components

Added:
    cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/STRenderer.java
Modified:
    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/StringTemplateTransformer.java

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=1195361&r1=1195360&r2=1195361&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
Mon Oct 31 07:36:26 2011
@@ -16,22 +16,13 @@
  */
 package org.apache.cocoon.stringtemplate;
 
+import java.io.IOException;
 import java.net.URL;
 import java.util.Map;
-import java.util.Map.Entry;
 import org.apache.cocoon.servlet.controller.ControllerContextHelper;
-import org.apache.commons.lang3.StringEscapeUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.stringtemplate.v4.ST;
-
-public class ControllerContextAwareStringTemplateGenerator extends StringTemplateGenerator
{
-
-    /**
-     * Logger.
-     */
-    private static final Logger LOG = LoggerFactory.getLogger(
-            ControllerContextAwareStringTemplateGenerator.class);
+
+public class ControllerContextAwareStringTemplateGenerator
+        extends StringTemplateGenerator {
 
     public ControllerContextAwareStringTemplateGenerator() {
         super();
@@ -42,32 +33,15 @@ public class ControllerContextAwareStrin
     }
 
     @Override
-    protected void addTemplateAttributes(final ST stringTemplate) {
-        super.addTemplateAttributes(stringTemplate);
+    protected String renderTemplate()
+            throws IOException {
 
         // put all objects that are passed by the controller context
         final Map<String, Object> controllerContext =
                 ControllerContextHelper.getContext(this.parameters);
-        String key;
-        Object attribute;
-        for (Entry<String, Object> eachEntry : controllerContext.entrySet()) {
-            key = eachEntry.getKey().replace(".", "_");
-
-            // 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.remove(key);
-            }
-
-            stringTemplate.add(key,
-                    StringEscapeUtils.escapeXml(eachEntry.getValue().toString()));
-
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Passing controller context parameter as attribute: "
-                        + "key=" + eachEntry.getKey()
-                        + ", value=" + eachEntry.getValue());
-            }
-        }
+
+        return STRenderer.getInstance().render(
+                getTemplate(), controllerContext);
+
     }
 }

Added: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/STRenderer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/STRenderer.java?rev=1195361&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/STRenderer.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/STRenderer.java
Mon Oct 31 07:36:26 2011
@@ -0,0 +1,68 @@
+/*
+ * 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 java.io.IOException;
+import java.util.Map;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.stringtemplate.v4.ST;
+
+public class STRenderer {
+
+    /**
+     * Logger.
+     */
+    private static final Logger LOG =
+            LoggerFactory.getLogger(STRenderer.class);
+
+    private static final STRenderer SINGLETON;
+
+    static {
+        SINGLETON = new STRenderer();
+    }
+
+    private STRenderer() {
+        super();
+    }
+
+    public static STRenderer getInstance() {
+        return SINGLETON;
+    }
+
+    public String render(final String template,
+            final Map<String, Object> parameters)
+            throws IOException {
+
+        final ST stringTemplate = new ST(template, '$', '$');
+
+        if (parameters == null || parameters.isEmpty()) {
+            LOG.warn("There are not any parameters passed to the template.");
+        } else {
+            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
+                stringTemplate.add(entry.getKey().replace(".", "_"),
+                        (entry.getValue() instanceof String)
+                        ? StringEscapeUtils.escapeXml(
+                        entry.getValue().toString())
+                        : entry.getValue());
+
+                LOG.debug("Passing pipeline parameter as attribute: key={}"
+                        + ", value={}", entry.getKey(), entry.getValue());
+            }
+        }
+
+        return stringTemplate.render();
+    }
+}

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=1195361&r1=1195360&r2=1195361&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
Mon Oct 31 07:36:26 2011
@@ -24,7 +24,6 @@ import java.io.StringReader;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Map.Entry;
 import org.apache.cocoon.pipeline.ProcessingException;
 import org.apache.cocoon.pipeline.SetupException;
 import org.apache.cocoon.pipeline.caching.CacheKey;
@@ -36,10 +35,8 @@ import org.apache.cocoon.pipeline.compon
 import org.apache.cocoon.sax.AbstractSAXProducer;
 import org.apache.cocoon.sax.util.XMLUtils;
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringEscapeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.stringtemplate.v4.ST;
 import org.xml.sax.InputSource;
 
 public class StringTemplateGenerator extends AbstractSAXProducer
@@ -95,6 +92,10 @@ public class StringTemplateGenerator ext
      * @see org.apache.cocoon.pipeline.component.CachingPipelineComponent#constructCacheKey()
      */
     public CacheKey constructCacheKey() {
+        if (this.url == null) {
+            return null;
+        }
+
         final CompoundCacheKey cacheKey = new CompoundCacheKey();
 
         try {
@@ -109,7 +110,7 @@ public class StringTemplateGenerator ext
         return cacheKey;
     }
 
-    private String getTemplate()
+    protected String getTemplate()
             throws IOException {
 
         String template = null;
@@ -139,18 +140,18 @@ public class StringTemplateGenerator ext
         }
 
         if (LOG.isDebugEnabled() && templateLocation != null) {
-            LOG.debug("Using template: " + templateLocation);
+            LOG.debug("Using template: {}", templateLocation);
         }
 
         return template;
     }
 
-    public String renderTemplate()
+    protected String renderTemplate()
             throws IOException {
 
-        final ST stringTemplate = new ST(getTemplate(), '$', '$');
-        this.addTemplateAttributes(stringTemplate);
-        return stringTemplate.render();
+        return STRenderer.getInstance().render(
+                getTemplate(), parameters);
+
     }
 
     /**
@@ -168,8 +169,7 @@ public class StringTemplateGenerator ext
 
         try {
             XMLUtils.createXMLReader(this.getSAXConsumer()).parse(
-                    new InputSource(new StringReader(
-                    renderTemplate())));
+                    new InputSource(new StringReader(renderTemplate())));
         } catch (Exception e) {
             throw new ProcessingException("Can't parse url connection "
                     + this.url, e);
@@ -195,30 +195,4 @@ public class StringTemplateGenerator ext
         this.url = (URL) parameters.get("source");
         setup((Map<String, Object>) parameters);
     }
-
-    /**
-     * Add attributes to the StringTemplate. Be careful to keep the constructCacheKey() method
-     * align.
-     *
-     * @param stringTemplate The template where the attributes are added to.
-     */
-    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> entry : this.parameters.entrySet()) {
-            stringTemplate.add(entry.getKey().replace(".", "_"),
-                    (entry.getValue() instanceof String)
-                    ? StringEscapeUtils.escapeXml(entry.getValue().toString())
-                    : entry.getValue());
-
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Passing pipeline parameter as attribute: key="
-                        + entry.getKey() + ", value="
-                        + entry.getValue());
-            }
-        }
-    }
 }

Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java?rev=1195361&r1=1195360&r2=1195361&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java
Mon Oct 31 07:36:26 2011
@@ -15,40 +15,23 @@
  */
 package org.apache.cocoon.stringtemplate;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.Map;
-import org.apache.cocoon.pipeline.caching.CacheKey;
-import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
 import org.apache.cocoon.sax.AbstractSAXTransformer;
 import org.apache.cocoon.sax.util.XMLUtils;
 import org.apache.cocoon.xml.sax.SAXBuffer;
 import org.apache.commons.io.output.ByteArrayOutputStream;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.xml.sax.SAXException;
 
-public class StringTemplateTransformer extends AbstractSAXTransformer
-        implements CachingPipelineComponent {
+public class StringTemplateTransformer extends AbstractSAXTransformer {
 
-    /**
-     * Logger.
-     */
-    private static final Logger LOG =
-            LoggerFactory.getLogger(StringTemplateTransformer.class);
-
-    private StringTemplateGenerator generator;
-
-    public StringTemplateTransformer() {
-        super();
-
-        this.generator = new StringTemplateGenerator();
-    }
+    private Map<String, Object> parameters = new HashMap<String, Object>();
 
     @Override
     public void setup(final Map<String, Object> parameters) {
         if (parameters != null && !parameters.isEmpty()) {
-            this.generator.setup(parameters);
+            this.parameters.putAll(parameters);
         }
     }
 
@@ -60,12 +43,9 @@ public class StringTemplateTransformer e
     }
 
     @Override
-    public CacheKey constructCacheKey() {
-        return this.generator.constructCacheKey();
-    }
+    public void startDocument()
+            throws SAXException {
 
-    @Override
-    public void startDocument() throws SAXException {
         super.startDocument();
         super.startSAXRecording();
     }
@@ -78,10 +58,9 @@ public class StringTemplateTransformer e
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         XMLUtils.toOutputStream(baos, buffer);
 
-        this.generator.setStream(new ByteArrayInputStream(baos.toByteArray()));
-        String rendered;
         try {
-            rendered = this.generator.renderTemplate();
+            final String rendered = STRenderer.getInstance().render(
+                    new String(baos.toByteArray()), parameters);
             XMLUtils.toSax(rendered, this.getSAXConsumer());
         } catch (IOException e) {
             throw new SAXException(e);



Mime
View raw message