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.

TestWordToHtmlConverter.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.hwpf.converter;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.POITestCase.assertNotContained;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import java.io.StringWriter;
  20. import javax.xml.transform.OutputKeys;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.dom.DOMSource;
  23. import javax.xml.transform.stream.StreamResult;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.hwpf.HWPFDocument;
  26. import org.apache.poi.util.XMLHelper;
  27. import org.junit.jupiter.params.ParameterizedTest;
  28. import org.junit.jupiter.params.provider.CsvSource;
  29. import org.w3c.dom.Document;
  30. /**
  31. * Test cases for {@link WordToHtmlConverter}
  32. */
  33. public class TestWordToHtmlConverter {
  34. private static final POIDataSamples SAMPLES = POIDataSamples.getDocumentInstance();
  35. @ParameterizedTest
  36. @CsvSource({
  37. "AIOOB-Tap.doc, <table class=\"t1\">",
  38. "Bug33519.doc, " +
  39. "\u041F\u043B\u0430\u043D\u0438\u043D\u0441\u043A\u0438 \u0442\u0443\u0440\u043E\u0432\u0435|" +
  40. "\u042F\u0432\u043E\u0440 \u0410\u0441\u0435\u043D\u043E\u0432",
  41. "Bug46610_2.doc, 012345678911234567892123456789312345678941234567890123456789112345678921234567893123456789412345678",
  42. "Bug46817.doc, <table class=\"t1\">",
  43. "Bug47286.doc, " +
  44. "!FORMTEXT|" +
  45. "color:#4f6228;|" +
  46. "Passport No and the date of expire|" +
  47. "mfa.gov.cy",
  48. "Bug48075.doc, \u041F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u21162",
  49. "innertable.doc, <span>A</span>",
  50. "o_kurs.doc, \u0412\u0441\u0435 \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u044B \u043D\u0443\u043C\u0435\u0440\u0443\u044E\u0442\u0441\u044F",
  51. "Bug52583.doc, <select><option selected>riri</option><option>fifi</option><option>loulou</option></select>",
  52. "Bug53182.doc, !italic",
  53. "documentProperties.doc, " +
  54. "<title>This is document title</title>|" +
  55. "<meta content=\"This is document keywords\" name=\"keywords\">",
  56. // email hyperlink
  57. "Bug47286.doc, provisastpet@mfa.gov.cy",
  58. "endingnote.doc, " +
  59. "<a class=\"a1 endnoteanchor\" href=\"#endnote_1\" name=\"endnote_back_1\">1</a>|" +
  60. "<a class=\"a1 endnoteindex\" href=\"#endnote_back_1\" name=\"endnote_1\">1</a><span|" +
  61. "Ending note text",
  62. "equation.doc, <!--Image link to '0.emf' can be here-->",
  63. "hyperlink.doc, " +
  64. "<span>Before text; </span><a |" +
  65. "<a href=\"http://testuri.org/\"><span class=\"s1\">Hyperlink text</span></a>|" +
  66. "</a><span>; after text</span>",
  67. "lists-margins.doc, " +
  68. ".s1{display: inline-block; text-indent: 0; min-width: 0.4861111in;}|" +
  69. ".s2{display: inline-block; text-indent: 0; min-width: 0.23055555in;}|" +
  70. ".s3{display: inline-block; text-indent: 0; min-width: 0.28541666in;}|" +
  71. ".s4{display: inline-block; text-indent: 0; min-width: 0.28333333in;}|" +
  72. ".p4{text-indent:-0.59652776in;margin-left:-0.70069444in;",
  73. "pageref.doc, " +
  74. "<a href=\"#userref\">|" +
  75. "<a name=\"userref\">|" +
  76. "1",
  77. "table-merges.doc, " +
  78. "<td class=\"td1\" colspan=\"3\">|" +
  79. "<td class=\"td2\" colspan=\"2\">",
  80. "52420.doc, " +
  81. "!FORMTEXT|" +
  82. "\u0417\u0410\u0414\u0410\u041d\u0418\u0415|" +
  83. "\u041f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c",
  84. "picture.doc, " +
  85. "src=\"0.emf\"|" +
  86. "width:3.1293333in;height:1.7247736in;|" +
  87. "left:-0.09433333;top:-0.2573611;|" +
  88. "width:3.4125in;height:2.3253334in;",
  89. "pictures_escher.doc, " +
  90. "<img src=\"s0.PNG\">|" +
  91. "<img src=\"s808.PNG\">"
  92. })
  93. void testFile(String file, String contains) throws Exception {
  94. boolean emulatePictureStorage = !file.contains("equation");
  95. String result = getHtmlText(file, emulatePictureStorage);
  96. assertNotNull(result);
  97. // starting with JDK 9 such unimportant whitespaces may be trimmed
  98. result = result.replace("</a> <span", "</a><span");
  99. for (String match : contains.split("\\|")) {
  100. if (match.startsWith("!")) {
  101. assertNotContained(result, match.substring(1));
  102. } else {
  103. assertContains(result, match);
  104. }
  105. }
  106. }
  107. private static String getHtmlText(final String sampleFileName, boolean emulatePictureStorage) throws Exception {
  108. Document newDocument = XMLHelper.newDocumentBuilder().newDocument();
  109. WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument);
  110. try (HWPFDocument hwpfDocument = new HWPFDocument(SAMPLES.openResourceAsStream(sampleFileName))) {
  111. if (emulatePictureStorage) {
  112. wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> suggestedName);
  113. }
  114. wordToHtmlConverter.processDocument(hwpfDocument);
  115. StringWriter stringWriter = new StringWriter();
  116. Transformer transformer = XMLHelper.newTransformer();
  117. transformer.setOutputProperty(OutputKeys.METHOD, "html");
  118. transformer.transform(
  119. new DOMSource(wordToHtmlConverter.getDocument()),
  120. new StreamResult(stringWriter));
  121. return stringWriter.toString();
  122. }
  123. }
  124. }