Index: D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/ArtifactCollector.java =================================================================== --- D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/ArtifactCollector.java (revision 16604) +++ D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/ArtifactCollector.java (working copy) @@ -1,7 +1,12 @@ package org.hibernate.tool.hbm2x; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -59,6 +64,39 @@ formatXml( "cfg.xml" ); } + + /** + * + * @param destination + * @param content + * @return false if file was not changed and true otherwise. + * This method realization simply overwrites the file if it is exists. + * @throws IOException + */ + public boolean merge(File destination, String content) throws IOException{ + String formattedResult = content; + try { + InputStream is = new ByteArrayInputStream(content.getBytes()); + OutputStream os = new ByteArrayOutputStream(); + XMLPrettyPrinter.prettyPrint(is, os); + formattedResult = os.toString(); + } catch (IOException e) {}//ignore + + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(destination); + fileWriter.write(formattedResult); + } + catch (Exception e) { + throw new ExporterException("Error while writing result to file", e); + } finally { + if(fileWriter!=null) { + fileWriter.flush(); + fileWriter.close(); + } + } + return true; + } private void formatXml(String type) throws ExporterException { List list = (List) files.get(type); Index: D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/TemplateProducer.java =================================================================== --- D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/TemplateProducer.java (revision 16604) +++ D:/workspaces/RedHat/hibernate-galileo2/hibernateext.tools/src/java/org/hibernate/tool/hbm2x/TemplateProducer.java (working copy) @@ -1,9 +1,12 @@ package org.hibernate.tool.hbm2x; import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.StringWriter; import java.util.Iterator; import java.util.Map; @@ -30,31 +33,19 @@ if(tempResult.trim().length()==0) { log.warn("Generated output is empty. Skipped creation for file " + destination); return; - } - FileWriter fileWriter = null; + } + + th.ensureExistence( destination ); + try { - - th.ensureExistence( destination ); - - ac.addFile(destination, fileType); - log.debug("Writing " + identifier + " to " + destination.getAbsolutePath() ); - fileWriter = new FileWriter(destination); - fileWriter.write(tempResult); - } - catch (Exception e) { - throw new ExporterException("Error while writing result to file", e); - } finally { - if(fileWriter!=null) { - try { - fileWriter.flush(); - fileWriter.close(); - } - catch (IOException e) { - log.warn("Exception while flushing/closing " + destination,e); - } + log.debug("Writing " + identifier + " to " + destination.getAbsolutePath() ); + if (ac.merge(destination, tempResult)){ + ac.addFile(destination, fileType); } + } catch (IOException e) { + log.warn("Exception while flushing/closing " + destination,e); + ac.addFile(destination, fileType); } - }