From b524d8bf267a64ed59ff1173d1972426e140a650 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 11 Sep 2013 12:00:09 +0200 Subject: [PATCH] Improve XML writers. --- src/fr/devinsy/util/FileTools.java | 42 ++++++++++++++---- src/fr/devinsy/util/xml/XMLWriter.java | 5 ++- src/fr/devinsy/util/xml/XMLZipWriter.java | 52 ++++++++++++++++++----- 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/fr/devinsy/util/FileTools.java b/src/fr/devinsy/util/FileTools.java index 47b30cd..b636679 100644 --- a/src/fr/devinsy/util/FileTools.java +++ b/src/fr/devinsy/util/FileTools.java @@ -333,22 +333,48 @@ public class FileTools } else { - String sourceFileName = source.getAbsolutePath(); - int separatorIndex = sourceFileName.lastIndexOf('.'); + result = new File(setExtension(source.getAbsolutePath(), extension)); + } + + // + return result; + } + + /** + * + * @param source + * @param extension + * @return + */ + public static String setExtension(final String source, final String extension) + { + String result; + + if ((source == null) || (extension == null)) + { + result = source; + } + else + { + int separatorIndex = source.lastIndexOf('.'); // - String targetFileName; if (separatorIndex > 0) { - targetFileName = sourceFileName.substring(0, separatorIndex) + extension; + String prefix = source.substring(0, separatorIndex); + if (prefix.endsWith(extension)) + { + result = prefix; + } + else + { + result = prefix + extension; + } } else { - targetFileName = sourceFileName + extension; + result = source + extension; } - - // - result = new File(targetFileName); } // diff --git a/src/fr/devinsy/util/xml/XMLWriter.java b/src/fr/devinsy/util/xml/XMLWriter.java index b681970..a6d08d7 100644 --- a/src/fr/devinsy/util/xml/XMLWriter.java +++ b/src/fr/devinsy/util/xml/XMLWriter.java @@ -3,6 +3,7 @@ package fr.devinsy.util.xml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -65,7 +66,7 @@ public class XMLWriter /** * */ - public void close() + public void close() throws IOException { if (this.out != null) { @@ -77,7 +78,7 @@ public class XMLWriter /** * */ - public void flush() + public void flush() throws IOException { if (this.out != null) { diff --git a/src/fr/devinsy/util/xml/XMLZipWriter.java b/src/fr/devinsy/util/xml/XMLZipWriter.java index f89432a..020d3e5 100644 --- a/src/fr/devinsy/util/xml/XMLZipWriter.java +++ b/src/fr/devinsy/util/xml/XMLZipWriter.java @@ -10,6 +10,8 @@ import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import fr.devinsy.util.FileTools; + /** * @author christian.momon@devinsy.fr, 2013, copyright. @@ -20,6 +22,7 @@ import java.util.zip.ZipOutputStream; */ public class XMLZipWriter extends XMLWriter { + private ZipOutputStream zos; /** * @@ -30,11 +33,11 @@ public class XMLZipWriter extends XMLWriter { super(); - ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file)); + this.zos = new ZipOutputStream(new FileOutputStream(file)); zos.setLevel(Deflater.BEST_COMPRESSION); zos.setMethod(ZipOutputStream.DEFLATED); zos.setComment("Generated by XMLZipWriter"); - zos.putNextEntry(new ZipEntry(file.getName() + ".xml")); + zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName())); this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8")); } @@ -47,11 +50,11 @@ public class XMLZipWriter extends XMLWriter { super(); - ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file)); + this.zos = new ZipOutputStream(new FileOutputStream(file)); zos.setLevel(Deflater.BEST_COMPRESSION); zos.setMethod(ZipOutputStream.DEFLATED); zos.setComment(generator); - zos.putNextEntry(new ZipEntry(file.getName() + ".xml")); + zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName())); this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8")); } @@ -63,23 +66,52 @@ public class XMLZipWriter extends XMLWriter public XMLZipWriter(final OutputStream target, final String fileName, final String generator) throws IOException { super(); - ZipOutputStream zos = new ZipOutputStream(target); + this.zos = new ZipOutputStream(target); zos.setLevel(Deflater.BEST_COMPRESSION); zos.setMethod(ZipOutputStream.DEFLATED); if (generator != null) { zos.setComment(generator); } - String entryName; + openEntry(fileName); + this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8")); + } + + /** + * @throws IOException + * + */ + @Override + public void close() throws IOException + { + closeEntry(); + super.close(); + } + + /** + * @throws IOException + * + */ + public void closeEntry() throws IOException + { + flush(); + this.zos.closeEntry(); + } + + /** + * + * @param fileName + * @throws IOException + */ + public void openEntry(final String fileName) throws IOException + { if (fileName == null) { - entryName = "noname"; + throw new NullPointerException("fileName is null."); } else { - entryName = fileName; + this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(fileName, ".xml"))); } - zos.putNextEntry(new ZipEntry(entryName)); - this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8")); } }