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.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 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public abstract class AbstractTemplateClassGenerator implements |
46 | |
TemplateClassGenerator { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private VelocityEngine velocityEngine; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 6 | public AbstractTemplateClassGenerator(VelocityEngine velocityEngine) { |
59 | 6 | this.velocityEngine = velocityEngine; |
60 | 6 | } |
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 | 6 | String filePath = |
67 | |
getDirectoryName(packageName, suite, clazz, parameters, runtimeClass, requestClass) |
68 | |
+ File.separator |
69 | |
+ getFilename(packageName, suite, clazz, parameters, runtimeClass, requestClass); |
70 | 6 | if (!outputLocator.isUptodate(filePath)) { |
71 | 6 | VelocityContext context = new VelocityContext(); |
72 | 6 | context.put("packageName", packageName); |
73 | 6 | context.put("suite", suite); |
74 | 6 | context.put("clazz", clazz); |
75 | 6 | context.put("stringTool", new StringTool()); |
76 | 6 | context.put("parameters", parameters); |
77 | 6 | context.put("runtimeClass", runtimeClass); |
78 | 6 | context.put("requestClass", requestClass); |
79 | |
try { |
80 | 6 | Template template = velocityEngine.getTemplate(getTemplatePath( |
81 | |
packageName, suite, clazz, parameters, runtimeClass, requestClass)); |
82 | 3 | Writer writer = new OutputStreamWriter(outputLocator.getOutputStream(filePath)); |
83 | |
try { |
84 | 3 | template.merge(context, writer); |
85 | |
} finally { |
86 | 3 | writer.close(); |
87 | 1 | } |
88 | 1 | } catch (ResourceNotFoundException e) { |
89 | 1 | throw new AutotagRuntimeException("Cannot find template resource", |
90 | |
e); |
91 | 1 | } catch (ParseErrorException e) { |
92 | 1 | throw new AutotagRuntimeException( |
93 | |
"The template resource is not parseable", e); |
94 | 1 | } catch (IOException e) { |
95 | 1 | throw new AutotagRuntimeException( |
96 | |
"I/O Exception when generating file", e); |
97 | 1 | } catch (RuntimeException e) { |
98 | 1 | throw e; |
99 | 1 | } catch (Exception e) { |
100 | 1 | throw new AutotagRuntimeException( |
101 | |
"Another generic exception while parsing the template resource", |
102 | |
e); |
103 | 1 | } |
104 | |
} |
105 | 1 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
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 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
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 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected abstract String getDirectoryName( |
143 | |
String packageName, TemplateSuite suite, TemplateClass clazz, Map<String, String> parameters, |
144 | |
String runtimeClass, String requestClass); |
145 | |
|
146 | |
} |