From b05606df8366f674ba872b90fe5f68648b8d885d Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 8 Jun 2014 19:25:05 +0200 Subject: [PATCH] Improve performance of tag content read in XMLReader.readTag method. --- src/fr/devinsy/util/xml/XMLReader.java | 10 +- test/fr/devinsy/util/xml/XMLReaderTest.java | 113 ++++++++++++++++++++ 2 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 test/fr/devinsy/util/xml/XMLReaderTest.java diff --git a/src/fr/devinsy/util/xml/XMLReader.java b/src/fr/devinsy/util/xml/XMLReader.java index 10a4ebe..68c64e1 100644 --- a/src/fr/devinsy/util/xml/XMLReader.java +++ b/src/fr/devinsy/util/xml/XMLReader.java @@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.util.StringList; import fr.devinsy.util.xml.XMLTag.TagType; /** @@ -386,7 +387,7 @@ public class XMLReader result = null; XMLAttributes attributesBuffer = null; QName nameBuffer = null; - String contentBuffer = null; + StringList contentBuffer = null; while (!ended) { // @@ -453,7 +454,8 @@ public class XMLReader break; case XMLEvent.CHARACTERS: // START_ELEMENT(X) + CHARACTERS(C) => ... - contentBuffer = event.asCharacters().getData(); + contentBuffer = new StringList(50); + contentBuffer.append(event.asCharacters().getData()); level += 1; break; case XMLEvent.END_ELEMENT: @@ -480,7 +482,7 @@ public class XMLReader case XMLEvent.CHARACTERS: // START_ELEMENT(X) + CHARACTERS(C1) + // CHARACTERS(C2)=> ... - contentBuffer += event.asCharacters().getData(); + contentBuffer.append(event.asCharacters().getData()); break; case XMLEvent.END_ELEMENT: // START_ELEMENT(X) + CHARACTERS(C) + @@ -488,7 +490,7 @@ public class XMLReader // => CONTENT TAG ended = true; result = new XMLTag(nameBuffer, TagType.CONTENT, attributesBuffer); - result.setContent(contentBuffer); + result.setContent(contentBuffer.toString()); break; default: throw new XMLBadFormatException("Unexpected XMLEvent [" + event.getEventType() + "]."); diff --git a/test/fr/devinsy/util/xml/XMLReaderTest.java b/test/fr/devinsy/util/xml/XMLReaderTest.java new file mode 100644 index 0000000..f495e87 --- /dev/null +++ b/test/fr/devinsy/util/xml/XMLReaderTest.java @@ -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"); + } + +}