From 1f1f6fe638dfa7539937a1eb0a239f31168832b5 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sat, 30 Nov 2024 18:39:45 +0000 Subject: [PATCH] New constructor Document(Path) allows applications using NIO to use Document without explicit type conversion to File If eventually OpenJDK replaces the native ZipFile implementation by a Java-based implementation, ODS Reader can internally switch from Path.toFile() to Files.newInputStream(path) without forcing applications to change their calling code. --- src/main/java/de/zedlitz/opendocument/Document.java | 5 +++++ .../java/de/zedlitz/opendocument/DocumentTest.java | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/de/zedlitz/opendocument/Document.java b/src/main/java/de/zedlitz/opendocument/Document.java index 276abc3..0e471e5 100644 --- a/src/main/java/de/zedlitz/opendocument/Document.java +++ b/src/main/java/de/zedlitz/opendocument/Document.java @@ -7,6 +7,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Path; import java.util.Optional; import java.util.function.Consumer; import java.util.zip.ZipEntry; @@ -41,6 +42,10 @@ public Document(final File file) throws XMLStreamException, IOException { this(new ZipFile(file)); } + public Document(final Path path) throws XMLStreamException, IOException { + this(new ZipFile(path.toFile())); + } + public Document(final ZipFile file) throws XMLStreamException, IOException { final XMLInputFactory factory = XMLInputFactory.newInstance(); final ZipEntry content = file.getEntry("content.xml"); diff --git a/src/test/java/de/zedlitz/opendocument/DocumentTest.java b/src/test/java/de/zedlitz/opendocument/DocumentTest.java index 2458de2..f192e39 100644 --- a/src/test/java/de/zedlitz/opendocument/DocumentTest.java +++ b/src/test/java/de/zedlitz/opendocument/DocumentTest.java @@ -7,6 +7,8 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Objects; import java.util.Optional; import java.util.zip.ZipFile; @@ -165,6 +167,14 @@ public void constructor_file() throws XMLStreamException, IOException { assertNotNull(table1, "1st table exits"); } + @Test + public void constructor_path() throws XMLStreamException, IOException { + Path path = Paths.get(Objects.requireNonNull(getClass().getResource("/test01.ods")).getFile()); + final Document doc = new Document(path); + final Table table1 = doc.nextTable(); + assertNotNull(table1, "1st table exits"); + } + @Test public void brokenXmlContent() throws Exception { PrintStream originalErr = System.err;