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.

TestOLE2Embeding.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.hssf.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.List;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.hssf.HSSFTestDataSamples;
  26. import org.apache.poi.poifs.filesystem.DirectoryNode;
  27. import org.apache.poi.poifs.filesystem.Ole10Native;
  28. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  29. import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
  30. import org.apache.poi.ss.usermodel.CreationHelper;
  31. import org.apache.poi.util.LocaleUtil;
  32. import org.junit.jupiter.api.Test;
  33. public final class TestOLE2Embeding {
  34. @Test
  35. public void testEmbeding() throws Exception {
  36. // This used to break, until bug #43116 was fixed
  37. try (HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls")) {
  38. // Check we can get at the Escher layer still
  39. assertDoesNotThrow(workbook::getAllPictures);
  40. }
  41. }
  42. @Test
  43. public void testEmbeddedObjects() throws Exception {
  44. try (HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls")) {
  45. List<HSSFObjectData> objects = workbook.getAllEmbeddedObjects();
  46. assertEquals(2, objects.size(), "Wrong number of objects");
  47. assertEquals("MBD06CAB431", objects.get(0).getDirectory().getName(), "Wrong name for first object");
  48. assertEquals("MBD06CAC85A", objects.get(1).getDirectory().getName(), "Wrong name for second object");
  49. }
  50. }
  51. @Test
  52. public void testReallyEmbedSomething() throws Exception {
  53. HSSFWorkbook wb1 = new HSSFWorkbook();
  54. HSSFSheet sheet = wb1.createSheet();
  55. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  56. byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");
  57. byte[] picturePPT = POIDataSamples.getSlideShowInstance().readFile("clock.jpg");
  58. int imgIdx = wb1.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
  59. POIFSFileSystem pptPoifs = getSamplePPT();
  60. int pptIdx = wb1.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt");
  61. POIFSFileSystem xlsPoifs = getSampleXLS();
  62. int imgPPT = wb1.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG);
  63. int xlsIdx = wb1.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls");
  64. int txtIdx = wb1.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt");
  65. int rowoffset = 5;
  66. int coloffset = 5;
  67. CreationHelper ch = wb1.getCreationHelper();
  68. HSSFClientAnchor anchor = (HSSFClientAnchor)ch.createClientAnchor();
  69. anchor.setAnchor((short)(2+coloffset), 1+rowoffset, 0, 0, (short)(3+coloffset), 5+rowoffset, 0, 0);
  70. anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
  71. patriarch.createObjectData(anchor, pptIdx, imgPPT);
  72. anchor = (HSSFClientAnchor)ch.createClientAnchor();
  73. anchor.setAnchor((short)(5+coloffset), 1+rowoffset, 0, 0, (short)(6+coloffset), 5+rowoffset, 0, 0);
  74. anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
  75. patriarch.createObjectData(anchor, xlsIdx, imgIdx);
  76. anchor = (HSSFClientAnchor)ch.createClientAnchor();
  77. anchor.setAnchor((short)(3+coloffset), 10+rowoffset, 0, 0, (short)(5+coloffset), 11+rowoffset, 0, 0);
  78. anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
  79. patriarch.createObjectData(anchor, txtIdx, imgIdx);
  80. anchor = (HSSFClientAnchor)ch.createClientAnchor();
  81. anchor.setAnchor((short)(1+coloffset), -2+rowoffset, 0, 0, (short)(7+coloffset), 14+rowoffset, 0, 0);
  82. anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
  83. HSSFSimpleShape circle = patriarch.createSimpleShape(anchor);
  84. circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  85. circle.setNoFill(true);
  86. HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
  87. wb1.close();
  88. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  89. HSSFObjectData od = wb2.getAllEmbeddedObjects().get(0);
  90. Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
  91. bos.reset();
  92. pptPoifs.writeFilesystem(bos);
  93. assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
  94. od = wb2.getAllEmbeddedObjects().get(1);
  95. ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
  96. bos.reset();
  97. xlsPoifs.writeFilesystem(bos);
  98. assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
  99. od = wb2.getAllEmbeddedObjects().get(2);
  100. ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
  101. assertArrayEquals(ole10.getDataBuffer(), getSampleTXT());
  102. xlsPoifs.close();
  103. pptPoifs.close();
  104. wb2.close();
  105. }
  106. static POIFSFileSystem getSamplePPT() throws IOException {
  107. // scratchpad classes are not available, so we use something pre-cooked
  108. InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream("with_textbox.ppt");
  109. POIFSFileSystem poifs = new POIFSFileSystem(is);
  110. is.close();
  111. return poifs;
  112. }
  113. static POIFSFileSystem getSampleXLS() throws IOException {
  114. HSSFWorkbook wb = new HSSFWorkbook();
  115. HSSFSheet sheet = wb.createSheet();
  116. sheet.createRow(5).createCell(2).setCellValue("yo dawg i herd you like embeddet objekts, so we put a ole in your ole so you can save a file while you save a file");
  117. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  118. wb.write(bos);
  119. wb.close();
  120. return new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
  121. }
  122. static byte[] getSampleTXT() {
  123. return "All your base are belong to us".getBytes(LocaleUtil.CHARSET_1252);
  124. }
  125. }