]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/internal/HtmlEscape.java
migrated to svn revision 33108
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / internal / HtmlEscape.java
1 package org.simantics.scl.compiler.markdown.internal;
2
3 import java.nio.ByteBuffer;
4 import java.nio.CharBuffer;
5 import java.nio.charset.Charset;
6
7 import gnu.trove.map.hash.TCharObjectHashMap;
8
9 public class HtmlEscape {
10
11     private static final Charset UTF8 = Charset.forName("UTF-8");
12     
13     private static final TCharObjectHashMap<String> ESCAPED_CHARS = new TCharObjectHashMap<String>();
14     static {
15         ESCAPED_CHARS.put('<', "&lt;");
16         ESCAPED_CHARS.put('>', "&gt;");
17         ESCAPED_CHARS.put('"', "&quot;");
18         ESCAPED_CHARS.put('&', "&amp;");
19     }
20     
21     public static CharSequence escape(CharSequence text) {
22         int length = text.length();
23         for(int i=0;i<length;++i) {
24             char c = text.charAt(i);
25             String esc = ESCAPED_CHARS.get(c);
26             if(esc != null) {
27                 StringBuilder b = new StringBuilder(length+16);
28                 b.append(text, 0, i);
29                 b.append(esc);
30                 for(++i;i<length;++i) {
31                     c = text.charAt(i);
32                     esc = ESCAPED_CHARS.get(c);
33                     if(esc != null)
34                         b.append(esc);
35                     else
36                         b.append(c);
37                 }
38                 return b.toString();
39             }
40         }
41         return text;
42     }       
43     
44     public static StringBuilder escapeURL(CharSequence str) {
45         StringBuilder result = new StringBuilder();
46         for(int i=0;i<str.length();++i) {
47             char c = str.charAt(i);
48             if(c < 0 || c >= 128) {
49                 ByteBuffer bs = UTF8.encode(CharBuffer.wrap(new char[] {c}));
50                 for(int j=0;j<bs.limit();++j)
51                     result.append(percentEncode(bs.get()));
52             }
53             else if(URL_CAN_CONTAIN[(int)c])
54                 result.append(c);
55             else if(c == '&')
56                 result.append("&amp;");
57             else
58                 result.append(percentEncode(c));
59         }
60         return result;
61     }
62     
63     private static String percentEncode(int c) {
64         if(c < 0)
65             c += 256;
66         String hex = Integer.toHexString(c).toUpperCase();
67         if(hex.length() == 1)
68             return "%0" + hex;
69         else
70             return "%" + hex;
71     }
72     
73     private static final boolean[] URL_CAN_CONTAIN = new boolean[] {
74         false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
75         false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
76         false, true, false, true, true, true, false, false, true, true, true, true, true, true, true, true,
77         true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, true,
78         true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
79         true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, true,
80         false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
81         true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, false,
82     };
83 }