1 package org.apache.torque.mojo;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.apache.commons.configuration.ConfigurationException;
8 import org.apache.commons.configuration.FileConfiguration;
9 import org.apache.commons.configuration.PropertiesConfiguration;
10 import org.apache.maven.plugin.MojoExecutionException;
11 import org.apache.texen.ant.TexenTask;
12
13
14
15
16 public abstract class TexenTaskMojo extends AntTaskMojo {
17
18
19
20
21
22 private String outputDir;
23
24
25
26
27
28
29 private String templatePath;
30
31
32
33
34
35
36 private boolean useClasspath;
37
38
39
40
41
42
43
44 private Map<?, ?> userContextProperties;
45
46
47
48
49
50
51 private String contextPropertiesPath;
52
53
54
55
56
57
58
59 public void setOutputDir(String outputDir) {
60 this.outputDir = outputDir;
61 }
62
63
64
65
66
67
68 public String getOutputDir() {
69 return this.outputDir;
70 }
71
72
73
74
75
76
77
78 public void setTemplatePath(String templatePath) {
79 this.templatePath = templatePath;
80 }
81
82
83
84
85
86
87 public String getTemplatePath() {
88 return this.templatePath;
89 }
90
91
92
93
94
95
96
97 public void setUseClasspath(boolean useClasspath) {
98 this.useClasspath = useClasspath;
99 }
100
101
102
103
104
105
106 public boolean getUseClasspath() {
107 return this.useClasspath;
108 }
109
110
111
112
113
114
115
116 public void setContextPropertiesPath(String contextPropertiesPath) {
117 this.contextPropertiesPath = contextPropertiesPath;
118 }
119
120
121
122
123
124
125 public String getContextPropertiesPath() {
126 return this.contextPropertiesPath;
127 }
128
129
130
131
132
133
134
135
136 public void setUserContextProperties(Map<?, ?> userContextProperties) {
137 this.userContextProperties = userContextProperties;
138 }
139
140
141
142
143
144
145 public Map<?, ?> getUserContextProperties() {
146 return userContextProperties;
147 }
148
149
150
151
152
153
154 protected TexenTask getGeneratorTask() {
155 return (TexenTask) getAntTask();
156 }
157
158
159
160
161
162
163
164 protected void generateContextProperties() throws MojoExecutionException {
165 try {
166 FileConfiguration configuration = getMojoContextProperties();
167 if (userContextProperties != null) {
168 for (Iterator<?> contextPropertyIt = userContextProperties.entrySet().iterator(); contextPropertyIt.hasNext();) {
169 Map.Entry<?, ?> entry = (Map.Entry<?, ?>) contextPropertyIt.next();
170 configuration.addProperty(entry.getKey().toString(), entry.getValue().toString());
171 }
172 }
173 configuration.save(contextPropertiesPath);
174 } catch (ConfigurationException e) {
175 getLog().error("Error writing temporary context properties: " + e.getMessage());
176 throw new MojoExecutionException(e.getMessage());
177 }
178 }
179
180
181
182
183 protected void configureTask() throws MojoExecutionException {
184 super.configureTask();
185 TexenTask task = getGeneratorTask();
186 task.setContextProperties(contextPropertiesPath);
187 task.setUseClasspath(useClasspath);
188 try {
189 task.setTemplatePath(templatePath);
190 } catch (Exception e) {
191 throw new MojoExecutionException("Error setting template path", e);
192 }
193
194 File outputDirectory = new File(outputDir);
195 outputDirectory.mkdirs();
196 getGeneratorTask().setOutputDirectory(outputDirectory);
197 }
198
199
200
201
202
203
204
205
206
207
208 public void executeMojo() throws MojoExecutionException {
209 generateContextProperties();
210 super.executeMojo();
211 }
212
213
214
215
216
217
218
219 protected abstract PropertiesConfiguration getMojoContextProperties();
220 }