==================================================================== */\r
package org.apache.poi.hssf.usermodel.examples;\r
\r
+import java.io.Closeable;\r
import java.io.FileInputStream;\r
-import java.util.Iterator;\r
\r
import org.apache.poi.hslf.usermodel.HSLFSlideShow;\r
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;\r
import org.apache.poi.hssf.usermodel.HSSFObjectData;\r
import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
import org.apache.poi.hwpf.HWPFDocument;\r
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {\r
//the OLE2 Class Name of the object\r
String oleName = obj.getOLE2ClassName();\r
+ DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory() : null;\r
+ Closeable document = null;\r
if (oleName.equals("Worksheet")) {\r
- DirectoryNode dn = (DirectoryNode) obj.getDirectory();\r
- HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);\r
- //System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets());\r
- embeddedWorkbook.close();\r
+ document = new HSSFWorkbook(dn, fs, false);\r
} else if (oleName.equals("Document")) {\r
- DirectoryNode dn = (DirectoryNode) obj.getDirectory();\r
- HWPFDocument embeddedWordDocument = new HWPFDocument(dn);\r
- //System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text());\r
+ document = new HWPFDocument(dn);\r
} else if (oleName.equals("Presentation")) {\r
- DirectoryNode dn = (DirectoryNode) obj.getDirectory();\r
- HSLFSlideShow embeddedPowerPointDocument = new HSLFSlideShow(new HSLFSlideShowImpl(dn));\r
- //System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length);\r
+ document = new HSLFSlideShow(dn);\r
} else {\r
- if(obj.hasDirectoryEntry()){\r
+ if(dn != null){\r
// The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is\r
- DirectoryNode dn = (DirectoryNode) obj.getDirectory();\r
- for (Iterator<Entry> entries = dn.getEntries(); entries.hasNext();) {\r
- Entry entry = entries.next();\r
- //System.out.println(oleName + "." + entry.getName());\r
+ for (Entry entry : dn) {\r
+ String name = entry.getName();\r
}\r
} else {\r
// There is no DirectoryEntry\r
byte[] objectData = obj.getObjectData();\r
}\r
}\r
+ if (document != null) {\r
+ document.close();\r
+ }\r
}\r
workbook.close();\r
}\r
\r
private long length = -1L;\r
private int chunkSize = -1;\r
- \r
+\r
static class StreamDescriptorEntry {\r
static BitField flagStream = BitFieldFactory.getInstance(1);\r
- \r
+\r
int streamOffset;\r
int streamSize;\r
int block;\r
throws IOException, GeneralSecurityException {\r
return new CryptoAPICipherInputStream(stream, size, initialPos);\r
}\r
- \r
+\r
/**\r
* Decrypt the Document-/SummaryInformation and other optionally streams.\r
* Opposed to other crypto modes, cryptoapi is record based and can't be used\r
* to stream-decrypt a whole file\r
- * \r
+ *\r
* @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>\r
*/\r
public POIFSFileSystem getSummaryEntries(DirectoryNode root, String encryptedStream)\r
throws IOException, GeneralSecurityException {\r
- POIFSFileSystem fsOut = new POIFSFileSystem();\r
// HSLF: encryptedStream\r
// HSSF: encryption\r
DocumentNode es = (DocumentNode) root.getEntry(encryptedStream);\r
dis.close();\r
CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray());\r
LittleEndianInputStream leis = new LittleEndianInputStream(sbis);\r
+ POIFSFileSystem fsOut = null;\r
try {\r
int streamDescriptorArrayOffset = (int) leis.readUInt();\r
/* int streamDescriptorArraySize = (int) */ leis.readUInt();\r
leis.readShort();\r
assert(entry.streamName.length() == nameSize);\r
}\r
- \r
+\r
+ fsOut = new POIFSFileSystem();\r
for (StreamDescriptorEntry entry : entries) {\r
sbis.seek(entry.streamOffset);\r
sbis.setBlock(entry.block);\r
fsOut.createDocument(is, entry.streamName);\r
is.close();\r
}\r
+ } catch (Exception e) {\r
+ IOUtils.closeQuietly(fsOut);\r
+ if (e instanceof GeneralSecurityException) {\r
+ throw (GeneralSecurityException)e;\r
+ } else if (e instanceof IOException) {\r
+ throw (IOException)e;\r
+ } else {\r
+ throw new IOException("summary entries can't be read", e);\r
+ }\r
} finally {\r
IOUtils.closeQuietly(leis);\r
IOUtils.closeQuietly(sbis);\r
}\r
- sbis = null;\r
return fsOut;\r
}\r
\r
return length;\r
}\r
\r
+ @Override\r
public void setChunkSize(int chunkSize) {\r
this.chunkSize = chunkSize;\r
}\r
- \r
+\r
@Override\r
public CryptoAPIDecryptor clone() throws CloneNotSupportedException {\r
return (CryptoAPIDecryptor)super.clone();\r
public CryptoAPICipherInputStream(InputStream stream, long size, int initialPos)\r
throws GeneralSecurityException {\r
super(stream, size, chunkSize, initialPos);\r
- } \r
+ }\r
}\r
}\r
ZipEntrySource ze;
try {
- final ZipFile zipFile = ZipHelper.openZipFile(file);
+ final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR
ze = new ZipFileZipEntrySource(zipFile);
} catch (IOException e) {
// probably not happening with write access - not sure how to handle the default read-write access ...
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream
try {
// open the file input stream
- fis = new FileInputStream(file);
+ fis = new FileInputStream(file); // NOSONAR
} catch (final FileNotFoundException e) {
// If the source cannot be acquired, abort (no resources to free at this level)
throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e);
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream
try {
// open the zip input stream
- zis = ZipHelper.openZipStream(fis);
+ zis = ZipHelper.openZipStream(fis); // NOSONAR
} catch (final IOException e) {
// If the source cannot be acquired, abort (no resources to free at this level)
throw new InvalidOperationException("Could not open the file input stream", e);