From 0850e7d846a71cbd90f919f841917a1cfedad006 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 22 Mar 2015 13:33:43 +0000 Subject: [PATCH] Bug 47304: use fixed encoding when extracting text in WordDocument git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1668367 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/stress/HWPFFileHandler.java | 48 +++++++++++++++++- .../poi/hdf/extractor/WordDocument.java | 2 +- .../poi/hdf/extractor/TestWordDocument.java | 36 +++++++++++++ test-data/document/47304.doc | Bin 0 -> 22016 bytes 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test-data/document/47304.doc diff --git a/src/integrationtest/org/apache/poi/stress/HWPFFileHandler.java b/src/integrationtest/org/apache/poi/stress/HWPFFileHandler.java index 1b6d4646c7..5f24337fbb 100644 --- a/src/integrationtest/org/apache/poi/stress/HWPFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/HWPFFileHandler.java @@ -18,12 +18,21 @@ package org.apache.poi.stress; import static org.junit.Assert.assertNotNull; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import org.apache.poi.hdf.extractor.WordDocument; import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.extractor.WordExtractor; import org.junit.Test; +@SuppressWarnings("deprecation") public class HWPFFileHandler extends POIFSFileHandler { @Override public void handleFile(InputStream stream) throws Exception { @@ -33,16 +42,53 @@ public class HWPFFileHandler extends POIFSFileHandler { assertNotNull(doc.getEndnotes()); handlePOIDocument(doc); + + // fails for many documents, but is deprecated anyway... + // handleWordDocument(doc); + } + + protected void handleWordDocument(HWPFDocument doc) throws IOException { + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + doc.write(outStream); + + WordDocument wordDoc = new WordDocument(new ByteArrayInputStream(outStream.toByteArray())); + + StringWriter docTextWriter = new StringWriter(); + PrintWriter out = new PrintWriter(docTextWriter); + try { + wordDoc.writeAllText(out); + } finally { + out.close(); + } + docTextWriter.close(); } + + // a test-case to test this locally without executing the full TestAllFiles @Test public void test() throws Exception { - InputStream stream = new FileInputStream("test-data/document/HeaderFooterUnicode.doc"); + File file = new File("test-data/document/47304.doc"); + + InputStream stream = new FileInputStream(file); try { handleFile(stream); } finally { stream.close(); } + + handleExtracting(file); + + stream = new FileInputStream(file); + try { + WordExtractor extractor = new WordExtractor(stream); + try { + assertNotNull(extractor.getText()); + } finally { + extractor.close(); + } + } finally { + stream.close(); + } } } \ No newline at end of file diff --git a/src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java b/src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java index 929de311b7..ff53300321 100644 --- a/src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java +++ b/src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java @@ -177,7 +177,7 @@ public final class WordDocument { } else { - String sText = new String(_header, start, end-start); + String sText = new String(_header, start, end-start, "windows-1252"); out.write(sText); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hdf/extractor/TestWordDocument.java b/src/scratchpad/testcases/org/apache/poi/hdf/extractor/TestWordDocument.java index 1cf29f4376..f0941674ff 100644 --- a/src/scratchpad/testcases/org/apache/poi/hdf/extractor/TestWordDocument.java +++ b/src/scratchpad/testcases/org/apache/poi/hdf/extractor/TestWordDocument.java @@ -17,6 +17,15 @@ package org.apache.poi.hdf.extractor; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.HWPFTestDataSamples; +import org.apache.poi.hwpf.extractor.WordExtractor; import org.junit.Test; @@ -31,4 +40,31 @@ public class TestWordDocument { //WordDocument.main(new String[] {"test-data/document/Word6.doc", "/tmp/test.doc"}); WordDocument.main(new String[] {"test-data/document/53446.doc", "/tmp/test.doc"}); } + + @SuppressWarnings("deprecation") + @Test + public void test47304() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("47304.doc"); + assertNotNull(doc); + + WordExtractor extractor = new WordExtractor(doc); + String text = extractor.getText(); + //System.out.println(text); + assertTrue("Had: " + text, text.contains("Just a \u201Ctest\u201D")); + extractor.close(); + + WordDocument wordDoc = new WordDocument("test-data/document/47304.doc"); + + StringWriter docTextWriter = new StringWriter(); + PrintWriter out = new PrintWriter(docTextWriter); + try { + wordDoc.writeAllText(out); + } finally { + out.close(); + } + docTextWriter.close(); + + //System.out.println(docTextWriter.toString()); + assertTrue("Had: " + docTextWriter.toString(), docTextWriter.toString().contains("Just a \u201Ctest\u201D")); + } } diff --git a/test-data/document/47304.doc b/test-data/document/47304.doc new file mode 100644 index 0000000000000000000000000000000000000000..d59d8d7ee1e8768039ca509bc1132f86ba260f1e GIT binary patch literal 22016 zcmeHP2|Sfq8$b76%OxUPmX_;MX_9Lx3@KU4WKBg2>EdR0Z|+5EipVrA2Bj1xOp1(T zl$Jr!GNmYOBzsMh8dJ1Mi~Bw2@|H9v^Zn+VZM<=JoLTe9RsPfT%G1jV#cfBv)lXM)W4E)pV2NOw3wmFc~ZdoTCH1fMVSmN{6Q zC~#B|$rJ5OW6GBAjsF%^AgTk6=_`NlH>ebpdjk2G66He`5qbz|eqX(h(OZ2=2U2B_ zLk2mjKyer<^-@CU8>lF%g3zqKdVL}Ua)=9I2SP8wCn&-1PobMp6SNmoqCKzs+E2(M zC~@C}Je;nF1OplaO+-Y!f%G=WlZ5<%at5ZcefkGNPJiA|;>7)ZOdcVhXa_-uqT|=f z5b_4*apKmuH{7Yf6Qdvy5QHy`}!R zD`6ijf7Bl*_D`2YjE{lz_cnx>A29WX%0#~sa{A|E-oSjK+&_)txp^Z@pi^P`H-S+v zj-*I3NE%}Jts)G6Z41l^IGDBIzS^KLsSzmt^lEt1!u_00I7sC*r5@Ie~<;f5|Kf2;O;BHnvD#5KKKv& zhcG3eoq*jRfgx-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