blob: 7bfe7d96774a5dc9856d357c4977c6d83385685b [file] [log] [blame]
Andrew Sapperstein79225282013-06-04 15:02:38 -07001/*
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 /**
Andrew Sapperstein79225282013-06-04 15:02:38 -070030 * Helper function to append text to a StringBuffer, creating it if necessary.
31 * Optimization: The majority of the time we are *not* appending - we should have a path
32 * that deals with single strings.
33 */
34 private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
35 if (newText == null) {
36 return sb;
37 }
38 else if (sb == null) {
39 sb = new StringBuffer(newText);
40 } else {
41 if (sb.length() > 0) {
42 sb.append('\n');
43 }
44 sb.append(newText);
45 }
46 return sb;
47 }
48
49 /**
50 * Plain-Old-Data class to return parsed body data from
51 * {@link ConversionUtilities#parseBodyFields}
52 */
53 public static class BodyFieldData {
54 public String textContent;
55 public String htmlContent;
Andrew Sapperstein79225282013-06-04 15:02:38 -070056 public String snippet;
57 public boolean isQuotedReply;
58 public boolean isQuotedForward;
59 }
60
61 /**
62 * Parse body text (plain and/or HTML) from MimeMessage to {@link BodyFieldData}.
63 */
64 public static BodyFieldData parseBodyFields(ArrayList<Part> viewables)
65 throws MessagingException {
66 final BodyFieldData data = new BodyFieldData();
67 StringBuffer sbHtml = null;
68 StringBuffer sbText = null;
Andrew Sapperstein79225282013-06-04 15:02:38 -070069
70 for (Part viewable : viewables) {
71 String text = MimeUtility.getTextFromPart(viewable);
Andrew Sapperstein79225282013-06-04 15:02:38 -070072 // Deploy text as marked by the various tags
73 boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
74
Andrew Sapperstein79225282013-06-04 15:02:38 -070075 // Most of the time, just process regular body parts
76 if (isHtml) {
77 sbHtml = appendTextPart(sbHtml, text);
78 } else {
79 sbText = appendTextPart(sbText, text);
80 }
81 }
82
83 // write the combined data to the body part
84 if (!TextUtils.isEmpty(sbText)) {
85 String text = sbText.toString();
86 data.textContent = text;
87 data.snippet = TextUtilities.makeSnippetFromPlainText(text);
88 }
89 if (!TextUtils.isEmpty(sbHtml)) {
90 String text = sbHtml.toString();
91 data.htmlContent = text;
92 if (data.snippet == null) {
93 data.snippet = TextUtilities.makeSnippetFromHtmlText(text);
94 }
95 }
Andrew Sapperstein79225282013-06-04 15:02:38 -070096 return data;
97 }
98}