diff --git a/src/fr/devinsy/util/xml/XMLTools.java b/src/fr/devinsy/util/xml/XMLTools.java
index a0519e4..8c8f96a 100644
--- a/src/fr/devinsy/util/xml/XMLTools.java
+++ b/src/fr/devinsy/util/xml/XMLTools.java
@@ -134,6 +134,11 @@ public class XMLTools
buffer.appendln();
break;
case EMPTY:
+ out.flush();
+ buffer.append(StringUtils.repeat('\t', level));
+ out.writeEmptyTag(tag.getLabel(), tag.attributes());
+ out.flush();
+ buffer.appendln();
break;
case FOOTER:
break;
@@ -498,4 +503,28 @@ public class XMLTools
//
return result;
}
+
+ /**
+ * Unindent.
+ *
+ * @param source
+ * the source
+ * @return the string
+ * @throws XMLStreamException
+ * the XML stream exception
+ * @throws XMLBadFormatException
+ * the XML bad format exception
+ * @throws IOException
+ * Signals that an I/O exception has occurred.
+ */
+ public static String unindent(final String source) throws XMLStreamException, XMLBadFormatException, IOException
+ {
+ String result;
+
+ result = source.replaceAll("[\t\n\r]", "");
+ result = result.replaceAll(" *<", "<");
+
+ //
+ return result;
+ }
}
diff --git a/src/fr/devinsy/util/xml/XMLWriter.java b/src/fr/devinsy/util/xml/XMLWriter.java
index a76c9c1..0d75048 100644
--- a/src/fr/devinsy/util/xml/XMLWriter.java
+++ b/src/fr/devinsy/util/xml/XMLWriter.java
@@ -363,6 +363,37 @@ public class XMLWriter
}
}
+ /**
+ * Write tag.
+ *
+ * @param tag
+ * the tag
+ */
+ public void writeTag(final XMLTag tag)
+ {
+ switch (tag.getType())
+ {
+ case HEADER:
+ writeXMLHeader(tag.attributes());
+ break;
+ case START:
+ writeStartTag(tag.getLabel(), tag.attributes());
+ break;
+ case CONTENT:
+ writeTag(tag.getLabel(), tag.getContent(), tag.attributes());
+ break;
+ case END:
+ writeEndTag(tag.getLabel());
+ break;
+ case EMPTY:
+ writeEmptyTag(tag.getLabel());
+ break;
+ case FOOTER:
+ // TODO?
+ break;
+ }
+ }
+
/**
* This method writes attributes of a tag.
*
diff --git a/test/fr/devinsy/util/xml/XMLWriterTest.java b/test/fr/devinsy/util/xml/XMLWriterTest.java
new file mode 100644
index 0000000..117bbc9
--- /dev/null
+++ b/test/fr/devinsy/util/xml/XMLWriterTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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
+ */
+package fr.devinsy.util.xml;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.lf5.util.StreamUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * The Class XMLReaderTest.
+ *
+ * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
+ */
+public class XMLWriterTest
+{
+ public static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(XMLWriterTest.class);
+
+ /**
+ * Before.
+ */
+ @Before
+ public void before()
+ {
+ BasicConfigurator.configure();
+ Logger.getRootLogger().setLevel(Level.INFO);
+ }
+
+ /**
+ * Test foo 01.
+ *
+ * @throws XMLStreamException
+ * the XML stream exception
+ * @throws XMLBadFormatException
+ * the XML bad format exception
+ * @throws IOException
+ */
+ @Test
+ public void testtFoo01() throws XMLStreamException, XMLBadFormatException, IOException
+ {
+ logger.debug(StreamUtils.getBytes(XMLWriter.class.getResourceAsStream("/fr/devinsy/util/xml/foo01.xml")).length);
+
+ //
+ String source = new String(StreamUtils.getBytes(XMLWriter.class.getResourceAsStream("/fr/devinsy/util/xml/foo01.xml")));
+
+ System.out.println("source=\n" + source);
+
+ //
+ XMLReader in = new XMLReader(source);
+
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ XMLWriter out = new XMLWriter(buffer);
+
+ boolean ended = false;
+ while (!ended)
+ {
+ XMLTag tag = in.readTag();
+
+ if (tag == null)
+ {
+ ended = true;
+ }
+ else
+ {
+ out.writeTag(tag);
+ }
+ }
+ out.close();
+
+ String target = buffer.toString();
+
+ System.out.println("=============");
+ System.out.println("XML generated:");
+ System.out.println(target);
+ System.out.println(XMLTools.indent(target));
+ System.out.println();
+ System.out.println("Compare:");
+ System.out.println(XMLTools.unindent(source));
+ System.out.println(target);
+
+ Assert.assertEquals(XMLTools.unindent(source), target);
+ }
+}
diff --git a/test/fr/devinsy/util/xml/foo01.xml b/test/fr/devinsy/util/xml/foo01.xml
new file mode 100644
index 0000000..c0159b8
--- /dev/null
+++ b/test/fr/devinsy/util/xml/foo01.xml
@@ -0,0 +1,7 @@
+
+
+ christian.momon@devinsy.fr
+ Christian Pierre
+ MOMON
+ Foo character <moo> " '
+
diff --git a/test/fr/devinsy/util/xml/foo02.xml b/test/fr/devinsy/util/xml/foo02.xml
new file mode 100644
index 0000000..78f5976
--- /dev/null
+++ b/test/fr/devinsy/util/xml/foo02.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+ Louis / XVI / de France
+ MALE
+
+
+
+ 21/1/1793
+
+
+
+ complete
+
+
+
+ Paris
+
+
+
+ 23/8/1754
+
+
+
+ Versailles
+
+
+
+ aaaaa
+
+
+
+ Bourbon
+
+
+
+
+