Example usage for org.apache.poi.hsmf.datatypes StringChunk getValue

List of usage examples for org.apache.poi.hsmf.datatypes StringChunk getValue

Introduction

In this page you can find the example usage for org.apache.poi.hsmf.datatypes StringChunk getValue.

Prototype

public String getValue() 

Source Link

Document

Returns the Text value of the chunk

Usage

From source file:org.alfresco.repo.content.transform.MSGParser.java

License:Apache License

/**
 * Process header./*  www . ja  va  2 s. c  o  m*/
 *
 * @param msg
 *            the msg
 * @param metadata
 *            the metadata
 * @param xhtml
 *            the xhtml
 * @throws Exception
 *             the exception
 */
private void processHeader(MAPIMessage msg, Metadata metadata, XHTMLContentHandler xhtml) throws Exception {
    StringChunk subjectChunk = msg.getMainChunks().subjectChunk;
    if (msg.has7BitEncodingStrings()) {
        CharsetDetector detector = new CharsetDetector();
        detector.setText(subjectChunk.getRawValue());
        CharsetMatch detect = detector.detect();
        if (detect.getConfidence() >= 20) {
            subjectChunk.set7BitEncoding(detect.getName());
        }
    }
    String subject = subjectChunk.getValue();
    String from = msg.getDisplayFrom();

    metadata.set(DublinCore.CREATOR, from);
    metadata.set(Metadata.MESSAGE_FROM, from);
    metadata.set(Metadata.MESSAGE_TO, msg.getDisplayTo());
    metadata.set(Metadata.MESSAGE_CC, msg.getDisplayCC());
    metadata.set(Metadata.MESSAGE_BCC, msg.getDisplayBCC());

    metadata.set(DublinCore.TITLE, subject);
    metadata.set(DublinCore.SUBJECT, msg.getConversationTopic());

    try {
        for (String recipientAddress : msg.getRecipientEmailAddressList()) {
            if (recipientAddress != null)
                metadata.add(Metadata.MESSAGE_RECIPIENT_ADDRESS, recipientAddress);
        }
    } catch (ChunkNotFoundException he) {
    } // Will be fixed in POI 3.7 Final

    // Date - try two ways to find it
    // First try via the proper chunk
    if (msg.getMessageDate() != null) {
        metadata.set(DublinCore.DATE, msg.getMessageDate().getTime());
        metadata.set(Office.CREATION_DATE, msg.getMessageDate().getTime());
        metadata.set(Office.SAVE_DATE, msg.getMessageDate().getTime());
    } else {
        try {
            // Failing that try via the raw headers
            String[] headers = msg.getHeaders();
            if (headers != null && headers.length > 0) {
                for (String header : headers) {
                    if (header.toLowerCase().startsWith("date:")) {
                        String date = header.substring(header.indexOf(':') + 1).trim();

                        // See if we can parse it as a normal mail date
                        try {
                            Date d = MboxParser.parseDate(date);
                            metadata.set(DublinCore.DATE, d);
                            metadata.set(Office.CREATION_DATE, d);
                            metadata.set(Office.SAVE_DATE, d);
                        } catch (ParseException e) {
                            // Store it as-is, and hope for the best...
                            metadata.set(DublinCore.DATE, date);
                            metadata.set(Office.CREATION_DATE, date);
                            metadata.set(Office.SAVE_DATE, date);
                        }
                        break;
                    }
                }
            }
        } catch (ChunkNotFoundException he) {
            // We can't find the date, sorry...
        }
    }

    xhtml.element("h1", subject);

    // Output the from and to details in text, as you
    // often want them in text form for searching
    xhtml.startElement("dl");
    if (from != null) {
        header(xhtml, "From", from);
    }
    header(xhtml, "To", msg.getDisplayTo());
    header(xhtml, "Cc", msg.getDisplayCC());
    header(xhtml, "Bcc", msg.getDisplayBCC());
    try {
        header(xhtml, "Recipients", msg.getRecipientEmailAddress());
    } catch (ChunkNotFoundException e) {
    }
    List<String> attachmentList = new ArrayList<String>();
    // // prepare attachments
    prepareExtractMultipart(xhtml, message, attachmentList);
    if (attachmentList.size() > 0) {
        header(xhtml, "Attachments", attachmentList.toString());
    }
    xhtml.endElement("dl");

}