Skip to content

Commit caf4e28

Browse files
committedDec 4, 2024
feat(#277): first failed attempt to optimize the code
1 parent 272c0dc commit caf4e28

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed
 

‎src/main/java/com/jcabi/xml/DomParser.java

+42-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131

3232
import com.jcabi.log.Logger;
3333
import java.io.ByteArrayInputStream;
34+
import java.io.File;
3435
import java.io.IOException;
3536
import java.nio.charset.StandardCharsets;
37+
import java.util.function.Function;
3638
import javax.xml.parsers.DocumentBuilder;
3739
import javax.xml.parsers.DocumentBuilderFactory;
3840
import 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
}

‎src/main/java/com/jcabi/xml/XMLDocument.java

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public XMLDocument(final Source source) {
203203
*/
204204
public XMLDocument(final File file) throws FileNotFoundException {
205205
this(new TextResource(file).toString());
206+
// this(new DomParser(XMLDocument.configuredDFactory(), file).document());
206207
}
207208

208209
/**

‎src/test/java/com/jcabi/xml/XMLDocumentTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ void createsManyXmlDocuments() throws ParserConfigurationException, IOException,
719719
}
720720

721721

722-
// ~ 1.6
722+
// ~ 1.6
723723
@RepeatedTest(10)
724724
void createsXmlFromFile(
725725
@TempDir final Path temp
@@ -772,7 +772,7 @@ private String large() {
772772
"<credit>test-2</credit>",
773773
"</payment>"
774774
);
775-
IntStream.range(0, 100).mapToObj(i -> payment).forEach(builder::append);
775+
IntStream.range(0, 1_000).mapToObj(i -> payment).forEach(builder::append);
776776
return builder.append("</root>").toString();
777777
}
778778

0 commit comments

Comments
 (0)
Please sign in to comment.