01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.util.*;
13:
14: /**
15: * Encodings related to URL's. The implementation is still in
16: * ../URL*Escape. Perhaps should be migrated to here...
17: *
18: * @author Michiel Meeuwissen
19: */
20:
21: public class Url extends ConfigurableStringTransformer implements
22: CharTransformer {
23:
24: public final static int ESCAPE = 1;
25:
26: // maybe should be dropped, as there is no longer a difference with ESCAPE
27: public final static int PARAM_ESCAPE = 2;
28:
29: public Url() {
30: super ();
31: }
32:
33: public Url(int conf) {
34: super (conf);
35: }
36:
37: /**
38: * Used when registering this class as a possible Transformer
39: */
40:
41: public Map<String, Config> transformers() {
42: Map<String, Config> h = new HashMap<String, Config>();
43: h
44: .put("escape_url".toUpperCase(), new Config(Url.class,
45: ESCAPE));
46: h.put("escape_url_param".toUpperCase(), new Config(Url.class,
47: PARAM_ESCAPE));
48: return h;
49: }
50:
51: public String transform(String r) {
52: switch (to) {
53: case PARAM_ESCAPE:
54: case ESCAPE:
55: try {
56: return java.net.URLEncoder.encode(r, "UTF-8");
57: } catch (java.io.UnsupportedEncodingException uee) { // cannot happen
58: return r;
59: }
60: default:
61: throw new UnknownCodingException(getClass(), to);
62: }
63: }
64:
65: public String transformBack(String r) {
66: switch (to) {
67: case ESCAPE:
68: case PARAM_ESCAPE:
69: try {
70: return java.net.URLDecoder.decode(r, "UTF-8");
71: } catch (java.io.UnsupportedEncodingException uee) { // cannot happen
72: return r;
73: }
74: default:
75: throw new UnknownCodingException(getClass(), to);
76: }
77: }
78:
79: public String getEncoding() {
80: switch (to) {
81: case ESCAPE:
82: return "ESCAPE_URL";
83: case PARAM_ESCAPE:
84: return "ESCAPE_URL_PARAM";
85: default:
86: throw new UnknownCodingException(getClass(), to);
87: }
88: }
89: }
|