ConversionUtilities.java revision 08207dab923fe90b32ac5dafd4c8f33d54e48063
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.emailcommon.utility;
18
19import com.android.emailcommon.internet.MimeHeader;
20import com.android.emailcommon.internet.MimeUtility;
21import com.android.emailcommon.mail.MessagingException;
22import com.android.emailcommon.mail.Part;
23
24import android.text.TextUtils;
25
26import java.util.ArrayList;
27
28public class ConversionUtilities {
29    /**
30     * Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
31     */
32    public static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
33    public static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
34    public static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
35
36    /**
37     * Helper function to append text to a StringBuffer, creating it if necessary.
38     * Optimization:  The majority of the time we are *not* appending - we should have a path
39     * that deals with single strings.
40     */
41    private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
42        if (newText == null) {
43            return sb;
44        }
45        else if (sb == null) {
46            sb = new StringBuffer(newText);
47        } else {
48            if (sb.length() > 0) {
49                sb.append('\n');
50            }
51            sb.append(newText);
52        }
53        return sb;
54    }
55
56    /**
57     * Plain-Old-Data class to return parsed body data from
58     * {@link ConversionUtilities#parseBodyFields}
59     */
60    public static class BodyFieldData {
61        public String textContent;
62        public String htmlContent;
63        public String snippet;
64        public boolean isQuotedReply;
65        public boolean isQuotedForward;
66    }
67
68    /**
69     * Parse body text (plain and/or HTML) from MimeMessage to {@link BodyFieldData}.
70     */
71    public static BodyFieldData parseBodyFields(ArrayList<Part> viewables)
72    throws MessagingException {
73        final BodyFieldData data = new BodyFieldData();
74        StringBuffer sbHtml = null;
75        StringBuffer sbText = null;
76
77        for (Part viewable : viewables) {
78            String text = MimeUtility.getTextFromPart(viewable);
79            // Deploy text as marked by the various tags
80            boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
81
82            // Most of the time, just process regular body parts
83            if (isHtml) {
84                sbHtml = appendTextPart(sbHtml, text);
85            } else {
86                sbText = appendTextPart(sbText, text);
87            }
88        }
89
90        // write the combined data to the body part
91        if (!TextUtils.isEmpty(sbText)) {
92            String text = sbText.toString();
93            data.textContent = text;
94            data.snippet = TextUtilities.makeSnippetFromPlainText(text);
95        }
96        if (!TextUtils.isEmpty(sbHtml)) {
97            String text = sbHtml.toString();
98            data.htmlContent = text;
99            if (data.snippet == null) {
100                data.snippet = TextUtilities.makeSnippetFromHtmlText(text);
101            }
102        }
103        return data;
104    }
105}
106