Return-Path: X-Original-To: apmail-cocoon-cvs-archive@www.apache.org Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 58AAE979E for ; Sat, 29 Oct 2011 14:27:20 +0000 (UTC) Received: (qmail 37809 invoked by uid 500); 29 Oct 2011 14:27:20 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 37744 invoked by uid 500); 29 Oct 2011 14:27:19 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 37736 invoked by uid 99); 29 Oct 2011 14:27:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 29 Oct 2011 14:27:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 29 Oct 2011 14:27:17 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 91261238897A for ; Sat, 29 Oct 2011 14:26:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: cvs@cocoon.apache.org From: ilgrosso@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111029142657.91261238897A@eris.apache.org> 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 eachEntry : this.parameters.entrySet()) { - stringTemplate.add(eachEntry.getKey().replace(".", "_"), - StringEscapeUtils.escapeXml(eachEntry.getValue().toString())); + for (Entry 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 parameters; - private final transient StringBuilder accumulator; + private StringTemplateGenerator generator; public StringTemplateTransformer() { super(); - this.parameters = new HashMap(); - this.accumulator = new StringBuilder(); + this.generator = new StringTemplateGenerator(); } @Override public void setup(final Map parameters) { if (parameters != null && !parameters.isEmpty()) { - this.parameters.putAll(parameters); + this.generator.setup(parameters); } } @Override - public void setConfiguration(final Map configuration) { + public void setConfiguration( + final Map configuration) { + this.setup((Map) 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 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 params = new HashMap(); + params.put("param", "value"); + params.put("booleanParam", Boolean.FALSE); + generator.setup(params); final Pipeline pipeline = new NonCachingPipeline(); @@ -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(".*true", ""); + 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 = - "" - + "" - + "This text content also contains $param$" - + "" - + "and also this contains $param$" - + "while this does not" - + ""; - @Test public void test() throws Exception { + final String input = FileUtils.readFileToString( + new File(getClass().getResource("/test.xml").toURI())); + + final Map params = new HashMap(); + 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 pipeline = new NonCachingPipeline(); - 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(".*true", ""); + 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. --> - $param$ + + This text content also contains $param$ + + and also this contains $param$ + while this does not +$if(!booleanParam)$ + booleanParam is false +$else$ + booleanParam is true +$endif$