/** * Wraps a buffered FileWriter for generation with Acceleo. Note that this can also be used to create a Writer * that will previously merge the content of an existing file with the new content through JMerge. * * @author Laurent Goubet * @since 3.0 */ public final class AcceleoFileWriter extends AbstractAcceleoWriter { . . . /** * Constructs a writer that will use JMerge to merge the content of the file existing at path * filePath with its new content. Note that the file will be written with the default System * encoding if using this. * * @param filePath * Path of the file this writer will contain the content of. * @param hasJMergeTags * Indicates whether the code has some parts to merge */ public GeneratorFileWriter(String filePath, boolean hasJMergeTags) { delegate = new StringWriter(DEFAULT_BUFFER_SIZE); targetPath = filePath; this.hasJMergeTags= hasJMergeTags; shouldMerge = true; } /** * Constructs a writer that will use JMerge to merge the content of the file existing at path * filePath with its new content. * * @param filePath * Path of the file this writer will contain the content of. * @param hasJMergeTags * Indicates whether the code has some parts to merge * @param charset * Encoding that's to be used to create the file with the merged content. */ public GeneratorFileWriter(String filePath, boolean hasJMergeTags, String charset) { this(filePath); selectedCharset = charset; } @Override public void close() throws IOException { if (!shouldMerge || !EMFPlugin.IS_ECLIPSE_RUNNING) { delegate.close(); } else { // The decorated writer is a StringWriter. Closing has no effect on it flush(); try { String generatedText= delegateWriter.toString(); String existingContent= null; File file= new File(targetPath); boolean fileExists= file.exists(); if(fileExists) existingContent= readFileAsString(targetPath); String finalContent= fileExists&& hasJMergeTags? merge(file, generatedText, selectedCharset): generatedText; Writer writer = null; boolean writeOk= false; if(fileExists){ if(! existingContent.equals(finalContent)){ writeOk= true; } }else{ writeOk= true; } try { if(writeOk) { if (selectedCharset == null) { writer = new BufferedWriter(new FileWriter(new File(targetPath))); } else { final OutputStream fileOutputStream = new FileOutputStream(new File(targetPath)); final OutputStreamWriter fileWriter = new OutputStreamWriter(fileOutputStream, selectedCharset); writer = new BufferedWriter(fileWriter); } writer.append(finalContent); } } finally { if (writer != null) { writer.close(); } } } catch (ClassNotFoundException e) { /* * shouldn't happen. This would mean we are in eclipse yet org.eclipse.emf.codegen cannot be * found as a dependency of the generator plugin. This shouldn't happen since it is a * reexported dependency of the engine. */ AcceleoEnginePlugin.log(e, true); } } } private String merge(File file, String generatedText, String selectedCharset) throws ClassNotFoundException, IOException { Class.forName("org.eclipse.emf.codegen.merge.java.JMerger"); //$NON-NLS-1$ return JMergeUtil.mergeFileContent(file, generatedText, selectedCharset); } private String readFileAsString(String targetPath) throws IOException { return new String(Files.readAllBytes(Paths.get(targetPath))); } . . . }