Display LocalVariables along with Locations in the html report.
[cfb.git] / prod / net / jaekl / cfb / util / XmlEscape.java
1 package net.jaekl.cfb.util;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 public class XmlEscape {
8         // The following code adapted from:
9         // http://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java#439311
10         private final static String ESCAPE_CHARS = "<>&\"\'";
11         private final static List<String> ESCAPE_STRINGS = 
12                         Collections.unmodifiableList(Arrays.asList(new String[] {"&lt;", "&gt;", "&amp;", "&quot;", "&apos;" }));
13         
14         //should only use for the content of an attribute or tag      
15         public static String toEscaped(String content) {
16                 String result = content;
17                 
18                 if ((content != null) && (content.length() > 0)) {
19                         boolean modified = false;
20                         StringBuilder stringBuilder = new StringBuilder(content.length());
21                         for (int i = 0, count = content.length(); i < count; ++i) {
22                                 String character = content.substring(i, i + 1);
23                                 int pos = ESCAPE_CHARS.indexOf(character);
24                                 if (pos > -1) {
25                                         stringBuilder.append(ESCAPE_STRINGS.get(pos));
26                                         modified = true;
27                                 }
28                                 else {
29                                     stringBuilder.append(character);
30                                 }
31                         }
32                         if (modified) {
33                             result = stringBuilder.toString();
34                         }
35                 }
36                 
37                 return result;
38         }
39 }