• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.ahat;
18 
19 class HtmlEscaper {
20   /**
21    * Escape html characters in the input string.
22    */
escape(String text)23   public static String escape(String text) {
24     String specials = "&<>\'\"";
25     String[] replacements = new String[]{"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
26     StringBuilder sb = null;
27     int low = 0;
28     for (int i = 0; i < text.length(); ++i) {
29       int s = specials.indexOf(text.charAt(i));
30       if (s != -1) {
31         if (sb == null) {
32           sb = new StringBuilder();
33         }
34         sb.append(text.substring(low, i));
35         sb.append(replacements[s]);
36         low = i + 1;
37       }
38     }
39     if (sb == null) {
40       return text;
41     }
42 
43     sb.append(text.substring(low));
44     return sb.toString();
45   }
46 }
47 
48 
49