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 17 package com.android.emailcommon.utility; 18 19 import com.android.emailcommon.internet.MimeHeader; 20 import com.android.emailcommon.internet.MimeUtility; 21 import com.android.emailcommon.mail.MessagingException; 22 import com.android.emailcommon.mail.Part; 23 24 import android.text.TextUtils; 25 26 import java.util.ArrayList; 27 28 public class ConversionUtilities { 29 /** 30 * 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 */ appendTextPart(StringBuffer sb, String newText)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; 56 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 */ parseBodyFields(ArrayList<Part> viewables)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; 69 70 for (Part viewable : viewables) { 71 String text = MimeUtility.getTextFromPart(viewable); 72 // Deploy text as marked by the various tags 73 boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType()); 74 75 // 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 } 96 return data; 97 } 98 } 99