1 package com.thaiopensource.util; 2 3 import java.net.URL ; 4 import java.net.MalformedURLException ; 5 import java.io.UnsupportedEncodingException ; 6 7 public class Uri { 8 private static String utf8 = "UTF-8"; 9 10 public static boolean isValid(String s) { 11 return isValidPercent(s) && isValidFragment(s) && isValidScheme(s); 12 } 13 14 private static final String HEX_DIGITS = "0123456789abcdef"; 15 16 public static String escapeDisallowedChars(String s) { 17 StringBuffer buf = null; 18 int len = s.length(); 19 int done = 0; 20 for (;;) { 21 int i = done; 22 for (;;) { 23 if (i == len) { 24 if (done == 0) 25 return s; 26 break; 27 } 28 if (isExcluded(s.charAt(i))) 29 break; 30 i++; 31 } 32 if (buf == null) 33 buf = new StringBuffer (); 34 if (i > done) { 35 buf.append(s.substring(done, i)); 36 done = i; 37 } 38 if (i == len) 39 break; 40 for (i++; i < len && isExcluded(s.charAt(i)); i++) 41 ; 42 String tem = s.substring(done, i); 43 byte[] bytes; 44 try { 45 bytes = tem.getBytes(utf8); 46 } 47 catch (UnsupportedEncodingException e) { 48 utf8 = "UTF8"; 49 try { 50 bytes = tem.getBytes(utf8); 51 } 52 catch (UnsupportedEncodingException e2) { 53 return s; 55 } 56 } 57 for (int j = 0; j < bytes.length; j++) { 58 buf.append('%'); 59 buf.append(HEX_DIGITS.charAt((bytes[j] & 0xFF) >> 4)); 60 buf.append(HEX_DIGITS.charAt(bytes[j] & 0xF)); 61 } 62 done = i; 63 } 64 return buf.toString(); 65 } 66 67 private static final String excluded = "<>\"{}|\\^`"; 68 69 private static boolean isExcluded(char c) { 70 return c <= 0x20 || c >= 0x7F || excluded.indexOf(c) >= 0; 71 } 72 73 private static boolean isAlpha(char c) { 74 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); 75 } 76 77 private static boolean isHexDigit(char c) { 78 return ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') || isDigit(c); 79 } 80 81 private static boolean isDigit(char c) { 82 return '0' <= c && c <= '9'; 83 } 84 85 private static boolean isSchemeChar(char c) { 86 return isAlpha(c) || isDigit(c) || c == '+' || c == '-' || c =='.'; 87 } 88 89 private static boolean isValidPercent(String s) { 90 int len = s.length(); 91 for (int i = 0; i < len; i++) 92 if (s.charAt(i) == '%') { 93 if (i + 2 >= len) 94 return false; 95 else if (!isHexDigit(s.charAt(i + 1)) 96 || !isHexDigit(s.charAt(i + 2))) 97 return false; 98 } 99 return true; 100 } 101 102 private static boolean isValidFragment(String s) { 103 int i = s.indexOf('#'); 104 return i < 0 || s.indexOf('#', i + 1) < 0; 105 } 106 107 private static boolean isValidScheme(String s) { 108 if (!isAbsolute(s)) 109 return true; 110 int i = s.indexOf(':'); 111 if (i == 0 112 || i + 1 == s.length() 113 || !isAlpha(s.charAt(0))) 114 return false; 115 while (--i > 0) 116 if (!isSchemeChar(s.charAt(i))) 117 return false; 118 return true; 119 } 120 121 public static String resolve(String baseUri, String uriReference) { 122 if (!isAbsolute(uriReference) && baseUri != null && isAbsolute(baseUri)) { 123 try { 124 return new URL (new URL (baseUri), uriReference).toString(); 125 } 126 catch (MalformedURLException e) { } 127 } 128 return uriReference; 129 } 130 131 public static boolean hasFragmentId(String uri) { 132 return uri.indexOf('#') >= 0; 133 } 134 135 public static boolean isAbsolute(String uri) { 136 int i = uri.indexOf(':'); 137 if (i < 0) 138 return false; 139 while (--i >= 0) { 140 switch (uri.charAt(i)) { 141 case '#': 142 case '/': 143 case '?': 144 return false; 145 } 146 } 147 return true; 148 } 149 } 150 | Popular Tags |