Home | History | Annotate | Download | only in internet
      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 
     17 package com.android.email.mail.internet;
     18 
     19 import java.util.regex.Matcher;
     20 import java.util.regex.Pattern;
     21 
     22 public class EmailHtmlUtil {
     23 
     24     // Regex that matches characters that have special meaning in HTML. '<', '>', '&' and
     25     // multiple continuous spaces.
     26     private static final Pattern PLAIN_TEXT_TO_ESCAPE = Pattern.compile("[<>&]| {2,}|\r?\n");
     27 
     28     /**
     29      * Escape some special character as HTML escape sequence.
     30      *
     31      * @param text Text to be displayed using WebView.
     32      * @return Text correctly escaped.
     33      */
     34     public static String escapeCharacterToDisplay(String text) {
     35         Pattern pattern = PLAIN_TEXT_TO_ESCAPE;
     36         Matcher match = pattern.matcher(text);
     37 
     38         if (match.find()) {
     39             StringBuilder out = new StringBuilder();
     40             int end = 0;
     41             do {
     42                 int start = match.start();
     43                 out.append(text.substring(end, start));
     44                 end = match.end();
     45                 int c = text.codePointAt(start);
     46                 if (c == ' ') {
     47                     // Escape successive spaces into series of "&nbsp;".
     48                     for (int i = 1, n = end - start; i < n; ++i) {
     49                         out.append("&nbsp;");
     50                     }
     51                     out.append(' ');
     52                 } else if (c == '\r' || c == '\n') {
     53                     out.append("<br>");
     54                 } else if (c == '<') {
     55                     out.append("&lt;");
     56                 } else if (c == '>') {
     57                     out.append("&gt;");
     58                 } else if (c == '&') {
     59                     out.append("&amp;");
     60                 }
     61             } while (match.find());
     62             out.append(text.substring(end));
     63             text = out.toString();
     64         }
     65         return text;
     66     }
     67 }
     68