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