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.

HWPFFileHandler.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Assert.assertNotNull;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.PrintWriter;
  24. import java.io.StringWriter;
  25. import org.apache.poi.hdf.extractor.WordDocument;
  26. import org.apache.poi.hwpf.HWPFDocument;
  27. import org.apache.poi.hwpf.extractor.WordExtractor;
  28. import org.junit.Test;
  29. @SuppressWarnings("deprecation")
  30. public class HWPFFileHandler extends POIFSFileHandler {
  31. @Override
  32. public void handleFile(InputStream stream) throws Exception {
  33. HWPFDocument doc = new HWPFDocument(stream);
  34. assertNotNull(doc.getBookmarks());
  35. assertNotNull(doc.getCharacterTable());
  36. assertNotNull(doc.getEndnotes());
  37. handlePOIDocument(doc);
  38. // fails for many documents, but is deprecated anyway...
  39. // handleWordDocument(doc);
  40. }
  41. protected void handleWordDocument(HWPFDocument doc) throws IOException {
  42. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  43. doc.write(outStream);
  44. WordDocument wordDoc = new WordDocument(new ByteArrayInputStream(outStream.toByteArray()));
  45. StringWriter docTextWriter = new StringWriter();
  46. PrintWriter out = new PrintWriter(docTextWriter);
  47. try {
  48. wordDoc.writeAllText(out);
  49. } finally {
  50. out.close();
  51. }
  52. docTextWriter.close();
  53. }
  54. // a test-case to test this locally without executing the full TestAllFiles
  55. @Test
  56. public void test() throws Exception {
  57. File file = new File("test-data/document/52117.doc");
  58. InputStream stream = new FileInputStream(file);
  59. try {
  60. handleFile(stream);
  61. } finally {
  62. stream.close();
  63. }
  64. handleExtracting(file);
  65. stream = new FileInputStream(file);
  66. try {
  67. WordExtractor extractor = new WordExtractor(stream);
  68. try {
  69. assertNotNull(extractor.getText());
  70. } finally {
  71. extractor.close();
  72. }
  73. } finally {
  74. stream.close();
  75. }
  76. }
  77. @Test
  78. public void testExtractingOld() throws Exception {
  79. File file = new File("test-data/document/52117.doc");
  80. handleExtracting(file);
  81. }
  82. }