1/*2 * $Id: AbstractTemplateSuiteGenerator.java 1656976 2015-02-04 02:27:40Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.autotag.generate;
2223import java.io.File;
24import java.io.IOException;
25import java.io.OutputStreamWriter;
26import java.io.Writer;
27import java.util.Map;
2829import org.apache.tiles.autotag.core.AutotagRuntimeException;
30import org.apache.tiles.autotag.core.OutputLocator;
31import org.apache.tiles.autotag.model.TemplateSuite;
32import org.apache.tiles.autotag.tool.StringTool;
33import org.apache.velocity.Template;
34import org.apache.velocity.VelocityContext;
35import org.apache.velocity.app.VelocityEngine;
36import org.apache.velocity.exception.ParseErrorException;
37import org.apache.velocity.exception.ResourceNotFoundException;
3839/**40 * A base template suite generator.41 *42 * @version $Rev: 1656976 $ $Date: 2015-02-04 03:27:40 +0100 (Wed, 04 Feb 2015) $43 */44publicabstractclassAbstractTemplateSuiteGeneratorimplementsTemplateSuiteGenerator {
4546/**47 * The velocity engine.48 */49private VelocityEngine velocityEngine;
5051/**52 * Constructor.53 *54 * @param velocityEngine The Velocity engine.55 */56publicAbstractTemplateSuiteGenerator(VelocityEngine velocityEngine) {
57this.velocityEngine = velocityEngine;
58 }
5960 @Override
61publicvoid 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);
66if (!outputLocator.isUptodate(filePath)) {
67 VelocityContext context = new VelocityContext();
68 context.put("packageName", packageName);
69 context.put("suite", suite);
70 context.put("stringTool", newStringTool());
71 context.put("parameters", parameters);
72try {
73 Template template = velocityEngine.getTemplate(getTemplatePath(
74 packageName, suite, parameters));
75 Writer writer = new OutputStreamWriter(outputLocator.getOutputStream(filePath));
76try {
77 template.merge(context, writer);
78 } finally {
79 writer.close();
80 }
81 } catch (ResourceNotFoundException e) {
82thrownew AutotagRuntimeException("Cannot find template resource", e);
83 } catch (ParseErrorException e) {
84thrownew AutotagRuntimeException("The template resource is not parseable", e);
85 } catch (IOException e) {
86thrownew AutotagRuntimeException(
87"I/O Exception when generating file", e);
88 } catch (RuntimeException e) {
89throw e;
90 } catch (Exception e) {
91thrownew AutotagRuntimeException(
92"Another generic exception while parsing the template resource",
93 e);
94 }
95 }
96 }
9798/**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 */106protectedabstract String getTemplatePath(
107 String packageName, TemplateSuite suite,
108 Map<String, String> parameters);
109110/**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 */118protectedabstract String getFilename(String packageName,
119TemplateSuite suite, Map<String, String> parameters);
120121/**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 */129protectedabstract String getDirectoryName(
130 String packageName, TemplateSuite suite,
131 Map<String, String> parameters);
132 }