cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stevend...@apache.org
Subject svn commit: r1155097 - /cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java
Date Mon, 08 Aug 2011 20:58:20 GMT
Author: stevendolg
Date: Mon Aug  8 20:58:20 2011
New Revision: 1155097

URL: http://svn.apache.org/viewvc?rev=1155097&view=rev
Log:
Reworked class to fix warnings

Modified:
    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/StringTemplateTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateTransformer.java?rev=1155097&r1=1155096&r2=1155097&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 Aug  8 20:58:20 2011
@@ -15,10 +15,10 @@
  */
 package org.apache.cocoon.stringtemplate;
 
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+
 import org.apache.cocoon.sax.AbstractSAXTransformer;
 import org.apache.commons.lang3.StringEscapeUtils;
 import org.apache.commons.logging.Log;
@@ -30,18 +30,16 @@ import org.xml.sax.helpers.AttributesImp
 
 public class StringTemplateTransformer extends AbstractSAXTransformer {
 
-    private static final Log LOG =
-            LogFactory.getLog(StringTemplateTransformer.class);
-
-    private transient final Map<String, Object> parameters;
+    private static final Log LOG = LogFactory.getLog(StringTemplateTransformer.class);
 
-    private transient final StringBuilder accumulator;
+    private final transient Map<String, Object> parameters;
+    private final transient StringBuilder accumulator;
 
     public StringTemplateTransformer() {
         super();
 
-        parameters = new HashMap<String, Object>();
-        accumulator = new StringBuilder();
+        this.parameters = new HashMap<String, Object>();
+        this.accumulator = new StringBuilder();
     }
 
     @Override
@@ -52,14 +50,12 @@ public class StringTemplateTransformer e
     }
 
     @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) {
+        if (this.parameters == null || this.parameters.isEmpty()) {
             LOG.warn("There are not any parameters passed to the template.");
             return template;
         }
@@ -67,8 +63,9 @@ public class StringTemplateTransformer e
         final ST stringTemplate = new ST(template, '$', '$');
 
         for (Entry<String, Object> eachEntry : this.parameters.entrySet()) {
-            stringTemplate.add(eachEntry.getKey().replace(".", "_"),
-                    StringEscapeUtils.escapeXml(eachEntry.getValue().toString()));
+            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="
@@ -80,45 +77,43 @@ public class StringTemplateTransformer e
         return stringTemplate.render();
     }
 
-    private void flushAccumulator()
-            throws SAXException {
-        if (accumulator.length() > 0) {
-            final String rendered = renderTemplate(accumulator.toString());
-            accumulator.setLength(0);
-
-            super.characters(rendered.toCharArray(), 0, rendered.length());
+    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 startElement(final String uri, final String localName,
-            final String name, final Attributes atts)
+    public void startElement(final String uri, final String localName, final String name,
final Attributes attributes)
             throws SAXException {
+        final AttributesImpl renderedAttributes = new AttributesImpl();
 
-        final AttributesImpl newAtts = new AttributesImpl();
-        for (int i = 0; i < atts.getLength(); i++) {
-            newAtts.addAttribute(atts.getURI(i),
-                    atts.getLocalName(i), atts.getQName(i), atts.getType(i),
-                    renderTemplate(atts.getValue(i)));
+        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);
         }
 
-        flushAccumulator();
-        super.startElement(uri, localName, name, newAtts);
+        this.flushAccumulator();
+        super.startElement(uri, localName, name, renderedAttributes);
     }
 
     @Override
-    public void characters(final char[] ch, final int start, final int length)
-            throws SAXException {
-
-        accumulator.append(Arrays.copyOfRange(ch, start, start + length));
+    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 {
-
-        flushAccumulator();
+    public void endElement(final String uri, final String localName, final String name) throws
SAXException {
+        this.flushAccumulator();
         super.endElement(uri, localName, name);
     }
 }



Mime
View raw message