use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class ArParserTest method testArParsing.
@Test
public void testArParsing() throws Exception {
Parser parser = new AutoDetectParser();
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
try (InputStream stream = ArParserTest.class.getResourceAsStream("/test-documents/testARofText.ar")) {
parser.parse(stream, handler, metadata, recursingContext);
}
assertEquals("application/x-archive", metadata.get(Metadata.CONTENT_TYPE));
String content = handler.toString();
assertContains("testTXT.txt", content);
assertContains("Test d'indexation de Txt", content);
assertContains("http://www.apache.org", content);
try (InputStream stream = ArParserTest.class.getResourceAsStream("/test-documents/testARofSND.ar")) {
parser.parse(stream, handler, metadata, recursingContext);
}
assertEquals("application/x-archive", metadata.get(Metadata.CONTENT_TYPE));
content = handler.toString();
assertContains("testAU.au", content);
}
use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class ChmParser method parsePage.
private void parsePage(byte[] byteObject, Parser htmlParser, ContentHandler xhtml, ParseContext context) throws TikaException {
// throws IOException
InputStream stream = null;
Metadata metadata = new Metadata();
// -1
ContentHandler handler = new EmbeddedContentHandler(new BodyContentHandler(xhtml));
try {
stream = new ByteArrayInputStream(byteObject);
htmlParser.parse(stream, handler, metadata, context);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
// Pushback overflow from tagsoup
}
}
use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class TIAParsingExample method testTeeContentHandler.
public static void testTeeContentHandler(String filename) throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[0]);
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
Parser parser = new AutoDetectParser();
LinkContentHandler linkCollector = new LinkContentHandler();
try (OutputStream output = new FileOutputStream(new File(filename))) {
ContentHandler handler = new TeeContentHandler(new BodyContentHandler(output), linkCollector);
parser.parse(stream, handler, metadata, context);
}
}
use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class StringsParserTest method testParse.
@Test
public void testParse() throws Exception {
assumeTrue(canRun());
String resource = "/test-documents/testOCTET_header.dbase3";
String[] content = { "CLASSNO", "TITLE", "ITEMNO", "LISTNO", "LISTDATE" };
String[] met_attributes = { "min-len", "encoding", "strings:file_output" };
StringsConfig stringsConfig = new StringsConfig();
FileConfig fileConfig = new FileConfig();
Parser parser = new StringsParser();
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
context.set(StringsConfig.class, stringsConfig);
context.set(FileConfig.class, fileConfig);
try (InputStream stream = StringsParserTest.class.getResourceAsStream(resource)) {
parser.parse(stream, handler, metadata, context);
} catch (Exception e) {
e.printStackTrace();
}
// Content
for (String word : content) {
assertTrue(handler.toString().contains(word));
}
// Metadata
Arrays.equals(met_attributes, metadata.names());
}
use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class TXTParserTest method testEmptyText.
@Test
public void testEmptyText() throws Exception {
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
parser.parse(new ByteArrayInputStream(new byte[0]), handler, metadata, new ParseContext());
assertEquals("text/plain; charset=UTF-8", metadata.get(Metadata.CONTENT_TYPE));
assertEquals("\n", handler.toString());
}
Aggregations