3535
3636
3737/**
38- *
38+ *
3939 * Helper and wrapper for a Template engine (currently only FreeMarker).
40- * Exposes only the essential functions to avoid too much coupling else where.
41- *
40+ * Exposes only the essential functions to avoid too much coupling else where.
41+ *
4242 * @author max
4343 *
4444 */
4545public class TemplateHelper {
46-
46+
4747 static final Logger log = Logger .getLogger (TemplateHelper .class );
48-
48+
4949 private String templatePrefix ;
5050 private File outputDirectory ;
5151
@@ -54,17 +54,17 @@ public class TemplateHelper {
5454 protected SimpleHash context ;
5555
5656 public TemplateHelper () {
57-
57+
5858 }
59-
59+
6060 public void init (File outputDirectory , String [] templatePaths ) {
6161 this .outputDirectory = outputDirectory ;
62-
62+
6363 context = new SimpleHash (new BeansWrapperBuilder (Configuration .VERSION_2_3_0 ).build ());
6464 freeMarkerEngine = new Configuration (Configuration .VERSION_2_3_0 );
65-
65+
6666 List <TemplateLoader > loaders = new ArrayList <TemplateLoader >();
67-
67+
6868 for (int i = 0 ; i < templatePaths .length ; i ++) {
6969 File file = new File (templatePaths [i ]);
7070 if (file .exists () && file .isDirectory ()) {
@@ -79,14 +79,14 @@ public void init(File outputDirectory, String[] templatePaths) {
7979 }
8080 }
8181 loaders .add (new ClassTemplateLoader (this .getClass (),"/" )); // the template names are like pojo/Somewhere so have to be a rooted classpathloader
82-
82+
8383 freeMarkerEngine .setTemplateLoader (new MultiTemplateLoader ((TemplateLoader []) loaders .toArray (new TemplateLoader [loaders .size ()])));
84-
84+
8585 }
86-
87-
86+
87+
8888 public class Templates {
89-
89+
9090 public void createFile (String content , String fileName ) {
9191 Writer fw = null ;
9292 try {
@@ -106,13 +106,13 @@ public void createFile(String content, String fileName) {
106106 }
107107 }
108108 }
109-
109+
110110 public File getOutputDirectory () {
111111 return outputDirectory ;
112112 }
113113
114-
115-
114+
115+
116116 public void putInContext (String key , Object value ) {
117117 log .trace ("putInContext " + key + "=" + value );
118118 if (value == null ) throw new IllegalStateException ("value must not be null for " + key );
@@ -121,7 +121,7 @@ public void putInContext(String key, Object value) {
121121 log .warn ( "Overwriting " + replaced + " when setting " + key + " to " + value + "." );
122122 }
123123 }
124-
124+
125125 public void removeFromContext (String key , Object expected ) {
126126 log .trace ("removeFromContext " + key + "=" + expected );
127127 Object replaced = internalRemoveFromContext (key );
@@ -130,7 +130,7 @@ public void removeFromContext(String key, Object expected) {
130130 throw new IllegalStateException("expected " + key + " to be bound to " + expected + " but was to " + replaced);
131131 }*/
132132 }
133-
133+
134134 public void ensureExistence (File destination ) {
135135 // if the directory exists, make sure it is a directory
136136 File dir = destination .getAbsoluteFile ().getParentFile ();
@@ -147,8 +147,8 @@ else if ( !dir.exists() ) {
147147 throw new RuntimeException ( "unable to create directory: " + dir .getAbsolutePath () );
148148 }
149149 }
150- }
151-
150+ }
151+
152152 protected String getTemplatePrefix () {
153153 return templatePrefix ;
154154 }
@@ -158,33 +158,33 @@ protected SimpleHash getContext() {
158158 }
159159
160160 public void processString (String template , Writer output ) {
161-
161+
162162 try {
163163 Reader r = new StringReader (template );
164164 Template t = new Template ("unknown" , r , freeMarkerEngine );
165-
166- t .process (getContext (), output );
167- }
165+
166+ t .process (getContext (), output );
167+ }
168168 catch (IOException e ) {
169169 throw new RuntimeException ("Error while processing template string" , e );
170- }
170+ }
171171 catch (TemplateException te ) {
172172 throw new RuntimeException ("Error while processing template string" , te );
173173 }
174174 catch (Exception e ) {
175175 throw new RuntimeException ("Error while processing template string" , e );
176176 }
177177 }
178-
178+
179179 public void setupContext () {
180- getContext ().put ("version" , Version .CURRENT_VERSION );
180+ getContext ().put ("version" , Version .versionString () );
181181 getContext ().put ("ctx" , getContext () ); //TODO: I would like to remove this, but don't know another way to actually get the list possible "root" keys for debugging.
182182 getContext ().put ("templates" , new Templates ());
183-
184- getContext ().put ("date" , new SimpleDate (new Date (), TemplateDateModel .DATETIME ));
185-
183+
184+ getContext ().put ("date" , new SimpleDate (new Date (), TemplateDateModel .DATETIME ));
185+
186186 }
187-
187+
188188 protected Object internalPutInContext (String key , Object value ) {
189189 TemplateModel model = null ;
190190 try {
@@ -196,7 +196,7 @@ protected Object internalPutInContext(String key, Object value) {
196196 getContext ().put (key , value );
197197 return model ;
198198 }
199-
199+
200200 protected Object internalRemoveFromContext (String key ) {
201201 TemplateModel model = null ;
202202 try {
@@ -208,54 +208,54 @@ protected Object internalRemoveFromContext(String key) {
208208 getContext ().remove (key );
209209 return model ;
210210 }
211-
211+
212212 /** look up the template named templateName via the paths and print the content to the output */
213213 public void processTemplate (String templateName , Writer output , String rootContext ) {
214214 if (rootContext == null ) {
215215 rootContext = "Unknown context" ;
216216 }
217-
217+
218218 try {
219219 Template template = freeMarkerEngine .getTemplate (templateName );
220- template .process (getContext (), output );
221- }
220+ template .process (getContext (), output );
221+ }
222222 catch (IOException e ) {
223223 throw new RuntimeException ("Error while processing " + rootContext + " with template " + templateName , e );
224224 }
225- catch (TemplateException te ) {
225+ catch (TemplateException te ) {
226226 throw new RuntimeException ("Error while processing " + rootContext + " with template " + templateName , te );
227- }
227+ }
228228 catch (Exception e ) {
229229 throw new RuntimeException ("Error while processing " + rootContext + " with template " + templateName , e );
230- }
230+ }
231231 }
232-
233-
232+
233+
234234 /**
235235 * Check if the template exists. Tries to search with the templatePrefix first and then secondly without the template prefix.
236- *
236+ *
237237 * @param name
238238 * @return
239- */
239+ */
240240 /*protected String getTemplateName(String name) {
241241 if(!name.endsWith(".ftl")) {
242- name = name + ".ftl";
242+ name = name + ".ftl";
243243 }
244-
244+
245245 if(getTemplatePrefix()!=null && templateExists(getTemplatePrefix() + name)) {
246246 return getTemplatePrefix() + name;
247- }
248-
247+ }
248+
249249 if(templateExists(name)) {
250250 return name;
251- }
252-
251+ }
252+
253253 throw new ExporterException("Could not find template with name: " + name);
254254 }*/
255-
255+
256256 public boolean templateExists (String templateName ) {
257257 TemplateLoader templateLoader = freeMarkerEngine .getTemplateLoader ();
258-
258+
259259 try {
260260 return templateLoader .findTemplateSource (templateName )!=null ;
261261 }
0 commit comments