/** * Merge the given template with the {@link SourceViewMojo#velocityContext} and produce the file * in the output documentation. * * @param templateName The name of the template to process. * @param targetDirectory The directory where to store the output file. * @throws MojoFailureException If the template could not be loaded, parsed, or the output file * could not be written. */ protected void processTemplate(String templateName, File targetDirectory) throws MojoFailureException { FileWriterWithEncoding pageWriter = null; try { pageWriter = new FileWriterWithEncoding( new File(targetDirectory.getCanonicalPath() + File.separator + templateName), Charset.forName(outputEncoding)); velocityEngine.mergeTemplate( "/templates/source-view/" + templateName + ".vm", Charset.forName("UTF-8").name(), velocityContext, pageWriter); } catch (ResourceNotFoundException e) { throw new MojoFailureException("The template '" + templateName + "' could not be found.", e); } catch (ParseErrorException e) { throw new MojoFailureException("Failed to parse the template '" + templateName + "' .", e); } catch (Exception e) { throw new MojoFailureException("Failed to load the template '" + templateName + "' .", e); } finally { try { pageWriter.close(); } catch (IOException e) { throw new MojoFailureException("Failed to write the template '" + templateName + "' .", e); } } }
/** * Simply write a String into a text file. * * @param string The String to write * @param file The file to write to (will be created if it doesn't exist * @param encoding The character encoding to use. If null, a standard utf-8 encoding will be used * @param append Whether to append the text to an existing file (true), or to overwrite it (false) * @return * @throws IOException */ public static File writeTextFile(String string, File file, String encoding, boolean append) throws IOException { if (encoding == null) encoding = CommonUtils.encoding; FileWriterWithEncoding writer = null; writer = new FileWriterWithEncoding(file, encoding, append); writer.write(string); if (writer != null) writer.close(); return file; }