Search in sources :

Example 1 with RemoteResourcesBundleXpp3Reader

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ArtifactFilterException(org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) InvalidProjectModelException(org.apache.maven.project.InvalidProjectModelException) MalformedURLException(java.net.MalformedURLException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1