in maven2-plugins/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/MakeConfigMojo.java [238:375]
private void generateConfigFromVelocity(Model model,
Properties cachedInfo, long lastModifiedMetadata) throws IOException,
MojoExecutionException
{
Writer writer = null;
File outFile = null;
File tf = new File(templateSourceDirectory, templateFile);
try
{
outFile = new File(outputDirectory, xmlFile);
if ( !outFile.getParentFile().exists() )
{
outFile.getParentFile().mkdirs();
}
if (isCachingEnabled() && outFile.exists())
{
boolean upToDate = isFileUpToDate(cachedInfo, lastModifiedMetadata, outFile);
if (upToDate && xmlBaseFile != null && xmlBaseFile.exists())
{
upToDate = isFileUpToDate(cachedInfo, xmlBaseFile);
}
if (upToDate && tf != null && tf.exists())
{
upToDate = isFileUpToDate(cachedInfo, tf);
}
if (upToDate)
{
getLog().info("generated file " +outFile.getName()+ " is up to date");
return;
}
}
VelocityEngine velocityEngine = initVelocity();
VelocityContext baseContext = new VelocityContext();
baseContext.put("utils", new MyfacesUtils());
String baseContent = "";
if (xmlBaseFile != null && xmlBaseFile.exists())
{
getLog().info("using base content file: "+xmlBaseFile.getPath());
Reader reader = null;
try
{
reader = new FileReader(xmlBaseFile);
Xpp3Dom root = Xpp3DomBuilder.build(reader);
StringWriter swriter = new StringWriter();
Xpp3Dom [] children = root.getChildren();
for (int i = 0; i< children.length; i++)
{
Xpp3Dom dom = children[i];
Xpp3DomWriter.write(swriter, dom);
swriter.write('\n');
}
baseContent = swriter.toString();
swriter.close();
}
catch (XmlPullParserException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
reader.close();
}
}
baseContext.put("baseContent", baseContent);
baseContext.put("model", model);
baseContext.put("modelIds", modelIds);
if (params != null)
{
//Load all parameters to the context, so the template can
//load it. This allow to generate any config file we want
//(faces-config, tld, facelet,....)
for (Iterator it = params.keySet().iterator(); it.hasNext();)
{
String key = (String) it.next();
baseContext.put(key,params.get(key));
}
}
writer = new OutputStreamWriter(new FileOutputStream(outFile));
Template template = velocityEngine.getTemplate(templateFile);
template.merge(baseContext, writer);
writer.flush();
if (isCachingEnabled())
{
cachedInfo.put(outFile.getAbsolutePath(), Long.toString(lastModifiedMetadata));
if (xmlBaseFile != null && xmlBaseFile.exists())
{
cachedInfo.put(xmlBaseFile.getAbsolutePath(), Long.toString(xmlBaseFile.lastModified()));
}
if (tf != null && tf.exists())
{
cachedInfo.put(tf.getAbsolutePath(), Long.toString(tf.lastModified()));
}
}
}
catch (ResourceNotFoundException e)
{
throw new MojoExecutionException(
"Error merging velocity templates: " + e.getMessage(), e);
}
catch (ParseErrorException e)
{
throw new MojoExecutionException(
"Error merging velocity templates: " + e.getMessage(), e);
}
catch (Exception e)
{
throw new MojoExecutionException(
"Error merging velocity templates: " + e.getMessage(), e);
}
finally
{
IOUtil.close(writer);
writer = null;
}
}