public void computeEnhancements(ContentItem ci) throws EngineException {
MediaTypeAndStream mtas = extractMediaType(ci);
if(mtas.mediaType == null){
return; //unable to parse and detect content type
}
MediaType plainMediaType = mtas.mediaType.getBaseType();
if(plainMediaType.equals(MediaType.TEXT_PLAIN)){
return; //we need not to process plain text!
}
final ParseContext context = new ParseContext();
context.set(Parser.class,parser);
Set<MediaType> supproted = parser.getSupportedTypes(context);
if(supproted.contains(plainMediaType)) {
final InputStream in;
if(mtas.in == null){
in = ci.getStream();
} else {
in = mtas.in;
}
final Metadata metadata = new Metadata();
//set the already parsed contentType
metadata.set(Metadata.CONTENT_TYPE, mtas.mediaType.toString());
//also explicitly set the charset as contentEncoding
String charset = mtas.mediaType.getParameters().get("charset");
if(charset != null){
metadata.set(Metadata.CONTENT_ENCODING, charset);
}
ContentSink plainTextSink;
try {
plainTextSink = ciFactory.createContentSink(TEXT_PLAIN +"; charset="+UTF8.name());
} catch (IOException e) {
IOUtils.closeQuietly(in); //close the input stream
throw new EngineException("Error while initialising Blob for" +
"writing the text/plain version of the parsed content",e);
}
final Writer plainTextWriter = new OutputStreamWriter(plainTextSink.getOutputStream(), UTF8);
final ContentHandler textHandler = new BodyContentHandler( //only the Body
new PlainTextHandler(plainTextWriter, false,skipLinebreaks)); //skip ignoreable
final ToXMLContentHandler xhtmlHandler;
final ContentHandler mainHandler;
ContentSink xhtmlSink = null;
try {
if(!plainMediaType.equals(XHTML)){ //do not parse XHTML from XHTML
try {
xhtmlSink = ciFactory.createContentSink(XHTML +"; charset="+UTF8.name());
} catch (IOException e) {
throw new EngineException("Error while initialising Blob for" +
"writing the application/xhtml+xml version of the parsed content",e);