Skip to content

Commit

Permalink
feat(jcabi#277): first failed attempt to optimize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 4, 2024
1 parent 272c0dc commit caf4e28
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
46 changes: 42 additions & 4 deletions src/main/java/com/jcabi/xml/DomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand All @@ -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(
Expand All @@ -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();
}

}
1 change: 1 addition & 0 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ void createsManyXmlDocuments() throws ParserConfigurationException, IOException,
}


// ~ 1.6
// ~ 1.6
@RepeatedTest(10)
void createsXmlFromFile(
@TempDir final Path temp
Expand Down Expand Up @@ -772,7 +772,7 @@ private String large() {
"<credit>test-2</credit>",
"</payment>"
);
IntStream.range(0, 100).mapToObj(i -> payment).forEach(builder::append);
IntStream.range(0, 1_000).mapToObj(i -> payment).forEach(builder::append);
return builder.append("</root>").toString();
}

Expand Down

0 comments on commit caf4e28

Please sign in to comment.