use of org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader in project maven-plugins by apache.
the class ProcessRemoteResourcesMojo method processResourceBundles.
protected void processResourceBundles(RemoteResourcesClassLoader classLoader, VelocityContext context) throws MojoExecutionException {
try {
// CHECKSTYLE_OFF: LineLength
for (Enumeration<URL> e = classLoader.getResources(BundleRemoteResourcesMojo.RESOURCES_MANIFEST); e.hasMoreElements(); ) {
URL url = e.nextElement();
InputStream in = null;
OutputStream out = null;
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(url.openStream());
RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();
RemoteResourcesBundle bundle = bundleReader.read(reader);
reader.close();
reader = null;
for (String bundleResource : bundle.getRemoteResources()) {
String projectResource = bundleResource;
boolean doVelocity = false;
if (projectResource.endsWith(TEMPLATE_SUFFIX)) {
projectResource = projectResource.substring(0, projectResource.length() - 3);
doVelocity = true;
}
// Don't overwrite resource that are already being provided.
File f = new File(outputDirectory, projectResource);
FileUtils.mkdir(f.getParentFile().getAbsolutePath());
if (!copyResourceIfExists(f, projectResource, context)) {
if (doVelocity) {
DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, f);
writer = bundle.getSourceEncoding() == null ? new OutputStreamWriter(os) : new OutputStreamWriter(os, bundle.getSourceEncoding());
if (bundle.getSourceEncoding() == null) {
// TODO: Is this correct? Shouldn't we behave like the rest of maven and fail
// down to JVM default instead ISO-8859-1 ?
velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer);
} else {
velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer);
}
writer.close();
writer = null;
fileWriteIfDiffers(os);
} else {
URL resUrl = classLoader.getResource(bundleResource);
if (resUrl != null) {
FileUtils.copyURLToFile(resUrl, f);
}
}
File appendedResourceFile = new File(appendedResourcesDirectory, projectResource);
File appendedVmResourceFile = new File(appendedResourcesDirectory, projectResource + ".vm");
if (appendedResourceFile.exists()) {
in = new FileInputStream(appendedResourceFile);
out = new FileOutputStream(f, true);
IOUtil.copy(in, out);
out.close();
out = null;
in.close();
in = null;
} else if (appendedVmResourceFile.exists()) {
reader = new FileReader(appendedVmResourceFile);
if (bundle.getSourceEncoding() == null) {
writer = new PrintWriter(new FileWriter(f, true));
} else {
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f, true), bundle.getSourceEncoding()));
}
Velocity.init();
Velocity.evaluate(context, writer, "remote-resources", reader);
writer.close();
writer = null;
reader.close();
reader = null;
}
}
}
} finally {
IOUtil.close(out);
IOUtil.close(in);
IOUtil.close(writer);
IOUtil.close(reader);
}
// CHECKSTYLE_ON: LineLength
}
} catch (IOException e) {
throw new MojoExecutionException("Error finding remote resources manifests", e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Error parsing remote resource bundle descriptor.", e);
} catch (Exception e) {
throw new MojoExecutionException("Error rendering velocity resource.", e);
}
}
Aggregations