KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > ParsedURLDataProtocolHandler


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Protocol Handler for the 'data' protocol.
27  * RFC: 2397
28  * http://www.ietf.org/rfc/rfc2397.txt
29  *
30  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
31  * @version $Id: ParsedURLDataProtocolHandler.java,v 1.8 2004/08/18 07:15:48 vhardy Exp $
32  */

33 public class ParsedURLDataProtocolHandler
34     extends AbstractParsedURLProtocolHandler {
35
36     static final String JavaDoc DATA_PROTOCOL = "data";
37     static final String JavaDoc BASE64 = "base64";
38     static final String JavaDoc CHARSET = "charset";
39
40     public ParsedURLDataProtocolHandler() {
41         super(DATA_PROTOCOL);
42     }
43
44     public ParsedURLData parseURL(ParsedURL baseURL, String JavaDoc urlStr) {
45         // No relative form...
46
return parseURL(urlStr);
47     }
48
49     public ParsedURLData parseURL(String JavaDoc urlStr) {
50         DataParsedURLData ret = new DataParsedURLData();
51
52         int pidx=0, idx;
53         idx = urlStr.indexOf(':');
54         if (idx != -1) {
55             // May have a protocol spec...
56
ret.protocol = urlStr.substring(pidx, idx);
57             if (ret.protocol.indexOf('/') == -1)
58                 pidx = idx+1;
59             else {
60                 // Got a slash in protocol probably means
61
// no protocol given, (host and port?)
62
ret.protocol = null;
63                 pidx = 0;
64             }
65         }
66
67         idx = urlStr.indexOf(',',pidx);
68         if ((idx != -1) && (idx != pidx)) {
69             ret.host = urlStr.substring(pidx, idx);
70             pidx = idx+1;
71
72             int aidx = ret.host.lastIndexOf(';');
73             if ((aidx == -1) || (aidx==ret.host.length())) {
74                 ret.contentType = ret.host;
75             } else {
76                 String JavaDoc enc = ret.host.substring(aidx+1);
77                 idx = enc.indexOf('=');
78                 if (idx == -1) {
79                     // It is an encoding.
80
ret.contentEncoding = enc;
81                     ret.contentType = ret.host.substring(0, aidx);
82                 } else {
83                     ret.contentType = ret.host;
84                 }
85                 // if theres a charset pull it out.
86
aidx = 0;
87                 idx = ret.contentType.indexOf(';', aidx);
88                 if (idx != -1) {
89                     aidx = idx+1;
90                     while (aidx < ret.contentType.length()) {
91                         idx = ret.contentType.indexOf(';', aidx);
92                         if (idx == -1) idx = ret.contentType.length();
93                         String JavaDoc param = ret.contentType.substring(aidx, idx);
94                         int eqIdx = param.indexOf('=');
95                         if ((eqIdx != -1) &&
96                             (CHARSET.equals(param.substring(0,eqIdx))))
97                             ret.charset = param.substring(eqIdx+1);
98                         aidx = idx+1;
99                     }
100                 }
101             }
102         }
103         
104         if (pidx != urlStr.length())
105             ret.path = urlStr.substring(pidx);
106
107         return ret;
108     }
109
110     /**
111      * Overrides some of the methods to support data protocol weirdness
112      */

113     static class DataParsedURLData extends ParsedURLData {
114         String JavaDoc charset= null;
115
116         public boolean complete() {
117             return (path != null);
118         }
119
120         public String JavaDoc getPortStr() {
121             String JavaDoc portStr ="data:";
122             if (host != null) portStr += host;
123             portStr += ",";
124             return portStr;
125         }
126                 
127         public String JavaDoc toString() {
128             String JavaDoc ret = getPortStr();
129             if (path != null) ret += path;
130             return ret;
131         }
132
133         /**
134          * Returns the content type if available. This is only available
135          * for some protocols.
136          */

137         public String JavaDoc getContentType(String JavaDoc userAgent) {
138             return contentType;
139         }
140
141         /**
142          * Returns the content encoding if available. This is only available
143          * for some protocols.
144          */

145         public String JavaDoc getContentEncoding(String JavaDoc userAgent) {
146             return contentEncoding;
147         }
148
149         protected InputStream JavaDoc openStreamInternal
150             (String JavaDoc userAgent, Iterator JavaDoc mimeTypes, Iterator JavaDoc encodingTypes)
151             throws IOException JavaDoc {
152             if (BASE64.equals(contentEncoding)) {
153                 byte [] data = path.getBytes();
154                 stream = new ByteArrayInputStream JavaDoc(data);
155                 stream = new Base64DecodeStream(stream);
156             } else {
157                 stream = decode(path);
158             }
159             return stream;
160         }
161
162         public static InputStream JavaDoc decode(String JavaDoc s) {
163             int len = s.length();
164             byte [] data = new byte[len];
165             int j=0;
166             for(int i=0; i<len; i++) {
167                 char c = s.charAt(i);
168                 switch (c) {
169                 default : data[j++]= (byte)c; break;
170                 case '%': {
171                     if (i+2 < len) {
172                         i += 2;
173                         byte b;
174                         char c1 = s.charAt(i-1);
175                         if (c1 >= '0' && c1 <= '9') b=(byte)(c1-'0');
176                         else if (c1 >= 'a' && c1 <= 'z') b=(byte)(c1-'a'+10);
177                         else if (c1 >= 'A' && c1 <= 'Z') b=(byte)(c1-'A'+10);
178                         else break;
179                         b*=16;
180
181                         char c2 = s.charAt(i);
182                         if (c2 >= '0' && c2 <= '9') b+=(byte)(c2-'0');
183                         else if (c2 >= 'a' && c2 <= 'z') b+=(byte)(c2-'a'+10);
184                         else if (c2 >= 'A' && c2 <= 'Z') b+=(byte)(c2-'A'+10);
185                         else break;
186                         data[j++] = b;
187                     }
188                 }
189                 break;
190                 }
191             }
192             return new ByteArrayInputStream JavaDoc(data, 0, j);
193         }
194     }
195 }
196
197
Popular Tags