You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POIXMLDocumentHandler.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.stress;
  16. import static org.junit.jupiter.api.Assertions.assertNotNull;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.ooxml.POIXMLDocument;
  20. import org.apache.poi.poifs.crypt.Decryptor;
  21. import org.apache.poi.poifs.filesystem.FileMagic;
  22. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  23. import org.apache.xmlbeans.XmlCursor;
  24. import org.apache.xmlbeans.XmlObject;
  25. public final class POIXMLDocumentHandler {
  26. protected void handlePOIXMLDocument(POIXMLDocument doc) throws Exception {
  27. assertNotNull(doc.getAllEmbeddedParts());
  28. assertNotNull(doc.getPackage());
  29. assertNotNull(doc.getPackagePart());
  30. assertNotNull(doc.getProperties());
  31. assertNotNull(doc.getRelations());
  32. }
  33. protected static boolean isEncrypted(InputStream stream) throws IOException {
  34. if (FileMagic.valueOf(stream) == FileMagic.OLE2) {
  35. try (POIFSFileSystem poifs = new POIFSFileSystem(stream)) {
  36. if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
  37. return true;
  38. }
  39. }
  40. throw new IOException("Wrong file format or file extension for OO XML file");
  41. }
  42. return false;
  43. }
  44. /**
  45. * Recurse through the document and convert all elements so they are available in the ooxml-lite jar.
  46. * This method only makes sense for hierarchical documents like .docx.
  47. * If the document is split up in different parts like in .pptx, each part needs to be provided.
  48. *
  49. * @param base the entry point
  50. */
  51. protected static void cursorRecursive(XmlObject base) {
  52. try (XmlCursor cur = base.newCursor()) {
  53. cursorRecursive(cur);
  54. }
  55. }
  56. private static void cursorRecursive(XmlCursor cur) {
  57. do {
  58. assertNotNull(cur.getObject());
  59. cur.push();
  60. for (boolean b = cur.toFirstAttribute(); b; b = cur.toNextAttribute()) {
  61. assertNotNull(cur.getObject());
  62. }
  63. cur.pop();
  64. cur.push();
  65. if (cur.toFirstChild()) {
  66. cursorRecursive(cur);
  67. }
  68. cur.pop();
  69. } while (cur.toNextSibling());
  70. }
  71. }