SonarQube fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777526 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-01-05 23:40:57 +00:00
parent 2b3aa29c53
commit fd1ff9f2db
3 changed files with 34 additions and 29 deletions

View File

@ -16,11 +16,10 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.usermodel.examples; package org.apache.poi.hssf.usermodel.examples;
import java.io.Closeable;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hssf.usermodel.HSSFObjectData; import org.apache.poi.hssf.usermodel.HSSFObjectData;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocument;
@ -39,26 +38,19 @@ public class EmbeddedObjects {
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) { for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
//the OLE2 Class Name of the object //the OLE2 Class Name of the object
String oleName = obj.getOLE2ClassName(); String oleName = obj.getOLE2ClassName();
DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory() : null;
Closeable document = null;
if (oleName.equals("Worksheet")) { if (oleName.equals("Worksheet")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory(); document = new HSSFWorkbook(dn, fs, false);
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);
//System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets());
embeddedWorkbook.close();
} else if (oleName.equals("Document")) { } else if (oleName.equals("Document")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory(); document = new HWPFDocument(dn);
HWPFDocument embeddedWordDocument = new HWPFDocument(dn);
//System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text());
} else if (oleName.equals("Presentation")) { } else if (oleName.equals("Presentation")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory(); document = new HSLFSlideShow(dn);
HSLFSlideShow embeddedPowerPointDocument = new HSLFSlideShow(new HSLFSlideShowImpl(dn));
//System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length);
} else { } else {
if(obj.hasDirectoryEntry()){ if(dn != null){
// The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
DirectoryNode dn = (DirectoryNode) obj.getDirectory(); for (Entry entry : dn) {
for (Iterator<Entry> entries = dn.getEntries(); entries.hasNext();) { String name = entry.getName();
Entry entry = entries.next();
//System.out.println(oleName + "." + entry.getName());
} }
} else { } else {
// There is no DirectoryEntry // There is no DirectoryEntry
@ -66,6 +58,9 @@ public class EmbeddedObjects {
byte[] objectData = obj.getObjectData(); byte[] objectData = obj.getObjectData();
} }
} }
if (document != null) {
document.close();
}
} }
workbook.close(); workbook.close();
} }

View File

@ -159,7 +159,6 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
*/ */
public POIFSFileSystem getSummaryEntries(DirectoryNode root, String encryptedStream) public POIFSFileSystem getSummaryEntries(DirectoryNode root, String encryptedStream)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
POIFSFileSystem fsOut = new POIFSFileSystem();
// HSLF: encryptedStream // HSLF: encryptedStream
// HSSF: encryption // HSSF: encryption
DocumentNode es = (DocumentNode) root.getEntry(encryptedStream); DocumentNode es = (DocumentNode) root.getEntry(encryptedStream);
@ -169,6 +168,7 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
dis.close(); dis.close();
CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray()); CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray());
LittleEndianInputStream leis = new LittleEndianInputStream(sbis); LittleEndianInputStream leis = new LittleEndianInputStream(sbis);
POIFSFileSystem fsOut = null;
try { try {
int streamDescriptorArrayOffset = (int) leis.readUInt(); int streamDescriptorArrayOffset = (int) leis.readUInt();
/* int streamDescriptorArraySize = (int) */ leis.readUInt(); /* int streamDescriptorArraySize = (int) */ leis.readUInt();
@ -194,6 +194,7 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
assert(entry.streamName.length() == nameSize); assert(entry.streamName.length() == nameSize);
} }
fsOut = new POIFSFileSystem();
for (StreamDescriptorEntry entry : entries) { for (StreamDescriptorEntry entry : entries) {
sbis.seek(entry.streamOffset); sbis.seek(entry.streamOffset);
sbis.setBlock(entry.block); sbis.setBlock(entry.block);
@ -201,11 +202,19 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
fsOut.createDocument(is, entry.streamName); fsOut.createDocument(is, entry.streamName);
is.close(); is.close();
} }
} catch (Exception e) {
IOUtils.closeQuietly(fsOut);
if (e instanceof GeneralSecurityException) {
throw (GeneralSecurityException)e;
} else if (e instanceof IOException) {
throw (IOException)e;
} else {
throw new IOException("summary entries can't be read", e);
}
} finally { } finally {
IOUtils.closeQuietly(leis); IOUtils.closeQuietly(leis);
IOUtils.closeQuietly(sbis); IOUtils.closeQuietly(sbis);
} }
sbis = null;
return fsOut; return fsOut;
} }
@ -220,6 +229,7 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
return length; return length;
} }
@Override
public void setChunkSize(int chunkSize) { public void setChunkSize(int chunkSize) {
this.chunkSize = chunkSize; this.chunkSize = chunkSize;
} }

View File

@ -131,7 +131,7 @@ public final class ZipPackage extends OPCPackage {
ZipEntrySource ze; ZipEntrySource ze;
try { try {
final ZipFile zipFile = ZipHelper.openZipFile(file); final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR
ze = new ZipFileZipEntrySource(zipFile); ze = new ZipFileZipEntrySource(zipFile);
} catch (IOException e) { } catch (IOException e) {
// probably not happening with write access - not sure how to handle the default read-write access ... // probably not happening with write access - not sure how to handle the default read-write access ...
@ -149,7 +149,7 @@ public final class ZipPackage extends OPCPackage {
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
try { try {
// open the file input stream // open the file input stream
fis = new FileInputStream(file); fis = new FileInputStream(file); // NOSONAR
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
// If the source cannot be acquired, abort (no resources to free at this level) // 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); throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e);
@ -175,7 +175,7 @@ public final class ZipPackage extends OPCPackage {
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
try { try {
// open the zip input stream // open the zip input stream
zis = ZipHelper.openZipStream(fis); zis = ZipHelper.openZipStream(fis); // NOSONAR
} catch (final IOException e) { } catch (final IOException e) {
// If the source cannot be acquired, abort (no resources to free at this level) // 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); throw new InvalidOperationException("Could not open the file input stream", e);