PRTParser extends AbstractParser { public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); Last5 l5 = new Last5(); int read; byte[] header = new byte[30]; IOUtils.readFully(stream, header); byte[] date = new byte[12]; IOUtils.readFully(stream, date); String dateStr = new String(date, US_ASCII); if(dateStr.startsWith("19") || dateStr.startsWith("20")) { String formattedDate = dateStr.substring(0, 4) + "-" + dateStr.substring(4,6) + "-" + dateStr.substring(6,8) + "T" + dateStr.substring(8,10) + ":" + dateStr.substring(10, 12) + ":00"; metadata.set(TikaCoreProperties.CREATED, formattedDate); metadata.set(TikaCoreProperties.CREATED, formattedDate); } metadata.set(Metadata.CONTENT_TYPE, PRT_MIME_TYPE); byte[] desc = new byte[500]; IOUtils.readFully(stream, desc); String description = extractText(desc, true); if(description.length() > 0) { metadata.set(TikaCoreProperties.DESCRIPTION, description); } while( (read = stream.read()) > -1) { if(read == 0xe0 || read == 0xe3 || read == 0xf0) { int nread = stream.read(); if(nread == 0x3f || nread == 0xbf) { if(read == 0xe3 && nread == 0x3f) { if(l5.is33()) { handleNoteText(stream, xhtml); } } else if(l5.is00()) { handleViewName(read, nread, stream, xhtml, l5); } } } else { l5.record(read); } } }  Set<MediaType> getSupportedTypes(ParseContext context); void parse(InputStream stream, ContentHandler handler, Metadata metadata, 
          ParseContext context); static final String PRT_MIME_TYPE; }
@Test public void testPRTParserBasics() throws Exception { try (InputStream input = getResourceAsStream("/test-documents/testCADKEY.prt")) { Metadata metadata = new Metadata(); ContentHandler handler = new BodyContentHandler(); new PRTParser().parse(input, handler, metadata); assertEquals("application/x-prt", metadata.get(Metadata.CONTENT_TYPE)); assertEquals("2011-06-20T16:54:00", metadata.get(TikaCoreProperties.CREATED)); assertEquals(null, metadata.get(TikaCoreProperties.DESCRIPTION)); String contents = handler.toString(); assertContains("Front View", contents); assertContains("Back View", contents); assertContains("Bottom View", contents); assertContains("Right View", contents); assertContains("Left View", contents); assertContains("Axonometric View", contents); assertContains("You've managed to extract all the text!", contents); assertContains("This is more text", contents); assertContains("Text Inside a PRT file", contents); } }
@Test public void testPRTParserComplex() throws Exception { try (InputStream input = getResourceAsStream("/test-documents/testCADKEY2.prt")) { Metadata metadata = new Metadata(); ContentHandler handler = new BodyContentHandler(); new PRTParser().parse(input, handler, metadata); assertEquals("application/x-prt", metadata.get(Metadata.CONTENT_TYPE)); assertEquals("1997-04-01T08:59:00", metadata.get(TikaCoreProperties.CREATED)); assertEquals("TIKA TEST PART DESCRIPTION INFORMATION\r\n", metadata.get(TikaCoreProperties.DESCRIPTION)); String contents = handler.toString(); assertContains("ITEM", contents); assertContains("REQ.", contents); assertContains("DESCRIPTION", contents); assertContains("MAT'L", contents); assertContains("TOLERANCES UNLESS", contents); assertContains("FRACTIONS", contents); assertContains("ANGLES", contents); assertContains("Acme Corporation", contents); assertContains("DATE", contents); assertContains("CHANGE", contents); assertContains("DRAWN BY", contents); assertContains("SCALE", contents); assertContains("TIKA TEST DRAWING", contents); assertContains("TIKA LETTERS", contents); assertContains("5.82", contents); assertContains("112" + '\u00b0', contents); assertContains("TIKA TEST LETTER", contents); assertContains("17.11", contents); assertContains('\u00d8' + "\ufffd2.000", contents); assertContains("Diameter", contents); assertContains("The Apache Tika toolkit", contents); } }