* @return true if the string is a valid URI
*/
public static boolean isValidURI(CharSequence value) {
LRUCache cache = (LRUCache)caches.get();
if (cache == null) {
cache = new LRUCache(10);
caches.set(cache);
}
if (cache.get(value) != null) {
return true;
}
String sv = Whitespace.trim(value);
// Allow zero-length strings (RFC2396 is ambivalent on this point)
if (sv.length() == 0) {
return true;
}
// Allow a string if the java.net.URI class accepts it
try {
new URI(sv);
cache.put(value, value);
return true;
} catch (URISyntaxException e) {
// keep trying
// Note: it's expensive to throw exceptions on a success path, so we keep a cache.
}
// Allow a string if it can be escaped into a form that java.net.URI accepts
sv = EscapeURI.iriToUri(sv).toString();
try {
new URI(sv);
cache.put(value, value);
return true;
} catch (URISyntaxException e) {
return false;
}
}