001: package org.andromda.core.cartridge;
002:
003: import java.io.File;
004:
005: import java.text.MessageFormat;
006:
007: import org.andromda.core.configuration.NamespaceProperties;
008: import org.andromda.core.configuration.Namespaces;
009: import org.andromda.core.configuration.Property;
010: import org.apache.commons.lang.StringUtils;
011:
012: /**
013: * <p/>
014: * This class implements the <code><resource></code> tag in a cartridge descriptor file and represents the base
015: * cartridge resource element. </p>
016: *
017: * @author Chad Brandon
018: */
019: public class Resource {
020: /**
021: * Stores the output location logical name.
022: */
023: private String outlet;
024:
025: /**
026: * Gets the logical location to which output from this resource will be written.
027: *
028: * @return Returns the outlet.
029: */
030: public String getOutlet() {
031: return outlet;
032: }
033:
034: /**
035: * Sets the logical location to which output from this resource will be written.
036: *
037: * @param outlet The outlet to set.
038: */
039: public void setOutlet(final String outlet) {
040: this .outlet = outlet;
041: }
042:
043: /**
044: * Stores the outputCondition that must evalute to true for the template to be written.
045: */
046: private String outputCondition;
047:
048: /**
049: * Sets the outputCondition that must evaluate to true in order for the template to be written.
050: *
051: * @param outputCondition the template engine outputCondition.
052: */
053: public void setOutputCondition(final String outputCondition) {
054: this .outputCondition = outputCondition;
055: }
056:
057: /**
058: * Gets the outputCondition that must evaluate to true in order for the template to be written.
059: *
060: * @return the template engine outputCondition.
061: */
062: public String getOutputCondition() {
063: return this .outputCondition;
064: }
065:
066: /**
067: * Returns the fully qualified name of the resource output to be written, this means: <ul> <li>the output pattern
068: * has been translated</li> <li>the output directory name has been prepended</li> </ul>
069: *
070: * @param arguments any arguments to be inserted into the MessageFormat style messages.
071: * @param directory the directory to which output will be written.
072: * @param outputPattern if undefined, the value of {@link #getOutputPattern()} will be used.
073: * @return File absolute directory.
074: */
075: public File getOutputLocation(final Object[] arguments,
076: final File directory, String outputPattern) {
077: File file = null;
078:
079: // - clean any whitespace off the arguments
080: if (directory != null && arguments != null
081: && arguments.length > 0) {
082: for (int ctr = 0; ctr < arguments.length; ctr++) {
083: arguments[ctr] = StringUtils.trimToEmpty(String
084: .valueOf(arguments[ctr]));
085: }
086: if (outputPattern == null
087: || outputPattern.trim().length() == 0) {
088: outputPattern = this .getOutputPattern();
089: }
090: String outputFileName;
091: try {
092: outputFileName = MessageFormat.format(outputPattern,
093: arguments);
094: } catch (final Exception exception) {
095: // - means the output pattern can't be parsed (but we still
096: // want to output the bad path anyway)
097: outputFileName = outputPattern;
098: }
099: file = new File(directory, outputFileName);
100: }
101: return file;
102: }
103:
104: /**
105: * Stores whether or not the resource should be overwritten.
106: */
107: private boolean overwrite = false;
108:
109: /**
110: * Tells us whether output files produced by this resource should be overwritten if they already exist. Overwriting
111: * can be turned on and off for entire cartridges by setting the <code>overwrite</code> property in a namespace.
112: * This is useful for cartridge developers when they always want produced resources to be overwritten at first.
113: *
114: * @return Returns the overwrite.
115: */
116: public boolean isOverwrite() {
117: final Property property = Namespaces.instance().getProperty(
118: this .getCartridge().getNamespace(),
119: NamespaceProperties.OVERWRITE, false);
120: if (property != null) {
121: this .overwrite = Boolean.valueOf(property.getValue())
122: .booleanValue();
123: }
124: return this .overwrite;
125: }
126:
127: /**
128: * Sets whether output files produced by this resource should be overwritten if they already exist.
129: *
130: * @param overwrite The overwrite to set.
131: */
132: public void setOverwrite(final boolean overwrite) {
133: this .overwrite = overwrite;
134: }
135:
136: /**
137: * Whether or not a last modified check should be performed before writing the resource.
138: */
139: private boolean lastModifiedCheck;
140:
141: /**
142: * Sets whether or not a last modified check should be performed before writing the resource.
143: *
144: * @param lastModifiedCheck true/false
145: */
146: public void setLastModifiedCheck(final boolean lastModifiedCheck) {
147: this .lastModifiedCheck = lastModifiedCheck;
148: }
149:
150: /**
151: * Whether or not a last modified check should be performed before writing the resource.
152: *
153: * @return true/false
154: */
155: public boolean isLastModifiedCheck() {
156: return this .lastModifiedCheck;
157: }
158:
159: /**
160: * Store the path to a cartridge resource.
161: */
162: private String path;
163:
164: /**
165: * Gets the path to the cartridge resource.
166: *
167: * @return Returns the path.
168: */
169: public String getPath() {
170: return this .path;
171: }
172:
173: /**
174: * Sets the path to the cartridge resource.
175: *
176: * @param path The path to set.
177: */
178: public void setPath(final String path) {
179: this .path = path;
180: }
181:
182: /**
183: * Stores the cartridge that owns this resource.
184: */
185: private Cartridge cartridge;
186:
187: /**
188: * The cartridge that owns this resource.
189: *
190: * @return Returns the owning cartridge.
191: */
192: public Cartridge getCartridge() {
193: return this .cartridge;
194: }
195:
196: /**
197: * Sets the Cartridge parent to which this Resource belongs.
198: *
199: * @param cartridge the parent Cartridge to set.
200: */
201: public void setCartridge(final Cartridge cartridge) {
202: this .cartridge = cartridge;
203: }
204:
205: /**
206: * Stores the output pattern for which the resource(s) should be written.
207: */
208: private String outputPattern;
209:
210: /**
211: * Sets the pattern that is used to build the name of the output file.
212: *
213: * @param outputPattern the pattern in java.text.MessageFormat syntax
214: */
215: public void setOutputPattern(final String outputPattern) {
216: this .outputPattern = outputPattern;
217: }
218:
219: /**
220: * Gets the pattern that is used to build the name of the output file.
221: *
222: * @return String the pattern in java.text.MessageFormat syntax
223: */
224: public String getOutputPattern() {
225: return StringUtils.trimToEmpty(this.outputPattern);
226: }
227: }
|