/** * Setup is used to get the document ready. Gets the DocumentSummaryInformation and the * SummaryInformation to reasonable values */ public void setUp() { bout = new ByteArrayOutputStream(); poifs = new POIFSFileSystem(); dir = poifs.getRoot(); dsi = null; try { DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); DocumentInputStream dis = new DocumentInputStream(dsiEntry); PropertySet ps = new PropertySet(dis); dis.close(); dsi = new DocumentSummaryInformation(ps); } catch (FileNotFoundException ex) { /* There is no document summary information yet. We have to create a * new one. */ dsi = PropertySetFactory.newDocumentSummaryInformation(); assertNotNull(dsi); } catch (IOException e) { e.printStackTrace(); fail(); } catch (NoPropertySetStreamException e) { e.printStackTrace(); fail(); } catch (MarkUnsupportedException e) { e.printStackTrace(); fail(); } catch (UnexpectedPropertySetTypeException e) { e.printStackTrace(); fail(); } assertNotNull(dsi); try { DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); DocumentInputStream dis = new DocumentInputStream(dsiEntry); PropertySet ps = new PropertySet(dis); dis.close(); si = new SummaryInformation(ps); } catch (FileNotFoundException ex) { /* There is no document summary information yet. We have to create a * new one. */ si = PropertySetFactory.newSummaryInformation(); assertNotNull(si); } catch (IOException e) { e.printStackTrace(); fail(); } catch (NoPropertySetStreamException e) { e.printStackTrace(); fail(); } catch (MarkUnsupportedException e) { e.printStackTrace(); fail(); } catch (UnexpectedPropertySetTypeException e) { e.printStackTrace(); fail(); } assertNotNull(dsi); }
/** * Compares two {@link DirectoryEntry} instances of a POI file system. The directories must * contain the same streams with the same names and contents. * * @param d1 The first directory. * @param d2 The second directory. * @param msg The method may append human-readable comparison messages to this string buffer. * @return <code>true</code> if the directories are equal, else <code>false</code>. * @exception MarkUnsupportedException if a POI document stream does not support the mark() * operation. * @exception NoPropertySetStreamException if the application tries to create a property set from * a POI document stream that is not a property set stream. * @throws UnsupportedEncodingException * @exception IOException if any I/O exception occurs. */ private static boolean equal( final DirectoryEntry d1, final DirectoryEntry d2, final StringBuffer msg) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException { boolean equal = true; /* Iterate over d1 and compare each entry with its counterpart in d2. */ for (final Iterator i = d1.getEntries(); equal && i.hasNext(); ) { final Entry e1 = (Entry) i.next(); final String n1 = e1.getName(); Entry e2 = null; try { e2 = d2.getEntry(n1); } catch (FileNotFoundException ex) { msg.append("Document \"" + e1 + "\" exists, document \"" + e2 + "\" does not.\n"); equal = false; break; } if (e1.isDirectoryEntry() && e2.isDirectoryEntry()) equal = equal((DirectoryEntry) e1, (DirectoryEntry) e2, msg); else if (e1.isDocumentEntry() && e2.isDocumentEntry()) equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg); else { msg.append( "One of \"" + e1 + "\" and \"" + e2 + "\" is a " + "document while the other one is a directory.\n"); equal = false; } } /* Iterate over d2 just to make sure that there are no entries in d2 * that are not in d1. */ for (final Iterator i = d2.getEntries(); equal && i.hasNext(); ) { final Entry e2 = (Entry) i.next(); final String n2 = e2.getName(); Entry e1 = null; try { e1 = d1.getEntry(n2); } catch (FileNotFoundException ex) { msg.append("Document \"" + e2 + "\" exitsts, document \"" + e1 + "\" does not.\n"); equal = false; break; } } return equal; }
/** * Is this one of the kinds of formats which uses CompObj to store all of their data, eg Star * Draw, Star Impress or (older) Works? If not, it's likely an embedded resource */ private static MediaType processCompObjFormatType(DirectoryEntry root) { try { Entry e = root.getEntry("\u0001CompObj"); if (e != null && e.isDocumentEntry()) { DocumentNode dn = (DocumentNode) e; DocumentInputStream stream = new DocumentInputStream(dn); byte[] bytes = IOUtils.toByteArray(stream); /* * This array contains a string with a normal ASCII name of the * application used to create this file. We want to search for that * name. */ if (arrayContains(bytes, MS_GRAPH_CHART_BYTES)) { return MS_GRAPH_CHART; } else if (arrayContains(bytes, STAR_DRAW)) { return SDA; } else if (arrayContains(bytes, STAR_IMPRESS)) { return SDD; } else if (arrayContains(bytes, WORKS_QUILL96)) { return WPS; } } } catch (Exception e) { /* * "root.getEntry" can throw FileNotFoundException. The code inside * "if" can throw IOExceptions. Theoretically. Practically no * exceptions will likely ever appear. * * Swallow all of them. If any occur, we just assume that we can't * distinguish between Draw and Impress and return something safe: * x-tika-msoffice */ } return OLE; }
/** * Closes the ByteArrayOutputStream and reads it into a ByteArrayInputStream. When finished * writing information this method is used in the tests to start reading from the created document * and then the see if the results match. */ public void closeAndReOpen() { try { dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME); si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME); } catch (WritingNotSupportedException e) { e.printStackTrace(); fail(); } catch (IOException e) { e.printStackTrace(); fail(); } si = null; dsi = null; try { poifs.writeFilesystem(bout); bout.flush(); } catch (IOException e) { e.printStackTrace(); fail(); } InputStream is = new ByteArrayInputStream(bout.toByteArray()); assertNotNull(is); POIFSFileSystem poifs = null; try { poifs = new POIFSFileSystem(is); } catch (IOException e) { e.printStackTrace(); fail(); } try { is.close(); } catch (IOException e) { e.printStackTrace(); fail(); } assertNotNull(poifs); /* Read the document summary information. */ DirectoryEntry dir = poifs.getRoot(); try { DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); DocumentInputStream dis = new DocumentInputStream(dsiEntry); PropertySet ps = new PropertySet(dis); dis.close(); dsi = new DocumentSummaryInformation(ps); } catch (FileNotFoundException ex) { fail(); } catch (IOException e) { e.printStackTrace(); fail(); } catch (NoPropertySetStreamException e) { e.printStackTrace(); fail(); } catch (MarkUnsupportedException e) { e.printStackTrace(); fail(); } catch (UnexpectedPropertySetTypeException e) { e.printStackTrace(); fail(); } try { DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); DocumentInputStream dis = new DocumentInputStream(dsiEntry); PropertySet ps = new PropertySet(dis); dis.close(); si = new SummaryInformation(ps); } catch (FileNotFoundException ex) { /* There is no document summary information yet. We have to create a * new one. */ si = PropertySetFactory.newSummaryInformation(); assertNotNull(si); } catch (IOException e) { e.printStackTrace(); fail(); } catch (NoPropertySetStreamException e) { e.printStackTrace(); fail(); } catch (MarkUnsupportedException e) { e.printStackTrace(); fail(); } catch (UnexpectedPropertySetTypeException e) { e.printStackTrace(); fail(); } }