This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 54f1f5deac add new XSSFParser (#870)
54f1f5deac is described below

commit 54f1f5deac7f4e6dd90e06de4016bd3b2ba6ee5a
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Aug 20 13:13:23 2025 +0100

    add new XSSFParser (#870)
    
    * add new XSSFParser
    
    * Update build.gradle
    
    * extra parse methods
---
 .../org/apache/poi/openxml4j/opc/OPCPackage.java   |   5 +-
 .../main/java/org/apache/poi/xslf/XSLFParser.java  | 160 +++++++++++++++++++
 .../org/apache/poi/xslf/XSLFReadException.java     |  56 +++++++
 .../main/java/org/apache/poi/xssf/XSSFParser.java  | 157 +++++++++++++++++++
 .../org/apache/poi/xssf/XSSFReadException.java     |  56 +++++++
 .../main/java/org/apache/poi/xwpf/XWPFParser.java  | 169 +++++++++++++++++++++
 .../org/apache/poi/xwpf/XWPFReadException.java     |  56 +++++++
 7 files changed, 657 insertions(+), 2 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
index 6caa32627d..237a516d8e 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
@@ -374,9 +374,10 @@ public abstract class OPCPackage implements 
RelationshipSource, Closeable {
      * @param opcComplianceFlags
      *            The level of OPC compliance to enforce when reading the 
package
      * @return A PackageBase object, else <b>null</b>.
+     * @throws IllegalArgumentException
+     *             If the specified file doesn't exist or is a directory.
      * @throws InvalidFormatException
-     *             If the specified file doesn't exist, and a parsing error
-     *             occur.
+     *             If a parsing error occurs.
      * @since POI 5.4.1
      */
     public static OPCPackage open(File file, PackageAccess access, 
OPCComplianceFlags opcComplianceFlags)
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFParser.java 
b/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFParser.java
new file mode 100644
index 0000000000..cd113f44bf
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFParser.java
@@ -0,0 +1,160 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xslf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.util.ExceptionUtil;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xslf.usermodel.XMLSlideShow;
+
+/**
+ * Methods that wrap {@link XMLSlideShow} parsing functionality.
+ * One difference is that the methods in this class try to
+ * throw {@link XSLFReadException} or {@link IOException} instead of {@link 
RuntimeException}.
+ * You can still get an {@link Error}s like an {@link OutOfMemoryError}.
+ *
+ * @since POI 5.5.0
+ */
+public final class XSLFParser {
+
+    private XSLFParser() {
+        // Prevent instantiation
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XMLSlideShow} 
instance.
+     *
+     * @param stream the data to parse (will be closed after parsing)
+     * @return a new {@link XMLSlideShow} instance
+     * @throws XSLFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XMLSlideShow parse(InputStream stream) throws 
XSLFReadException, IOException {
+        try {
+            return new XMLSlideShow(stream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        } catch (Exception e) {
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XMLSlideShow} 
instance.
+     *
+     * @param stream the data to parse
+     * @param closeStream whether to close the InputStream after parsing
+     * @return a new {@link XMLSlideShow} instance
+     * @throws XSLFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XMLSlideShow parse(InputStream stream, boolean closeStream) 
throws XSLFReadException, IOException {
+        try {
+            return new XMLSlideShow(stream, closeStream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        } catch (Exception e) {
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XMLSlideShow} 
instance.
+     *
+     * @param file to parse
+     * @return a new {@link XMLSlideShow} instance
+     * @throws XSLFReadException if an error occurs while reading the file
+     */
+    public static XMLSlideShow parse(File file) throws XSLFReadException {
+        OPCPackage pkg = null;
+        try {
+            pkg = OPCPackage.open(file);
+            return new XMLSlideShow(pkg);
+        } catch (Error | RuntimeException e) {
+            if (pkg != null) {
+                IOUtils.closeQuietly(pkg);
+            }
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        } catch (Exception e) {
+            if (pkg != null) {
+                IOUtils.closeQuietly(pkg);
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link OPCPackage} and return a new {@link 
XMLSlideShow} instance.
+     *
+     * @param pkg to parse
+     * @return a new {@link XMLSlideShow} instance
+     * @throws XSLFReadException if an error occurs while reading the file
+     */
+    public static XMLSlideShow parse(OPCPackage pkg) throws XSLFReadException {
+        try {
+            return new XMLSlideShow(pkg);
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        } catch (Exception e) {
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link PackagePart} and return a new {@link 
XMLSlideShow} instance.
+     *
+     * @param packagePart to parse
+     * @return a new {@link XMLSlideShow} instance
+     * @throws XSLFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XMLSlideShow parse(PackagePart packagePart) throws 
XSLFReadException, IOException {
+        try (InputStream is = packagePart.getInputStream()) {
+            return new XMLSlideShow(is);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        } catch (Exception e) {
+            throw new XSLFReadException("Exception reading XMLSlideShow", e);
+        }
+    }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFReadException.java 
b/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFReadException.java
new file mode 100644
index 0000000000..bdb8c800d1
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/XSLFReadException.java
@@ -0,0 +1,56 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xslf;
+
+import org.apache.poi.POIException;
+
+/**
+ * An exception that indicates a problem reading a pptx file.
+ * <p>This exception is only used by some new methods.
+ * Historically, POI has used {@link RuntimeException} for most of its
+ * exceptions, but this is not a good practice. This class is a checked
+ * class that extends {@link Exception} so needs to be explicitly
+ * caught or declared in the method signature.</p>
+ * @since POI 5.5.0
+ */
+public class XSLFReadException extends POIException {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Create a new {@code XSLFReadException} with
+     * the {@code String} specified as an error message.
+     *
+     * @param msg The error message for the exception.
+     */
+    public XSLFReadException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * Create a new {@code XSLFReadException} with
+     * the {@code String} specified as an error message and the cause.
+     *
+     * @param msg The error message for the exception.
+     * @param cause the cause (which is saved for later retrieval by the
+     *         {@link #getCause()} method).  (A {@code null} value is
+     *         permitted, and indicates that the cause is nonexistent or
+     *         unknown.)
+     */
+    public XSLFReadException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFParser.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFParser.java
new file mode 100644
index 0000000000..e56c71da99
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFParser.java
@@ -0,0 +1,157 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.util.ExceptionUtil;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ * Methods that wrap {@link XSSFWorkbook} parsing functionality.
+ * One difference is that the methods in this class try to
+ * throw {@link XSSFReadException} or {@link IOException} instead of {@link 
RuntimeException}.
+ * You can still get an {@link Error}s like an {@link OutOfMemoryError}.
+ *
+ * @since POI 5.5.0
+ */
+public final class XSSFParser {
+
+    private XSSFParser() {
+        // Prevent instantiation
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XSSFWorkbook} 
instance.
+     *
+     * @param stream the data to parse (will be closed after parsing)
+     * @return a new {@link XSSFWorkbook} instance
+     * @throws XSSFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XSSFWorkbook parse(InputStream stream) throws 
XSSFReadException, IOException {
+        try {
+            return new XSSFWorkbook(stream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        } catch (Exception e) {
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XSSFWorkbook} 
instance.
+     *
+     * @param stream the data to parse
+     * @param closeStream whether to close the InputStream after parsing
+     * @return a new {@link XSSFWorkbook} instance
+     * @throws XSSFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XSSFWorkbook parse(InputStream stream, boolean closeStream) 
throws XSSFReadException, IOException {
+        try {
+            return new XSSFWorkbook(stream, closeStream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        } catch (Exception e) {
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XSSFWorkbook} 
instance.
+     *
+     * @param file to parse
+     * @return a new {@link XSSFWorkbook} instance
+     * @throws XSSFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XSSFWorkbook parse(File file) throws XSSFReadException, 
IOException {
+        try {
+            return new XSSFWorkbook(file);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        } catch (Exception e) {
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link OPCPackage} and return a new {@link 
XSSFWorkbook} instance.
+     *
+     * @param pkg to parse
+     * @return a new {@link XSSFWorkbook} instance
+     * @throws XSSFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XSSFWorkbook parse(OPCPackage pkg) throws XSSFReadException, 
IOException {
+        try {
+            return new XSSFWorkbook(pkg);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        } catch (Exception e) {
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link PackagePart} and return a new {@link 
XSSFWorkbook} instance.
+     *
+     * @param packagePart to parse
+     * @return a new {@link XSSFWorkbook} instance
+     * @throws XSSFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XSSFWorkbook parse(PackagePart packagePart) throws 
XSSFReadException, IOException {
+        try {
+            return new XSSFWorkbook(packagePart);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        } catch (Exception e) {
+            throw new XSSFReadException("Exception reading XSSFWorkbook", e);
+        }
+    }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFReadException.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFReadException.java
new file mode 100644
index 0000000000..545dd0ffbf
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/XSSFReadException.java
@@ -0,0 +1,56 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf;
+
+import org.apache.poi.POIException;
+
+/**
+ * An exception that indicates a problem reading an xlsx file.
+ * <p>This exception is only used by some new methods.
+ * Historically, POI has used {@link RuntimeException} for most of its
+ * exceptions, but this is not a good practice. This class is a checked
+ * class that extends {@link Exception} so needs to be explicitly
+ * caught or declared in the method signature.</p>
+ * @since POI 5.5.0
+ */
+public class XSSFReadException extends POIException {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Create a new {@code XSSFReadException} with
+     * the {@code String} specified as an error message.
+     *
+     * @param msg The error message for the exception.
+     */
+    public XSSFReadException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * Create a new {@code XSSFReadException} with
+     * the {@code String} specified as an error message and the cause.
+     *
+     * @param msg The error message for the exception.
+     * @param cause the cause (which is saved for later retrieval by the
+     *         {@link #getCause()} method).  (A {@code null} value is
+     *         permitted, and indicates that the cause is nonexistent or
+     *         unknown.)
+     */
+    public XSSFReadException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFParser.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFParser.java
new file mode 100644
index 0000000000..e4b8c00e81
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFParser.java
@@ -0,0 +1,169 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.util.ExceptionUtil;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+
+/**
+ * Methods that wrap {@link XWPFDocument} parsing functionality.
+ * One difference is that the methods in this class try to
+ * throw {@link XWPFReadException} or {@link IOException} instead of {@link 
RuntimeException}.
+ * You can still get an {@link Error}s like an {@link OutOfMemoryError}.
+ *
+ * @since POI 5.5.0
+ */
+public final class XWPFParser {
+
+    private XWPFParser() {
+        // Prevent instantiation
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XWPFDocument} 
instance.
+     *
+     * @param stream the data to parse (will be closed after parsing)
+     * @return a new {@link XWPFDocument} instance
+     * @throws XWPFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XWPFDocument parse(InputStream stream) throws 
XWPFReadException, IOException {
+        try {
+            return new XWPFDocument(stream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        } catch (Exception e) {
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XWPFDocument} 
instance.
+     *
+     * @param stream the data to parse
+     * @param closeStream whether to close the InputStream after parsing
+     * @return a new {@link XWPFDocument} instance
+     * @throws XWPFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XWPFDocument parse(InputStream stream, boolean closeStream) 
throws XWPFReadException, IOException {
+        try {
+            return new XWPFDocument(stream, closeStream);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        } catch (Exception e) {
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        }
+    }
+
+    /**
+     * Parse the given InputStream and return a new {@link XWPFDocument} 
instance.
+     *
+     * @param file to parse
+     * @return a new {@link XWPFDocument} instance
+     * @throws XWPFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XWPFDocument parse(File file) throws XWPFReadException, 
IOException {
+        OPCPackage pkg = null;
+        try {
+            pkg = OPCPackage.open(file);
+            return new XWPFDocument(pkg);
+        } catch (IOException e) {
+            if (pkg != null) {
+                IOUtils.closeQuietly(pkg);
+            }
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (pkg != null) {
+                IOUtils.closeQuietly(pkg);
+            }
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        } catch (Exception e) {
+            if (pkg != null) {
+                IOUtils.closeQuietly(pkg);
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link OPCPackage} and return a new {@link 
XWPFDocument} instance.
+     *
+     * @param pkg to parse
+     * @return a new {@link XWPFDocument} instance
+     * @throws XWPFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XWPFDocument parse(OPCPackage pkg) throws XWPFReadException, 
IOException {
+        try {
+            return new XWPFDocument(pkg);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        } catch (Exception e) {
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        }
+    }
+
+    /**
+     * Parse the given {@link PackagePart} and return a new {@link 
XWPFDocument} instance.
+     *
+     * @param packagePart to parse
+     * @return a new {@link XWPFDocument} instance
+     * @throws XWPFReadException if an error occurs while reading the file
+     * @throws IOException if an I/O error occurs while reading the file
+     */
+    public static XWPFDocument parse(PackagePart packagePart) throws 
XWPFReadException, IOException {
+        try (InputStream is = packagePart.getInputStream()) {
+            return new XWPFDocument(is);
+        } catch (IOException e) {
+            throw e;
+        } catch (Error | RuntimeException e) {
+            if (ExceptionUtil.isFatal(e)) {
+                throw e;
+            }
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        } catch (Exception e) {
+            throw new XWPFReadException("Exception reading XWPFDocument", e);
+        }
+    }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFReadException.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFReadException.java
new file mode 100644
index 0000000000..1b0e6a5bae
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/XWPFReadException.java
@@ -0,0 +1,56 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf;
+
+import org.apache.poi.POIException;
+
+/**
+ * An exception that indicates a problem reading a docx file.
+ * <p>This exception is only used by some new methods.
+ * Historically, POI has used {@link RuntimeException} for most of its
+ * exceptions, but this is not a good practice. This class is a checked
+ * class that extends {@link Exception} so needs to be explicitly
+ * caught or declared in the method signature.</p>
+ * @since POI 5.5.0
+ */
+public class XWPFReadException extends POIException {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Create a new {@code XWPFReadException} with
+     * the {@code String} specified as an error message.
+     *
+     * @param msg The error message for the exception.
+     */
+    public XWPFReadException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * Create a new {@code XWPFReadException} with
+     * the {@code String} specified as an error message and the cause.
+     *
+     * @param msg The error message for the exception.
+     * @param cause the cause (which is saved for later retrieval by the
+     *         {@link #getCause()} method).  (A {@code null} value is
+     *         permitted, and indicates that the cause is nonexistent or
+     *         unknown.)
+     */
+    public XWPFReadException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to