]> source.dussan.org Git - poi.git/commitdiff
findbugs fixes
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 17 May 2016 23:34:52 +0000 (23:34 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 17 May 2016 23:34:52 +0000 (23:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744332 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java
src/ooxml/java/org/apache/poi/util/OOXMLLite.java
src/ooxml/java/org/apache/poi/util/XmlSort.java
src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java

index 2c5716d2a537da558880715e45ed1663d68ed269..f0b14bffce54eed252c245b889e63dd356f1554c 100644 (file)
@@ -35,10 +35,14 @@ import org.apache.poi.poifs.filesystem.POIFSWriterListener;
 import org.apache.poi.util.Internal;\r
 import org.apache.poi.util.LittleEndian;\r
 import org.apache.poi.util.LittleEndianConsts;\r
+import org.apache.poi.util.POILogFactory;\r
+import org.apache.poi.util.POILogger;\r
 import org.apache.poi.util.TempFile;\r
 \r
 @Internal\r
 public abstract class ChunkedCipherOutputStream extends FilterOutputStream {\r
+    private static final POILogger logger = POILogFactory.getLogger(ChunkedCipherOutputStream.class);\r
+    \r
     protected final int chunkSize;\r
     protected final int chunkMask;\r
     protected final int chunkBits;\r
@@ -67,9 +71,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
     protected abstract Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk)\r
     throws GeneralSecurityException;    \r
     \r
+    @SuppressWarnings("hiding")\r
     protected abstract void calculateChecksum(File fileOut, int oleStreamSize)\r
     throws GeneralSecurityException, IOException;\r
     \r
+    @SuppressWarnings("hiding")\r
     protected abstract void createEncryptionInfoEntry(DirectoryNode dir, File tmpFile)\r
     throws IOException, GeneralSecurityException;\r
 \r
@@ -164,7 +170,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
 \r
                 os.close();\r
                 \r
-                fileOut.delete();\r
+                if (!fileOut.delete()) {\r
+                    logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);\r
+                }\r
             } catch (IOException e) {\r
                 throw new EncryptedDocumentException(e);\r
             }\r
index b782d8329947a080480233d8435523277fc2df30..fded6c6061e633dc7fadcd34768f74c584c52305 100644 (file)
@@ -49,9 +49,13 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LittleEndianByteArrayOutputStream;\r
 import org.apache.poi.util.LittleEndianConsts;\r
 import org.apache.poi.util.LittleEndianOutputStream;\r
