Improve performance of tag content read in XMLReader.readTag method.

This commit is contained in:
Christian P. MOMON 2014-06-08 19:25:05 +02:00
parent cdddc5e435
commit b05606df83
2 changed files with 119 additions and 4 deletions

View file

@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringList;
import fr.devinsy.util.xml.XMLTag.TagType; import fr.devinsy.util.xml.XMLTag.TagType;
/** /**
@ -386,7 +387,7 @@ public class XMLReader
result = null; result = null;
XMLAttributes attributesBuffer = null; XMLAttributes attributesBuffer = null;
QName nameBuffer = null; QName nameBuffer = null;
String contentBuffer = null; StringList contentBuffer = null;
while (!ended) while (!ended)
{ {
// //
@ -453,7 +454,8 @@ public class XMLReader
break; break;
case XMLEvent.CHARACTERS: case XMLEvent.CHARACTERS:
// START_ELEMENT(X) + CHARACTERS(C) => ... // START_ELEMENT(X) + CHARACTERS(C) => ...
contentBuffer = event.asCharacters().getData(); contentBuffer = new StringList(50);
contentBuffer.append(event.asCharacters().getData());
level += 1; level += 1;
break; break;
case XMLEvent.END_ELEMENT: case XMLEvent.END_ELEMENT:
@ -480,7 +482,7 @@ public class XMLReader
case XMLEvent.CHARACTERS: case XMLEvent.CHARACTERS:
// START_ELEMENT(X) + CHARACTERS(C1) + // START_ELEMENT(X) + CHARACTERS(C1) +
// CHARACTERS(C2)=> ... // CHARACTERS(C2)=> ...
contentBuffer += event.asCharacters().getData(); contentBuffer.append(event.asCharacters().getData());
break; break;
case XMLEvent.END_ELEMENT: case XMLEvent.END_ELEMENT:
// START_ELEMENT(X) + CHARACTERS(C) + // START_ELEMENT(X) + CHARACTERS(C) +
@ -488,7 +490,7 @@ public class XMLReader
// => CONTENT TAG // => CONTENT TAG
ended = true; ended = true;
result = new XMLTag(nameBuffer, TagType.CONTENT, attributesBuffer); result = new XMLTag(nameBuffer, TagType.CONTENT, attributesBuffer);
result.setContent(contentBuffer); result.setContent(contentBuffer.toString());
break; break;
default: default:
throw new XMLBadFormatException("Unexpected XMLEvent [" + event.getEventType() + "]."); throw new XMLBadFormatException("Unexpected XMLEvent [" + event.getEventType() + "].");

View file

@ -0,0 +1,113 @@
package fr.devinsy.util.xml;
import java.io.File;
import java.io.FileNotFoundException;
import javax.xml.stream.XMLStreamException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Before;
import fr.devinsy.util.StringList;
/**
*
* @author Christian P. Momon
*/
public class XMLReaderTest
{
static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(XMLReaderTest.class);
/**
*
*/
@Before
public void before()
{
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
* @throws XMLStreamException
* @throws FileNotFoundException
* @throws XMLBadFormatException
*
*/
// @Test
public void testFoo01() throws FileNotFoundException, XMLStreamException, XMLBadFormatException
{
//
logger.debug("===== test starting...");
// XMLReader in = new XMLReader(new
// File("/home/cpm/C/Puck/TY/Ebrei 08.puc"));
// XMLReader in = new XMLReader(new
// File("/home/cpm/C/Puck/TY/T/kidarep.xml"));
XMLReader in = new XMLReader(new File("/home/cpm/C/Puck/TY/T2/sikevadb-2014-06-08-17h55mn49s.xml"));
boolean ended = false;
while (!ended)
{
XMLTag tag = in.readTag();
if (tag == null)
{
ended = true;
}
else
{
// System.out.println(String.format("tag %s", tag.getLabel()));
}
//
logger.debug("===== test done.");
}
System.out.println("over");
}
/**
* @throws XMLStreamException
* @throws FileNotFoundException
* @throws XMLBadFormatException
*
*/
// @Test
public void testFoo02() throws FileNotFoundException, XMLStreamException, XMLBadFormatException
{
//
logger.debug("===== test starting...");
// XMLReader in = new XMLReader(new
// File("/home/cpm/C/Puck/TY/Ebrei 08.puc"));
XMLReader in = new XMLReader(new File("/home/cpm/C/Puck/TY/T/accounts.xml"));
// XMLReader in = new XMLReader(new
// File("/home/cpm/C/Puck/TY/T2/sikevadb-2014-06-08-17h55mn49s.xml"));
boolean ended = false;
StringList buffer = new StringList();
while (!ended)
{
XMLTag tag = in.readTag();
if (tag == null)
{
ended = true;
}
else
{
if (tag.getContent() != null)
{
System.out.println(buffer.append(tag.getContent()));
}
}
//
logger.debug("===== test done.");
}
System.out.println("over");
}
}