try {
// Use TIS so we can spool a temp file for parsing.
TikaInputStream tis = TikaInputStream.get(stream);
//Extract information from header file
MatFileReader mfr = new MatFileReader(tis.getFile()); //input .mat file
MatFileHeader hdr = mfr.getMatFileHeader(); //.mat header information
// Example header: "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Sun Mar 2 23:41:57 2014"
String[] parts = hdr.getDescription().split(","); // Break header information into its parts
if (parts[2].contains("Created")) {
int lastIndex1 = parts[2].lastIndexOf("Created on:");
String dateCreated = parts[2].substring(lastIndex1 + "Created on:".length()).trim();
metadata.set("createdOn", dateCreated);
}
if (parts[1].contains("Platform")) {
int lastIndex2 = parts[1].lastIndexOf("Platform:");
String platform = parts[1].substring(lastIndex2 + "Platform:".length()).trim();
metadata.set("platform" , platform);
}
if (parts[0].contains("MATLAB")) {
metadata.set("fileType", parts[0]);
}
// Get endian indicator from header file
String endianBytes = new String(hdr.getEndianIndicator(), "UTF-8"); // Retrieve endian bytes and convert to string
String endianCode = String.valueOf(endianBytes.toCharArray()); // Convert bytes to characters to string
metadata.set("endian", endianCode);
//Text output
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
xhtml.newline();
//Loop through each variable
for (Map.Entry<String, MLArray> entry : mfr.getContent().entrySet()) {
String varName = entry.getKey();
MLArray varData = entry.getValue();
xhtml.element("p", varName + ":" + String.valueOf(varData));
// If the variable is a structure, extract variable info from structure
if (varData.isStruct()){
MLStructure mlStructure = (MLStructure) mfr.getMLArray(varName);
xhtml.startElement("ul");
xhtml.newline();
for (MLArray element : mlStructure.getAllFields()){
xhtml.startElement("li");
xhtml.characters(String.valueOf(element));