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