1 18 package org.apache.batik.util.io; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.apache.batik.util.EncodingUtilities; 27 28 35 public class StreamNormalizingReader extends NormalizingReader { 36 37 40 protected CharDecoder charDecoder; 41 42 45 protected int nextChar = -1; 46 47 50 protected int line = 1; 51 52 55 protected int column; 56 57 62 public StreamNormalizingReader(InputStream is) throws IOException { 63 this(is, null); 64 } 65 66 72 public StreamNormalizingReader(InputStream is, String enc) 73 throws IOException { 74 if (enc == null) { 75 enc = "ISO-8859-1"; 76 } 77 charDecoder = createCharDecoder(is, enc); 78 } 79 80 84 public StreamNormalizingReader(Reader r) throws IOException { 85 charDecoder = new GenericDecoder(r); 86 } 87 88 91 protected StreamNormalizingReader() { 92 } 93 94 99 public int read() throws IOException { 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 133 public int getLine() { 134 return line; 135 } 136 137 140 public int getColumn() { 141 return column; 142 } 143 144 147 public void close() throws IOException { 148 charDecoder.dispose(); 149 charDecoder = null; 150 } 151 152 155 protected CharDecoder createCharDecoder(InputStream is, String enc) 156 throws IOException { 157 CharDecoderFactory cdf = 158 (CharDecoderFactory)charDecoderFactories.get(enc.toUpperCase()); 159 if (cdf != null) { 160 return cdf.createCharDecoder(is); 161 } 162 String e = EncodingUtilities.javaEncoding(enc); 163 if (e == null) { 164 e = enc; 165 } 166 return new GenericDecoder(is, e); 167 } 168 169 172 protected final static Map charDecoderFactories = new HashMap (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 185 protected interface CharDecoderFactory { 186 CharDecoder createCharDecoder(InputStream is) throws IOException ; 187 } 188 189 192 protected static class ASCIIDecoderFactory 193 implements CharDecoderFactory { 194 public CharDecoder createCharDecoder(InputStream is) 195 throws IOException { 196 return new ASCIIDecoder(is); 197 } 198 } 199 200 203 protected static class ISO_8859_1DecoderFactory 204 implements CharDecoderFactory { 205 public CharDecoder createCharDecoder(InputStream is) 206 throws IOException { 207 return new ISO_8859_1Decoder(is); 208 } 209 } 210 211 214 protected static class UTF8DecoderFactory 215 implements CharDecoderFactory { 216 public CharDecoder createCharDecoder(InputStream is) 217 throws IOException { 218 return new UTF8Decoder(is); 219 } 220 } 221 222 225 protected static class UTF16DecoderFactory 226 implements CharDecoderFactory { 227 public CharDecoder createCharDecoder(InputStream is) 228 throws IOException { 229 return new UTF16Decoder(is); 230 } 231 } 232 } 233 | Popular Tags |