KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > util > Convert


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5  
6 package org.joseki.util;
7
8 import java.net.URLEncoder JavaDoc;
9 import java.net.URLDecoder JavaDoc;
10 import java.io.UnsupportedEncodingException JavaDoc ;
11
12 /**
13  * @version $Id: Convert.java,v 1.7 2004/04/30 14:13:13 andy_seaborne Exp $
14  * @author Andy Seaborne
15  */

16 public class Convert
17 {
18     // UTF-8 is required inJava implementations
19

20     // Choose the characters to escape.
21

22     // Chars from RDF 2396 to escape:
23
// 2.2. Reserved Characters
24
// 2.4.3 Excluded US-ASCII Characters
25
//
26
// reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
27
// "$" | ","
28
//
29
// control = <US-ASCII coded characters 00-1F and 7F hexadecimal>
30
// space = <US-ASCII coded character 20 hexadecimal>
31
//
32
// delims = "<" | ">" | "#" | "%" | <">
33
// unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
34
//
35
// These should be OK:
36
//
37
// unreserved = alphanum | mark
38
// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
39

40     // URLEncoder is savage:
41

42     // + The alphanumeric characters "a" through "z",
43
// "A" through "Z" and "0" through "9" remain the same.
44
// + The special characters ".", "-", "*", and "_" remain the same.
45
// + The space character " " is converted into a plus sign "+".
46
// + All other characters are unsafe and are first converted into
47
// one or more bytes using some encoding scheme.
48

49     public static String JavaDoc encWWWForm(String JavaDoc s)
50     {
51         try {
52             return URLEncoder.encode(s, "UTF-8") ;
53             // Can't fail - UTF-8 is required
54
} catch (UnsupportedEncodingException JavaDoc ex) { return null ;}
55     }
56
57     // TODO: This is the inefficient way wround!
58

59     public static String JavaDoc encWWWForm(StringBuffer JavaDoc sbuff)
60     {
61         return encWWWForm(sbuff.toString()) ;
62     }
63     
64     
65     public static String JavaDoc decWWWForm(String JavaDoc s)
66     {
67         try {
68             return URLDecoder.decode(s, "UTF-8") ;
69             // Can't fail - UTF-8 is required
70
} catch (UnsupportedEncodingException JavaDoc ex) { return null ;}
71     }
72
73     public static String JavaDoc decWWWForm(StringBuffer JavaDoc sbuff)
74     {
75         return decWWWForm(sbuff.toString()) ;
76     }
77     
78 }
79
80 /*
81  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
82  * All rights reserved.
83  *
84  * Redistribution and use in source and binary forms, with or without
85  * modification, are permitted provided that the following conditions
86  * are met:
87  * 1. Redistributions of source code must retain the above copyright
88  * notice, this list of conditions and the following disclaimer.
89  * 2. Redistributions in binary form must reproduce the above copyright
90  * notice, this list of conditions and the following disclaimer in the
91  * documentation and/or other materials provided with the distribution.
92  * 3. The name of the author may not be used to endorse or promote products
93  * derived from this software without specific prior written permission.
94  *
95  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
96  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
97  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
98  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
99  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
101  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
103  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
104  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105  */

106
107
Popular Tags