You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2021/01/24 14:32:07 UTC
[maven-checkstyle-plugin] 01/01: use try with resources and charset
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch dep
in repository https://gitbox.apache.org/repos/asf/maven-checkstyle-plugin.git
commit c77ec8e9c563d1db351360d93f667e5873128bb1
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Sun Jan 24 09:31:52 2021 -0500
use try with resources and charset
---
.../plugins/checkstyle/rss/VelocityTemplate.java | 50 ++++++++--------------
1 file changed, 17 insertions(+), 33 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/rss/VelocityTemplate.java b/src/main/java/org/apache/maven/plugins/checkstyle/rss/VelocityTemplate.java
index a25090b..329ceb7 100644
--- a/src/main/java/org/apache/maven/plugins/checkstyle/rss/VelocityTemplate.java
+++ b/src/main/java/org/apache/maven/plugins/checkstyle/rss/VelocityTemplate.java
@@ -28,13 +28,15 @@ import org.apache.velocity.exception.VelocityException;
import org.codehaus.plexus.velocity.VelocityComponent;
import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.nio.charset.StandardCharsets;
/**
* <p>
- * A component to work with VelocityTemplates from within plugins.
+ * A component to work with Velocity templates from within plugins.
* <p>
* You will need to reference the velocity component as a parameter
* in your plugin. Like this:
@@ -77,52 +79,34 @@ public class VelocityTemplate
* Using a specified Velocity Template and provided context, create the outputFilename.
*
* @param outputFilename the file to be generated.
- * @param template the velocity template to use.
- * @param context the velocity context map.
- * @throws VelocityException if the template was not found or any other Velocity exception.
- * @throws MojoExecutionException if merging the velocity template failed.
- * @throws IOException if there was an error when writing to the output file.
+ * @param template the velocity template to use
+ * @param context the velocity context map
+ * @throws VelocityException if the template was not found or any other Velocity exception
+ * @throws MojoExecutionException if merging the velocity template failed
+ * @throws IOException if there was an error writing to the output file
*/
public void generate( String outputFilename, String template, Context context )
throws VelocityException, MojoExecutionException, IOException
{
- Writer writer = null;
- try
+ File f = new File( outputFilename );
+ if ( !f.getParentFile().exists() )
+ {
+ f.getParentFile().mkdirs();
+ }
+
+ try ( Writer writer = new OutputStreamWriter( new FileOutputStream( f ), StandardCharsets.UTF_8 ) )
{
- File f = new File( outputFilename );
-
- if ( !f.getParentFile().exists() )
- {
- f.getParentFile().mkdirs();
- }
-
- writer = new FileWriter( f );
-
getVelocity().getEngine().mergeTemplate( templateDirectory + "/" + template, context, writer );
}
catch ( ResourceNotFoundException e )
{
throw new ResourceNotFoundException( "Template not found: " + templateDirectory + "/" + template, e );
}
- catch ( VelocityException | IOException e )
- {
- throw e; // to get past generic catch for Exception.
- }
- catch ( Exception e )
+ catch ( RuntimeException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
- finally
- {
- if ( writer != null )
- {
- writer.flush();
- writer.close();
-
- getLog().debug( "File " + outputFilename + " created..." );
- }
- }
}
public void setTemplateDirectory( String templateDirectory )