1 package org.andromda.core.cartridge;
2
3 import java.io.File;
4
5 import java.text.MessageFormat;
6
7 import org.andromda.core.configuration.NamespaceProperties;
8 import org.andromda.core.configuration.Namespaces;
9 import org.andromda.core.configuration.Property;
10 import org.apache.commons.lang.StringUtils;
11
12
13 /***
14 * <p/>
15 * This class implements the <code><resource></code> tag in a cartridge descriptor file and represents the base
16 * cartridge resource element. </p>
17 *
18 * @author Chad Brandon
19 */
20 public class Resource
21 {
22 /***
23 * Stores the output location logical name.
24 */
25 private String outlet;
26
27 /***
28 * Gets the logical location to which output from this resource will be written.
29 *
30 * @return Returns the outlet.
31 */
32 public String getOutlet()
33 {
34 return outlet;
35 }
36
37 /***
38 * Sets the logical location to which output from this resource will be written.
39 *
40 * @param outlet The outlet to set.
41 */
42 public void setOutlet(final String outlet)
43 {
44 this.outlet = outlet;
45 }
46
47 /***
48 * Stores the outputCondition that must evalute to true for the template to be written.
49 */
50 private String outputCondition;
51
52 /***
53 * Sets the outputCondition that must evaluate to true in order for the template to be written.
54 *
55 * @param outputCondition the template engine outputCondition.
56 */
57 public void setOutputCondition(final String outputCondition)
58 {
59 this.outputCondition = outputCondition;
60 }
61
62 /***
63 * Gets the outputCondition that must evaluate to true in order for the template to be written.
64 *
65 * @return the template engine outputCondition.
66 */
67 public String getOutputCondition()
68 {
69 return this.outputCondition;
70 }
71
72 /***
73 * Returns the fully qualified name of the resource output to be written, this means: <ul> <li>the output pattern
74 * has been translated</li> <li>the output directory name has been prepended</li> </ul>
75 *
76 * @param arguments any arguments to be inserted into the MessageFormat style messages.
77 * @param directory the directory to which output will be written.
78 * @param outputPattern if undefined, the value of {@link #getOutputPattern()} will be used.
79 * @return File absolute directory.
80 */
81 public File getOutputLocation(
82 final Object[] arguments,
83 final File directory,
84 String outputPattern)
85 {
86 File file = null;
87
88
89 if (directory != null && arguments != null && arguments.length > 0)
90 {
91 for (int ctr = 0; ctr < arguments.length; ctr++)
92 {
93 arguments[ctr] = StringUtils.trimToEmpty(String.valueOf(arguments[ctr]));
94 }
95 if (outputPattern == null || outputPattern.trim().length() == 0)
96 {
97 outputPattern = this.getOutputPattern();
98 }
99 String outputFileName;
100 try
101 {
102 outputFileName = MessageFormat.format(
103 outputPattern,
104 arguments);
105 }
106 catch (final Exception exception)
107 {
108
109
110 outputFileName = outputPattern;
111 }
112 file = new File(
113 directory,
114 outputFileName);
115 }
116 return file;
117 }
118
119 /***
120 * Stores whether or not the resource should be overwritten.
121 */
122 private boolean overwrite = false;
123
124 /***
125 * Tells us whether output files produced by this resource should be overwritten if they already exist. Overwriting
126 * can be turned on and off for entire cartridges by setting the <code>overwrite</code> property in a namespace.
127 * This is useful for cartridge developers when they always want produced resources to be overwritten at first.
128 *
129 * @return Returns the overwrite.
130 */
131 public boolean isOverwrite()
132 {
133 final Property property =
134 Namespaces.instance().getProperty(
135 this.getCartridge().getNamespace(),
136 NamespaceProperties.OVERWRITE,
137 false);
138 if (property != null)
139 {
140 this.overwrite = Boolean.valueOf(property.getValue()).booleanValue();
141 }
142 return this.overwrite;
143 }
144
145 /***
146 * Sets whether output files produced by this resource should be overwritten if they already exist.
147 *
148 * @param overwrite The overwrite to set.
149 */
150 public void setOverwrite(final boolean overwrite)
151 {
152 this.overwrite = overwrite;
153 }
154
155 /***
156 * Whether or not a last modified check should be performed before writing the resource.
157 */
158 private boolean lastModifiedCheck;
159
160 /***
161 * Sets whether or not a last modified check should be performed before writing the resource.
162 *
163 * @param lastModifiedCheck true/false
164 */
165 public void setLastModifiedCheck(final boolean lastModifiedCheck)
166 {
167 this.lastModifiedCheck = lastModifiedCheck;
168 }
169
170 /***
171 * Whether or not a last modified check should be performed before writing the resource.
172 *
173 * @return true/false
174 */
175 public boolean isLastModifiedCheck()
176 {
177 return this.lastModifiedCheck;
178 }
179
180 /***
181 * Store the path to a cartridge resource.
182 */
183 private String path;
184
185 /***
186 * Gets the path to the cartridge resource.
187 *
188 * @return Returns the path.
189 */
190 public String getPath()
191 {
192 return this.path;
193 }
194
195 /***
196 * Sets the path to the cartridge resource.
197 *
198 * @param path The path to set.
199 */
200 public void setPath(final String path)
201 {
202 this.path = path;
203 }
204
205 /***
206 * Stores the cartridge that owns this resource.
207 */
208 private Cartridge cartridge;
209
210 /***
211 * The cartridge that owns this resource.
212 *
213 * @return Returns the owning cartridge.
214 */
215 public Cartridge getCartridge()
216 {
217 return this.cartridge;
218 }
219
220 /***
221 * Sets the Cartridge parent to which this Resource belongs.
222 *
223 * @param cartridge the parent Cartridge to set.
224 */
225 public void setCartridge(final Cartridge cartridge)
226 {
227 this.cartridge = cartridge;
228 }
229
230 /***
231 * Stores the output pattern for which the resource(s) should be written.
232 */
233 private String outputPattern;
234
235 /***
236 * Sets the pattern that is used to build the name of the output file.
237 *
238 * @param outputPattern the pattern in java.text.MessageFormat syntax
239 */
240 public void setOutputPattern(final String outputPattern)
241 {
242 this.outputPattern = outputPattern;
243 }
244
245 /***
246 * Gets the pattern that is used to build the name of the output file.
247 *
248 * @return String the pattern in java.text.MessageFormat syntax
249 */
250 public String getOutputPattern()
251 {
252 return StringUtils.trimToEmpty(this.outputPattern);
253 }
254 }