+import org.apache.poi.util.POILogFactory;\r
+import org.apache.poi.util.POILogger;\r
 import org.apache.poi.util.TempFile;\r
 \r
 public class StandardEncryptor extends Encryptor {\r
+    private static final POILogger logger = POILogFactory.getLogger(StandardEncryptor.class);\r
+\r
     private final StandardEncryptionInfoBuilder builder;\r
     \r
     protected StandardEncryptor(StandardEncryptionInfoBuilder builder) {\r
@@ -184,7 +188,9 @@ public class StandardEncryptor extends Encryptor {
                 FileInputStream fis = new FileInputStream(fileOut);\r
                 IOUtils.copy(fis, leos);\r
                 fis.close();\r
-                fileOut.delete();\r
+                if (!fileOut.delete()) {\r
+                    logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);\r
+                }\r
 \r
                 leos.close();\r
             } catch (IOException e) {\r
index d8a72391e57d3d57a88ff15399b15e71221523f6..b3207712383d9aacadbc9d41cc27f92c7cf12602 100644 (file)
@@ -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);
index 483b657d54ec718f057b56e6675b79fd1db3e807..16a444e4254abc5affc7c16780217d79856bc829 100644 (file)
 \r
 package org.apache.poi.util;\r
 \r
-import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.Serializable;\r
 import java.util.Comparator;\r
 \r
-import javax.xml.namespace.QName;\r
-\r
 import org.apache.xmlbeans.XmlCursor;\r
-import org.apache.xmlbeans.XmlException;\r
 import org.apache.xmlbeans.XmlObject;\r
 \r
-/**\r
- */\r
-public final class XmlSort\r
-{\r
-    /**\r
-     * Receives an XML element instance and sorts the children of this\r
-     * element in lexicographical (by default) order.\r
-     *\r
-     * @param args An array in which the first item is a\r
-     * path to the XML instance file and the second item (optional) is\r
-     * an XPath inside the document identifying the element to be sorted\r
-     */\r
-    public static void main(String[] args)\r
-    {\r
-        if (args.length < 1 || args.length > 2)\r
-        {\r
-            System.out.println("    java XmlSort <XML_File> [<XPath>]");\r
-            return;\r
-        }\r
-        File f = new File(args[0]);\r
-        try\r
-        {\r
-            XmlObject docInstance = XmlObject.Factory.parse(f, DEFAULT_XML_OPTIONS);\r
-            XmlObject element = null;\r
-            if (args.length > 1)\r
-            {\r
-                String xpath = args[1];\r
-                XmlObject[] result = docInstance.selectPath(xpath);\r
-                if (result.length == 0)\r
-                {\r
-                    System.out.println("ERROR: XPath \"" + xpath + "\" did not return any results");\r
-                }\r
-                else if (result.length > 1)\r
-                {\r
-                    System.out.println("ERROR: XPath \"" + xpath + "\" returned more than one " +\r
-                        "node (" + result.length + ")");\r
-                }\r
-                else\r
-                    element = result[0];\r
-            }\r
-            else\r
-            {\r
-                // Navigate to the root element\r
-                XmlCursor c = docInstance.newCursor();\r
-                c.toFirstChild();\r
-                element = c.getObject();\r
-                c.dispose();\r
-            }\r
-            if (element != null)\r
-                sort(element, new QNameComparator(QNameComparator.ASCENDING));\r
-            System.out.println(docInstance.xmlText());\r
-        }\r
-        catch (IOException ioe)\r
-        {\r
-            System.out.println("ERROR: Could not open file: \"" + args[0] + "\": " +\r
-                ioe.getMessage());\r
-        }\r
-        catch (XmlException xe)\r
-        {\r
-            System.out.println("ERROR: Could not parse file: \"" + args[0] + "\": " +\r
-                xe.getMessage());\r
-        }\r
-    }\r
-\r
+public final class XmlSort {\r
     /**\r
      * Sorts the children of <code>element</code> according to the order indicated by the\r
      * comparator.\r
@@ -108,11 +37,11 @@ public final class XmlSort
      * @throws IllegalArgumentException if the input <code>XmlObject</code> does not represent\r
      * an element\r
      */\r
-    public static void sort(XmlObject element, Comparator<XmlCursor> comp)\r
-    {\r
+    public static void sort(XmlObject element, Comparator<XmlCursor> comp) {\r
         XmlCursor headCursor = element.newCursor();\r
-        if (!headCursor.isStart())\r
+        if (!headCursor.isStart()) {\r
             throw new IllegalStateException("The element parameter must point to a STARTDOC");\r
+        }\r
         // We use insertion sort to minimize the number of swaps, because each swap means\r
         // moving a part of the document\r
         /* headCursor points to the beginning of the list of the already sorted items and\r
@@ -121,44 +50,21 @@ public final class XmlSort
            second element. The algorithm ends when listCursor cannot be moved to the "next"\r
            element in the unsorted list, i.e. the unsorted list becomes empty */\r
         boolean moved = headCursor.toFirstChild();\r
-        if (!moved)\r
-        {\r
+        if (!moved) {\r
             // Cursor was not moved, which means that the given element has no children and\r
             // therefore there is nothing to sort\r
             return;\r
         }\r
         XmlCursor listCursor = headCursor.newCursor();\r
         boolean moreElements = listCursor.toNextSibling();\r
-        while (moreElements)\r
-        {\r
+        while (moreElements) {\r
             moved = false;\r
             // While we can move the head of the unsorted list, it means that there are still\r
             // items (elements) that need to be sorted\r
-            while (headCursor.comparePosition(listCursor) < 0)\r
-            {\r
-                if (comp.compare(headCursor, listCursor) > 0)\r
-                {\r
+            while (headCursor.comparePosition(listCursor) < 0) {\r
+                if (comp.compare(headCursor, listCursor) > 0) {\r
                     // We have found the position in the sorted list, insert the element and the\r
                     // text following the element in the current position\r
-                    /*\r
-                     * Uncomment this code to cause the text before the element to move along\r
-                     * with the element, rather than the text after the element. Notice that this\r
-                     * is more difficult to do, because the cursor's "type" refers to the position\r
-                     * to the right of the cursor, so to get the type of the token to the left, the\r
-                     * cursor needs to be first moved to the left (previous token)\r
-                     *\r
-                    headCursor.toPrevToken();\r
-                    while (headCursor.isComment() || headCursor.isProcinst() || headCursor.isText())\r
-                        headCursor.toPrevToken();\r
-                    headCursor.toNextToken();\r
-                    listCursor.toPrevToken();\r
-                    while (listCursor.isComment() || listCursor.isProcinst() || listCursor.isText())\r
-                        listCursor.toPrevToken();\r
-                    listCursor.toNextToken();\r
-                    while (!listCursor.isStart())\r
-                        listCursor.moveXml(headCursor);\r
-                    listCursor.moveXml(headCursor);\r
-                    */\r
                     // Move the element\r
                     listCursor.moveXml(headCursor);\r
                     // Move the text following the element\r
@@ -170,8 +76,7 @@ public final class XmlSort
                 }\r
                 headCursor.toNextSibling();\r
             }\r
-            if (!moved)\r
-            {\r
+            if (!moved) {\r
                 // Because during the move of a fragment of XML, the listCursor is also moved, in\r
                 // case we didn't need to move XML (the new element to be inserted happened to\r
                 // be the last one in order), we need to move this cursor\r
@@ -182,40 +87,5 @@ public final class XmlSort
             headCursor.toFirstChild();\r
         }\r
     }\r
-\r
-    /**\r
-     * Implements a <code>java.util.Comparator</code> for comparing <code>QName</code>values.\r
-     * The namespace URIs are compared first and if they are equal, the local parts are compared.\r
-     * <p/>\r
-     * The constructor accepts an argument indicating whether the comparison order is the same as\r
-     * the lexicographic order of the strings or the reverse.\r
-     */\r
-    public static final class QNameComparator implements Comparator<XmlCursor>, Serializable\r
-    {\r
-        public static final int ASCENDING = 1;\r
-        public static final int DESCENDING = 2;\r
-\r
-        private int order;\r
-\r
-        public QNameComparator(int order)\r
-        {\r
-            this.order = order;\r
-            if (order != ASCENDING && order != DESCENDING)\r
-                throw new IllegalArgumentException("Please specify one of ASCENDING or DESCENDING "+\r
-                    "comparison orders");\r
-        }\r
-\r
-        public int compare(XmlCursor cursor1, XmlCursor cursor2) {\r
-            QName qname1 = cursor1.getName();\r
-            QName qname2 = cursor2.getName();\r
-            int qnameComparisonRes = qname1.getNamespaceURI().compareTo(qname2.getNamespaceURI());\r
-            if (qnameComparisonRes == 0)\r
-                return order == ASCENDING ?\r
-                    qname1.getLocalPart().compareTo(qname2.getLocalPart()) :\r
-                    -qname1.getLocalPart().compareTo(qname2.getLocalPart());\r
-            else\r
-                return order == ASCENDING ? qnameComparisonRes : -qnameComparisonRes;\r
-        }\r
-    }\r
 }\r
  
\ No newline at end of file
index 014fc189beeea17f5ec9a8e599dd5ea9982110f3..4782ca2fd8dd1edfe4d05b0ce8705e20a32141e9 100644 (file)
@@ -33,6 +33,8 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;\r
 import org.apache.poi.ss.usermodel.FormulaError;\r
 import org.apache.poi.ss.util.CellReference;\r
+import org.apache.poi.util.POILogFactory;\r
+import org.apache.poi.util.POILogger;\r
 import org.apache.poi.util.TempFile;\r
 import org.apache.poi.xssf.model.SharedStringsTable;\r
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
@@ -45,6 +47,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
  * so that it was renamed to "SheetDataWriter"\r
  */\r
 public class SheetDataWriter {\r
+    private static final POILogger logger = POILogFactory.getLogger(SheetDataWriter.class);\r
+    \r
     private final File _fd;\r
     private final Writer _out;\r
     private int _rownum;\r
@@ -128,7 +132,9 @@ public class SheetDataWriter {
 \r
     @Override\r
     protected void finalize() throws Throwable {\r
-        _fd.delete();\r
+        if (!_fd.delete()) {\r
+            logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+_fd);\r
+        }\r
 \r
         super.finalize();\r
     }\r