@Test public void testParseToPlainTextChunks() throws IOException, SAXException, TikaException { List<String> result = example.parseToPlainTextChunks(); assertEquals(3, result.size()); for (String chunk : result) { assertTrue("Chunk under max size", chunk.length() <= example.MAXIMUM_TEXT_CHUNK_SIZE); } assertContains("This is in the header", result.get(0)); assertContains("Test Document", result.get(0)); assertContains("Testing", result.get(1)); assertContains("1 2 3", result.get(1)); assertContains("TestTable", result.get(1)); assertContains("Testing 123", result.get(2)); }
public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException { final List<String> chunks = new ArrayList<>(); chunks.add(""); ContentHandlerDecorator handler = new ContentHandlerDecorator() { @Override public void characters(char[] ch, int start, int length) { String lastChunk = chunks.get(chunks.size() - 1); String thisStr = new String(ch, start, length); if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) { chunks.add(thisStr); } else { chunks.set(chunks.size() - 1, lastChunk + thisStr); } } }; AutoDetectParser parser = new AutoDetectParser(); Metadata metadata = new Metadata(); try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) { parser.parse(stream, handler, metadata); return chunks; } }
ContentHandlerExample { public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException { final List<String> chunks = new ArrayList<>(); chunks.add(""); ContentHandlerDecorator handler = new ContentHandlerDecorator() { @Override public void characters(char[] ch, int start, int length) { String lastChunk = chunks.get(chunks.size() - 1); String thisStr = new String(ch, start, length); if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) { chunks.add(thisStr); } else { chunks.set(chunks.size() - 1, lastChunk + thisStr); } } }; AutoDetectParser parser = new AutoDetectParser(); Metadata metadata = new Metadata(); try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) { parser.parse(stream, handler, metadata); return chunks; } } }
ContentHandlerExample { public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException { final List<String> chunks = new ArrayList<>(); chunks.add(""); ContentHandlerDecorator handler = new ContentHandlerDecorator() { @Override public void characters(char[] ch, int start, int length) { String lastChunk = chunks.get(chunks.size() - 1); String thisStr = new String(ch, start, length); if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) { chunks.add(thisStr); } else { chunks.set(chunks.size() - 1, lastChunk + thisStr); } } }; AutoDetectParser parser = new AutoDetectParser(); Metadata metadata = new Metadata(); try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) { parser.parse(stream, handler, metadata); return chunks; } }  }
ContentHandlerExample { public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException { final List<String> chunks = new ArrayList<>(); chunks.add(""); ContentHandlerDecorator handler = new ContentHandlerDecorator() { @Override public void characters(char[] ch, int start, int length) { String lastChunk = chunks.get(chunks.size() - 1); String thisStr = new String(ch, start, length); if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) { chunks.add(thisStr); } else { chunks.set(chunks.size() - 1, lastChunk + thisStr); } } }; AutoDetectParser parser = new AutoDetectParser(); Metadata metadata = new Metadata(); try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) { parser.parse(stream, handler, metadata); return chunks; } }  String parseToPlainText(); String parseToHTML(); String parseBodyToHTML(); String parseOnePartToHTML(); List<String> parseToPlainTextChunks(); }
ContentHandlerExample { public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException { final List<String> chunks = new ArrayList<>(); chunks.add(""); ContentHandlerDecorator handler = new ContentHandlerDecorator() { @Override public void characters(char[] ch, int start, int length) { String lastChunk = chunks.get(chunks.size() - 1); String thisStr = new String(ch, start, length); if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) { chunks.add(thisStr); } else { chunks.set(chunks.size() - 1, lastChunk + thisStr); } } }; AutoDetectParser parser = new AutoDetectParser(); Metadata metadata = new Metadata(); try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) { parser.parse(stream, handler, metadata); return chunks; } }  String parseToPlainText(); String parseToHTML(); String parseBodyToHTML(); String parseOnePartToHTML(); List<String> parseToPlainTextChunks();  }