EmailHtmlUtil.java revision a290f503f14432163f74548a5e5d1dc5003ad049
1/*
2 * Copyright (C) 2008 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.email.mail.internet;
18
19import com.android.email.mail.MessagingException;
20import com.android.email.mail.Multipart;
21import com.android.email.mail.Part;
22import com.android.email.mail.store.LocalStore.LocalAttachmentBodyPart;
23import com.android.email.provider.AttachmentProvider;
24import com.android.email.provider.EmailContent;
25
26import android.content.ContentResolver;
27import android.net.Uri;
28
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32public class EmailHtmlUtil {
33
34    // Regex that matches characters that have special meaning in HTML. '<', '>', '&' and
35    // multiple continuous spaces.
36    private static final Pattern PLAIN_TEXT_TO_ESCAPE = Pattern.compile("[<>&]| {2,}|\r?\n");
37
38    /**
39     * Resolve content-id reference in src attribute of img tag to AttachmentProvider's
40     * content uri.  This method calls itself recursively at most the number of
41     * LocalAttachmentPart that mime type is image and has content id.
42     * The attribute src="cid:content_id" is resolved as src="content://...".
43     * This method is package scope for testing purpose.
44     *
45     * @param text html email text
46     * @param part mime part which may contain inline image
47     * @return html text in which src attribute of img tag may be replaced with content uri
48     */
49    public static String resolveInlineImage(
50            ContentResolver resolver, EmailContent.Account account, String text, Part part, int depth)
51        throws MessagingException {
52        // avoid too deep recursive call.
53        if (depth >= 10 || text == null) {
54            return text;
55        }
56        String contentType = MimeUtility.unfoldAndDecode(part.getContentType());
57        String contentId = part.getContentId();
58        if (contentType.startsWith("image/") &&
59            contentId != null &&
60            part instanceof LocalAttachmentBodyPart) {
61            LocalAttachmentBodyPart attachment = (LocalAttachmentBodyPart)part;
62            Uri contentUri = AttachmentProvider.resolveAttachmentIdToContentUri(
63                    resolver, AttachmentProvider.getAttachmentUri(account, attachment.getAttachmentId()));
64            // Regexp which matches ' src="cid:contentId"'.
65            String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\"";
66            // Replace all occurrences of src attribute with ' src="content://contentUri"'.
67            text = text.replaceAll(contentIdRe, " src=\"" + contentUri + "\"");
68        }
69
70        if (part.getBody() instanceof Multipart) {
71            Multipart mp = (Multipart)part.getBody();
72            for (int i = 0; i < mp.getCount(); i++) {
73                text = resolveInlineImage(resolver, account, text, mp.getBodyPart(i), depth + 1);
74            }
75        }
76
77        return text;
78    }
79
80    /**
81     * Escape some special character as HTML escape sequence.
82     *
83     * @param text Text to be displayed using WebView.
84     * @return Text correctly escaped.
85     */
86    public static String escapeCharacterToDisplay(String text) {
87        Pattern pattern = PLAIN_TEXT_TO_ESCAPE;
88        Matcher match = pattern.matcher(text);
89
90        if (match.find()) {
91            StringBuilder out = new StringBuilder();
92            int end = 0;
93            do {
94                int start = match.start();
95                out.append(text.substring(end, start));
96                end = match.end();
97                int c = text.codePointAt(start);
98                if (c == ' ') {
99                    // Escape successive spaces into series of "&nbsp;".
100                    for (int i = 1, n = end - start; i < n; ++i) {
101                        out.append("&nbsp;");
102                    }
103                    out.append(' ');
104                } else if (c == '\r' || c == '\n') {
105                    out.append("<br>");
106                } else if (c == '<') {
107                    out.append("&lt;");
108                } else if (c == '>') {
109                    out.append("&gt;");
110                } else if (c == '&') {
111                    out.append("&amp;");
112                }
113            } while (match.find());
114            out.append(text.substring(end));
115            text = out.toString();
116        }
117        return text;
118    }
119}
120