diff options
Diffstat (limited to 'src')
4 files changed, 64 insertions, 29 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index a357487..f7112f4 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -12,6 +12,7 @@ import org.zwobble.mammoth.internal.docx.ZippedDocxFile; import org.zwobble.mammoth.internal.html.Html; import org.zwobble.mammoth.internal.results.InternalResult; import org.zwobble.mammoth.internal.util.Casts; +import org.zwobble.mammoth.internal.util.PassThroughException; import java.io.File; import java.io.IOException; @@ -76,16 +77,18 @@ public class DocumentConverter { * means that relative paths to other files, such as images, cannot be resolved. */ public Result<String> convertToHtml(InputStream stream) throws IOException { - return withDocxFile(stream, zipFile -> - convertToHtml(Optional.empty(), zipFile)).toResult(); + return PassThroughException.unwrap(() -> + withDocxFile(stream, zipFile -> + convertToHtml(Optional.empty(), zipFile)).toResult()); } /** * Converts {@code file} into an HTML string. */ public Result<String> convertToHtml(File file) throws IOException { - return withDocxFile(file, zipFile -> - convertToHtml(Optional.of(file.toPath()), zipFile)).toResult(); + return PassThroughException.unwrap(() -> + withDocxFile(file, zipFile -> + convertToHtml(Optional.of(file.toPath()), zipFile)).toResult()); } private InternalResult<String> convertToHtml(Optional<Path> path, DocxFile zipFile) { @@ -102,8 +105,9 @@ public class DocumentConverter { * Each paragraph is followed by two newlines. */ public Result<String> extractRawText(InputStream stream) throws IOException { - return withDocxFile(stream, zipFile -> - extractRawText(Optional.empty(), zipFile)).toResult(); + return PassThroughException.unwrap(() -> + withDocxFile(stream, zipFile -> + extractRawText(Optional.empty(), zipFile)).toResult()); } /** @@ -112,8 +116,9 @@ public class DocumentConverter { * Each paragraph is followed by two newlines. */ public Result<String> extractRawText(File file) throws IOException { - return withDocxFile(file, zipFile -> - extractRawText(Optional.of(file.toPath()), zipFile)).toResult(); + return PassThroughException.unwrap(() -> + withDocxFile(file, zipFile -> + extractRawText(Optional.of(file.toPath()), zipFile)).toResult()); } private InternalResult<String> extractRawText(Optional<Path> path, DocxFile zipFile) { diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/DocumentReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/DocumentReader.java index 4a0bb7d..b53c9dd 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/DocumentReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/DocumentReader.java @@ -4,6 +4,7 @@ import org.zwobble.mammoth.internal.documents.Document; import org.zwobble.mammoth.internal.documents.Notes; import org.zwobble.mammoth.internal.results.InternalResult; import org.zwobble.mammoth.internal.util.Lists; +import org.zwobble.mammoth.internal.util.PassThroughException; import org.zwobble.mammoth.internal.xml.XmlElement; import java.io.IOException; @@ -70,17 +71,12 @@ public class DocumentReader { } private static Optional<XmlElement> tryParseOfficeXml(DocxFile zipFile, String name) { - try { - return zipFile.tryGetInputStream(name).map(OfficeXml::parseXml); - } catch (IOException exception) { - // TODO: wrap in more specific exception and catch at the top - throw new RuntimeException(exception); - } + return PassThroughException.wrap(() -> + zipFile.tryGetInputStream(name).map(OfficeXml::parseXml)); } private static XmlElement parseOfficeXml(DocxFile zipFile, String name) { return tryParseOfficeXml(zipFile, name) - // TODO: wrap in more specific exception and catch at the top - .orElseThrow(() -> new RuntimeException(new IOException("Missing entry in file: " + name))); + .orElseThrow(() -> new PassThroughException(new IOException("Missing entry in file: " + name))); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java new file mode 100644 index 0000000..155490f --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java @@ -0,0 +1,32 @@ +package org.zwobble.mammoth.internal.util; + +import java.io.IOException; + +public class PassThroughException extends RuntimeException { + public interface SupplierWithException<T, E extends Throwable> { + public T get() throws E; + } + + public static <T> T wrap(SupplierWithException<T, IOException> supplier) { + try { + return supplier.get(); + } catch (IOException exception) { + throw new PassThroughException(exception); + } + } + + public static <T> T unwrap(SupplierWithException<T, IOException> supplier) throws IOException { + try { + return supplier.get(); + } catch (PassThroughException exception) { + throw exception.exception; + } + } + + private final IOException exception; + + public PassThroughException(IOException exception) { + super(exception); + this.exception = exception; + } +} diff --git a/src/main/java/org/zwobble/mammoth/internal/xml/parsing/SimpleSax.java b/src/main/java/org/zwobble/mammoth/internal/xml/parsing/SimpleSax.java index 047e02d..4e1b127 100644 --- a/src/main/java/org/zwobble/mammoth/internal/xml/parsing/SimpleSax.java +++ b/src/main/java/org/zwobble/mammoth/internal/xml/parsing/SimpleSax.java @@ -1,20 +1,20 @@ package org.zwobble.mammoth.internal.xml.parsing; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.util.Map; -import java.util.stream.IntStream; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; +import org.zwobble.mammoth.internal.util.PassThroughException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Map; +import java.util.stream.IntStream; import static java.util.stream.Collectors.toMap; @@ -45,19 +45,21 @@ class SimpleSax { )); handler.startElement(name, attributesMap); } - + @Override public void endElement(String uri, String localName, String qName) throws SAXException { handler.endElement(); } - + @Override public void characters(char[] ch, int start, int length) throws SAXException { handler.characters(new String(ch, start, length)); } }); xmlReader.parse(inputSource); - } catch (ParserConfigurationException | SAXException | IOException exception) { + } catch (IOException exception) { + throw new PassThroughException(exception); + } catch (ParserConfigurationException | SAXException exception) { throw new RuntimeException(exception); } } |
