01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.ows.kvp;
06:
07: import org.geoserver.ows.KvpParser;
08: import java.net.URL;
09:
10: /**
11: * Parses url kvp's of the form 'key=<url>'.
12: * <p>
13: *
14: * </p>
15: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
16: *
17: */
18: public class URLKvpParser extends KvpParser {
19: /**
20: * Creates the parser specifying the name of the key to latch to.
21: *
22: * @param key The key whose associated value to parse.
23: */
24: public URLKvpParser(String key) {
25: super (key, URL.class);
26: }
27:
28: public Object parse(String value) throws Exception {
29: return new URL(fixURL(value));
30: }
31:
32: /**
33: * URLEncoder.encode does not respect the RFC 2396, so we rolled our own little
34: * encoder. It's not complete, but should work in most cases
35: * @param url
36: * @return
37: */
38: String fixURL(String url) {
39: StringBuffer sb = new StringBuffer();
40:
41: for (int i = 0; i < url.length(); i++) {
42: char c = url.charAt(i);
43:
44: // From RFC, "Only alphanumerics [0-9a-zA-Z], the special
45: // characters "$-_.+!*'(),", and reserved characters used
46: // for their reserved purposes may be used unencoded within a URL
47: // Here we keep all the good ones, and remove the few uneeded in their
48: // ascii range. We also keep / and : to make sure basic URL elements
49: // don't get encoded
50: if ((c > ' ') && (c < '{')
51: && ("\"\\<>%^[]`+$,".indexOf(c) == -1)) {
52: sb.append(c);
53: } else {
54: sb.append("%").append(Integer.toHexString(c));
55: }
56: }
57:
58: return sb.toString();
59: }
60: }
|