Refactored code about build entry file name.

This commit is contained in:
Christian P. MOMON 2017-05-07 16:37:44 +02:00
parent 5a5bd040ce
commit 8cb6250cde
4 changed files with 66 additions and 849 deletions

View file

@ -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 <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.strings.StringListUtils;
/**
* The Class FileTools.
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class FileTools
{
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
* Adds the before extension.
*
* @param fileName
* Source.
* @param addition
* the addition
* @return Extension value or null.
*/
public static String addBeforeExtension(final String fileName, final String addition)
{
String result;
if (fileName == null)
{
result = null;
}
else if (addition == null)
{
result = fileName;
}
else
{
//
int separatorIndex = fileName.lastIndexOf('.');
//
if (separatorIndex > 0)
{
result = fileName.substring(0, separatorIndex) + addition + fileName.substring(separatorIndex);
}
else
{
result = fileName + addition;
}
}
//
return result;
}
/**
* Adds the to name.
*
* @param file
* Source.
* @param addition
* the addition
* @return Extension value or null.
*/
public static File addToName(final File file, final String addition)
{
File result;
if (file == null)
{
result = null;
}
else if (addition == null)
{
result = file;
}
else
{
//
String sourceFileName = file.getAbsolutePath();
String targetFileName = addBeforeExtension(sourceFileName, addition);
result = new File(targetFileName);
}
//
return result;
}
/**
* Get the extension of a file.
*
* @param file
* Source.
*
* @return Extension value or null.
*/
public static String getExtension(final File file)
{
String result;
if (file == null)
{
result = null;
}
else
{
result = getExtension(file.getName());
}
//
return result;
}
/**
* Get the extension of a file.
*
* <ul>
* <li>getExtension(null) = null</li>
* <li>getExtension("") = null</li>
* <li>getExtension("abc") = null</li>
* <li>getExtension("abc.efg") = "efg"</li>
* </ul>
*
* @param fileName
* the file name
* @return Extension value or null.
* @deprecated See
* <code>org.apache.commons.io.FilenameUtils.getExtension</code>
*/
@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
* <code>org.apache.commons.io.FilenameUtils.removeExtension</code>
*/
@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;
}
}

View file

@ -28,7 +28,7 @@ import java.util.zip.Deflater;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import fr.devinsy.util.FileTools; import org.apache.commons.lang3.StringUtils;
/** /**
* The Class XMLZipWriter. * The Class XMLZipWriter.
@ -37,7 +37,7 @@ import fr.devinsy.util.FileTools;
*/ */
public class XMLZipWriter extends XMLWriter public class XMLZipWriter extends XMLWriter
{ {
private ZipOutputStream zos; private ZipOutputStream stream;
/** /**
* Instantiates a new XML zip writer. * Instantiates a new XML zip writer.
@ -51,12 +51,12 @@ public class XMLZipWriter extends XMLWriter
{ {
super(); super();
this.zos = new ZipOutputStream(new FileOutputStream(file)); this.stream = new ZipOutputStream(new FileOutputStream(file));
this.zos.setLevel(Deflater.BEST_COMPRESSION); this.stream.setLevel(Deflater.BEST_COMPRESSION);
this.zos.setMethod(ZipOutputStream.DEFLATED); this.stream.setMethod(ZipOutputStream.DEFLATED);
this.zos.setComment("Generated by XMLZipWriter"); this.stream.setComment("Generated by XMLZipWriter");
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName())); this.stream.putNextEntry(new ZipEntry(buildEntryName(file)));
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8")); this.out = new PrintWriter(new OutputStreamWriter(this.stream, "UTF-8"));
} }
/** /**
@ -73,12 +73,12 @@ public class XMLZipWriter extends XMLWriter
{ {
super(); super();
this.zos = new ZipOutputStream(new FileOutputStream(file)); this.stream = new ZipOutputStream(new FileOutputStream(file));
this.zos.setLevel(Deflater.BEST_COMPRESSION); this.stream.setLevel(Deflater.BEST_COMPRESSION);
this.zos.setMethod(ZipOutputStream.DEFLATED); this.stream.setMethod(ZipOutputStream.DEFLATED);
this.zos.setComment(generator); this.stream.setComment(generator);
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName())); this.stream.putNextEntry(new ZipEntry(buildEntryName(file)));
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8")); 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 public XMLZipWriter(final OutputStream target, final String generator) throws IOException
{ {
super(); super();
this.zos = new ZipOutputStream(target);
this.zos.setLevel(Deflater.BEST_COMPRESSION); this.stream = new ZipOutputStream(target);
this.zos.setMethod(ZipOutputStream.DEFLATED); this.stream.setLevel(Deflater.BEST_COMPRESSION);
this.stream.setMethod(ZipOutputStream.DEFLATED);
if (generator != null) 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 public XMLZipWriter(final OutputStream target, final String fileName, final String generator) throws IOException
{ {
super(); super();
this.zos = new ZipOutputStream(target);
this.zos.setLevel(Deflater.BEST_COMPRESSION); this.stream = new ZipOutputStream(target);
this.zos.setMethod(ZipOutputStream.DEFLATED); this.stream.setLevel(Deflater.BEST_COMPRESSION);
if (generator != null) this.stream.setMethod(ZipOutputStream.DEFLATED);
if (StringUtils.isNotBlank(generator))
{ {
this.zos.setComment(generator); this.stream.setComment(generator);
} }
openEntry(fileName); 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 public void closeEntry() throws IOException
{ {
flush(); flush();
this.zos.closeEntry(); this.stream.closeEntry();
} }
/** /**
@ -165,13 +167,48 @@ public class XMLZipWriter extends XMLWriter
*/ */
public void openEntry(final String fileName) throws IOException 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 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;
}
} }

View file

@ -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 <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.IOException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import fr.devinsy.util.strings.StringList;
/**
* The Class FileToolsTest.
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class FileToolsTest
{
public final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileToolsTest.class);
/**
* Before.
*/
@Before
public void before()
{
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
* Load to string list URL 01.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void loadToStringListURL01() throws IOException
{
//
this.logger.debug("===== test starting...");
//
StringList source = FileTools.loadToStringList(FileTools.class.getResource("/fr/devinsy/util/lines.txt"));
//
Assert.assertEquals(4, source.size());
Assert.assertEquals("trois", source.get(3 - 1));
//
this.logger.debug("===== test done.");
}
/**
* Test get extension.
*/
@Test
public void testGetExtension()
{
//
this.logger.debug("===== test starting...");
//
String extension = FileTools.getExtension("test.ext");
//
Assert.assertEquals(extension, "ext");
//
this.logger.debug("===== test done.");
}
}

View file

@ -1,4 +0,0 @@
un
deux
trois
quatre