Added indent and unindent methods. Added test.

This commit is contained in:
Christian P. MOMON 2017-05-08 12:45:45 +02:00
parent a287e14eab
commit 6ee4b4d582
5 changed files with 214 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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.
*

View file

@ -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 <http://www.gnu.org/licenses/>
*/
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);
}
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<account id="77">
<email>christian.momon@devinsy.fr</email>
<first_names>Christian Pierre</first_names>
<last_name>MOMON</last_name>
<comment>Foo character &lt;moo&gt; &quot; &apos;</comment>
</account>

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<corpus xmlns="urn:schema:PUCK/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="PUCK-1.0" generator="PUCK" date="2013-05-14T00:21:40.421+02:00" filename="t.puc">
<attributes/>
<individuals>
<individual id="1">
<name>Louis / XVI / de France</name>
<gender>MALE</gender>
<attributes size="7">
<attribute>
<label>DEAT Date</label>
<value>21/1/1793</value>
</attribute>
<attribute>
<label>COMPL</label>
<value>complete</value>
</attribute>
<attribute>
<label>DEAT Place</label>
<value>Paris</value>
</attribute>
<attribute>
<label>BIRT Date</label>
<value>23/8/1754</value>
</attribute>
<attribute>
<label>BIRT Place</label>
<value>Versailles</value>
</attribute>
<attribute>
<label>SHEET</label>
<value>aaaaa</value>
</attribute>
<attribute>
<label>DYNASTY</label>
<value>Bourbon</value>
</attribute>
</attributes>
</individual>
</individuals>
</corpus>