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

1   /*
2    * $Id: AbstractTemplateSuiteGenerator.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.TemplateSuite;
32  import org.apache.tiles.autotag.tool.StringTool;
33  import org.apache.velocity.Template;
34  import org.apache.velocity.VelocityContext;
35  import org.apache.velocity.app.VelocityEngine;
36  import org.apache.velocity.exception.ParseErrorException;
37  import org.apache.velocity.exception.ResourceNotFoundException;
38  
39  /**
40   * A base template suite generator.
41   *
42   * @version $Rev: 1656976 $ $Date: 2015-02-04 03:27:40 +0100 (Wed, 04 Feb 2015) $
43   */
44  public abstract class AbstractTemplateSuiteGenerator implements TemplateSuiteGenerator {
45  
46      /**
47       * The velocity engine.
48       */
49      private VelocityEngine velocityEngine;
50  
51      /**
52       * Constructor.
53       *
54       * @param velocityEngine The Velocity engine.
55       */
56      public AbstractTemplateSuiteGenerator(VelocityEngine velocityEngine) {
57          this.velocityEngine = velocityEngine;
58      }
59  
60      @Override
61      public void generate(OutputLocator outputLocator, String packageName, TemplateSuite suite, Map<String, String> parameters) {
62          String filePath = 
63          		getDirectoryName(packageName, suite, parameters)
64                  + File.separator
65                  + getFilename(packageName, suite, parameters);
66  		if (!outputLocator.isUptodate(filePath)) {
67  	        VelocityContext context = new VelocityContext();
68  	        context.put("packageName", packageName);
69  	        context.put("suite", suite);
70  	        context.put("stringTool", new StringTool());
71  	        context.put("parameters", parameters);
72  	        try {
73  	            Template template = velocityEngine.getTemplate(getTemplatePath(
74  	                    packageName, suite, parameters));
75  	            Writer writer = new OutputStreamWriter(outputLocator.getOutputStream(filePath));
76  	            try {
77  	                template.merge(context, writer);
78  	            } finally {
79  	                writer.close();
80  	            }
81  	        } catch (ResourceNotFoundException e) {
82  	            throw new AutotagRuntimeException("Cannot find template resource", e);
83  	        } catch (ParseErrorException e) {
84  	            throw new AutotagRuntimeException("The template resource is not parseable", e);
85  	        } catch (IOException e) {
86  	            throw new AutotagRuntimeException(
87  	                    "I/O Exception when generating file", e);
88  	        } catch (RuntimeException e) {
89  	            throw e;
90  	        } catch (Exception e) {
91  	            throw new AutotagRuntimeException(
92  	                    "Another generic exception while parsing the template resource",
93  	                    e);
94  	        }
95          }
96      }
97  
98      /**
99       * Calculates and returns the template path.
100      *
101      * @param packageName The name of the package.
102      * @param suite The template suite.
103      * @param parameters The map of parameters.
104      * @return The template path.
105      */
106     protected abstract String getTemplatePath(
107             String packageName, TemplateSuite suite,
108             Map<String, String> parameters);
109 
110     /**
111      * Calculates and returns the filename of the generated file.
112      *
113      * @param packageName The name of the package.
114      * @param suite The template suite.
115      * @param parameters The map of parameters.
116      * @return The template path.
117      */
118     protected abstract String getFilename(String packageName,
119             TemplateSuite suite, Map<String, String> parameters);
120 
121     /**
122      * Calculates and returns the directory where the file will be written..
123      *
124      * @param packageName The name of the package.
125      * @param suite The template suite.
126      * @param parameters The map of parameters.
127      * @return The template path.
128      */
129     protected abstract String getDirectoryName(
130             String packageName, TemplateSuite suite,
131             Map<String, String> parameters);
132 }