Search in sources :

Example 31 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class TestWriteWellKnown method write2ndFile.

/*
     * Open <em>doc2</em> for reading and check summary information and
     * document summary information. All properties written before must be
     * found in the property streams of <em>doc2</em> and have the correct
     * values.
     */
private static CustomProperties write2ndFile(File fileIn, File fileOut) throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(fileIn, false);
    SummaryInformation si = getSummaryInformation(poifs);
    DocumentSummaryInformation dsi = getDocumentSummaryInformation(poifs);
    assertEquals(P_APPLICATION_NAME, si.getApplicationName());
    assertEquals(P_AUTHOR, si.getAuthor());
    assertEquals(P_CHAR_COUNT, si.getCharCount());
    assertEquals(P_COMMENTS, si.getComments());
    assertEquals(P_CREATE_DATE_TIME, si.getCreateDateTime());
    assertEquals(P_EDIT_TIME, si.getEditTime());
    assertEquals(P_KEYWORDS, si.getKeywords());
    assertEquals(P_LAST_AUTHOR, si.getLastAuthor());
    assertEquals(P_LAST_PRINTED, si.getLastPrinted());
    assertEquals(P_LAST_SAVE_DATE_TIME, si.getLastSaveDateTime());
    assertEquals(P_PAGE_COUNT, si.getPageCount());
    assertEquals(P_REV_NUMBER, si.getRevNumber());
    assertEquals(P_SECURITY, si.getSecurity());
    assertEquals(P_SUBJECT, si.getSubject());
    assertEquals(P_TEMPLATE, si.getTemplate());
    // FIXME (byte array properties not yet implemented): assertEquals(P_THUMBNAIL, si.getThumbnail());
    assertEquals(P_TITLE, si.getTitle());
    assertEquals(P_WORD_COUNT, si.getWordCount());
    assertEquals(P_BYTE_COUNT, dsi.getByteCount());
    assertEquals(P_CATEGORY, dsi.getCategory());
    assertEquals(P_COMPANY, dsi.getCompany());
    // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getDocparts());
    // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getHeadingPair());
    assertEquals(P_HIDDEN_COUNT, dsi.getHiddenCount());
    assertEquals(P_LINE_COUNT, dsi.getLineCount());
    assertEquals(P_LINKS_DIRTY, dsi.getLinksDirty());
    assertEquals(P_MANAGER, dsi.getManager());
    assertEquals(P_MM_CLIP_COUNT, dsi.getMMClipCount());
    assertEquals(P_NOTE_COUNT, dsi.getNoteCount());
    assertEquals(P_PAR_COUNT, dsi.getParCount());
    assertEquals(P_PRESENTATION_FORMAT, dsi.getPresentationFormat());
    assertEquals(P_SCALE, dsi.getScale());
    assertEquals(P_SLIDE_COUNT, dsi.getSlideCount());
    final CustomProperties cps = dsi.getCustomProperties();
    assertNotNull(cps);
    assertNull(cps.get("No value available"));
    assertEquals("Wert ä", cps.get("Schlüssel ä"));
    assertEquals("Wert äö", cps.get("Schlüssel äö"));
    assertEquals("Wert äöü", cps.get("Schlüssel äöü"));
    assertEquals("Wert äöüÖ", cps.get("Schlüssel äöüÖ"));
    assertEquals(POSITIVE_INTEGER, cps.get("positive_Integer"));
    assertEquals(POSITIVE_LONG, cps.get("positive_Long"));
    assertEquals(POSITIVE_DOUBLE, cps.get("positive_Double"));
    assertEquals(NEGATIVE_INTEGER, cps.get("negative_Integer"));
    assertEquals(NEGATIVE_LONG, cps.get("negative_Long"));
    assertEquals(NEGATIVE_DOUBLE, cps.get("negative_Double"));
    assertEquals(Boolean.TRUE, cps.get("Boolean"));
    assertEquals(now, cps.get("Date"));
    assertEquals(MAX_INTEGER, cps.get("max_Integer"));
    assertEquals(MIN_INTEGER, cps.get("min_Integer"));
    assertEquals(MAX_LONG, cps.get("max_Long"));
    assertEquals(MIN_LONG, cps.get("min_Long"));
    assertEquals(MAX_DOUBLE, cps.get("max_Double"));
    assertEquals(MIN_DOUBLE, cps.get("min_Double"));
    /* Remove all properties supported by HPSF from the summary
         * information (e.g. author, edit date, application name) and from the
         * document summary information (e.g. company, manager). */
    si.removeApplicationName();
    si.removeAuthor();
    si.removeCharCount();
    si.removeComments();
    si.removeCreateDateTime();
    si.removeEditTime();
    si.removeKeywords();
    si.removeLastAuthor();
    si.removeLastPrinted();
    si.removeLastSaveDateTime();
    si.removePageCount();
    si.removeRevNumber();
    si.removeSecurity();
    si.removeSubject();
    si.removeTemplate();
    si.removeThumbnail();
    si.removeTitle();
    si.removeWordCount();
    dsi.removeByteCount();
    dsi.removeCategory();
    dsi.removeCompany();
    dsi.removeCustomProperties();
    dsi.removeDocparts();
    dsi.removeHeadingPair();
    dsi.removeHiddenCount();
    dsi.removeLineCount();
    dsi.removeLinksDirty();
    dsi.removeManager();
    dsi.removeMMClipCount();
    dsi.removeNoteCount();
    dsi.removeParCount();
    dsi.removePresentationFormat();
    dsi.removeScale();
    dsi.removeSlideCount();
    // Write the summary information stream and the document summary
    // information stream to the POI filesystem.
    si.write(poifs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    // Write the POI filesystem to a (temporary) file doc3 and close the latter.
    FileOutputStream out = new FileOutputStream(fileOut);
    poifs.writeFilesystem(out);
    out.close();
    poifs.close();
    return cps;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 32 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class TestBiffViewer method runOneFile.

@Override
void runOneFile(File fileIn) throws IOException {
    NPOIFSFileSystem fs = new NPOIFSFileSystem(fileIn, true);
    try {
        InputStream is = BiffViewer.getPOIFSInputStream(fs);
        try {
            // use a NullOutputStream to not write the bytes anywhere for best runtime
            PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252));
            BiffViewer.runBiffViewer(dummy, is, true, true, true, false);
        } finally {
            is.close();
        }
    } finally {
        fs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) RecordInputStream(org.apache.poi.hssf.record.RecordInputStream) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 33 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class TestHSSFWorkbook method inPlaceWrite.

