use of org.apache.tika.sax.BodyContentHandler in project tika by apache.
the class UnpackerResource method process.
private Map<String, byte[]> process(InputStream is, @Context HttpHeaders httpHeaders, @Context UriInfo info, boolean saveAll) throws Exception {
Metadata metadata = new Metadata();
ParseContext pc = new ParseContext();
Parser parser = TikaResource.createParser();
if (parser instanceof DigestingParser) {
//no need to digest for unwrapping
parser = ((DigestingParser) parser).getWrappedParser();
}
TikaResource.fillMetadata(parser, metadata, pc, httpHeaders.getRequestHeaders());
TikaResource.logRequest(LOG, info, metadata);
ContentHandler ch;
ByteArrayOutputStream text = new ByteArrayOutputStream();
if (saveAll) {
ch = new BodyContentHandler(new RichTextContentHandler(new OutputStreamWriter(text, UTF_8)));
} else {
ch = new DefaultHandler();
}
Map<String, byte[]> files = new HashMap<>();
MutableInt count = new MutableInt();
pc.set(EmbeddedDocumentExtractor.class, new MyEmbeddedDocumentExtractor(count, files));
TikaResource.parse(parser, LOG, info.getPath(), is, ch, metadata, pc);
if (count.intValue() == 0 && !saveAll) {
throw new WebApplicationException(Response.Status.NO_CONTENT);
}
if (saveAll) {
files.put(TEXT_FILENAME, text.toByteArray());
ByteArrayOutputStream metaStream = new ByteArrayOutputStream();
metadataToCsv(metadata, metaStream);
files.put(META_FILENAME, metaStream.toByteArray());
}
return files;
}
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