cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject svn commit: r1194892 - in /cocoon/cocoon3/trunk/cocoon-stringtemplate/src: main/java/org/apache/cocoon/stringtemplate/ test/java/org/apache/cocoon/stringtemplate/ test/resources/
Date Sat, 29 Oct 2011 14:26:57 GMT
Author: ilgrosso
Date: Sat Oct 29 14:26:56 2011
New Revision: 1194892

URL: http://svn.apache.org/viewvc?rev=1194892&view=rev
Log:
Making StringTemplateTransformer using StringTemplateGenerator's commodities

Modified:
    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
    cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java
    cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateTransformerTest.java
    cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml

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=1194892&r1=1194891&r2=1194892&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
Sat Oct 29 14:26:56 2011
@@ -85,6 +85,10 @@ public class StringTemplateGenerator ext
         this.string = string;
     }
 
+    public void setStream(InputStream stream) {
+        this.stream = stream;
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -94,8 +98,8 @@ public class StringTemplateGenerator ext
         final CompoundCacheKey cacheKey = new CompoundCacheKey();
 
         try {
-            cacheKey.addCacheKey(new TimestampCacheKey(this.url, this.url.
-                    openConnection().getLastModified()));
+            cacheKey.addCacheKey(new TimestampCacheKey(this.url,
+                    this.url.openConnection().getLastModified()));
             cacheKey.addCacheKey(new ParameterCacheKey("contextParameters",
                     this.parameters));
         } catch (IOException e) {
@@ -141,6 +145,14 @@ public class StringTemplateGenerator ext
         return template;
     }
 
+    public String renderTemplate()
+            throws IOException {
+
+        final ST stringTemplate = new ST(getTemplate(), '$', '$');
+        this.addTemplateAttributes(stringTemplate);
+        return stringTemplate.render();
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -155,12 +167,9 @@ public class StringTemplateGenerator ext
         }
 
         try {
-            final ST stringTemplate = new ST(getTemplate(), '$', '$');
-            this.addTemplateAttributes(stringTemplate);
-
             XMLUtils.createXMLReader(this.getSAXConsumer()).parse(
                     new InputSource(new StringReader(
-                    stringTemplate.render())));
+                    renderTemplate())));
         } catch (Exception e) {
             throw new ProcessingException("Can't parse url connection "
                     + this.url, e);
@@ -199,14 +208,16 @@ public class StringTemplateGenerator ext
             return;
         }
 
-        for (Entry<String, Object> eachEntry : this.parameters.entrySet()) {
-            stringTemplate.add(eachEntry.getKey().replace(".", "_"),
-                    StringEscapeUtils.escapeXml(eachEntry.getValue().toString()));
+        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="
-                        + eachEntry.getKey() + ", value="
-                        + eachEntry.getValue());
+                        + 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=1194892&r1=1194891&r2=1194892&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
Sat Oct 29 14:26:56 2011
@@ -15,20 +15,21 @@
  */
 package org.apache.cocoon.stringtemplate;
 
-import java.util.HashMap;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.util.Map;
-import java.util.Map.Entry;
-
+import org.apache.cocoon.pipeline.caching.CacheKey;
+import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
 import org.apache.cocoon.sax.AbstractSAXTransformer;
-import org.apache.commons.lang3.StringEscapeUtils;
+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.stringtemplate.v4.ST;
-import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
-import org.xml.sax.helpers.AttributesImpl;
 
