001: package org.andromda.core.cartridge.template;
002:
003: import java.io.File;
004:
005: import org.andromda.core.cartridge.Resource;
006: import org.andromda.core.metafacade.MetafacadeConstants;
007: import org.apache.commons.lang.StringUtils;
008: import org.apache.commons.lang.builder.ToStringBuilder;
009:
010: /**
011: * This class implements the <code><template></code> tag in a cartridge
012: * descriptor file.
013: *
014: * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
015: * @author Anthony Mowers
016: * @author Chad Brandon
017: */
018: public class Template extends Resource {
019: /**
020: * The default constructor used by the {@link XmlObjectFactory} to instantiate the template configuration.
021: */
022: public Template() {
023: this .supportedModelElements = new ModelElements();
024: }
025:
026: /**
027: * A flag indicating whether or not empty files should
028: * be generated.
029: */
030: private boolean generateEmptyFiles = false;
031:
032: /**
033: * Tells us whether output files should be generated if this template does not produce any output.
034: *
035: * @param generateEmptyFiles generate files for empty output yes/no
036: */
037: public void setGenerateEmptyFiles(final boolean generateEmptyFiles) {
038: this .generateEmptyFiles = generateEmptyFiles;
039: }
040:
041: /**
042: * Tells us whether output files are generated by this template if the template produces empty output.
043: *
044: * @return boolean
045: */
046: public boolean isGenerateEmptyFiles() {
047: return generateEmptyFiles;
048: }
049:
050: /**
051: * Returns the fully qualified output file, this means:
052: * <ul>
053: * <li>the output pattern has been translated</li>
054: * <li>the output dir name has been prepended</li>
055: * </ul>
056: *
057: * @param metafacadeName name of the metafacade.
058: * @param packageName name of the package from the model in which the class
059: * is contained
060: * @param directory the directory as a File.
061: * @param outputPattern if defined, this overrides the value of {@link Resource#getOutputPattern()}.
062: * @return File absolute directory.
063: */
064: public File getOutputLocation(final String metafacadeName,
065: final String packageName, final File directory,
066: String outputPattern) {
067: File file;
068:
069: if (outputPattern == null || outputPattern.trim().length() == 0) {
070: outputPattern = this .getOutputPattern();
071: }
072: // - if singleFileOutput is set to true, then
073: // just use the output pattern as the file to
074: // output to, otherwise we replace using message format.
075: if (this .isOutputToSingleFile()) {
076: file = super .getOutputLocation(
077: new String[] { outputPattern }, directory,
078: outputPattern);
079: } else {
080: file = super
081: .getOutputLocation(
082: new String[] {
083: StringUtils
084: .replace(
085: StringUtils
086: .trimToEmpty(packageName),
087: MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
088: File.separator),
089: metafacadeName }, directory,
090: outputPattern);
091: }
092: return file;
093: }
094:
095: /**
096: * Tells us the model elements that are supported by this template (i.e. will be processed by this template)
097: *
098: * @return ModelElements all the model elements that should be processed by thsi template
099: * @see org.andromda.core.cartridge.template.ModelElements
100: */
101: public ModelElements getSupportedModeElements() {
102: final String methodName = "Template.getModelElements";
103: if (this .supportedModelElements == null) {
104: throw new TemplateException(methodName
105: + " - supportedModelElements is null!");
106: }
107: return this .supportedModelElements;
108: }
109:
110: /**
111: * Sets the model elements that are suported by this template.
112: *
113: * @param supportedModelElements the ModelElements instance.
114: * @see org.andromda.core.cartridge.template.ModelElements
115: */
116: public void setSupportedModelElements(
117: final ModelElements supportedModelElements) {
118: this .supportedModelElements = supportedModelElements;
119: }
120:
121: private boolean outputToSingleFile = false;
122:
123: /**
124: * If output to single file is <code>true</code> then all model elements found by the processor (i.e. all those
125: * having matching modelElements) will aggregated and output to one single file.
126: *
127: * @return Returns the outputToSingleFile.
128: */
129: public boolean isOutputToSingleFile() {
130: return outputToSingleFile;
131: }
132:
133: /**
134: * Sets whether or not we should aggregate elements and output to a single file.
135: *
136: * @param outputToSingleFile The outputToSingleFile to set.
137: */
138: public void setOutputToSingleFile(final boolean outputToSingleFile) {
139: this .outputToSingleFile = outputToSingleFile;
140: }
141:
142: /**
143: * Indicates whether or not files should be output when there are no elements when aggregating.
144: */
145: private boolean outputOnEmptyElements = true;
146:
147: /**
148: * Indicates that when there are no elements in the collection of elements (when {@link #isOutputToSingleFile()} is
149: * <code>true</code>, whether or not the file should be output. Default is <code>true</code>
150: *
151: * @return true/false
152: * @see #isOutputToSingleFile()
153: */
154: public boolean isOutputOnEmptyElements() {
155: return this .outputOnEmptyElements;
156: }
157:
158: /**
159: * Sets whether or not we should output a file when no elements exist in the collection of elements when {@link
160: * #isOutputToSingleFile()} returns <code>true</code>.
161: *
162: * @param outputOnEmptyElements the boolean flag.
163: * @see #isOutputOnEmptyElements()
164: * @see #isOutputToSingleFile()
165: */
166: public void setOutputOnEmptyElements(
167: final boolean outputOnEmptyElements) {
168: this .outputOnEmptyElements = outputOnEmptyElements;
169: }
170:
171: /**
172: * @see java.lang.Object#toString()
173: */
174: public String toString() {
175: return ToStringBuilder.reflectionToString(this );
176: }
177:
178: /**
179: * The model elements (i.e. metafacades) supported by this template.
180: */
181: private ModelElements supportedModelElements = null;
182: }
|