Search in sources :

Example 1 with StringsIterator

use of org.apache.poi.util.StringUtil.StringsIterator in project poi by apache.

the class TestStringUtil method testStringsIterator.

@Test
public void testStringsIterator() {
    StringsIterator i;
    i = new StringsIterator(new String[0]);
    assertFalse(i.hasNext());
    try {
        i.next();
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
    i = new StringsIterator(new String[] { "1" });
    assertTrue(i.hasNext());
    assertEquals("1", i.next());
    assertFalse(i.hasNext());
    try {
        i.next();
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
    i = new StringsIterator(new String[] { "1", "2", "3" });
    assertTrue(i.hasNext());
    assertEquals("1", i.next());
    assertTrue(i.hasNext());
    assertEquals("2", i.next());
    assertTrue(i.hasNext());
    assertEquals("3", i.next());
    assertFalse(i.hasNext());
    try {
        i.next();
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
}
Also used : StringsIterator(org.apache.poi.util.StringUtil.StringsIterator) Test(org.junit.Test)

Example 2 with StringsIterator

use of org.apache.poi.util.StringUtil.StringsIterator in project poi by apache.

the class OutlookTextExtactor method getText.

/**
    * Outputs something a little like a RFC822 email
    */
public String getText() {
    MAPIMessage msg = (MAPIMessage) document;
    StringBuffer s = new StringBuffer();
    // See if we can get a suitable encoding for any
    //  non unicode text in the file
    msg.guess7BitEncoding();
    // Off we go
    StringsIterator emails;
    try {
        emails = new StringsIterator(msg.getRecipientEmailAddressList());
    } catch (ChunkNotFoundException e) {
        emails = new StringsIterator(new String[0]);
    }
    try {
        s.append("From: " + msg.getDisplayFrom() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    //  people in To + CC + BCC.
    try {
        handleEmails(s, "To", msg.getDisplayTo(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "CC", msg.getDisplayCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "BCC", msg.getDisplayBCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    // Date - try two ways to find it
    try {
        // First try via the proper chunk
        SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT);
        f.setTimeZone(LocaleUtil.getUserTimeZone());
        s.append("Date: " + f.format(msg.getMessageDate().getTime()) + "\n");
    } catch (ChunkNotFoundException e) {
        try {
            // Failing that try via the raw headers 
            String[] headers = msg.getHeaders();
            for (String header : headers) {
                if (startsWithIgnoreCase(header, "date:")) {
                    s.append("Date:" + header.substring(header.indexOf(':') + 1) + "\n");
                    break;
                }
            }
        } catch (ChunkNotFoundException he) {
        // We can't find the date, sorry...
        }
    }
    try {
        s.append("Subject: " + msg.getSubject() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    // To get the attachments, use ExtractorFactory
    for (AttachmentChunks att : msg.getAttachmentFiles()) {
        StringChunk name = att.getAttachLongFileName();
        if (name == null)
            name = att.getAttachFileName();
        String attName = name == null ? null : name.getValue();
        if (att.getAttachMimeTag() != null && att.getAttachMimeTag().getValue() != null) {
            attName = att.getAttachMimeTag().getValue() + " = " + attName;
        }
        s.append("Attachment: " + attName + "\n");
    }
    try {
        s.append("\n" + msg.getTextBody() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    return s.toString();
}
Also used : ChunkNotFoundException(org.apache.poi.hsmf.exceptions.ChunkNotFoundException) MAPIMessage(org.apache.poi.hsmf.MAPIMessage) SimpleDateFormat(java.text.SimpleDateFormat) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) StringsIterator(org.apache.poi.util.StringUtil.StringsIterator) StringChunk(org.apache.poi.hsmf.datatypes.StringChunk)

Aggregations

StringsIterator (org.apache.poi.util.StringUtil.StringsIterator)2 SimpleDateFormat (java.text.SimpleDateFormat)1 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)1 AttachmentChunks (org.apache.poi.hsmf.datatypes.AttachmentChunks)1 StringChunk (org.apache.poi.hsmf.datatypes.StringChunk)1 ChunkNotFoundException (org.apache.poi.hsmf.exceptions.ChunkNotFoundException)1 Test (org.junit.Test)1