1 package org.simantics.scl.compiler.markdown.internal;
3 import gnu.trove.map.hash.TCharObjectHashMap;
5 import java.nio.ByteBuffer;
6 import java.nio.CharBuffer;
7 import java.nio.charset.Charset;
9 public class HtmlEscape {
11 private static final Charset UTF8 = Charset.forName("UTF-8");
13 private static final TCharObjectHashMap<String> ESCAPED_CHARS = new TCharObjectHashMap<String>();
15 ESCAPED_CHARS.put('<', "<");
16 ESCAPED_CHARS.put('>', ">");
17 ESCAPED_CHARS.put('"', """);
18 ESCAPED_CHARS.put('&', "&");
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);
27 StringBuilder b = new StringBuilder(length+16);
30 for(++i;i<length;++i) {
32 esc = ESCAPED_CHARS.get(c);
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()));
53 else if(URL_CAN_CONTAIN[(int)c])
56 result.append("&");
58 result.append(percentEncode(c));
63 private static String percentEncode(int c) {
66 String hex = Integer.toHexString(c).toUpperCase();
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,