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
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
\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
\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
* @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
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
}\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
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