diff --git a/src/main/java/com/jcabi/xml/DomParser.java b/src/main/java/com/jcabi/xml/DomParser.java index 02728520..8377e776 100644 --- a/src/main/java/com/jcabi/xml/DomParser.java +++ b/src/main/java/com/jcabi/xml/DomParser.java @@ -31,8 +31,10 @@ import com.jcabi.log.Logger; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.function.Function; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -55,13 +57,15 @@ final class DomParser { /** * The XML as a text. */ - private final transient byte[] xml; +// private final transient byte[] xml; /** * Document builder factory to use for parsing. */ private final transient DocumentBuilderFactory factory; + private final Parser parser; + /** * Public ctor. * @@ -92,7 +96,33 @@ final class DomParser { */ @SuppressWarnings("PMD.ArrayIsStoredDirectly") DomParser(final DocumentBuilderFactory fct, final byte[] bytes) { - this.xml = bytes; +// this.xml = bytes; + this.parser = new Parser() { + @Override + public Document apply(final DocumentBuilder builder) throws IOException, SAXException { + return builder.parse(new ByteArrayInputStream(bytes)); + } + + @Override + public long length() { + return bytes.length; + } + }; + this.factory = fct; + } + + DomParser(final DocumentBuilderFactory fct, final File file){ + this.parser = new Parser() { + @Override + public Document apply(final DocumentBuilder builder) throws IOException, SAXException { + return builder.parse(file); + } + + @Override + public long length() { + return file.length(); + } + }; this.factory = fct; } @@ -116,7 +146,8 @@ public Document document() { final long start = System.nanoTime(); final Document doc; try { - doc = builder.parse(new ByteArrayInputStream(this.xml)); +// doc = builder.parse(new ByteArrayInputStream(this.xml)); + doc = this.parser.apply(builder); } catch (final IOException | SAXException ex) { throw new IllegalArgumentException( String.format( @@ -131,11 +162,18 @@ public Document document() { this, "%s parsed %d bytes of XML in %[nano]s", builder.getClass().getName(), - this.xml.length, + this.parser.length(), System.nanoTime() - start ); } return doc; } + private interface Parser { + + Document apply(DocumentBuilder builder) throws IOException, SAXException; + + long length(); + } + } diff --git a/src/main/java/com/jcabi/xml/XMLDocument.java b/src/main/java/com/jcabi/xml/XMLDocument.java index 82be144c..78ba3d6e 100644 --- a/src/main/java/com/jcabi/xml/XMLDocument.java +++ b/src/main/java/com/jcabi/xml/XMLDocument.java @@ -203,6 +203,7 @@ public XMLDocument(final Source source) { */ public XMLDocument(final File file) throws FileNotFoundException { this(new TextResource(file).toString()); +// this(new DomParser(XMLDocument.configuredDFactory(), file).document()); } /** diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index f6162c20..65500aaa 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -719,7 +719,7 @@ void createsManyXmlDocuments() throws ParserConfigurationException, IOException, } - // ~ 1.6 + // ~ 1.6 @RepeatedTest(10) void createsXmlFromFile( @TempDir final Path temp @@ -772,7 +772,7 @@ private String large() { "test-2", "" ); - IntStream.range(0, 100).mapToObj(i -> payment).forEach(builder::append); + IntStream.range(0, 1_000).mapToObj(i -> payment).forEach(builder::append); return builder.append("").toString(); }