KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecURL


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util.codec;
37
38 import java.net.*;
39 import java.io.*;
40
41 import com.micronova.util.*;
42
43 public class CodecURL extends Codec
44 {
45     /** URL-encodes using given encoding */
46
47     public static Object JavaDoc encode(Object JavaDoc object, Object JavaDoc encoding) throws Exception JavaDoc
48     {
49         if (object != null)
50         {
51             String JavaDoc string = object.toString();
52             
53             object = java.net.URLEncoder.encode(string, encoding.toString());
54         }
55
56         return object;
57     }
58
59     /** URL-encodes using "utf-8" */
60
61     public static Object JavaDoc encode(Object JavaDoc object) throws Exception JavaDoc
62     {
63         return encode(object, "utf-8");
64     }
65
66     /** URL-decodes using given encoding */
67
68     public static Object JavaDoc decode(Object JavaDoc object, Object JavaDoc encoding) throws Exception JavaDoc
69     {
70         if (object != null)
71         {
72             String JavaDoc string = object.toString();
73             
74             object = java.net.URLDecoder.decode(string, encoding.toString());
75         }
76
77         return object;
78     }
79
80     /** URL-decodes using "utf-8" */
81
82     public static Object JavaDoc decode(Object JavaDoc object) throws Exception JavaDoc
83     {
84         return decode(object, "utf-8");
85     }
86
87     /** content type from byte array */
88
89     public static Object JavaDoc streamType(Object JavaDoc object)
90     {
91         if (object != null)
92         {
93             try
94             {
95                 InputStream is = null;
96
97                 if (object instanceof String JavaDoc)
98                 {
99                     is = new ByteArrayInputStream(StringUtil.fromBinaryString(object.toString()));
100                 }
101                 else if (object instanceof byte[])
102                 {
103                     is = new ByteArrayInputStream((byte[])object);
104                 }
105                 else if (object instanceof InputStream)
106                 {
107                     is = (InputStream)object;
108                 }
109                 
110                 if (is != null)
111                 {
112                     String JavaDoc guessedType = URLConnection.guessContentTypeFromStream(is);
113                     if (!object.equals(guessedType))
114                     {
115                         object = guessedType;
116                     }
117                 }
118             }
119             catch (Exception JavaDoc e)
120             {
121                 object = null;
122             }
123         }
124
125         return object;
126     }
127
128     /** returns name type */
129
130     public static Object JavaDoc nameType(Object JavaDoc object)
131     {
132         if (object != null)
133         {
134             object = URLConnection.guessContentTypeFromName(object.toString());
135         }
136
137         return object;
138     }
139
140     public static Object JavaDoc normalize(Object JavaDoc object)
141     {
142         URI uri = null;
143
144         if (object != null)
145         {
146             URI uriObject = TypeUtil.isURI(object);
147
148             if (uriObject != null)
149             {
150                 uri = uriObject.normalize();
151             }
152         }
153
154         return uri;
155     }
156
157     public static Object JavaDoc relativize(Object JavaDoc object, Object JavaDoc base)
158     {
159         URI uri = null;
160
161         if (object != null)
162         {
163             URI uriObject = TypeUtil.isURI(object);
164
165             if (uriObject != null)
166             {
167                 URI uriBase = TypeUtil.isURI(base);
168
169                 if (uriBase != null)
170                 {
171                     uri = uriBase.relativize(uriObject);
172                 }
173             }
174         }
175
176         return uri;
177     }
178
179     public static Object JavaDoc resolve(Object JavaDoc object, Object JavaDoc base)
180     {
181         URI uri = null;
182
183         if (object != null)
184         {
185             URI uriObject = TypeUtil.isURI(object);
186
187             if (uriObject != null)
188             {
189                 URI uriBase = TypeUtil.isURI(base);
190
191                 if (uriBase != null)
192                 {
193                     uri = uriBase.resolve(uriObject);
194                 }
195             }
196         }
197
198         return uri;
199     }
200 }
201
Popular Tags