more parsers

This commit is contained in:
PJ Fanning 2025-08-20 18:34:09 +01:00
parent 54f1f5deac
commit 061d6a3d0f
6 changed files with 350 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/* ====================================================================
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.hslf;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.util.ExceptionUtil;
/**
* Methods that wrap {@link HSLFSlideShow} parsing functionality.
* One difference is that the methods in this class try to
* throw {@link HSLFReadException} 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 HSLFParser {
private HSLFParser() {
// Prevent instantiation
}
/**
* Parse the given InputStream and return a new {@link HSLFSlideShow} instance.
*
* @param stream the data to parse (will be closed after parsing)
* @return a new {@link HSLFSlideShow} instance
* @throws HSLFReadException if an error occurs while reading the file
* @throws IOException if an I/O error occurs while reading the file
*/
public static HSLFSlideShow parse(InputStream stream) throws HSLFReadException, IOException {
try {
return new HSLFSlideShow(stream);
} catch (IOException e) {
throw e;
} catch (Error | RuntimeException e) {
if (ExceptionUtil.isFatal(e)) {
throw e;
}
throw new HSLFReadException("Exception reading HSLFSlideShow", e);
} catch (Exception e) {
throw new HSLFReadException("Exception reading HSLFSlideShow", e);
}
}
}

View File

@ -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.hslf;
import org.apache.poi.POIException;
/**
* An exception that indicates a problem reading a ppt 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 HSLFReadException extends POIException {
private static final long serialVersionUID = 1L;
/**
* Create a new {@code HSLFReadException} with
* the {@code String} specified as an error message.
*
* @param msg The error message for the exception.
*/
public HSLFReadException(String msg) {
super(msg);
}
/**
* Create a new {@code HSLFReadException} 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 HSLFReadException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@ -0,0 +1,60 @@
/* ====================================================================
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.hwpf;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.util.ExceptionUtil;
/**
* Methods that wrap {@link HWPFDocument} parsing functionality.
* One difference is that the methods in this class try to
* throw {@link HWPFReadException} 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 HWPFParser {
private HWPFParser() {
// Prevent instantiation
}
/**
* Parse the given InputStream and return a new {@link HWPFDocument} instance.
*
* @param stream the data to parse (will be closed after parsing)
* @return a new {@link HWPFDocument} instance
* @throws HWPFReadException if an error occurs while reading the file
* @throws IOException if an I/O error occurs while reading the file
*/
public static HWPFDocument parse(InputStream stream) throws HWPFReadException, IOException {
try {
return new HWPFDocument(stream);
} catch (IOException e) {
throw e;
} catch (Error | RuntimeException e) {
if (ExceptionUtil.isFatal(e)) {
throw e;
}
throw new HWPFReadException("Exception reading HWPFDocument", e);
} catch (Exception e) {
throw new HWPFReadException("Exception reading HWPFDocument", e);
}
}
}

View File

@ -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.hwpf;
import org.apache.poi.POIException;
/**
* An exception that indicates a problem reading a doc 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 HWPFReadException extends POIException {
private static final long serialVersionUID = 1L;
/**
* Create a new {@code HWPFReadException} with
* the {@code String} specified as an error message.
*
* @param msg The error message for the exception.
*/
public HWPFReadException(String msg) {
super(msg);
}
/**
* Create a new {@code HWPFReadException} 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 HWPFReadException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@ -0,0 +1,61 @@
/* ====================================================================
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.hssf;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.ExceptionUtil;
/**
* Methods that wrap {@link HSSFWorkbook} parsing functionality.
* One difference is that the methods in this class try to
* throw {@link HSSFReadException} 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 HSSFParser {
private HSSFParser() {
// Prevent instantiation
}
/**
* Parse the given InputStream and return a new {@link HSSFWorkbook} instance.
*
* @param stream the data to parse (will be closed after parsing)
* @return a new {@link HSSFWorkbook} instance
* @throws HSSFReadException if an error occurs while reading the file
* @throws IOException if an I/O error occurs while reading the file
*/
public static HSSFWorkbook parse(InputStream stream) throws HSSFReadException, IOException {
try {
return new HSSFWorkbook(stream);
} catch (IOException e) {
throw e;
} catch (Error | RuntimeException e) {
if (ExceptionUtil.isFatal(e)) {
throw e;
}
throw new HSSFReadException("Exception reading HSSFWorkbook", e);
} catch (Exception e) {
throw new HSSFReadException("Exception reading HSSFWorkbook", e);
}
}
}

View File

@ -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.hssf;
import org.apache.poi.POIException;
/**
* An exception that indicates a problem reading an xls 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 HSSFReadException extends POIException {
private static final long serialVersionUID = 1L;
/**
* Create a new {@code HSSFReadException} with
* the {@code String} specified as an error message.
*
* @param msg The error message for the exception.
*/
public HSSFReadException(String msg) {
super(msg);
}
/**
* Create a new {@code HSSFReadException} 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 HSSFReadException(String msg, Throwable cause) {
super(msg, cause);
}
}