MatParser extends AbstractParser { public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { metadata.set(Metadata.CONTENT_TYPE, MATLAB_MIME_TYPE); TemporaryResources tmp = TikaInputStream.isTikaInputStream(stream) ? null : new TemporaryResources(); try { TikaInputStream tis = TikaInputStream.get(stream, tmp); MatFileReader mfr = new MatFileReader(tis.getFile()); MatFileHeader hdr = mfr.getMatFileHeader(); String[] parts = hdr.getDescription().split(","); 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]); } String endianBytes = new String(hdr.getEndianIndicator(), UTF_8); String endianCode = String.valueOf(endianBytes.toCharArray()); metadata.set("endian", endianCode); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.newline(); 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 (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)); if (element.isStruct()){ xhtml.startElement("ul"); xhtml.element("li", element.contentToString()); xhtml.endElement("ul"); } xhtml.endElement("li"); } xhtml.endElement("ul"); } } xhtml.endDocument(); } catch (IOException e) { throw new TikaException("Error parsing Matlab file with MatParser", e); } finally { if (tmp != null) { tmp.dispose(); } } }  Set<MediaType> getSupportedTypes(ParseContext context); void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context); static final String MATLAB_MIME_TYPE; }
@Test public void testParserForText() throws Exception { Parser parser = new MatParser(); ToXMLContentHandler handler = new ToXMLContentHandler(); Metadata metadata = new Metadata(); String path = "/test-documents/test_mat_text.mat"; try (InputStream stream = getResourceAsStream(path)) { parser.parse(stream, handler, metadata, new ParseContext()); } String content = handler.toString(); assertContains("<p>double:[2x2 double array]</p>", content); }