KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > source > SourceUtil


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.source;
9
10 import org.xml.sax.InputSource JavaDoc;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStreamWriter JavaDoc;
15 import java.util.BitSet JavaDoc;
16
17
18 /**
19  *
20  * Utility class for source resolving.
21  *
22  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
23  * @version CVS $Revision: 1.4 $ $Date: 2002/01/04 13:25:36 $
24  */

25 public final class SourceUtil
26 {
27
28     private static final char [ ] alphabet = {
29        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
30
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
31
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
32
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
33
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
34
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
35
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
36
'4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
37

38     /**
39      * BASE 64 encoding.
40      * See also RFC 1421
41      */

42     public static String JavaDoc encodeBASE64 ( String JavaDoc s )
43     {
44         return encodeBASE64 ( s.getBytes ( ) );
45     }
46
47     /**
48      * BASE 64 encoding.
49      * See also RFC 1421
50      */

51     public static String JavaDoc encodeBASE64 ( byte [ ] octetString )
52     {
53         int bits24;
54         int bits6;
55
56         char [ ] out = new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
57
58         int outIndex = 0;
59         int i = 0;
60
61         while ( ( i + 3 ) <= octetString.length )
62         {
63             // store the octets
64
bits24 = ( octetString [ i++ ] & 0xFF ) << 16;
65             bits24 |= ( octetString [ i++ ] & 0xFF ) << 8;
66             bits24 |= ( octetString [ i++ ] & 0xFF ) << 0;
67
68             bits6 = ( bits24 & 0x00FC0000 ) >> 18;
69             out [ outIndex++ ] = alphabet [ bits6 ];
70             bits6 = ( bits24 & 0x0003F000 ) >> 12;
71             out [ outIndex++ ] = alphabet [ bits6 ];
72             bits6 = ( bits24 & 0x00000FC0 ) >> 6;
73             out [ outIndex++ ] = alphabet [ bits6 ];
74             bits6 = ( bits24 & 0x0000003F );
75             out [ outIndex++ ] = alphabet [ bits6 ];
76         }
77
78         if ( octetString.length - i == 2 )
79         {
80             // store the octets
81
bits24 = ( octetString [ i ] & 0xFF ) << 16;
82             bits24 |= ( octetString [ i + 1 ] & 0xFF ) << 8;
83
84             bits6 = ( bits24 & 0x00FC0000 ) >> 18;
85             out [ outIndex++ ] = alphabet [ bits6 ];
86             bits6 = ( bits24 & 0x0003F000 ) >> 12;
87             out [ outIndex++ ] = alphabet [ bits6 ];
88             bits6 = ( bits24 & 0x00000FC0 ) >> 6;
89             out [ outIndex++ ] = alphabet [ bits6 ];
90
91             // padding
92
out [ outIndex++ ] = '=';
93         }
94         else if ( octetString.length - i == 1 )
95         {
96             // store the octets
97
bits24 = ( octetString [ i ] & 0xFF ) << 16;
98
99             bits6 = ( bits24 & 0x00FC0000 ) >> 18;
100             out [ outIndex++ ] = alphabet [ bits6 ];
101             bits6 = ( bits24 & 0x0003F000 ) >> 12;
102             out [ outIndex++ ] = alphabet [ bits6 ];
103
104             // padding
105
out [ outIndex++ ] = '=';
106             out [ outIndex++ ] = '=';
107         }
108
109         return new String JavaDoc ( out );
110     }
111
112     /** A BitSet defining the characters which don't need encoding */
113     static BitSet JavaDoc charactersDontNeedingEncoding;
114     static final int characterCaseDiff = ('a' - 'A');
115
116     /** Initialize the BitSet */
117     static
118     {
119         charactersDontNeedingEncoding = new BitSet JavaDoc(256);
120         int i;
121         for (i = 'a'; i <= 'z'; i++)
122         {
123             charactersDontNeedingEncoding.set(i);
124         }
125         for (i = 'A'; i <= 'Z'; i++)
126         {
127             charactersDontNeedingEncoding.set(i);
128         }
129         for (i = '0'; i <= '9'; i++)
130         {
131             charactersDontNeedingEncoding.set(i);
132         }
133         charactersDontNeedingEncoding.set('-');
134         charactersDontNeedingEncoding.set('_');
135         charactersDontNeedingEncoding.set('.');
136         charactersDontNeedingEncoding.set('*');
137         charactersDontNeedingEncoding.set('"');
138     }
139
140     /**
141      * Translates a string into <code>x-www-form-urlencoded</code> format.
142      *
143      * @param s <code>String</code> to be translated.
144      * @return the translated <code>String</code>.
145      */

146     public static String JavaDoc encode(String JavaDoc s)
147     {
148         final StringBuffer JavaDoc out = new StringBuffer JavaDoc( s.length() );
149         final ByteArrayOutputStream JavaDoc buf = new ByteArrayOutputStream JavaDoc( 32 );
150         final OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc( buf );
151         for (int i = 0; i < s.length(); i++)
152         {
153             int c = (int)s.charAt(i);
154             if (charactersDontNeedingEncoding.get(c))
155             {
156                 out.append((char)c);
157             }
158             else
159             {
160                 try
161                 {
162                     writer.write(c);
163                     writer.flush();
164                 }
165                 catch(IOException JavaDoc e)
166                 {
167                     buf.reset();
168                     continue;
169                 }
170                 byte[] ba = buf.toByteArray();
171                 for (int j = 0; j < ba.length; j++)
172                 {
173                     out.append('%');
174                     char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
175                     // converting to use uppercase letter as part of
176
// the hex value if ch is a letter.
177
if (Character.isLetter(ch))
178                     {
179                         ch -= characterCaseDiff;
180                     }
181                     out.append(ch);
182                     ch = Character.forDigit(ba[j] & 0xF, 16);
183                     if (Character.isLetter(ch))
184                     {
185                         ch -= characterCaseDiff;
186                     }
187                     out.append(ch);
188                 }
189                 buf.reset();
190             }
191         }
192
193         return out.toString();
194     }
195
196     /**
197      * Return a new <code>InputSource</code> object
198      *
199      * @throws IOException if I/O error occured.
200      */

201     public static InputSource JavaDoc getInputSource(Source source)
202     throws IOException JavaDoc
203     {
204         final InputSource JavaDoc newObject = new InputSource JavaDoc( source.getInputStream() );
205         newObject.setSystemId( source.getSystemId() );
206         return newObject;
207     }
208
209 }
210
Popular Tags