3131
3232import com .jcabi .log .Logger ;
3333import java .io .ByteArrayInputStream ;
34+ import java .io .File ;
3435import java .io .IOException ;
3536import java .nio .charset .StandardCharsets ;
37+ import java .util .function .Function ;
3638import javax .xml .parsers .DocumentBuilder ;
3739import javax .xml .parsers .DocumentBuilderFactory ;
3840import javax .xml .parsers .ParserConfigurationException ;
@@ -55,13 +57,15 @@ final class DomParser {
5557 /**
5658 * The XML as a text.
5759 */
58- private final transient byte [] xml ;
60+ // private final transient byte[] xml;
5961
6062 /**
6163 * Document builder factory to use for parsing.
6264 */
6365 private final transient DocumentBuilderFactory factory ;
6466
67+ private final Parser parser ;
68+
6569 /**
6670 * Public ctor.
6771 *
@@ -92,7 +96,33 @@ final class DomParser {
9296 */
9397 @ SuppressWarnings ("PMD.ArrayIsStoredDirectly" )
9498 DomParser (final DocumentBuilderFactory fct , final byte [] bytes ) {
95- this .xml = bytes ;
99+ // this.xml = bytes;
100+ this .parser = new Parser () {
101+ @ Override
102+ public Document apply (final DocumentBuilder builder ) throws IOException , SAXException {
103+ return builder .parse (new ByteArrayInputStream (bytes ));
104+ }
105+
106+ @ Override
107+ public long length () {
108+ return bytes .length ;
109+ }
110+ };
111+ this .factory = fct ;
112+ }
113+
114+ DomParser (final DocumentBuilderFactory fct , final File file ){
115+ this .parser = new Parser () {
116+ @ Override
117+ public Document apply (final DocumentBuilder builder ) throws IOException , SAXException {
118+ return builder .parse (file );
119+ }
120+
121+ @ Override
122+ public long length () {
123+ return file .length ();
124+ }
125+ };
96126 this .factory = fct ;
97127 }
98128
@@ -116,7 +146,8 @@ public Document document() {
116146 final long start = System .nanoTime ();
117147 final Document doc ;
118148 try {
119- doc = builder .parse (new ByteArrayInputStream (this .xml ));
149+ // doc = builder.parse(new ByteArrayInputStream(this.xml));
150+ doc = this .parser .apply (builder );
120151 } catch (final IOException | SAXException ex ) {
121152 throw new IllegalArgumentException (
122153 String .format (
@@ -131,11 +162,18 @@ public Document document() {
131162 this ,
132163 "%s parsed %d bytes of XML in %[nano]s" ,
133164 builder .getClass ().getName (),
134- this .xml .length ,
165+ this .parser .length () ,
135166 System .nanoTime () - start
136167 );
137168 }
138169 return doc ;
139170 }
140171
172+ private interface Parser {
173+
174+ Document apply (DocumentBuilder builder ) throws IOException , SAXException ;
175+
176+ long length ();
177+ }
178+
141179}
0 commit comments