Tika for metadata extraction

First pass of converting a few extractors to use Tika rather than 3rd party libraries directly, or use the new style tika structure


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@20640 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-06-14 19:02:37 +00:00
parent 7aeff72605
commit 63b2f5983a
9 changed files with 329 additions and 226 deletions

View File

@@ -18,26 +18,13 @@
*/
package org.alfresco.repo.content.metadata;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.ArrayList;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.ContentReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLPropertiesTextExtractor;
import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
/**
* POI-based metadata extractor for Office 07 documents.
@@ -50,77 +37,28 @@ import org.apache.xmlbeans.XmlException;
* <b>Any custom property:</b> -- [not mapped]
* </pre>
*
* TIKA Note - all the fields (plus a few others) are present
* in the tika metadata.
* Uses Apache Tika
*
* @author Neil McErlean
*/
public class PoiMetadataExtracter extends AbstractMappingMetadataExtracter
public class PoiMetadataExtracter extends TikaPoweredMetadataExtracter
{
protected static Log logger = LogFactory.getLog(PoiMetadataExtracter.class);
private static final String KEY_AUTHOR = "author";
private static final String KEY_TITLE = "title";
private static final String KEY_SUBJECT = "subject";
private static final String KEY_CREATED = "created";
private static final String KEY_DESCRIPTION = "description";
public static String[] SUPPORTED_MIMETYPES = new String[] {MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING,
MimetypeMap.MIMETYPE_OPENXML_SPREADSHEET,
MimetypeMap.MIMETYPE_OPENXML_PRESENTATION};
public static ArrayList<String> SUPPORTED_MIMETYPES = buildSupportedMimetypes(
new String[] {MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING,
MimetypeMap.MIMETYPE_OPENXML_SPREADSHEET,
MimetypeMap.MIMETYPE_OPENXML_PRESENTATION},
new OOXMLParser()
);
public PoiMetadataExtracter()
{
super(new HashSet<String>(Arrays.asList(SUPPORTED_MIMETYPES)));
super(SUPPORTED_MIMETYPES);
}
@Override
public Map<String, Serializable> extractRaw(ContentReader reader) throws Throwable
{
Map<String, Serializable> rawProperties = newRawMap();
InputStream is = null;
try
{
is = reader.getContentInputStream();
POIXMLDocument document = readDocumentFromStream(is, reader.getMimetype());
POIXMLPropertiesTextExtractor extracter = new POIXMLPropertiesTextExtractor(document);
CoreProperties coreProps = extracter.getCoreProperties();
putRawValue(KEY_AUTHOR, coreProps.getCreator(), rawProperties);
putRawValue(KEY_TITLE, coreProps.getTitle(), rawProperties);
putRawValue(KEY_SUBJECT, coreProps.getSubject(), rawProperties);
putRawValue(KEY_DESCRIPTION, coreProps.getDescription(), rawProperties);
putRawValue(KEY_CREATED, coreProps.getCreated(), rawProperties);
}
finally
{
if (is != null)
{
try { is.close(); } catch (IOException e) {}
}
}
return rawProperties;
protected Parser getParser() {
return new OOXMLParser();
}
private POIXMLDocument readDocumentFromStream(InputStream is, String mimetype)
throws IOException, OpenXML4JException, XmlException {
POIXMLDocument document = null;
if (MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING.equals(mimetype))
{
document = new XWPFDocument(OPCPackage.open(is));
}
else if (MimetypeMap.MIMETYPE_OPENXML_SPREADSHEET.equals(mimetype))
{
document = new XSSFWorkbook(OPCPackage.open(is));
}
else if (MimetypeMap.MIMETYPE_OPENXML_PRESENTATION.equals(mimetype))
{
document = new XSLFSlideShow(OPCPackage.open(is));
}
return document;
}
}