This commit is contained in:
Christian P. MOMON 2013-09-09 00:57:10 +02:00
parent 19bc64bedc
commit 6d7db8b7ef

View file

@ -7,6 +7,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import org.apache.commons.lang3.ArrayUtils;
@ -17,14 +18,16 @@ import org.apache.commons.lang3.ArrayUtils;
* Public License as published by the Free Software Foundation version 3
* or any later version.
*/
public class XMLWriter {
public class XMLWriter
{
protected PrintWriter out;
/**
*
*/
protected XMLWriter() {
protected XMLWriter()
{
this.out = null;
}
@ -34,7 +37,8 @@ public class XMLWriter {
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public XMLWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException {
public XMLWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException
{
this.out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
}
@ -43,15 +47,28 @@ public class XMLWriter {
* @param target
* @throws UnsupportedEncodingException
*/
public XMLWriter(final OutputStream target) throws UnsupportedEncodingException {
public XMLWriter(final OutputStream target) throws UnsupportedEncodingException
{
this.out = new PrintWriter(new OutputStreamWriter(target, "UTF-8"));
}
/**
*
* @param target
* @throws UnsupportedEncodingException
*/
public void close() {
if (this.out != null) {
public XMLWriter(final Writer target) throws UnsupportedEncodingException
{
this.out = new PrintWriter(target);
}
/**
*
*/
public void close()
{
if (this.out != null)
{
this.out.flush();
this.out.close();
}
@ -60,8 +77,10 @@ public class XMLWriter {
/**
*
*/
public void flush() {
if (this.out != null) {
public void flush()
{
if (this.out != null)
{
this.out.flush();
}
}
@ -70,9 +89,11 @@ public class XMLWriter {
*
* @param comment
*/
public void writeComment(final String comment) {
public void writeComment(final String comment)
{
out.print("<!-- ");
if (comment != null) {
if (comment != null)
{
writeTagContent(comment);
}
out.print(" -->");
@ -81,7 +102,8 @@ public class XMLWriter {
/**
*
*/
public void writeEmptyTag(final String label, final String... attributes) {
public void writeEmptyTag(final String label, final String... attributes)
{
out.print("<");
out.print(label);
writeTagAttributes(attributes);
@ -91,7 +113,8 @@ public class XMLWriter {
/**
*
*/
public void writeEndTag(final String label) {
public void writeEndTag(final String label)
{
out.print("</");
out.print(label);
out.print(">");
@ -100,7 +123,8 @@ public class XMLWriter {
/**
*
*/
public void writeStartTag(final String label, final String... attributes) {
public void writeStartTag(final String label, final String... attributes)
{
out.print("<");
out.print(label);
writeTagAttributes(attributes);
@ -110,11 +134,26 @@ public class XMLWriter {
/**
*
*/
public void writeTag(final String label, final String content, final String... attributes) {
public void writeTag(final String label, final long content, final String... attributes)
{
if (content == null) {
writeStartTag(label, attributes);
writeTagContent(String.valueOf(content));
writeEndTag(label);
}
/**
*
*/
public void writeTag(final String label, final String content, final String... attributes)
{
if (content == null)
{
writeEmptyTag(label, attributes);
} else {
}
else
{
writeStartTag(label, attributes);
writeTagContent(content);
writeEndTag(label);
@ -124,10 +163,13 @@ public class XMLWriter {
/**
*
*/
public void writeTagAttributes(final String... attributes) {
public void writeTagAttributes(final String... attributes)
{
//
if ((attributes != null) && (attributes.length > 0)) {
for (int count = 0; count < attributes.length; count += 2) {
if ((attributes != null) && (attributes.length > 0))
{
for (int count = 0; count < attributes.length; count += 2)
{
out.print(" ");
out.print(attributes[count]);
out.print("=\"");
@ -140,13 +182,16 @@ public class XMLWriter {
/**
*
*/
public void writeTagContent(final String content) {
public void writeTagContent(final String content)
{
//
for (int count = 0; count < content.length(); count++) {
for (int count = 0; count < content.length(); count++)
{
//
char car = content.charAt(count);
switch (car) {
switch (car)
{
case '<':
out.print("&lt;");
break;
@ -171,30 +216,36 @@ public class XMLWriter {
/**
*
*/
public void writeXMLHeader(final String... attributes) {
public void writeXMLHeader(final String... attributes)
{
//
out.print("<?xml");
//
if (!ArrayUtils.contains(attributes, "version")) {
if (!ArrayUtils.contains(attributes, "version"))
{
out.print(" version=\"1.0\"");
}
//
if (!ArrayUtils.contains(attributes, "encoding")) {
if (!ArrayUtils.contains(attributes, "encoding"))
{
out.print(" encoding=\"UTF-8\"");
}
//
if (!ArrayUtils.contains(attributes, "encoding")) {
if (!ArrayUtils.contains(attributes, "encoding"))
{
out.print(" standalone=\"no\"");
}
//
if (attributes != null) {
if (attributes != null)
{
//
for (int count = 0; count < attributes.length; count += 2) {
for (int count = 0; count < attributes.length; count += 2)
{
out.print(" ");
out.print(attributes[count]);
out.print("=\"");