KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > Url


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

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 CharTransformer {
22
23     public final static int ESCAPE = 1;
24
25     // maybe should be dropped, as there is no longer a difference with ESCAPE
26
public final static int PARAM_ESCAPE = 2;
27
28     public Url() {
29         super();
30     }
31
32     public Url(int conf) {
33         super(conf);
34     }
35
36     /**
37      * Used when registering this class as a possible Transformer
38      */

39
40     public Map transformers() {
41         HashMap h = new HashMap();
42         h.put("escape_url".toUpperCase(), new Config(Url.class, ESCAPE));
43         h.put("escape_url_param".toUpperCase(), new Config(Url.class, PARAM_ESCAPE));
44         return h;
45     }
46
47     public String JavaDoc transform(String JavaDoc r) {
48         switch(to){
49         case PARAM_ESCAPE:
50         case ESCAPE:
51             try {
52                 return java.net.URLEncoder.encode(r, "UTF-8");
53             } catch (java.io.UnsupportedEncodingException JavaDoc uee) { // cannot happen
54
return r;
55             }
56         default: throw new UnknownCodingException(getClass(), to);
57         }
58     }
59     public String JavaDoc transformBack(String JavaDoc r) {
60         switch(to){
61         case ESCAPE:
62         case PARAM_ESCAPE:
63             try {
64                 return java.net.URLDecoder.decode(r, "UTF-8");
65             } catch (java.io.UnsupportedEncodingException JavaDoc uee) { // cannot happen
66
return r;
67             }
68         default: throw new UnknownCodingException(getClass(), to);
69         }
70     }
71     public String JavaDoc getEncoding() {
72         switch(to){
73         case ESCAPE: return "ESCAPE_URL";
74         case PARAM_ESCAPE: return "ESCAPE_URL_PARAM";
75         default: throw new UnknownCodingException(getClass(), to);
76         }
77     }
78 }
79
Popular Tags