Search in sources :

Example 16 with NPOIFSFileSystem

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

the class TestBugs method getMacrosFromHSLF.

//It isn't pretty, but it works...
private Map<String, String> getMacrosFromHSLF(String fileName) throws IOException {
    InputStream is = null;
    NPOIFSFileSystem npoifs = null;
    try {
        is = new FileInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName));
        npoifs = new NPOIFSFileSystem(is);
        //TODO: should we run the VBAMacroReader on this npoifs?
        //TBD: We know that ppt typically don't store macros in the regular place,
        //but _can_ they?
        HSLFSlideShow ppt = new HSLFSlideShow(npoifs);
        //get macro persist id
        DocInfoListContainer list = (DocInfoListContainer) ppt.getDocumentRecord().findFirstOfType(RecordTypes.List.typeID);
        VBAInfoContainer vbaInfo = (VBAInfoContainer) list.findFirstOfType(RecordTypes.VBAInfo.typeID);
        VBAInfoAtom vbaAtom = (VBAInfoAtom) vbaInfo.findFirstOfType(RecordTypes.VBAInfoAtom.typeID);
        long persistId = vbaAtom.getPersistIdRef();
        for (HSLFObjectData objData : ppt.getEmbeddedObjects()) {
            if (objData.getExOleObjStg().getPersistId() == persistId) {
                VBAMacroReader mr = new VBAMacroReader(objData.getData());
                try {
                    return mr.readMacros();
                } finally {
                    mr.close();
                }
            }
        }
        ppt.close();
    } finally {
        IOUtils.closeQuietly(npoifs);
        IOUtils.closeQuietly(is);
    }
    return null;
}
Also used : VBAInfoContainer(org.apache.poi.hslf.record.VBAInfoContainer) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) VBAInfoAtom(org.apache.poi.hslf.record.VBAInfoAtom) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) VBAMacroReader(org.apache.poi.poifs.macros.VBAMacroReader) DocInfoListContainer(org.apache.poi.hslf.record.DocInfoListContainer) FileInputStream(java.io.FileInputStream)

Example 17 with NPOIFSFileSystem

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

the class TestRichTextRun method assertMatchesFileC.

/**
	 * Checks that the supplied slideshow still matches the bytes
	 *  of slideshow c
	 */
private static void assertMatchesFileC(HSLFSlideShow s) throws IOException {
    // Grab the bytes of the file
    NPOIFSFileSystem fs = new NPOIFSFileSystem(HSLFTestDataSamples.openSampleFileStream(filenameC));
    InputStream is = fs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
    byte[] raw_file = IOUtils.toByteArray(is);
    is.close();
    fs.close();
    // Now write out the slideshow
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    s.write(baos);
    fs = new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
    is = fs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
    byte[] raw_ss = IOUtils.toByteArray(is);
    is.close();
    fs.close();
    // different paragraph mask, because of sanitizing
    raw_ss[169030] = 0x0a;
    // Ensure they're the same
    assertArrayEquals(raw_file, raw_ss);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 18 with NPOIFSFileSystem

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

the class TestOutlookTextExtractor method testEncodings.

public void testEncodings() throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(samples.getFile("chinese-traditional.msg"), true);
    MAPIMessage msg = new MAPIMessage(poifs);
    OutlookTextExtactor ext = new OutlookTextExtactor(msg);
    String text = ext.getText();
    // Check the english bits
    assertContains(text, "From: Tests Chang@FT");
    assertContains(text, "tests.chang@fengttt.com");
    // And check some chinese bits
    assertContains(text, "(張毓倫)");
    assertContains(text, "( MSG 格式測試 )");
    ext.close();
    poifs.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem)

Example 19 with NPOIFSFileSystem

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

the class TestOutlookTextExtractor method testWithAttachedMessage.

public void testWithAttachedMessage() throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(samples.getFile("58214_with_attachment.msg"), true);
    MAPIMessage msg = new MAPIMessage(poifs);
    OutlookTextExtactor ext = new OutlookTextExtactor(msg);
    String text = ext.getText();
    // Check we got bits from the main message
    assertContains(text, "Master mail");
    assertContains(text, "ante in lacinia euismod");
    // But not the attached message
    assertNotContained(text, "Test mail attachment");
    assertNotContained(text, "Lorem ipsum dolor sit");
    ext.close();
    poifs.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem)

Example 20 with NPOIFSFileSystem

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

the class TestOutlookTextExtractor method testQuick.

@Test
public void testQuick() throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
    MAPIMessage msg = new MAPIMessage(poifs);
    OutlookTextExtactor ext = new OutlookTextExtactor(msg);
    String text = ext.getText();
    assertContains(text, "From: Kevin Roast\n");
    assertContains(text, "To: Kevin Roast <kevin.roast@alfresco.org>\n");
    assertNotContained(text, "CC:");
    assertNotContained(text, "BCC:");
    assertNotContained(text, "Attachment:");
    assertContains(text, "Subject: Test the content transformer\n");
    Calendar cal = LocaleUtil.getLocaleCalendar(2007, 5, 14, 9, 42, 55);
    SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT);
    f.setTimeZone(LocaleUtil.getUserTimeZone());
    String dateText = f.format(cal.getTime());
    assertContains(text, "Date: " + dateText + "\n");
    assertContains(text, "The quick brown fox jumps over the lazy dog");
    ext.close();
    poifs.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

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