Author: rdonkin
Date: Sat Mar 30 16:36:08 2013
New Revision: 1462793
URL: http://svn.apache.org/r1462793
Log:
Factor out nested type
Added:
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
(with props)
Modified:
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
Added:
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
URL:
http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java?rev=1462793&view=auto
==============================================================================
---
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
(added)
+++
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
Sat Mar 30 16:36:08 2013
@@ -0,0 +1,91 @@
+/**
+ * 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.
+ */
+package org.apache.creadur.tentacles;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+
+public class TemplateBuilder {
+ private final VelocityEngine engine;
+ private final IOSystem ioSystem;
+ private final String template;
+ private final Map<String, Object> map = new HashMap<String, Object>();
+
+ public TemplateBuilder(final String template, final IOSystem ioSystem,
+ final VelocityEngine engine) {
+ this.template = template;
+ this.ioSystem = ioSystem;
+ this.engine = engine;
+ }
+
+ public TemplateBuilder add(final String key, final Object value) {
+ this.map.put(key, value);
+ return this;
+ }
+
+ public TemplateBuilder addAll(final Map<String, Object> map) {
+ this.map.putAll(map);
+ return this;
+ }
+
+ public String apply() {
+ final StringWriter writer = new StringWriter();
+
+ try {
+ evaluate(this.template, this.map, writer);
+ } catch (final IOException ioe) {
+ throw new RuntimeException("can't apply template "
+ + this.template, ioe);
+ }
+
+ return writer.toString();
+ }
+
+ public File write(final File file) throws IOException {
+ this.ioSystem.writeString(file, apply());
+ return file;
+ }
+
+ private void evaluate(final String template,
+ final Map<String, Object> mapContext, final Writer writer)
+ throws IOException {
+
+ final URL resource =
+ Thread.currentThread().getContextClassLoader()
+ .getResource(template);
+
+ if (resource == null) {
+ throw new IllegalStateException(template);
+ }
+
+ final VelocityContext context = new VelocityContext(mapContext);
+ this.engine.evaluate(context, writer, Templates.class.getName(),
+ new InputStreamReader(resource.openStream()));
+ }
+
+}
\ No newline at end of file
Propchange:
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/TemplateBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
URL:
http://svn.apache.org/viewvc/creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java?rev=1462793&r1=1462792&r2=1462793&view=diff
==============================================================================
---
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
(original)
+++
creadur/tentacles/trunk/src/main/java/org/apache/creadur/tentacles/Templates.java
Sat Mar 30 16:36:08 2013
@@ -16,17 +16,8 @@
*/
package org.apache.creadur.tentacles;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Properties;
-import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.CommonsLogLogChute;
@@ -54,63 +45,11 @@ public final class Templates {
this.engine.init(properties);
}
- private void evaluate(final String template,
- final Map<String, Object> mapContext, final Writer writer)
- throws IOException {
-
- final URL resource =
- Thread.currentThread().getContextClassLoader()
- .getResource(template);
-
- if (resource == null) {
- throw new IllegalStateException(template);
- }
-
- final VelocityContext context = new VelocityContext(mapContext);
- this.engine.evaluate(context, writer, Templates.class.getName(),
- new InputStreamReader(resource.openStream()));
+ public static TemplateBuilder template(final String name, final IOSystem
ioSystem) {
+ return INSTANCE.builder(name, ioSystem);
}
- public static Builder template(final String name, final IOSystem ioSystem)
{
- return INSTANCE.new Builder(name, ioSystem);
- }
-
- public class Builder {
- private final IOSystem ioSystem;
- private final String template;
- private final Map<String, Object> map = new HashMap<String, Object>();
-
- public Builder(final String template, final IOSystem ioSystem) {
- this.template = template;
- this.ioSystem = ioSystem;
- }
-
- public Builder add(final String key, final Object value) {
- this.map.put(key, value);
- return this;
- }
-
- public Builder addAll(final Map<String, Object> map) {
- this.map.putAll(map);
- return this;
- }
-
- public String apply() {
- final StringWriter writer = new StringWriter();
-
- try {
- evaluate(this.template, this.map, writer);
- } catch (final IOException ioe) {
- throw new RuntimeException("can't apply template "
- + this.template, ioe);
- }
-
- return writer.toString();
- }
-
- public File write(final File file) throws IOException {
- this.ioSystem.writeString(file, apply());
- return file;
- }
+ private TemplateBuilder builder(final String name, final IOSystem
ioSystem) {
+ return new TemplateBuilder(name, ioSystem, this.engine);
}
}