-public class StringTemplateTransformer extends AbstractSAXTransformer {
+public class StringTemplateTransformer extends AbstractSAXTransformer
+        implements CachingPipelineComponent {
 
     /**
      * Logger.
@@ -36,88 +37,56 @@ public class StringTemplateTransformer e
     private static final Logger LOG =
             LoggerFactory.getLogger(StringTemplateTransformer.class);
 
-    private final transient Map<String, Object> parameters;
-    private final transient StringBuilder accumulator;
+    private StringTemplateGenerator generator;
 
     public StringTemplateTransformer() {
         super();
 
-        this.parameters = new HashMap<String, Object>();
-        this.accumulator = new StringBuilder();
+        this.generator = new StringTemplateGenerator();
     }
 
     @Override
     public void setup(final Map<String, Object> parameters) {
         if (parameters != null && !parameters.isEmpty()) {
-            this.parameters.putAll(parameters);
+            this.generator.setup(parameters);
         }
     }
 
     @Override
-    public void setConfiguration(final Map<String, ? extends Object> configuration)
{
+    public void setConfiguration(
+            final Map<String, ? extends Object> configuration) {
+
         this.setup((Map<String, Object>) configuration);
     }
 
-    private String renderTemplate(final String template) {
-        if (this.parameters == null || this.parameters.isEmpty()) {
-            LOG.warn("There are not any parameters passed to the template.");
-            return template;
-        }
-
-        final ST stringTemplate = new ST(template, '$', '$');
-
-        for (Entry<String, Object> eachEntry : this.parameters.entrySet()) {
-            final String key = eachEntry.getKey().replace(".", "_");
-            final String value = StringEscapeUtils.escapeXml(eachEntry.getValue().toString());
-            stringTemplate.add(key, value);
-
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Passing pipeline parameter as attribute: key="
-                        + eachEntry.getKey() + ", value="
-                        + eachEntry.getValue());
-            }
-        }
-
-        return stringTemplate.render();
+    @Override
+    public CacheKey constructCacheKey() {
+        return this.generator.constructCacheKey();
     }
 
-    private void flushAccumulator() throws SAXException {
-        if (this.accumulator.length() == 0) {
-            return;
-        }
-
-        final String rendered = this.renderTemplate(this.accumulator.toString());
-        this.accumulator.setLength(0);
-
-        super.characters(rendered.toCharArray(), 0, rendered.length());
+    @Override
+    public void startDocument() throws SAXException {
+        super.startDocument();
+        super.startSAXRecording();
     }
 
     @Override
-    public void startElement(final String uri, final String localName, final String name,
final Attributes attributes)
+    public void endDocument()
             throws SAXException {
-        final AttributesImpl renderedAttributes = new AttributesImpl();
 
-        for (int i = 0; i < attributes.getLength(); i++) {
-            final String attributeURI = attributes.getURI(i);
-            final String attributeLocalName = attributes.getLocalName(i);
-            final String attributeQName = attributes.getQName(i);
-            final String attributeType = attributes.getType(i);
-            final String attributeValue = this.renderTemplate(attributes.getValue(i));
-            renderedAttributes.addAttribute(attributeURI, attributeLocalName, attributeQName,
attributeType, attributeValue);
+        final SAXBuffer buffer = super.endSAXRecording();
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        XMLUtils.toOutputStream(baos, buffer);
+
+        this.generator.setStream(new ByteArrayInputStream(baos.toByteArray()));
+        String rendered;
+        try {
+            rendered = this.generator.renderTemplate();
+            XMLUtils.toSax(rendered, this.getSAXConsumer());
+        } catch (IOException e) {
+            throw new SAXException(e);
         }
 
-        this.flushAccumulator();
-        super.startElement(uri, localName, name, renderedAttributes);
-    }
-
-    @Override
-    public void characters(final char[] characters, final int start, final int length) throws
SAXException {
-        this.accumulator.append(characters, start, length);
-    }
-
-    @Override
-    public void endElement(final String uri, final String localName, final String name) throws
SAXException {
-        this.flushAccumulator();
-        super.endElement(uri, localName, name);
+        super.endDocument();
     }
 }

Modified: 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=1194892&r1=1194891&r2=1194892&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateGeneratorTest.java
Sat Oct 29 14:26:56 2011
@@ -19,14 +19,15 @@ import static junit.framework.Assert.*;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.util.Collections;
-import junit.framework.Assert;
+import java.util.HashMap;
+import java.util.Map;
 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.custommonkey.xmlunit.XMLUnit;
 import org.junit.Test;
 
 public class StringTemplateGeneratorTest {
@@ -34,7 +35,10 @@ public class StringTemplateGeneratorTest
     private boolean doTest(final StringTemplateGenerator generator)
             throws Exception {
 
-        generator.setup(Collections.singletonMap("param", (Object) "value"));
+        final Map<String, Object> params = new HashMap<String, Object>();
+        params.put("param", "value");
+        params.put("booleanParam", Boolean.FALSE);
+        generator.setup(params);
 
         final Pipeline<SAXPipelineComponent> pipeline =
                 new NonCachingPipeline<SAXPipelineComponent>();
@@ -46,12 +50,15 @@ public class StringTemplateGeneratorTest
         pipeline.execute();
 
         final String actualDocument = new String(baos.toByteArray(), "UTF-8");
-        Assert.assertNotNull(actualDocument);
+        assertNotNull(actualDocument);
 
         final String expectedDocument = FileUtils.readFileToString(
                 new File(getClass().getResource("/test.xml").toURI())).
-                replaceAll("\\$param\\$", "value");
+                replaceAll("\\$param\\$", "value").
+                replaceAll("\\$.*\\$", "").
+                replaceAll("<element>.*true</element>", "");
 
+        XMLUnit.setIgnoreWhitespace(true);
         final Diff diff = new Diff(expectedDocument, actualDocument);
         assertTrue("StringTemplate generation didn't work as expected " + diff,
                 diff.identical());
@@ -87,7 +94,8 @@ public class StringTemplateGeneratorTest
     public final void testString()
             throws Exception {
 
-        assertTrue(doTest(new StringTemplateGenerator(FileUtils.readFileToString(
+        assertTrue(doTest(new StringTemplateGenerator(
+                FileUtils.readFileToString(
                 new File(getClass().getResource("/test.xml").toURI())))));
     }
 }

Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateTransformerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateTransformerTest.java?rev=1194892&r1=1194891&r2=1194892&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateTransformerTest.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/java/org/apache/cocoon/stringtemplate/StringTemplateTransformerTest.java
Sat Oct 29 14:26:56 2011
@@ -18,38 +18,39 @@ package org.apache.cocoon.stringtemplate
 import static junit.framework.Assert.*;
 
 import java.io.ByteArrayOutputStream;
-import java.util.Collections;
-import junit.framework.Assert;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
 import org.apache.cocoon.pipeline.NonCachingPipeline;
 import org.apache.cocoon.pipeline.Pipeline;
 import org.apache.cocoon.sax.SAXPipelineComponent;
 import org.apache.cocoon.sax.component.XMLGenerator;
 import org.apache.cocoon.sax.component.XMLSerializer;
+import org.apache.commons.io.FileUtils;
 import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Test;
 
 public class StringTemplateTransformerTest {
 
-    private static final String INPUT =
-            "<test>"
-            + "<element param=\"$param$\">"
-            + "This text content also contains $param$"
-            + "</element>"
-            + "and also this contains $param$"
-            + "<element>while this does not</element>"
-            + "</test>";
-
     @Test
     public void test()
             throws Exception {
 
+        final String input = FileUtils.readFileToString(
+                new File(getClass().getResource("/test.xml").toURI()));
+
+        final Map<String, Object> params = new HashMap<String, Object>();
+        params.put("param", "value");
+        params.put("booleanParam", Boolean.FALSE);
+
         final StringTemplateTransformer transformer =
                 new StringTemplateTransformer();
-        transformer.setup(Collections.singletonMap("param", (Object) "value"));
+        transformer.setup(params);
 
         final Pipeline<SAXPipelineComponent> pipeline =
                 new NonCachingPipeline<SAXPipelineComponent>();
-        pipeline.addComponent(new XMLGenerator(INPUT));
+        pipeline.addComponent(new XMLGenerator(input));
         pipeline.addComponent(transformer);
         pipeline.addComponent(new XMLSerializer());
 
@@ -58,10 +59,13 @@ public class StringTemplateTransformerTe
         pipeline.execute();
 
         final String actualDocument = new String(baos.toByteArray(), "UTF-8");
-        Assert.assertNotNull(actualDocument);
+        assertNotNull(actualDocument);
 
-        final String expectedDocument = INPUT.replaceAll("\\$param\\$", "value");
+        final String expectedDocument = input.replaceAll("\\$param\\$", "value").
+                replaceAll("\\$.*\\$", "").
+                replaceAll("<element>.*true</element>", "");
 
+        XMLUnit.setIgnoreWhitespace(true);
         final Diff diff = new Diff(expectedDocument, actualDocument);
         assertTrue("StringTemplate transformer didn't work as expected " + diff,
                 diff.identical());

Modified: 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=1194892&r1=1194891&r2=1194892&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/test/resources/test.xml Sat Oct 29 14:26:56
2011
@@ -18,5 +18,14 @@
   under the License.
  -->
 <test>
-    <element>$param$</element>
+    <element param="$param$">
+            This text content also contains $param$
+    </element>
+            and also this contains $param$
+    <element>while this does not</element>
+$if(!booleanParam)$
+    <element>booleanParam is false</element>
+$else$
+    <element>booleanParam is true</element>
+$endif$ 
 </test>



Mime
View raw message