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.

TestContentTypeManager.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.openxml4j.opc.internal;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNull;
  18. import static org.junit.Assert.fail;
  19. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  20. import org.apache.poi.openxml4j.opc.OPCPackage;
  21. import org.apache.poi.openxml4j.opc.PackageAccess;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackagePartName;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  26. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  27. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  28. import org.apache.poi.ss.usermodel.CreationHelper;
  29. import org.apache.poi.ss.usermodel.Drawing;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.xssf.XSSFTestDataSamples;
  33. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  34. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  35. import org.apache.poi.xssf.usermodel.XSSFPicture;
  36. import org.apache.poi.xssf.usermodel.XSSFPictureData;
  37. import org.apache.poi.xssf.usermodel.XSSFShape;
  38. import org.apache.poi.xssf.usermodel.XSSFSheet;
  39. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  40. import org.junit.Ignore;
  41. import org.junit.Test;
  42. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  43. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.IOException;
  46. import java.util.List;
  47. public final class TestContentTypeManager {
  48. /**
  49. * Test the properties part content parsing.
  50. */
  51. @Test
  52. public void testContentType() throws Exception {
  53. String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
  54. // Retrieves core properties part
  55. try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) {
  56. PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
  57. PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
  58. PackagePart coreDocument = p.getPart(corePropertiesRelationship);
  59. assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
  60. }
  61. }
  62. /**
  63. * Test the addition of several default and override content types.
  64. */
  65. @Test
  66. public void testContentTypeAddition() throws Exception {
  67. ContentTypeManager ctm = new ZipContentTypeManager(null, null);
  68. PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.XML");
  69. PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
  70. PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
  71. PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
  72. // Add content types
  73. ctm.addContentType(name1, "foo-type1");
  74. ctm.addContentType(name2, "foo-type2");
  75. ctm.addContentType(name3, "text/xml+rel");
  76. ctm.addContentType(name4, "text/xml+rel");
  77. assertEquals(ctm.getContentType(name1), "foo-type1");
  78. assertEquals(ctm.getContentType(name2), "foo-type2");
  79. assertEquals(ctm.getContentType(name3), "text/xml+rel");
  80. assertEquals(ctm.getContentType(name3), "text/xml+rel");
  81. }
  82. /**
  83. * Test the addition then removal of content types.
  84. */
  85. @Test
  86. public void testContentTypeRemoval() throws Exception {
  87. ContentTypeManager ctm = new ZipContentTypeManager(null, null);
  88. PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.xml");
  89. PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
  90. PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
  91. PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
  92. // Add content types
  93. ctm.addContentType(name1, "foo-type1");
  94. ctm.addContentType(name2, "foo-type2");
  95. ctm.addContentType(name3, "text/xml+rel");
  96. ctm.addContentType(name4, "text/xml+rel");
  97. ctm.removeContentType(name2);
  98. ctm.removeContentType(name3);
  99. assertEquals(ctm.getContentType(name1), "foo-type1");
  100. assertEquals(ctm.getContentType(name2), "foo-type1");
  101. assertNull(ctm.getContentType(name3));
  102. ctm.removeContentType(name1);
  103. assertNull(ctm.getContentType(name1));
  104. assertNull(ctm.getContentType(name2));
  105. }
  106. /**
  107. * Test the addition then removal of content types in a package.
  108. */
  109. @Ignore
  110. @Test
  111. public void testContentTypeRemovalPackage() {
  112. // TODO
  113. fail("test not written");
  114. }
  115. protected byte[] toByteArray(Workbook wb) {
  116. try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
  117. wb.write(os);
  118. return os.toByteArray();
  119. } catch (IOException e) {
  120. throw new RuntimeException("failed to write excel file.");
  121. }
  122. }
  123. @Test
  124. public void bug62629CombinePictures() throws Exception {
  125. // this file has incorrect default content-types which caused problems in Apache POI
  126. // we now handle this broken file more gracefully
  127. XSSFWorkbook book = XSSFTestDataSamples.openSampleWorkbook("62629_target.xlsm");
  128. XSSFWorkbook b = XSSFTestDataSamples.openSampleWorkbook("62629_toMerge.xlsx");
  129. for (int i = 0; i < b.getNumberOfSheets(); i++) {
  130. XSSFSheet sheet = book.createSheet(b.getSheetName(i));
  131. copyPictures(sheet, b.getSheetAt(i));
  132. }
  133. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(book);
  134. wbBack.close();
  135. book.close();
  136. b.close();
  137. }
  138. private static void copyPictures(Sheet newSheet, Sheet sheet) {
  139. Drawing drawingOld = sheet.createDrawingPatriarch();
  140. Drawing drawingNew = newSheet.createDrawingPatriarch();
  141. CreationHelper helper = newSheet.getWorkbook().getCreationHelper();
  142. if (drawingNew instanceof XSSFDrawing) {
  143. List<XSSFShape> shapes = ((XSSFDrawing) drawingOld).getShapes();
  144. for (int i = 0; i < shapes.size(); i++) {
  145. if (shapes.get(i) instanceof XSSFPicture) {
  146. XSSFPicture pic = (XSSFPicture) shapes.get(i);
  147. XSSFPictureData picData = pic.getPictureData();
  148. int pictureIndex = newSheet.getWorkbook().addPicture(picData.getData(), picData.getPictureType());
  149. XSSFClientAnchor anchor = null;
  150. CTTwoCellAnchor oldAnchor = ((XSSFDrawing) drawingOld).getCTDrawing().getTwoCellAnchorArray(i);
  151. if (oldAnchor != null) {
  152. anchor = (XSSFClientAnchor) helper.createClientAnchor();
  153. CTMarker markerFrom = oldAnchor.getFrom();
  154. CTMarker markerTo = oldAnchor.getTo();
  155. anchor.setDx1((int) markerFrom.getColOff());
  156. anchor.setDx2((int) markerTo.getColOff());
  157. anchor.setDy1((int) markerFrom.getRowOff());
  158. anchor.setDy2((int) markerTo.getRowOff());
  159. anchor.setCol1(markerFrom.getCol());
  160. anchor.setCol2(markerTo.getCol());
  161. anchor.setRow1(markerFrom.getRow());
  162. anchor.setRow2(markerTo.getRow());
  163. }
  164. drawingNew.createPicture(anchor, pictureIndex);
  165. }
  166. }
  167. }
  168. }
  169. }