Template.java
package org.andromda.core.cartridge.template;
import java.io.File;
import org.andromda.core.cartridge.Resource;
import org.andromda.core.common.XmlObjectFactory;
import org.andromda.core.metafacade.MetafacadeConstants;
import org.apache.commons.lang.StringUtils;
/**
* This class implements the <code><template></code> tag in a cartridge
* descriptor file.
*
* @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
* @author Anthony Mowers
* @author Chad Brandon
* @author Bob Fields
*/
public class Template
extends Resource
{
/**
* The default constructor used by the {@link XmlObjectFactory} to instantiate the template configuration.
*/
public Template()
{
this.supportedModelElements = new ModelElements();
}
/**
* A flag indicating whether or not empty files should
* be generated.
*/
private boolean generateEmptyFiles = false;
/**
* Tells us whether output files should be generated if this template does not produce any output.
*
* @param generateEmptyFiles generate files for empty output yes/no
*/
public void setGenerateEmptyFiles(final boolean generateEmptyFiles)
{
this.generateEmptyFiles = generateEmptyFiles;
}
/**
* Tells us whether output files are generated by this template if the template produces empty output.
*
* @return boolean
*/
public boolean isGenerateEmptyFiles()
{
return generateEmptyFiles;
}
/**
* Returns the fully qualified output file, this means:
* <ul>
* <li>the output pattern has been translated</li>
* <li>the output dir name has been prepended</li>
* </ul>
*
* @param metafacadeName name of the metafacade.
* @param packageName name of the package from the model in which the class
* is contained
* @param directory the directory as a File.
* @param outputPattern if defined, this overrides the value of {@link Resource#getOutputPattern()}.
* @return File absolute directory.
*/
public File getOutputLocation(
final String metafacadeName,
final String packageName,
final File directory,
String outputPattern)
{
File file;
if (StringUtils.isBlank(outputPattern))
{
outputPattern = this.getOutputPattern();
}
// - if singleFileOutput is set to true, then
// just use the output pattern as the file to
// output to, otherwise we replace using message format.
if (this.isOutputToSingleFile())
{
file = super.getOutputLocation(
new String[] {outputPattern},
directory,
outputPattern);
}
else
{
file =
super.getOutputLocation(
new String[]
{
StringUtils.replace(
StringUtils.trimToEmpty(packageName),
MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
File.separator), metafacadeName
},
directory,
outputPattern);
}
return file;
}
/**
* Tells us the model elements that are supported by this template (i.e. will be processed by this template)
*
* @return ModelElements all the model elements that should be processed by thsi template
* @see org.andromda.core.cartridge.template.ModelElements
*/
public ModelElements getSupportedModeElements()
{
final String methodName = "Template.getModelElements";
if (this.supportedModelElements == null)
{
throw new TemplateException(methodName + " - supportedModelElements is null!");
}
return this.supportedModelElements;
}
/**
* Sets the model elements that are supported by this template.
*
* @param supportedModelElements the ModelElements instance.
* @see org.andromda.core.cartridge.template.ModelElements
*/
public void setSupportedModelElements(final ModelElements supportedModelElements)
{
this.supportedModelElements = supportedModelElements;
}
private boolean outputToSingleFile = false;
/**
* If output to single file is <code>true</code> then all model elements found by the processor (i.e. all those
* having matching modelElements) will aggregated and output to one single file.
*
* @return Returns the outputToSingleFile.
*/
public boolean isOutputToSingleFile()
{
return outputToSingleFile;
}
/**
* Sets whether or not we should aggregate elements and output to a single file.
*
* @param outputToSingleFile The outputToSingleFile to set.
*/
public void setOutputToSingleFile(final boolean outputToSingleFile)
{
this.outputToSingleFile = outputToSingleFile;
}
/**
* Indicates whether or not files should be output when there are no elements when aggregating.
*/
private boolean outputOnEmptyElements = true;
/**
* Indicates that when there are no elements in the collection of elements (when {@link #isOutputToSingleFile()} is
* <code>true</code>, whether or not the file should be output. Default is <code>true</code>
*
* @return true/false
* @see #isOutputToSingleFile()
*/
public boolean isOutputOnEmptyElements()
{
return this.outputOnEmptyElements;
}
/**
* Sets whether or not we should output a file when no elements exist in the collection of elements when {@link
* #isOutputToSingleFile()} returns <code>true</code>.
*
* @param outputOnEmptyElements the boolean flag.
* @see #isOutputOnEmptyElements()
* @see #isOutputToSingleFile()
*/
public void setOutputOnEmptyElements(final boolean outputOnEmptyElements)
{
this.outputOnEmptyElements = outputOnEmptyElements;
}
/**
* @see Object#toString()
*/
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append(super.toString());
builder.append(" [generateEmptyFiles=").append(this.generateEmptyFiles);
builder.append(", outputToSingleFile=").append(this.outputToSingleFile);
builder.append(", outputOnEmptyElements=").append(this.outputOnEmptyElements);
builder.append(", supportedModelElements=").append(this.supportedModelElements);
builder.append("]");
return builder.toString();
}
/**
* The model elements (i.e. metafacades) supported by this template.
*/
private ModelElements supportedModelElements = null;
}