• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.io.UnsupportedEncodingException;
4 import java.util.Locale;
5 
6 import com.ibm.icu.text.UnicodeSet;
7 
8 public class EscapingUtilities {
9     public static UnicodeSet OK_TO_NOT_QUOTE = (UnicodeSet) new UnicodeSet("[!(-*,-\\:A-Z_a-z~]").freeze();
10 
urlEscape(String path)11     public static String urlEscape(String path) {
12         try {
13             StringBuilder result = new StringBuilder();
14             byte[] bytes = path.getBytes("utf-8");
15             for (byte b : bytes) {
16                 char c = (char) (b & 0xFF);
17                 if (OK_TO_NOT_QUOTE.contains(c)) {
18                     result.append(c);
19                 } else {
20                     result.append('%');
21                     if (c < 16) {
22                         result.append('0');
23                     }
24                     result.append(Integer.toHexString(c).toUpperCase(Locale.ENGLISH));
25                 }
26             }
27             return result.toString();
28         } catch (UnsupportedEncodingException e) {
29             throw (IllegalArgumentException) new IllegalArgumentException().initCause(e);
30         }
31     }
32 }
33