This project has retired. For details please refer to its Attic page.
AbstractTemplateClassGenerator xref
View Javadoc

1   /*
2    * $Id: AbstractTemplateClassGenerator.java 1656976 2015-02-04 02:27:40Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.autotag.generate;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  import java.util.Map;
28  
29  import org.apache.tiles.autotag.core.AutotagRuntimeException;
30  import org.apache.tiles.autotag.core.OutputLocator;
31  import org.apache.tiles.autotag.model.TemplateClass;
32  import org.apache.tiles.autotag.model.TemplateSuite;
33  import org.apache.tiles.autotag.tool.StringTool;
34  import org.apache.velocity.Template;
35  import org.apache.velocity.VelocityContext;
36  import org.apache.velocity.app.VelocityEngine;
37  import org.apache.velocity.exception.ParseErrorException;
38  import org.apache.velocity.exception.ResourceNotFoundException;
39  
40  /**
41   * A base template class generator.
42   *
43   * @version $Rev: 1656976 $ $Date: 2015-02-04 03:27:40 +0100 (Wed, 04 Feb 2015) $
44   */
45  public abstract class AbstractTemplateClassGenerator implements
46          TemplateClassGenerator {
47  
48      /**
49       * The Velocity engine to use.
50       */
51      private VelocityEngine velocityEngine;
52  
53      /**
54       * Constructor.
55       *
56       * @param velocityEngine The Velocity engine.
57       */
58      public AbstractTemplateClassGenerator(VelocityEngine velocityEngine) {
59          this.velocityEngine = velocityEngine;
60      }
61  
62      @Override
63      public void generate(OutputLocator outputLocator, String packageName,
64              TemplateSuite suite, TemplateClass clazz, Map<String, String> parameters,
65              String runtimeClass, String requestClass) {
66          String filePath = 
67          		getDirectoryName(packageName, suite, clazz, parameters, runtimeClass, requestClass)
68                  + File.separator
69                  + getFilename(packageName, suite, clazz, parameters, runtimeClass, requestClass);
70  		if (!outputLocator.isUptodate(filePath)) {
71  	        VelocityContext context = new VelocityContext();
72  	        context.put("packageName", packageName);
73  	        context.put("suite", suite);
74  	        context.put("clazz", clazz);
75  	        context.put("stringTool", new StringTool());
76  	        context.put("parameters", parameters);
77  	        context.put("runtimeClass", runtimeClass);
78  	        context.put("requestClass", requestClass);
79  	        try {
80  	            Template template = velocityEngine.getTemplate(getTemplatePath(
81  	                    packageName, suite, clazz, parameters, runtimeClass, requestClass));
82  	            Writer writer = new OutputStreamWriter(outputLocator.getOutputStream(filePath));
83  	            try {
84  	                template.merge(context, writer);
85  	            } finally {
86  	                writer.close();
87  	            }
88  	        } catch (ResourceNotFoundException e) {
89  	            throw new AutotagRuntimeException("Cannot find template resource",
90  	                    e);
91  	        } catch (ParseErrorException e) {
92  	            throw new AutotagRuntimeException(
93  	                    "The template resource is not parseable", e);
94  	        } catch (IOException e) {
95  	            throw new AutotagRuntimeException(
96  	                    "I/O Exception when generating file", e);
97  	        } catch (RuntimeException e) {
98  	            throw e;
99  	        } catch (Exception e) {
100 	            throw new AutotagRuntimeException(
101 	                    "Another generic exception while parsing the template resource",
102 	                    e);
103 	        }
104 		}
105     }
106 
107     /**
108      * Calculates and returns the template path.
109      *
110      * @param packageName The name of the package.
111      * @param suite The template suite.
112      * @param clazz The template class.
113      * @param parameters The map of parameters.
114      * @return The template path.
115      */
116     protected abstract String getTemplatePath(
117             String packageName, TemplateSuite suite, TemplateClass clazz, Map<String, String> parameters,
118             String runtimeClass, String requestClass);
119 
120     /**
121      * Calculates and returns the filename of the generated file.
122      *
123      * @param packageName The name of the package.
124      * @param suite The template suite.
125      * @param clazz The template class.
126      * @param parameters The map of parameters.
127      * @return The template path.
128      */
129     protected abstract String getFilename(String packageName,
130             TemplateSuite suite, TemplateClass clazz, Map<String, String> parameters, String runtimeClass,
131             String requestClass);
132 
133     /**
134      * Calculates and returns the directory where the file will be written..
135      *
136      * @param packageName The name of the package.
137      * @param suite The template suite.
138      * @param clazz The template class.
139      * @param parameters The map of parameters.
140      * @return The template path.
141      */
142     protected abstract String getDirectoryName(
143             String packageName, TemplateSuite suite, TemplateClass clazz, Map<String, String> parameters, 
144             String runtimeClass, String requestClass);
145     
146 }