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.

HemfExtractorTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.hemf.extractor;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.InputStream;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.hemf.record.AbstractHemfComment;
  26. import org.apache.poi.hemf.record.HemfCommentPublic;
  27. import org.apache.poi.hemf.record.HemfCommentRecord;
  28. import org.apache.poi.hemf.record.HemfHeader;
  29. import org.apache.poi.hemf.record.HemfRecord;
  30. import org.apache.poi.hemf.record.HemfRecordType;
  31. import org.apache.poi.hemf.record.HemfText;
  32. import org.apache.poi.util.IOUtils;
  33. import org.apache.poi.util.RecordFormatException;
  34. import org.junit.Test;
  35. public class HemfExtractorTest {
  36. @Test
  37. public void testBasicWindows() throws Exception {
  38. InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_windows.emf");
  39. HemfExtractor ex = new HemfExtractor(is);
  40. HemfHeader header = ex.getHeader();
  41. assertEquals(27864, header.getBytes());
  42. assertEquals(31, header.getRecords());
  43. assertEquals(3, header.getHandles());
  44. assertEquals(346000, header.getMicrometersX());
  45. assertEquals(194000, header.getMicrometersY());
  46. int records = 0;
  47. for (HemfRecord record : ex) {
  48. records++;
  49. }
  50. assertEquals(header.getRecords() - 1, records);
  51. }
  52. @Test
  53. public void testBasicMac() throws Exception {
  54. InputStream is =
  55. POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_mac.emf");
  56. HemfExtractor ex = new HemfExtractor(is);
  57. HemfHeader header = ex.getHeader();
  58. int records = 0;
  59. boolean extractedData = false;
  60. for (HemfRecord record : ex) {
  61. if (record.getRecordType() == HemfRecordType.comment) {
  62. AbstractHemfComment comment = ((HemfCommentRecord) record).getComment();
  63. if (comment instanceof HemfCommentPublic.MultiFormats) {
  64. for (HemfCommentPublic.HemfMultiFormatsData d : ((HemfCommentPublic.MultiFormats) comment).getData()) {
  65. byte[] data = d.getData();
  66. //make sure header starts at 0
  67. assertEquals('%', data[0]);
  68. assertEquals('P', data[1]);
  69. assertEquals('D', data[2]);
  70. assertEquals('F', data[3]);
  71. //make sure byte array ends at EOF\n
  72. assertEquals('E', data[data.length - 4]);
  73. assertEquals('O', data[data.length - 3]);
  74. assertEquals('F', data[data.length - 2]);
  75. assertEquals('\n', data[data.length - 1]);
  76. extractedData = true;
  77. }
  78. }
  79. }
  80. records++;
  81. }
  82. assertTrue(extractedData);
  83. assertEquals(header.getRecords() - 1, records);
  84. }
  85. @Test
  86. public void testMacText() throws Exception {
  87. InputStream is =
  88. POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_mac.emf");
  89. HemfExtractor ex = new HemfExtractor(is);
  90. long lastY = -1;
  91. long lastX = -1;
  92. long fudgeFactorX = 1000;//derive this from the font information!
  93. StringBuilder sb = new StringBuilder();
  94. for (HemfRecord record : ex) {
  95. if (record.getRecordType().equals(HemfRecordType.exttextoutw)) {
  96. HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
  97. if (lastY > -1 && lastY != extTextOutW.getY()) {
  98. sb.append("\n");
  99. lastX = -1;
  100. }
  101. if (lastX > -1 && extTextOutW.getX() - lastX > fudgeFactorX) {
  102. sb.append(" ");
  103. }
  104. sb.append(extTextOutW.getText());
  105. lastY = extTextOutW.getY();
  106. lastX = extTextOutW.getX();
  107. }
  108. }
  109. String txt = sb.toString();
  110. assertContains(txt, "Tika http://incubator.apache.org");
  111. assertContains(txt, "Latest News\n");
  112. }
  113. @Test
  114. public void testWindowsText() throws Exception {
  115. InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_windows.emf");
  116. HemfExtractor ex = new HemfExtractor(is);
  117. long lastY = -1;
  118. long lastX = -1;
  119. long fudgeFactorX = 1000;//derive this from the font or frame/bounds information
  120. StringBuilder sb = new StringBuilder();
  121. Set<String> expectedParts = new HashSet<String>();
  122. expectedParts.add("C:\\Users\\tallison\\");
  123. expectedParts.add("testPDF.pdf");
  124. int foundExpected = 0;
  125. for (HemfRecord record : ex) {
  126. if (record.getRecordType().equals(HemfRecordType.exttextoutw)) {
  127. HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
  128. if (lastY > -1 && lastY != extTextOutW.getY()) {
  129. sb.append("\n");
  130. lastX = -1;
  131. }
  132. if (lastX > -1 && extTextOutW.getX() - lastX > fudgeFactorX) {
  133. sb.append(" ");
  134. }
  135. String txt = extTextOutW.getText();
  136. if (expectedParts.contains(txt)) {
  137. foundExpected++;
  138. }
  139. sb.append(txt);
  140. lastY = extTextOutW.getY();
  141. lastX = extTextOutW.getX();
  142. }
  143. }
  144. String txt = sb.toString();
  145. assertContains(txt, "C:\\Users\\tallison\\\n");
  146. assertContains(txt, "asf2-git-1.x\\tika-\n");
  147. assertEquals(expectedParts.size(), foundExpected);
  148. }
  149. @Test(expected = RecordFormatException.class)
  150. public void testInfiniteLoopOnFile() throws Exception {
  151. InputStream is = null;
  152. try {
  153. is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("61294.emf");
  154. HemfExtractor ex = new HemfExtractor(is);
  155. for (HemfRecord record : ex) {
  156. }
  157. } finally {
  158. IOUtils.closeQuietly(is);
  159. }
  160. }
  161. @Test(expected = RecordFormatException.class)
  162. public void testInfiniteLoopOnByteArray() throws Exception {
  163. InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("61294.emf");
  164. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  165. IOUtils.copy(is, bos);
  166. is.close();
  167. HemfExtractor ex = new HemfExtractor(new ByteArrayInputStream(bos.toByteArray()));
  168. for (HemfRecord record : ex) {
  169. }
  170. }
  171. /*
  172. govdocs1 064213.doc-0.emf contains an example of extextouta
  173. */
  174. }