Improve XML writers.
This commit is contained in:
parent
b8187036aa
commit
b524d8bf26
3 changed files with 79 additions and 20 deletions
|
@ -333,22 +333,48 @@ public class FileTools
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
String sourceFileName = source.getAbsolutePath();
|
result = new File(setExtension(source.getAbsolutePath(), extension));
|
||||||
int separatorIndex = sourceFileName.lastIndexOf('.');
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
String targetFileName;
|
return result;
|
||||||
if (separatorIndex > 0)
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @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
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -3,6 +3,7 @@ package fr.devinsy.util.xml;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
@ -65,7 +66,7 @@ public class XMLWriter
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void close()
|
public void close() throws IOException
|
||||||
{
|
{
|
||||||
if (this.out != null)
|
if (this.out != null)
|
||||||
{
|
{
|
||||||
|
@ -77,7 +78,7 @@ public class XMLWriter
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void flush()
|
public void flush() throws IOException
|
||||||
{
|
{
|
||||||
if (this.out != null)
|
if (this.out != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,6 +10,8 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@author christian.momon@devinsy.fr, 2013, copyright.
|
@author christian.momon@devinsy.fr, 2013, copyright.
|
||||||
|
@ -20,6 +22,7 @@ import java.util.zip.ZipOutputStream;
|
||||||
*/
|
*/
|
||||||
public class XMLZipWriter extends XMLWriter
|
public class XMLZipWriter extends XMLWriter
|
||||||
{
|
{
|
||||||
|
private ZipOutputStream zos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -30,11 +33,11 @@ public class XMLZipWriter extends XMLWriter
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
|
this.zos = new ZipOutputStream(new FileOutputStream(file));
|
||||||
zos.setLevel(Deflater.BEST_COMPRESSION);
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||||
zos.setMethod(ZipOutputStream.DEFLATED);
|
zos.setMethod(ZipOutputStream.DEFLATED);
|
||||||
zos.setComment("Generated by XMLZipWriter");
|
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"));
|
this.out = new PrintWriter(new OutputStreamWriter(zos, "UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,11 +50,11 @@ public class XMLZipWriter extends XMLWriter
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
|
this.zos = new ZipOutputStream(new FileOutputStream(file));
|
||||||
zos.setLevel(Deflater.BEST_COMPRESSION);
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||||
zos.setMethod(ZipOutputStream.DEFLATED);
|
zos.setMethod(ZipOutputStream.DEFLATED);
|
||||||
zos.setComment(generator);
|
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"));
|
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
|
public XMLZipWriter(final OutputStream target, final String fileName, final String generator) throws IOException
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
ZipOutputStream zos = new ZipOutputStream(target);
|
this.zos = new ZipOutputStream(target);
|
||||||
zos.setLevel(Deflater.BEST_COMPRESSION);
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||||
zos.setMethod(ZipOutputStream.DEFLATED);
|
zos.setMethod(ZipOutputStream.DEFLATED);
|
||||||
if (generator != null)
|
if (generator != null)
|
||||||
{
|
{
|
||||||
zos.setComment(generator);
|
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)
|
if (fileName == null)
|
||||||
{
|
{
|
||||||
entryName = "noname";
|
throw new NullPointerException("fileName is null.");
|
||||||
}
|
}
|
||||||
else
|
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"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue