+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.sl;\r
-\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.PushbackInputStream;\r
-import java.security.GeneralSecurityException;\r
-\r
-import org.apache.poi.EmptyFileException;\r
-import org.apache.poi.EncryptedDocumentException;\r
-import org.apache.poi.POIXMLDocument;\r
-import org.apache.poi.hslf.usermodel.HSLFSlideShow;\r
-import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;\r
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;\r
-import org.apache.poi.openxml4j.opc.OPCPackage;\r
-import org.apache.poi.openxml4j.opc.PackageAccess;\r
-import org.apache.poi.poifs.crypt.Decryptor;\r
-import org.apache.poi.poifs.crypt.EncryptionInfo;\r
-import org.apache.poi.poifs.filesystem.DirectoryNode;\r
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;\r
-import org.apache.poi.poifs.filesystem.OfficeXmlFileException;\r
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
-import org.apache.poi.sl.usermodel.SlideShow;\r
-import org.apache.poi.util.IOUtils;\r
-import org.apache.poi.xslf.usermodel.XMLSlideShow;\r
-\r
-public class SlideShowFactory {\r
- /**\r
- * Creates a HSLFSlideShow from the given POIFSFileSystem\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.\r
- */\r
- public static SlideShow create(POIFSFileSystem fs) throws IOException {\r
- return new HSLFSlideShow(fs);\r
- }\r
-\r
- /**\r
- * Creates a HSLFSlideShow from the given NPOIFSFileSystem\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.\r
- */\r
- public static SlideShow create(NPOIFSFileSystem fs) throws IOException {\r
- try {\r
- return create(fs, null);\r
- } catch (InvalidFormatException e) {\r
- // Special case of OOXML-in-POIFS which is broken\r
- throw new IOException(e);\r
- }\r
- }\r
-\r
- /**\r
- * Creates a SlideShow from the given NPOIFSFileSystem, which may\r
- * be password protected\r
- *\r
- * @param fs The {@link NPOIFSFileSystem} to read the document from\r
- * @param password The password that should be used or null if no password is necessary.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- */\r
- private static SlideShow create(NPOIFSFileSystem fs, String password) throws IOException, InvalidFormatException {\r
- DirectoryNode root = fs.getRoot();\r
-\r
- // Encrypted OOXML files go inside OLE2 containers, is this one?\r
- if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {\r
- EncryptionInfo info = new EncryptionInfo(fs);\r
- Decryptor d = Decryptor.getInstance(info);\r
-\r
- boolean passwordCorrect = false;\r
- InputStream stream = null;\r
- try {\r
- if (password != null && d.verifyPassword(password)) {\r
- passwordCorrect = true;\r
- }\r
- if (!passwordCorrect && d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) {\r
- passwordCorrect = true;\r
- }\r
- if (passwordCorrect) {\r
- stream = d.getDataStream(root);\r
- }\r
- } catch (GeneralSecurityException e) {\r
- throw new IOException(e);\r
- }\r
-\r
- if (! passwordCorrect) {\r
- if (password != null)\r
- throw new EncryptedDocumentException("Password incorrect");\r
- else\r
- throw new EncryptedDocumentException("The supplied spreadsheet is protected, but no password was supplied");\r
- }\r
-\r
- OPCPackage pkg = OPCPackage.open(stream);\r
- return create(pkg);\r
- }\r
-\r
- // If we get here, it isn't an encrypted PPTX file\r
- // So, treat it as a regular HSLF PPT one\r
- if (password != null) {\r
- Biff8EncryptionKey.setCurrentUserPassword(password);\r
- }\r
- SlideShow wb = new HSLFSlideShow(root);\r
- Biff8EncryptionKey.setCurrentUserPassword(null);\r
- return wb;\r
- }\r
-\r
- /**\r
- * Creates a XMLSlideShow from the given OOXML Package\r
- *\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.</p>\r
- *\r
- * @param pkg The {@link OPCPackage} opened for reading data.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- */\r
- public static SlideShow create(OPCPackage pkg) throws IOException {\r
- return new XMLSlideShow(pkg);\r
- }\r
-\r
- /**\r
- * Creates the appropriate HSLFSlideShow / XMLSlideShow from\r
- * the given InputStream.\r
- *\r
- * <p>Your input stream MUST either support mark/reset, or\r
- * be wrapped as a {@link PushbackInputStream}! Note that\r
- * using an {@link InputStream} has a higher memory footprint\r
- * than using a {@link File}.</p>\r
- *\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use. Note also that loading\r
- * from an InputStream requires more memory than loading\r
- * from a File, so prefer {@link #create(File)} where possible.\r
- *\r
- * @param inp The {@link InputStream} to read data from.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- * @throws EncryptedDocumentException If the SlideShow given is password protected\r
- */\r
- public static SlideShow create(InputStream inp) throws IOException, InvalidFormatException, EncryptedDocumentException {\r
- return create(inp, null);\r
- }\r
-\r
- /**\r
- * Creates the appropriate HSLFSlideShow / XMLSlideShow from\r
- * the given InputStream, which may be password protected.\r
- * <p>Your input stream MUST either support mark/reset, or\r
- * be wrapped as a {@link PushbackInputStream}! Note that\r
- * using an {@link InputStream} has a higher memory footprint\r
- * than using a {@link File}.</p>\r
- *\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use. Note also that loading\r
- * from an InputStream requires more memory than loading\r
- * from a File, so prefer {@link #create(File)} where possible.</p>\r
- *\r
- * @param inp The {@link InputStream} to read data from.\r
- * @param password The password that should be used or null if no password is necessary.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- * @throws EncryptedDocumentException If the wrong password is given for a protected file\r
- * @throws EmptyFileException If an empty stream is given\r
- */\r
- public static SlideShow create(InputStream inp, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {\r
- // If clearly doesn't do mark/reset, wrap up\r
- if (! inp.markSupported()) {\r
- inp = new PushbackInputStream(inp, 8);\r
- }\r
-\r
- // Ensure that there is at least some data there\r
- byte[] header8 = IOUtils.peekFirst8Bytes(inp);\r
-\r
- // Try to create\r
- if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {\r
- NPOIFSFileSystem fs = new NPOIFSFileSystem(inp);\r
- return create(fs, password);\r
- }\r
- if (POIXMLDocument.hasOOXMLHeader(inp)) {\r
- return new XMLSlideShow(OPCPackage.open(inp));\r
- }\r
- throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");\r
- }\r
-\r
- /**\r
- * Creates the appropriate HSLFSlideShow / XMLSlideShow from\r
- * the given File, which must exist and be readable.\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.\r
- *\r
- * @param file The file to read data from.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- * @throws EncryptedDocumentException If the SlideShow given is password protected\r
- */\r
- public static SlideShow create(File file) throws IOException, InvalidFormatException, EncryptedDocumentException {\r
- return create(file, null);\r
- }\r
-\r
- /**\r
- * Creates the appropriate HSLFSlideShow / XMLSlideShow from\r
- * the given File, which must exist and be readable, and\r
- * may be password protected\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.\r
- *\r
- * @param file The file to read data from.\r
- * @param password The password that should be used or null if no password is necessary.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- * @throws EncryptedDocumentException If the wrong password is given for a protected file\r
- * @throws EmptyFileException If an empty stream is given\r
- */\r
- public static SlideShow create(File file, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {\r
- return create(file, password, false);\r
- }\r
-\r
- /**\r
- * Creates the appropriate HSLFSlideShow / XMLSlideShow from\r
- * the given File, which must exist and be readable, and\r
- * may be password protected\r
- * <p>Note that in order to properly release resources the\r
- * SlideShow should be closed after use.\r
- *\r
- * @param file The file to read data from.\r
- * @param password The password that should be used or null if no password is necessary.\r
- * @param readOnly If the SlideShow should be opened in read-only mode to avoid writing back\r
- * changes when the document is closed.\r
- *\r
- * @return The created SlideShow\r
- *\r
- * @throws IOException if an error occurs while reading the data\r
- * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link SlideShow}\r
- * @throws EncryptedDocumentException If the wrong password is given for a protected file\r
- * @throws EmptyFileException If an empty stream is given\r
- */\r
- public static SlideShow create(File file, String password, boolean readOnly) throws IOException, InvalidFormatException, EncryptedDocumentException {\r
- if (! file.exists()) {\r
- throw new FileNotFoundException(file.toString());\r
- }\r
-\r
- try {\r
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly);\r
- return create(fs, password);\r
- } catch(OfficeXmlFileException e) {\r
- // opening as .ppt failed => try opening as .pptx\r
- OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE);\r
- try {\r
- return new XMLSlideShow(pkg);\r
-// } catch (IOException ioe) {\r
-// // ensure that file handles are closed (use revert() to not re-write the file)\r
-// pkg.revert();\r
-// //pkg.close();\r
-//\r
-// // rethrow exception\r
-// throw ioe;\r
- } catch (IllegalArgumentException ioe) {\r
- // ensure that file handles are closed (use revert() to not re-write the file)\r
- pkg.revert();\r
- //pkg.close();\r
-\r
- // rethrow exception\r
- throw ioe;\r
- }\r
- }\r
- }\r
-}\r