1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public abstract class AbstractTemplateSuiteGenerator implements TemplateSuiteGenerator { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private VelocityEngine velocityEngine; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 6 | public AbstractTemplateSuiteGenerator(VelocityEngine velocityEngine) { |
57 | 6 | this.velocityEngine = velocityEngine; |
58 | 6 | } |
59 | |
|
60 | |
@Override |
61 | |
public void generate(OutputLocator outputLocator, String packageName, TemplateSuite suite, Map<String, String> parameters) { |
62 | 6 | String filePath = |
63 | |
getDirectoryName(packageName, suite, parameters) |
64 | |
+ File.separator |
65 | |
+ getFilename(packageName, suite, parameters); |
66 | 6 | if (!outputLocator.isUptodate(filePath)) { |
67 | 6 | VelocityContext context = new VelocityContext(); |
68 | 6 | context.put("packageName", packageName); |
69 | 6 | context.put("suite", suite); |
70 | 6 | context.put("stringTool", new StringTool()); |
71 | 6 | context.put("parameters", parameters); |
72 | |
try { |
73 | 6 | Template template = velocityEngine.getTemplate(getTemplatePath( |
74 | |
packageName, suite, parameters)); |
75 | 3 | Writer writer = new OutputStreamWriter(outputLocator.getOutputStream(filePath)); |
76 | |
try { |
77 | 3 | template.merge(context, writer); |
78 | |
} finally { |
79 | 3 | writer.close(); |
80 | 1 | } |
81 | 1 | } catch (ResourceNotFoundException e) { |
82 | 1 | throw new AutotagRuntimeException("Cannot find template resource", e); |
83 | 1 | } catch (ParseErrorException e) { |
84 | 1 | throw new AutotagRuntimeException("The template resource is not parseable", e); |
85 | 1 | } catch (IOException e) { |
86 | 1 | throw new AutotagRuntimeException( |
87 | |
"I/O Exception when generating file", e); |
88 | 1 | } catch (RuntimeException e) { |
89 | 1 | throw e; |
90 | 1 | } catch (Exception e) { |
91 | 1 | throw new AutotagRuntimeException( |
92 | |
"Another generic exception while parsing the template resource", |
93 | |
e); |
94 | 1 | } |
95 | |
} |
96 | 1 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected abstract String getTemplatePath( |
107 | |
String packageName, TemplateSuite suite, |
108 | |
Map<String, String> parameters); |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected abstract String getFilename(String packageName, |
119 | |
TemplateSuite suite, Map<String, String> parameters); |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected abstract String getDirectoryName( |
130 | |
String packageName, TemplateSuite suite, |
131 | |
Map<String, String> parameters); |
132 | |
} |