SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler xmlHandler;
try {
xmlHandler = factory.newTransformerHandler();
} catch (TransformerConfigurationException e) {
throw new DroidsException(e);
}
xmlHandler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
xmlHandler.setResult(new StreamResult(dataBuffer));
BoilerpipeContentHandler mainContentHandler = new BoilerpipeContentHandler(mainContentBuffer);
BodyContentHandler bodyHandler = new BodyContentHandler(bodyBuffer);
LinkContentHandler linkHandler = new LinkContentHandler();
TeeContentHandler parallelHandler = new TeeContentHandler(xmlHandler, mainContentHandler, bodyHandler, linkHandler );
InputStream instream = entity.obtainContent();
try {
parser.parse(instream, parallelHandler, metadata, new ParseContext());
ArrayList<Link> extractedTasks = new ArrayList<Link>();
int depth = task.getDepth() + 1;
if (task instanceof LinkTask) {
for(org.apache.tika.sax.Link tikaLink : linkHandler.getLinks()) {
try {
URI uri = new URI(tikaLink.getUri());
// Test to see if the scheme is empty
// This would indicate a relative URL, so resolve it against the task URI
if(uri.getScheme() == null) {
uri = ((Link) task).getURI().resolve(uri);
}
extractedTasks.add(new LinkTask((Link)task, uri, depth, tikaLink.getText()));
} catch (URISyntaxException e) {
if(LOG.isWarnEnabled()) {
LOG.warn("URI not valid: "+ tikaLink.getUri());
}
}
}
}
return new TikaParseImpl(dataBuffer.toString(), extractedTasks, bodyBuffer.toString(), mainContentBuffer.toString(), metadata);
} catch (SAXException ex) {
throw new DroidsException("Failure parsing document " + task.getId(), ex);
} catch (TikaException ex) {
throw new DroidsException("Failure parsing document " + task.getId(), ex);
} finally {
instream.close();
}
}