View Javadoc

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