KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > util > Uri


1 package com.thaiopensource.util;
2
3 import java.net.URL JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5 import java.io.UnsupportedEncodingException JavaDoc;
6
7 public class Uri {
8   private static String JavaDoc utf8 = "UTF-8";
9
10   public static boolean isValid(String JavaDoc s) {
11     return isValidPercent(s) && isValidFragment(s) && isValidScheme(s);
12   }
13
14   private static final String JavaDoc HEX_DIGITS = "0123456789abcdef";
15
16   public static String JavaDoc escapeDisallowedChars(String JavaDoc s) {
17     StringBuffer JavaDoc 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 JavaDoc();
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 JavaDoc tem = s.substring(done, i);
43       byte[] bytes;
44       try {
45     bytes = tem.getBytes(utf8);
46       }
47       catch (UnsupportedEncodingException JavaDoc e) {
48     utf8 = "UTF8";
49     try {
50       bytes = tem.getBytes(utf8);
51     }
52     catch (UnsupportedEncodingException JavaDoc e2) {
53       // Give up
54
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 JavaDoc 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 JavaDoc 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 JavaDoc s) {
103     int i = s.indexOf('#');
104     return i < 0 || s.indexOf('#', i + 1) < 0;
105   }
106
107   private static boolean isValidScheme(String JavaDoc 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 JavaDoc resolve(String JavaDoc baseUri, String JavaDoc uriReference) {
122     if (!isAbsolute(uriReference) && baseUri != null && isAbsolute(baseUri)) {
123       try {
124     return new URL JavaDoc(new URL JavaDoc(baseUri), uriReference).toString();
125       }
126       catch (MalformedURLException JavaDoc e) { }
127     }
128     return uriReference;
129   }
130
131   public static boolean hasFragmentId(String JavaDoc uri) {
132     return uri.indexOf('#') >= 0;
133   }
134
135   public static boolean isAbsolute(String JavaDoc 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