final Template template)
{
ExceptionUtils.checkNull(
"template",
template);
final ModelElements modelElements = template.getSupportedModeElements();
if (modelElements != null && !modelElements.isEmpty())
{
try
{
final Collection allMetafacades = modelElements.getAllMetafacades();
// - if outputToSingleFile is true AND outputOnEmptyElements
// is true or we have at least one metafacade in the
// allMetafacades collection, then we collect the template
// model elements and place them into the template context
// by their variable names.
if (template.isOutputToSingleFile() &&
(template.isOutputOnEmptyElements() || !allMetafacades.isEmpty()))
{
final Map templateContext = new LinkedHashMap();
// - first place all relevant model elements by the
// <modelElements/> variable name. If the variable
// isn't defined (which is possible), ignore.
final String modelElementsVariable = modelElements.getVariable();
if (modelElementsVariable != null && modelElementsVariable.trim().length() > 0)
{
templateContext.put(
modelElements.getVariable(),
allMetafacades);
}
// - now place the collections of elements by the given variable names.
// (skip if the variable is NOT defined)
for (final Iterator iterator = modelElements.getModelElements().iterator(); iterator.hasNext();)
{
final ModelElement modelElement = (ModelElement)iterator.next();
final String modelElementVariable = modelElement.getVariable();
if (modelElementVariable != null && modelElementVariable.trim().length() > 0)
{
// - if a modelElement has the same variable defined
// more than one time, then get the existing
// model elements added from the last iteration
// and add the new ones to that collection
Collection metafacades = (Collection)templateContext.get(modelElementVariable);
if (metafacades != null)
{
metafacades.addAll(modelElement.getMetafacades());
}
else
{
metafacades = modelElement.getMetafacades();
templateContext.put(
modelElementVariable,
new LinkedHashSet(metafacades));
}
}
}
this.processWithTemplate(
template,
templateContext,
null,
null);
}
else
{
// - if outputToSingleFile isn't true, then
// we just place the model element with the default
// variable defined on the <modelElements/> into the
// template.
for (final Iterator iterator = allMetafacades.iterator(); iterator.hasNext();)
{
final Map templateContext = new LinkedHashMap();
final Object metafacade = iterator.next();
final ModelAccessFacade model = factory.getModel();
for (final Iterator elements = modelElements.getModelElements().iterator(); elements.hasNext();)
{
final ModelElement modelElement = (ModelElement)elements.next();
String variable = modelElement.getVariable();
// - if the variable isn't defined on the <modelElement/>, try
// the <modelElements/>
if (variable == null || variable.trim().length() == 0)
{
variable = modelElements.getVariable();
}
// - only add the metafacade to the template context if the variable
// is defined (which is possible)
if (variable != null && variable.trim().length() > 0)