KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > io > StreamNormalizingReader


1 /*
2
3    Copyright 2002-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.io;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.batik.util.EncodingUtilities;
27
28 /**
29  * This class represents a NormalizingReader which handles streams of
30  * bytes.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: StreamNormalizingReader.java,v 1.5 2004/08/18 07:15:58 vhardy Exp $
34  */

35 public class StreamNormalizingReader extends NormalizingReader {
36
37     /**
38      * The char decoder.
39      */

40     protected CharDecoder charDecoder;
41
42     /**
43      * The next char.
44      */

45     protected int nextChar = -1;
46
47     /**
48      * The current line in the stream.
49      */

50     protected int line = 1;
51
52     /**
53      * The current column in the stream.
54      */

55     protected int column;
56
57     /**
58      * Creates a new NormalizingReader. The encoding is assumed to be
59      * ISO-8859-1.
60      * @param is The input stream to decode.
61      */

62     public StreamNormalizingReader(InputStream JavaDoc is) throws IOException JavaDoc {
63         this(is, null);
64     }
65
66     /**
67      * Creates a new NormalizingReader.
68      * @param is The input stream to decode.
69      * @param enc The standard encoding name. A null encoding means
70      * ISO-8859-1.
71      */

72     public StreamNormalizingReader(InputStream JavaDoc is, String JavaDoc enc)
73         throws IOException JavaDoc {
74         if (enc == null) {
75             enc = "ISO-8859-1";
76         }
77         charDecoder = createCharDecoder(is, enc);
78     }
79
80     /**
81      * Creates a new NormalizingReader.
82      * @param r The reader to wrap.
83      */

84     public StreamNormalizingReader(Reader JavaDoc r) throws IOException JavaDoc {
85         charDecoder = new GenericDecoder(r);
86     }
87
88     /**
89      * This constructor is intended for use by subclasses.
90      */

91     protected StreamNormalizingReader() {
92     }
93
94     /**
95      * Read a single character. This method will block until a
96      * character is available, an I/O error occurs, or the end of the
97      * stream is reached.
98      */

99     public int read() throws IOException JavaDoc {
100         int result = nextChar;
101         if (result != -1) {
102             nextChar = -1;
103             if (result == 13) {
104                 column = 0;
105                 line++;
106             } else {
107                 column++;
108             }
109             return result;
110         }
111         result = charDecoder.readChar();
112         switch (result) {
113         case 13:
114             column = 0;
115             line++;
116             int c = charDecoder.readChar();
117             if (c == 10) {
118                 return 10;
119             }
120             nextChar = c;
121             return 10;
122                 
123         case 10:
124             column = 0;
125             line++;
126         }
127         return result;
128     }
129
130     /**
131      * Returns the current line in the stream.
132      */

133     public int getLine() {
134         return line;
135     }
136
137     /**
138      * Returns the current column in the stream.
139      */

140     public int getColumn() {
141         return column;
142     }
143
144     /**
145      * Close the stream.
146      */

147     public void close() throws IOException JavaDoc {
148         charDecoder.dispose();
149         charDecoder = null;
150     }
151
152     /**
153      * Creates the CharDecoder mapped with the given encoding name.
154      */

155     protected CharDecoder createCharDecoder(InputStream JavaDoc is, String JavaDoc enc)
156         throws IOException JavaDoc {
157         CharDecoderFactory cdf =
158             (CharDecoderFactory)charDecoderFactories.get(enc.toUpperCase());
159         if (cdf != null) {
160             return cdf.createCharDecoder(is);
161         }
162         String JavaDoc e = EncodingUtilities.javaEncoding(enc);
163         if (e == null) {
164             e = enc;
165         }
166         return new GenericDecoder(is, e);
167     }
168
169     /**
170      * The CharDecoder factories map.
171      */

172     protected final static Map JavaDoc charDecoderFactories = new HashMap JavaDoc(11);
173     static {
174         CharDecoderFactory cdf = new ASCIIDecoderFactory();
175         charDecoderFactories.put("ASCII", cdf);
176         charDecoderFactories.put("US-ASCII", cdf);
177         charDecoderFactories.put("ISO-8859-1", new ISO_8859_1DecoderFactory());
178         charDecoderFactories.put("UTF-8", new UTF8DecoderFactory());
179         charDecoderFactories.put("UTF-16", new UTF16DecoderFactory());
180     }
181
182     /**
183      * Represents a CharDecoder factory.
184      */

185     protected interface CharDecoderFactory {
186         CharDecoder createCharDecoder(InputStream JavaDoc is) throws IOException JavaDoc;
187     }
188
189     /**
190      * To create an ASCIIDecoder.
191      */

192     protected static class ASCIIDecoderFactory
193         implements CharDecoderFactory {
194         public CharDecoder createCharDecoder(InputStream JavaDoc is)
195             throws IOException JavaDoc {
196             return new ASCIIDecoder(is);
197         }
198     }
199
200     /**
201      * To create an ISO_8859_1Decoder.
202      */

203     protected static class ISO_8859_1DecoderFactory
204         implements CharDecoderFactory {
205         public CharDecoder createCharDecoder(InputStream JavaDoc is)
206             throws IOException JavaDoc {
207             return new ISO_8859_1Decoder(is);
208         }
209     }
210
211     /**
212      * To create a UTF8Decoder.
213      */

214     protected static class UTF8DecoderFactory
215         implements CharDecoderFactory {
216         public CharDecoder createCharDecoder(InputStream JavaDoc is)
217             throws IOException JavaDoc {
218             return new UTF8Decoder(is);
219         }
220     }
221
222     /**
223      * To create a UTF16Decoder.
224      */

225     protected static class UTF16DecoderFactory
226         implements CharDecoderFactory {
227         public CharDecoder createCharDecoder(InputStream JavaDoc is)
228             throws IOException JavaDoc {
229             return new UTF16Decoder(is);
230         }
231     }
232 }
233
Popular Tags