Improve XML writers.

This commit is contained in:
Christian P. MOMON 2013-09-11 12:00:09 +02:00
parent b8187036aa
commit b524d8bf26
3 changed files with 79 additions and 20 deletions

View file

@ -333,22 +333,48 @@ public class FileTools
}
else
{
String sourceFileName = source.getAbsolutePath();
int separatorIndex = sourceFileName.lastIndexOf('.');
result = new File(setExtension(source.getAbsolutePath(), extension));
}
//
String targetFileName;
if (separatorIndex > 0)
return result;
}
/**
*
* @param source
* @param extension
* @return
*/
public static String setExtension(final String source, final String extension)
{
targetFileName = sourceFileName.substring(0, separatorIndex) + extension;
String result;
if ((source == null) || (extension == null))
{
result = source;
}
else
{
targetFileName = sourceFileName + extension;
}
int separatorIndex = source.lastIndexOf('.');
//
result = new File(targetFileName);
if (separatorIndex > 0)
{
String prefix = source.substring(0, separatorIndex);
if (prefix.endsWith(extension))
{
result = prefix;
}
else
{
result = prefix + extension;
}
}
else
{
result = source + extension;
}
}
//

View file

@ -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)
{

View file

@ -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;
}
zos.putNextEntry(new ZipEntry(entryName));
this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8"));
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(fileName, ".xml")));
}
}
}