SUPPORTED_MIME_TYPES.add("application/vnd.openxmlformats-officedocument.spreadsheetml.template");
}
private Document[] parse(final MultiProtocolURI location, final String mimeType, final String charset, final File dest) throws Parser.Failure, InterruptedException {
CharBuffer writer = null;
try {
String docDescription = null;
String docKeywordStr = null;
String docShortTitle = null;
String docLongTitle = null;
String docAuthor = null;
String docLanguage = null;
// opening the file as zip file
final ZipFile zipFile= new ZipFile(dest);
final Enumeration<? extends ZipEntry> zipEnum = zipFile.entries();
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
// looping through all containing files
while (zipEnum.hasMoreElements()) {
// get next zip file entry
final ZipEntry zipEntry= zipEnum.nextElement();
final String entryName = zipEntry.getName();
// content.xml contains the document content in xml format
if (entryName.equals("word/document.xml")
|| entryName.startsWith("ppt/slides/slide")
|| entryName.startsWith("xl/worksheets/sheet")) {
// create a writer for output
writer = new CharBuffer();
// extract data
final InputStream zipFileEntryStream = zipFile.getInputStream(zipEntry);
final SAXParser saxParser = saxParserFactory.newSAXParser();
saxParser.parse(zipFileEntryStream, new ODContentHandler(writer));
// close readers and writers
zipFileEntryStream.close();
writer.close();
} else if (entryName.equals("docProps/core.xml")) {
// meta.xml contains metadata about the document
final InputStream zipFileEntryStream = zipFile.getInputStream(zipEntry);
final SAXParser saxParser = saxParserFactory.newSAXParser();
final ODMetaHandler metaData = new ODMetaHandler();
saxParser.parse(zipFileEntryStream, metaData);
docDescription = metaData.getDescription();
docKeywordStr = metaData.getKeyword();
docShortTitle = metaData.getTitle();
docLongTitle = metaData.getSubject();
docAuthor = metaData.getCreator();
docLanguage = metaData.getLanguage();
}
}
// make the languages set
final Set<String> languages = new HashSet<String>(1);
if (docLanguage != null && docLanguage.length() == 0)
languages.add(docLanguage);
// if there is no title availabe we generate one
if ((docLongTitle == null || docLongTitle.length() == 0) && (docShortTitle != null)) {
docLongTitle = docShortTitle;
}
// split the keywords
String[] docKeywords = null;
if (docKeywordStr != null) docKeywords = docKeywordStr.split(" |,");
// create the parser document
Document[] docs = null;
final byte[] contentBytes = UTF8.getBytes(writer.toString());
docs = new Document[]{new Document(
location,
mimeType,
"UTF-8",
this,
languages,
docKeywords,
docLongTitle,
docAuthor,
"",
null,
docDescription,
0.0f, 0.0f,
contentBytes,
null,
null,
null,
false)};
return docs;
} catch (final Exception e) {
if (e instanceof InterruptedException) throw (InterruptedException) e;
if (e instanceof Parser.Failure) throw (Parser.Failure) e;
// close the writer
if (writer != null) try {
writer.close();
} catch (final Exception ex) {/* ignore this */}
Log.logException(e);
throw new Parser.Failure("Unexpected error while parsing odt file. " + e.getMessage(),location);
}