diff --git a/src/fr/devinsy/util/FileTools.java b/src/fr/devinsy/util/FileTools.java
deleted file mode 100644
index 9ab0414..0000000
--- a/src/fr/devinsy/util/FileTools.java
+++ /dev/null
@@ -1,725 +0,0 @@
-/*
- * Copyright (C) 2008-2015,2017 Christian Pierre MOMON
- *
- * This file is part of Devinsy-xml.
- *
- * Devinsy-xml is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Devinsy-xml is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with Devinsy-xml. If not, see
org.apache.commons.io.FilenameUtils.getExtension
- */
- @Deprecated
- public static String getExtension(final String fileName)
- {
- String result;
-
- if (fileName == null)
- {
- result = null;
- }
- else
- {
- int separatorIndex = fileName.lastIndexOf('.');
- if (separatorIndex > 0)
- {
- result = fileName.substring(separatorIndex + 1).toLowerCase();
- }
- else
- {
- result = null;
- }
- }
-
- //
- return result;
- }
-
- /**
- * Load.
- *
- * @param source
- * the source
- * @return the string
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static String load(final File source) throws IOException
- {
- String result;
-
- result = load(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load.
- *
- * @param source
- * the source
- * @param charsetName
- * the charset name
- * @return the string
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static String load(final File source, final String charsetName) throws IOException
- {
- String result;
-
- result = loadToStringBuffer(source, charsetName).toString();
-
- //
- return result;
- }
-
- /**
- * Load.
- *
- * @param source
- * the source
- * @return the string
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static String load(final URL source) throws IOException
- {
- String result;
-
- result = load(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load.
- *
- * @param source
- * the source
- * @param charsetName
- * the charset name
- * @return the string
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static String load(final URL source, final String charsetName) throws IOException
- {
- String result;
-
- //
- StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1);
- read(buffer, source.openStream(), charsetName);
-
- //
- result = buffer.toString();
-
- //
- return result;
- }
-
- /**
- * Load string list.
- *
- * @param source
- * the source
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadStringList(final File source) throws IOException
- {
- StringList result;
-
- result = loadStringList(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load string list.
- *
- * @param file
- * the file
- * @param charsetName
- * the charset name
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadStringList(final File file, final String charsetName) throws IOException
- {
- StringList result;
-
- result = StringListUtils.load(file, charsetName);
-
- //
- return result;
- }
-
- /**
- * Load to string buffer.
- *
- * @param source
- * the source
- * @return the string buffer
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringBuffer loadToStringBuffer(final File source) throws IOException
- {
- StringBuffer result;
-
- result = loadToStringBuffer(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load to string buffer.
- *
- * @param file
- * the file
- * @param charsetName
- * the charset name
- * @return the string buffer
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringBuffer loadToStringBuffer(final File file, final String charsetName) throws IOException
- {
- StringBuffer result;
-
- BufferedReader in = null;
- try
- {
- in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
-
- boolean ended = false;
- final String LINE_SEPARATOR = System.getProperty("line.separator");
- result = new StringBuffer((int) file.length() + 1);
- while (!ended)
- {
- String line = in.readLine();
-
- if (line == null)
- {
- ended = true;
- }
- else
- {
- result.append(line).append(LINE_SEPARATOR);
- }
- }
- }
- finally
- {
- try
- {
- if (in != null)
- {
- in.close();
- }
- }
- catch (IOException exception)
- {
- exception.printStackTrace();
- }
- }
-
- //
- return result;
- }
-
- /**
- * Load to string list.
- *
- * @param source
- * the source
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadToStringList(final File source) throws IOException
- {
- StringList result;
-
- result = loadToStringList(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load to string list.
- *
- * @param file
- * the file
- * @param charsetName
- * the charset name
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadToStringList(final File file, final String charsetName) throws IOException
- {
- StringList result;
-
- BufferedReader in = null;
- try
- {
- in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
-
- boolean ended = false;
- final String LINE_SEPARATOR = System.getProperty("line.separator");
- result = new StringList();
- while (!ended)
- {
- String line = in.readLine();
-
- if (line == null)
- {
- ended = true;
- }
- else
- {
- result.append(line).append(LINE_SEPARATOR);
- }
- }
- }
- finally
- {
- try
- {
- if (in != null)
- {
- in.close();
- }
- }
- catch (IOException exception)
- {
- exception.printStackTrace();
- }
- }
-
- //
- return result;
- }
-
- /**
- * Load to string list.
- *
- * @param source
- * the source
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadToStringList(final URL source) throws IOException
- {
- StringList result;
-
- result = loadToStringList(source, DEFAULT_CHARSET_NAME);
-
- //
- return result;
- }
-
- /**
- * Load to string list.
- *
- * @param source
- * the source
- * @param charsetName
- * the charset name
- * @return the string list
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static StringList loadToStringList(final URL source, final String charsetName) throws IOException
- {
- StringList result;
-
- //
- result = StringListUtils.load(source, charsetName);
-
- //
- return result;
- }
-
- /**
- * Read.
- *
- * @param out
- * the out
- * @param is
- * the is
- * @param charsetName
- * the charset name
- * @throws IOException
- * Signals that an I/O exception has occurred.
- */
- public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException
- {
- BufferedReader in = null;
- try
- {
- in = new BufferedReader(new InputStreamReader(is, charsetName));
-
- boolean ended = false;
- final String LINE_SEPARATOR = System.getProperty("line.separator");
-
- while (!ended)
- {
- String line = in.readLine();
-
- if (line == null)
- {
- ended = true;
- }
- else
- {
- out.append(line).append(LINE_SEPARATOR);
- }
- }
- }
- finally
- {
- try
- {
- if (in != null)
- {
- in.close();
- }
- }
- catch (IOException exception)
- {
- exception.printStackTrace();
- }
- }
- }
-
- /**
- * Removes the extension.
- *
- * @param source
- * the source
- * @return the string
- * @deprecated See
- * org.apache.commons.io.FilenameUtils.removeExtension
- */
- @Deprecated
- public static String removeExtension(final String source)
- {
- String result;
-
- if (source == null)
- {
- result = source;
- }
- else
- {
- int separatorIndex = source.lastIndexOf('.');
-
- //
- if (separatorIndex > 0)
- {
- result = source.substring(0, separatorIndex);
- }
- else
- {
- result = source;
- }
- }
-
- //
- return result;
- }
-
- /**
- * Save.
- *
- * @param file
- * the file
- * @param source
- * the source
- * @throws UnsupportedEncodingException
- * the unsupported encoding exception
- * @throws FileNotFoundException
- * the file not found exception
- */
- public static void save(final File file, final String source) throws UnsupportedEncodingException, FileNotFoundException
- {
- PrintWriter out = null;
- try
- {
- out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME));
-
- out.println(source);
- }
- finally
- {
- if (out != null)
- {
- out.close();
- }
- }
- }
-
- /**
- * Save.
- *
- * @param file
- * the file
- * @param source
- * the source
- * @throws UnsupportedEncodingException
- * the unsupported encoding exception
- * @throws FileNotFoundException
- * the file not found exception
- */
- public static void save(final File file, final StringBuffer source) throws UnsupportedEncodingException, FileNotFoundException
- {
- save(file, source.toString());
- }
-
- /**
- * Save.
- *
- * @param file
- * the file
- * @param source
- * the source
- * @throws UnsupportedEncodingException
- * the unsupported encoding exception
- * @throws FileNotFoundException
- * the file not found exception
- */
- public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException
- {
- StringListUtils.save(file, source);
- }
-
- /**
- * Sets the extension.
- *
- * @param source
- * the source
- * @param extension
- * the extension
- * @return the file
- */
- public static File setExtension(final File source, final String extension)
- {
- File result;
-
- if ((source == null) || (extension == null))
- {
- result = source;
- }
- else
- {
- result = new File(setExtension(source.getAbsolutePath(), extension));
- }
-
- //
- return result;
- }
-
- /**
- * Sets the extension.
- *
- * @param source
- * the source
- * @param extension
- * the extension
- * @return the string
- */
- public static String setExtension(final String source, final String extension)
- {
- String result;
-
- if ((source == null) || (extension == null))
- {
- result = source;
- }
- else
- {
- int separatorIndex = source.lastIndexOf('.');
-
- //
- if (separatorIndex > 0)
- {
- String prefix = source.substring(0, separatorIndex);
- if (prefix.endsWith(extension))
- {
- result = prefix;
- }
- else
- {
- result = prefix + extension;
- }
- }
- else
- {
- result = source + extension;
- }
- }
-
- //
- return result;
- }
-}
diff --git a/src/fr/devinsy/util/xml/XMLZipWriter.java b/src/fr/devinsy/util/xml/XMLZipWriter.java
index 6cc2073..b70acc2 100644
--- a/src/fr/devinsy/util/xml/XMLZipWriter.java
+++ b/src/fr/devinsy/util/xml/XMLZipWriter.java
@@ -28,7 +28,7 @@ import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
-import fr.devinsy.util.FileTools;
+import org.apache.commons.lang3.StringUtils;
/**
* The Class XMLZipWriter.
@@ -37,7 +37,7 @@ import fr.devinsy.util.FileTools;
*/
public class XMLZipWriter extends XMLWriter
{
- private ZipOutputStream zos;
+ private ZipOutputStream stream;
/**
* Instantiates a new XML zip writer.
@@ -51,12 +51,12 @@ public class XMLZipWriter extends XMLWriter
{
super();
- this.zos = new ZipOutputStream(new FileOutputStream(file));
- this.zos.setLevel(Deflater.BEST_COMPRESSION);
- this.zos.setMethod(ZipOutputStream.DEFLATED);
- this.zos.setComment("Generated by XMLZipWriter");
- this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName()));
- this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
+ this.stream = new ZipOutputStream(new FileOutputStream(file));
+ this.stream.setLevel(Deflater.BEST_COMPRESSION);
+ this.stream.setMethod(ZipOutputStream.DEFLATED);
+ this.stream.setComment("Generated by XMLZipWriter");
+ this.stream.putNextEntry(new ZipEntry(buildEntryName(file)));
+ this.out = new PrintWriter(new OutputStreamWriter(this.stream, "UTF-8"));
}
/**
@@ -73,12 +73,12 @@ public class XMLZipWriter extends XMLWriter
{
super();
- this.zos = new ZipOutputStream(new FileOutputStream(file));
- this.zos.setLevel(Deflater.BEST_COMPRESSION);
- this.zos.setMethod(ZipOutputStream.DEFLATED);
- this.zos.setComment(generator);
- this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName()));
- this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
+ this.stream = new ZipOutputStream(new FileOutputStream(file));
+ this.stream.setLevel(Deflater.BEST_COMPRESSION);
+ this.stream.setMethod(ZipOutputStream.DEFLATED);
+ this.stream.setComment(generator);
+ this.stream.putNextEntry(new ZipEntry(buildEntryName(file)));
+ this.out = new PrintWriter(new OutputStreamWriter(this.stream, "UTF-8"));
}
/**
@@ -94,14 +94,15 @@ public class XMLZipWriter extends XMLWriter
public XMLZipWriter(final OutputStream target, final String generator) throws IOException
{
super();
- this.zos = new ZipOutputStream(target);
- this.zos.setLevel(Deflater.BEST_COMPRESSION);
- this.zos.setMethod(ZipOutputStream.DEFLATED);
+
+ this.stream = new ZipOutputStream(target);
+ this.stream.setLevel(Deflater.BEST_COMPRESSION);
+ this.stream.setMethod(ZipOutputStream.DEFLATED);
if (generator != null)
{
- this.zos.setComment(generator);
+ this.stream.setComment(generator);
}
- this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
+ this.out = new PrintWriter(new OutputStreamWriter(this.stream, "UTF-8"));
}
/**
@@ -119,15 +120,16 @@ public class XMLZipWriter extends XMLWriter
public XMLZipWriter(final OutputStream target, final String fileName, final String generator) throws IOException
{
super();
- this.zos = new ZipOutputStream(target);
- this.zos.setLevel(Deflater.BEST_COMPRESSION);
- this.zos.setMethod(ZipOutputStream.DEFLATED);
- if (generator != null)
+
+ this.stream = new ZipOutputStream(target);
+ this.stream.setLevel(Deflater.BEST_COMPRESSION);
+ this.stream.setMethod(ZipOutputStream.DEFLATED);
+ if (StringUtils.isNotBlank(generator))
{
- this.zos.setComment(generator);
+ this.stream.setComment(generator);
}
openEntry(fileName);
- this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
+ this.out = new PrintWriter(new OutputStreamWriter(this.stream, "UTF-8"));
}
/**
@@ -152,7 +154,7 @@ public class XMLZipWriter extends XMLWriter
public void closeEntry() throws IOException
{
flush();
- this.zos.closeEntry();
+ this.stream.closeEntry();
}
/**
@@ -165,13 +167,48 @@ public class XMLZipWriter extends XMLWriter
*/
public void openEntry(final String fileName) throws IOException
{
- if (fileName == null)
+ if (StringUtils.isBlank(fileName))
{
- throw new IllegalArgumentException("fileName is null.");
+ throw new IllegalArgumentException("fileName is blank.");
}
else
{
- this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(fileName, ".xml")));
+ this.stream.putNextEntry(new ZipEntry(buildEntryName(new File(fileName))));
}
}
+
+ /**
+ * Builds the entry name.
+ *
+ * @param source
+ * the source
+ * @return the string
+ */
+ private static String buildEntryName(final File source)
+ {
+ String result;
+
+ if (source == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ result = source.getName();
+
+ int separatorIndex = result.lastIndexOf('.');
+ if (separatorIndex > 0)
+ {
+ result = result.substring(0, separatorIndex);
+ }
+
+ if (!result.endsWith(".xml"))
+ {
+ result += ".xml";
+ }
+ }
+
+ //
+ return result;
+ }
}
diff --git a/test/fr/devinsy/util/FileToolsTest.java b/test/fr/devinsy/util/FileToolsTest.java
deleted file mode 100644
index d7e3826..0000000
--- a/test/fr/devinsy/util/FileToolsTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2014,2017 Christian Pierre MOMON
- *
- * This file is part of Devinsy-xml.
- *
- * Devinsy-xml is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Devinsy-xml is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with Devinsy-xml. If not, see