View Javadoc
1   package org.andromda.core.cartridge.template;
2   
3   import java.io.File;
4   import org.andromda.core.cartridge.Resource;
5   import org.andromda.core.common.XmlObjectFactory;
6   import org.andromda.core.metafacade.MetafacadeConstants;
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * This class implements the <code>&lt;template&gt;</code> tag in a cartridge
11   * descriptor file.
12   *
13   * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
14   * @author Anthony Mowers
15   * @author Chad Brandon
16   * @author Bob Fields
17   */
18  public class Template
19      extends Resource
20  {
21      /**
22       * The default constructor used by the {@link XmlObjectFactory} to instantiate the template configuration.
23       */
24      public Template()
25      {
26          this.supportedModelElements = new ModelElements();
27      }
28  
29      /**
30       * A flag indicating whether or not empty files should
31       * be generated.
32       */
33      private boolean generateEmptyFiles = false;
34  
35      /**
36       * Tells us whether output files should be generated if this template does not produce any output.
37       *
38       * @param generateEmptyFiles generate files for empty output yes/no
39       */
40      public void setGenerateEmptyFiles(final boolean generateEmptyFiles)
41      {
42          this.generateEmptyFiles = generateEmptyFiles;
43      }
44  
45      /**
46       * Tells us whether output files are generated by this template if the template produces empty output.
47       *
48       * @return boolean
49       */
50      public boolean isGenerateEmptyFiles()
51      {
52          return generateEmptyFiles;
53      }
54  
55      /**
56       * Returns the fully qualified output file, this means:
57       * <ul>
58       * <li>the output pattern has been translated</li>
59       * <li>the output dir name has been prepended</li>
60       * </ul>
61       *
62       * @param metafacadeName name of the metafacade.
63       * @param packageName name of the package from the model in which the class
64       *        is contained
65       * @param directory the directory as a File.
66       * @param outputPattern if defined, this overrides the value of {@link Resource#getOutputPattern()}.
67       * @return File absolute directory.
68       */
69      public File getOutputLocation(
70          final String metafacadeName,
71          final String packageName,
72          final File directory,
73          String outputPattern)
74      {
75          File file;
76  
77          if (StringUtils.isBlank(outputPattern))
78          {
79              outputPattern = this.getOutputPattern();
80          }
81          // - if singleFileOutput is set to true, then
82          //   just use the output pattern as the file to
83          //   output to, otherwise we replace using message format.
84          if (this.isOutputToSingleFile())
85          {
86              file = super.getOutputLocation(
87                      new String[] {outputPattern},
88                      directory,
89                      outputPattern);
90          }
91          else
92          {
93              file =
94                  super.getOutputLocation(
95                      new String[]
96                      {
97                          StringUtils.replace(
98                              StringUtils.trimToEmpty(packageName),
99                              MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
100                             File.separator), metafacadeName
101                     },
102                     directory,
103                     outputPattern);
104         }
105         return file;
106     }
107 
108     /**
109      * Tells us the model elements that are supported by this template (i.e. will be processed by this template)
110      *
111      * @return ModelElements all the model elements that should be processed by thsi template
112      * @see org.andromda.core.cartridge.template.ModelElements
113      */
114     public ModelElements getSupportedModeElements()
115     {
116         final String methodName = "Template.getModelElements";
117         if (this.supportedModelElements == null)
118         {
119             throw new TemplateException(methodName + " - supportedModelElements is null!");
120         }
121         return this.supportedModelElements;
122     }
123 
124     /**
125      * Sets the model elements that are supported by this template.
126      *
127      * @param supportedModelElements the ModelElements instance.
128      * @see org.andromda.core.cartridge.template.ModelElements
129      */
130     public void setSupportedModelElements(final ModelElements supportedModelElements)
131     {
132         this.supportedModelElements = supportedModelElements;
133     }
134 
135     private boolean outputToSingleFile = false;
136 
137     /**
138      * If output to single file is <code>true</code> then all model elements found by the processor (i.e. all those
139      * having matching modelElements) will aggregated and output to one single file.
140      *
141      * @return Returns the outputToSingleFile.
142      */
143     public boolean isOutputToSingleFile()
144     {
145         return outputToSingleFile;
146     }
147 
148     /**
149      * Sets whether or not we should aggregate elements and output to a single file.
150      *
151      * @param outputToSingleFile The outputToSingleFile to set.
152      */
153     public void setOutputToSingleFile(final boolean outputToSingleFile)
154     {
155         this.outputToSingleFile = outputToSingleFile;
156     }
157 
158     /**
159      * Indicates whether or not files should be output when there are no elements when aggregating.
160      */
161     private boolean outputOnEmptyElements = true;
162 
163     /**
164      * Indicates that when there are no elements in the collection of elements (when {@link #isOutputToSingleFile()} is
165      * <code>true</code>, whether or not the file should be output.  Default is <code>true</code>
166      *
167      * @return true/false
168      * @see #isOutputToSingleFile()
169      */
170     public boolean isOutputOnEmptyElements()
171     {
172         return this.outputOnEmptyElements;
173     }
174 
175     /**
176      * Sets whether or not we should output a file when no elements exist in the collection of elements when {@link
177      * #isOutputToSingleFile()} returns <code>true</code>.
178      *
179      * @param outputOnEmptyElements the boolean flag.
180      * @see #isOutputOnEmptyElements()
181      * @see #isOutputToSingleFile()
182      */
183     public void setOutputOnEmptyElements(final boolean outputOnEmptyElements)
184     {
185         this.outputOnEmptyElements = outputOnEmptyElements;
186     }
187 
188     /**
189      * @see Object#toString()
190      */
191     @Override
192     public String toString()
193     {
194         StringBuilder builder = new StringBuilder();
195         builder.append(super.toString());
196         builder.append(" [generateEmptyFiles=").append(this.generateEmptyFiles);
197         builder.append(", outputToSingleFile=").append(this.outputToSingleFile);
198         builder.append(", outputOnEmptyElements=").append(this.outputOnEmptyElements);
199         builder.append(", supportedModelElements=").append(this.supportedModelElements);
200         builder.append("]");
201         return builder.toString();
202     }
203 
204     /**
205      * The model elements (i.e. metafacades) supported by this template.
206      */
207     private ModelElements supportedModelElements = null;
208 }