From: Andreas Beeker Date: Tue, 17 May 2016 23:34:52 +0000 (+0000) Subject: findbugs fixes X-Git-Tag: REL_3_15_BETA2~239 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ee62ac573ccf4791b553b2e70c2a77d8f15d33e5;p=poi.git findbugs fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744332 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index 2c5716d2a5..f0b14bffce 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -35,10 +35,14 @@ import org.apache.poi.poifs.filesystem.POIFSWriterListener; import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.TempFile; @Internal public abstract class ChunkedCipherOutputStream extends FilterOutputStream { + private static final POILogger logger = POILogFactory.getLogger(ChunkedCipherOutputStream.class); + protected final int chunkSize; protected final int chunkMask; protected final int chunkBits; @@ -67,9 +71,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { protected abstract Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk) throws GeneralSecurityException; + @SuppressWarnings("hiding") protected abstract void calculateChecksum(File fileOut, int oleStreamSize) throws GeneralSecurityException, IOException; + @SuppressWarnings("hiding") protected abstract void createEncryptionInfoEntry(DirectoryNode dir, File tmpFile) throws IOException, GeneralSecurityException; @@ -164,7 +170,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { os.close(); - fileOut.delete(); + if (!fileOut.delete()) { + logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut); + } } catch (IOException e) { throw new EncryptedDocumentException(e); } diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index b782d83299..fded6c6061 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -49,9 +49,13 @@ import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndianByteArrayOutputStream; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianOutputStream; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.TempFile; public class StandardEncryptor extends Encryptor { + private static final POILogger logger = POILogFactory.getLogger(StandardEncryptor.class); + private final StandardEncryptionInfoBuilder builder; protected StandardEncryptor(StandardEncryptionInfoBuilder builder) { @@ -184,7 +188,9 @@ public class StandardEncryptor extends Encryptor { FileInputStream fis = new FileInputStream(fileOut); IOUtils.copy(fis, leos); fis.close(); - fileOut.delete(); + if (!fileOut.delete()) { + logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut); + } leos.close(); } catch (IOException e) { diff --git a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java b/src/ooxml/java/org/apache/poi/util/OOXMLLite.java index d8a72391e5..b320771238 100644 --- a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java +++ b/src/ooxml/java/org/apache/poi/util/OOXMLLite.java @@ -278,7 +278,10 @@ public final class OOXMLLite { private static void copyFile(InputStream srcStream, File destFile) throws IOException { File destDirectory = destFile.getParentFile(); - destDirectory.mkdirs(); + if (!(destDirectory.exists() || destDirectory.mkdirs())) { + throw new RuntimeException("Can't create destination directory: "+destDirectory); + } + ; OutputStream destStream = new FileOutputStream(destFile); try { IOUtils.copy(srcStream, destStream); diff --git a/src/ooxml/java/org/apache/poi/util/XmlSort.java b/src/ooxml/java/org/apache/poi/util/XmlSort.java index 483b657d54..16a444e425 100644 --- a/src/ooxml/java/org/apache/poi/util/XmlSort.java +++ b/src/ooxml/java/org/apache/poi/util/XmlSort.java @@ -17,83 +17,12 @@ package org.apache.poi.util; -import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; - -import java.io.File; -import java.io.IOException; -import java.io.Serializable; import java.util.Comparator; -import javax.xml.namespace.QName; - import org.apache.xmlbeans.XmlCursor; -import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; -/** - */ -public final class XmlSort -{ - /** - * Receives an XML element instance and sorts the children of this - * element in lexicographical (by default) order. - * - * @param args An array in which the first item is a - * path to the XML instance file and the second item (optional) is - * an XPath inside the document identifying the element to be sorted - */ - public static void main(String[] args) - { - if (args.length < 1 || args.length > 2) - { - System.out.println(" java XmlSort []"); - return; - } - File f = new File(args[0]); - try - { - XmlObject docInstance = XmlObject.Factory.parse(f, DEFAULT_XML_OPTIONS); - XmlObject element = null; - if (args.length > 1) - { - String xpath = args[1]; - XmlObject[] result = docInstance.selectPath(xpath); - if (result.length == 0) - { - System.out.println("ERROR: XPath \"" + xpath + "\" did not return any results"); - } - else if (result.length > 1) - { - System.out.println("ERROR: XPath \"" + xpath + "\" returned more than one " + - "node (" + result.length + ")"); - } - else - element = result[0]; - } - else - { - // Navigate to the root element - XmlCursor c = docInstance.newCursor(); - c.toFirstChild(); - element = c.getObject(); - c.dispose(); - } - if (element != null) - sort(element, new QNameComparator(QNameComparator.ASCENDING)); - System.out.println(docInstance.xmlText()); - } - catch (IOException ioe) - { - System.out.println("ERROR: Could not open file: \"" + args[0] + "\": " + - ioe.getMessage()); - } - catch (XmlException xe) - { - System.out.println("ERROR: Could not parse file: \"" + args[0] + "\": " + - xe.getMessage()); - } - } - +public final class XmlSort { /** * Sorts the children of element according to the order indicated by the * comparator. @@ -108,11 +37,11 @@ public final class XmlSort * @throws IllegalArgumentException if the input XmlObject does not represent * an element */ - public static void sort(XmlObject element, Comparator comp) - { + public static void sort(XmlObject element, Comparator comp) { XmlCursor headCursor = element.newCursor(); - if (!headCursor.isStart()) + if (!headCursor.isStart()) { throw new IllegalStateException("The element parameter must point to a STARTDOC"); + } // We use insertion sort to minimize the number of swaps, because each swap means // moving a part of the document /* headCursor points to the beginning of the list of the already sorted items and @@ -121,44 +50,21 @@ public final class XmlSort second element. The algorithm ends when listCursor cannot be moved to the "next" element in the unsorted list, i.e. the unsorted list becomes empty */ boolean moved = headCursor.toFirstChild(); - if (!moved) - { + if (!moved) { // Cursor was not moved, which means that the given element has no children and // therefore there is nothing to sort return; } XmlCursor listCursor = headCursor.newCursor(); boolean moreElements = listCursor.toNextSibling(); - while (moreElements) - { + while (moreElements) { moved = false; // While we can move the head of the unsorted list, it means that there are still // items (elements) that need to be sorted - while (headCursor.comparePosition(listCursor) < 0) - { - if (comp.compare(headCursor, listCursor) > 0) - { + while (headCursor.comparePosition(listCursor) < 0) { + if (comp.compare(headCursor, listCursor) > 0) { // We have found the position in the sorted list, insert the element and the // text following the element in the current position - /* - * Uncomment this code to cause the text before the element to move along - * with the element, rather than the text after the element. Notice that this - * is more difficult to do, because the cursor's "type" refers to the position - * to the right of the cursor, so to get the type of the token to the left, the - * cursor needs to be first moved to the left (previous token) - * - headCursor.toPrevToken(); - while (headCursor.isComment() || headCursor.isProcinst() || headCursor.isText()) - headCursor.toPrevToken(); - headCursor.toNextToken(); - listCursor.toPrevToken(); - while (listCursor.isComment() || listCursor.isProcinst() || listCursor.isText()) - listCursor.toPrevToken(); - listCursor.toNextToken(); - while (!listCursor.isStart()) - listCursor.moveXml(headCursor); - listCursor.moveXml(headCursor); - */ // Move the element listCursor.moveXml(headCursor); // Move the text following the element @@ -170,8 +76,7 @@ public final class XmlSort } headCursor.toNextSibling(); } - if (!moved) - { + if (!moved) { // Because during the move of a fragment of XML, the listCursor is also moved, in // case we didn't need to move XML (the new element to be inserted happened to // be the last one in order), we need to move this cursor @@ -182,40 +87,5 @@ public final class XmlSort headCursor.toFirstChild(); } } - - /** - * Implements a java.util.Comparator for comparing QNamevalues. - * The namespace URIs are compared first and if they are equal, the local parts are compared. - *

- * The constructor accepts an argument indicating whether the comparison order is the same as - * the lexicographic order of the strings or the reverse. - */ - public static final class QNameComparator implements Comparator, Serializable - { - public static final int ASCENDING = 1; - public static final int DESCENDING = 2; - - private int order; - - public QNameComparator(int order) - { - this.order = order; - if (order != ASCENDING && order != DESCENDING) - throw new IllegalArgumentException("Please specify one of ASCENDING or DESCENDING "+ - "comparison orders"); - } - - public int compare(XmlCursor cursor1, XmlCursor cursor2) { - QName qname1 = cursor1.getName(); - QName qname2 = cursor2.getName(); - int qnameComparisonRes = qname1.getNamespaceURI().compareTo(qname2.getNamespaceURI()); - if (qnameComparisonRes == 0) - return order == ASCENDING ? - qname1.getLocalPart().compareTo(qname2.getLocalPart()) : - -qname1.getLocalPart().compareTo(qname2.getLocalPart()); - else - return order == ASCENDING ? qnameComparisonRes : -qnameComparisonRes; - } - } } \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java index 014fc189be..4782ca2fd8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java @@ -33,6 +33,8 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.usermodel.XSSFRichTextString; @@ -45,6 +47,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; * so that it was renamed to "SheetDataWriter" */ public class SheetDataWriter { + private static final POILogger logger = POILogFactory.getLogger(SheetDataWriter.class); + private final File _fd; private final Writer _out; private int _rownum; @@ -128,7 +132,9 @@ public class SheetDataWriter { @Override protected void finalize() throws Throwable { - _fd.delete(); + if (!_fd.delete()) { + logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+_fd); + } super.finalize(); }