@Test
public void inPlaceWrite() throws Exception {
    // Setup as a copy of a known-good file
    final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
    InputStream inputStream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls");
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        try {
            IOUtils.copy(inputStream, outputStream);
        } finally {
            outputStream.close();
        }
    } finally {
        inputStream.close();
    }
    // Open from the temp file in read-write mode
    HSSFWorkbook wb = new HSSFWorkbook(new NPOIFSFileSystem(file, false));
    assertEquals(3, wb.getNumberOfSheets());
    // Change
    wb.removeSheetAt(2);
    wb.removeSheetAt(1);
    wb.getSheetAt(0).getRow(0).getCell(0).setCellValue("Changed!");
    // Save in-place, close, re-open and check
    wb.write();
    wb.close();
    wb = new HSSFWorkbook(new NPOIFSFileSystem(file));
    assertEquals(1, wb.getNumberOfSheets());
    assertEquals("Changed!", wb.getSheetAt(0).getRow(0).getCell(0).toString());
    wb.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 34 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class TestHSSFWorkbook method invalidInPlaceWrite.

@Test
public void invalidInPlaceWrite() throws Exception {
    HSSFWorkbook wb;
    // Can't work for new files
    wb = new HSSFWorkbook();
    try {
        wb.write();
        fail("Shouldn't work for new files");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for InputStream opened files
    wb = new HSSFWorkbook(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
    try {
        wb.write();
        fail("Shouldn't work for InputStream");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for OPOIFS
    OPOIFSFileSystem ofs = new OPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
    wb = new HSSFWorkbook(ofs.getRoot(), true);
    try {
        wb.write();
        fail("Shouldn't work for OPOIFSFileSystem");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for Read-Only files
    NPOIFSFileSystem fs = new NPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"), true);
    wb = new HSSFWorkbook(fs);
    try {
        wb.write();
        fail("Shouldn't work for Read Only");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) Test(org.junit.Test)

Example 35 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.

the class TestVBAMacroReader method fromNPOIFS.

protected void fromNPOIFS(POIDataSamples dataSamples, String filename) throws IOException {
    File f = dataSamples.getFile(filename);
    NPOIFSFileSystem fs = new NPOIFSFileSystem(f);
    try {
        VBAMacroReader r = new VBAMacroReader(fs);
        try {
            assertMacroContents(dataSamples, r);
        } finally {
            r.close();
        }
    } finally {
        fs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) File(java.io.File)

Aggregations

NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)101 Test (org.junit.Test)57 File (java.io.File)35 InputStream (java.io.InputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)14 FileOutputStream (java.io.FileOutputStream)12 TempFile (org.apache.poi.util.TempFile)12 FileInputStream (java.io.FileInputStream)11 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)10 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)10 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)9 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)8 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)7 TikaInputStream (org.apache.tika.io.TikaInputStream)6 AgileDecryptor (org.apache.poi.poifs.crypt.agile.AgileDecryptor)5 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)5