1 package org.andromda.cartridges.interfaces;
2
3 import java.io.File;
4 import java.text.MessageFormat;
5
6 /***
7 * This class implements the <code><template></code> tag
8 * in a cartridge descriptor file.
9 *
10 * @author <a href="http://www.mbohlen.de">Matthias Bohlen</a>
11 * @author Anthony Mowers
12 */
13 public class TemplateConfiguration
14 {
15 private ICartridgeDescriptor cartridgeDescriptor;
16
17 /***
18 * Constructor which is used to build default
19 * template configurations when initializing the AndroMDAGenTask.
20 *
21 * @param stereotype the name of the stereotype
22 * @param sheet name of the style sheet file
23 * @param outputPattern the pattern to build the output file name
24 * @param outlet the output directory
25 * @param overwrite yes/no whether output file should be overwritten
26 * @param generateEmptyFiles yes/no whether empty output files should be
27 * produced
28 */
29 public TemplateConfiguration(
30 ICartridgeDescriptor cartridgeDescriptor,
31 String stereotype,
32 String sheet,
33 String outputPattern,
34 String outlet,
35 boolean overwrite,
36 boolean generateEmptyFiles)
37 {
38 this.cartridgeDescriptor = cartridgeDescriptor;
39 this.stereotype = stereotype;
40 this.sheet = sheet;
41 this.outputPattern = outputPattern;
42 this.outlet = outlet;
43 this.overwrite = overwrite;
44 this.generateEmptyFiles = generateEmptyFiles;
45 }
46
47 /***
48 * Sets the class name of object that the
49 * template code generation scripts will use
50 * to access the object model. The class must implement
51 * the ScriptHelper interface.
52 *
53 * <p> This is an optional parameter and if it is not set
54 * it defaults to the default transform class or the
55 * one which was configured using the
56 * <code><repository></code> tag. </p>
57 *
58 * <p> By writing ones own transformer class some of the
59 * more complicated code generation logic can be moved from
60 * the code generation script and into the transformer
61 * implementation. </p>
62 *
63 * @see org.andromda.core.common.ScriptHelper
64 *
65 * @param scriptHelperClassName
66 */
67 public void setTransformClassname(String scriptHelperClassName)
68 throws ClassNotFoundException
69 {
70 transformClass = Class.forName(scriptHelperClassName);
71 }
72
73 /***
74 * Returns the class of the transform object
75 * that will be used to the code generation templates
76 * to access the object model.
77 *
78 * @return Class
79 */
80 public Class getTransformClass()
81 {
82 return transformClass;
83 }
84
85 /***
86 * Tells us the stereotype in the UML model that
87 * should drive code generation with this template.
88 * @param stereotype the name of the stereotype
89 */
90 public void setStereotype(String stereotype)
91 {
92 this.stereotype = stereotype;
93 }
94
95 /***
96 * Tells us the stereotype in the UML model that
97 * should drive code generation with this template.
98 * @return String the name of the stereotype
99 */
100 public String getStereotype()
101 {
102 return stereotype;
103 }
104
105 /***
106 * Tells us which Velocity stylesheet to use as a template.
107 * @param sheet points to the script
108 */
109 public void setSheet(String sheet)
110 {
111 this.sheet = sheet;
112 }
113
114 /***
115 * Tells us which Velocity stylesheet to use as a template.
116 * @return File points to the script
117 */
118 public String getSheet()
119 {
120 return sheet;
121 }
122
123 /***
124 * Sets the pattern that is used to build the
125 * name of the output file.
126 * @param outputPattern the pattern in java.text.MessageFormat syntax
127 */
128 public void setOutputPattern(String outputPattern)
129 {
130 this.outputPattern = outputPattern;
131 }
132
133 /***
134 * Gets the pattern that is used to build the
135 * name of the output file.
136 * @return String the pattern in java.text.MessageFormat syntax
137 */
138 public String getOutputPattern()
139 {
140 return outputPattern;
141 }
142
143 /***
144 * Sets the outlet where the output file that is generated from this
145 * template should be placed,
146 * @param outlet points to the outlet
147 */
148 public void setOutlet(String outlet)
149 {
150 this.outlet = outlet;
151 }
152
153 /***
154 * Gets the outlet where the output file that is generated from this
155 * template should be placed.
156 * @return String the outlet alias name
157 */
158 public String getOutlet()
159 {
160 return outlet;
161 }
162
163 /***
164 * Tells us whether output files generated by this
165 * template should be overwritten if they already exist.
166 * @param overwrite overwrite the file yes/no
167 */
168 public void setOverwrite(boolean overwrite)
169 {
170 this.overwrite = overwrite;
171 }
172
173 /***
174 * Tells us whether output files generated by this
175 * template should be overwritten if they already exist.
176 * @return boolean
177 */
178 public boolean isOverwrite()
179 {
180 return overwrite;
181 }
182
183 /***
184 * Tells us whether output files should be generated if this
185 * template does not produce any output.
186 * @param generateEmptyFiles generate files for empty output yes/no
187 */
188 public void setGenerateEmptyFiles(boolean generateEmptyFiles)
189 {
190 this.generateEmptyFiles = generateEmptyFiles;
191 }
192
193 /***
194 * Tells us whether output files are generated by this
195 * template if the template produces empty output.
196 * @return boolean
197 */
198 public boolean isGenerateEmptyFiles()
199 {
200 return generateEmptyFiles;
201 }
202
203 /***
204 * Returns the fully qualified output file, that means:
205 * <ul>
206 * <li>the output pattern has been translated</li>
207 * <li>the output dir name has been prepended</li>
208 * </ul>
209 *
210 * @param inputClassName name of the class from the UML model
211 * @param inputPackageName name of the package from the UML model
212 * in which the class is contained
213 * @param oldict the dictionary where outlet names can be resolved to
214 * physical directories
215 * @return File absolute file
216 */
217 public File getFullyQualifiedOutputFile(
218 String inputClassName,
219 String inputPackageName,
220 OutletDictionary oldict)
221 {
222 int dotIndex = sheet.indexOf(".");
223 String sheetBaseName = sheet.substring(0, dotIndex);
224
225 Object[] arguments =
226 {
227 inputPackageName.replace('.', File.separatorChar),
228 inputClassName,
229 sheetBaseName,
230 getStereotype()};
231
232 String outputFileName =
233 MessageFormat.format(outputPattern, arguments);
234
235 File physDir = oldict.lookupOutlet(
236 cartridgeDescriptor.getCartridgeName(),
237 outlet);
238
239 return physDir == null ? null : new File(physDir, outputFileName);
240 }
241
242 /***
243 * Just for debugging.
244 * @see java.lang.Object#toString()
245 */
246 public String toString()
247 {
248 return "TemplateConfiguration: "
249 + stereotype
250 + " "
251 + sheet
252 + " "
253 + outputPattern
254 + " "
255 + outlet
256 + " "
257 + overwrite
258 + " "
259 + generateEmptyFiles;
260 }
261
262 private String stereotype;
263 private String sheet;
264 private String outputPattern;
265 private String outlet;
266 private boolean overwrite;
267 private boolean generateEmptyFiles;
268 private Class transformClass;
269
270 }