1 7 package com.ibm.icu.impl.data; 8 9 import java.io.*; 10 11 import com.ibm.icu.impl.ICUData; 12 import com.ibm.icu.impl.Utility; 13 14 26 public class ResourceReader { 27 private BufferedReader reader; 28 private String resourceName; 29 private String encoding; private Class root; 31 32 37 private int lineNo; 38 39 49 public ResourceReader(String resourceName, String encoding) 50 throws UnsupportedEncodingException { 51 this(ICUData.class, "data/" + resourceName, encoding); 52 } 53 54 60 public ResourceReader(String resourceName) { 61 this(ICUData.class, "data/" + resourceName); 62 } 63 64 74 public ResourceReader(Class rootClass, String resourceName, String encoding) 75 throws UnsupportedEncodingException { 76 this.root = rootClass; 77 this.resourceName = resourceName; 78 this.encoding = encoding; 79 lineNo = -1; 80 _reset(); 81 } 82 83 87 public ResourceReader(InputStream is, String resourceName) { 88 this.root = null; 89 this.resourceName = resourceName; 90 this.encoding = null; 91 92 this.lineNo = -1; 93 try { 94 InputStreamReader isr = (encoding == null) 95 ? new InputStreamReader(is) 96 : new InputStreamReader(is, encoding); 97 98 this.reader = new BufferedReader(isr); 99 this.lineNo= 0; 100 } 101 catch (UnsupportedEncodingException e) { 102 } 103 } 104 105 111 public ResourceReader(Class rootClass, String resourceName) { 112 this.root = rootClass; 113 this.resourceName = resourceName; 114 this.encoding = null; 115 lineNo = -1; 116 try { 117 _reset(); 118 } catch (UnsupportedEncodingException e) {} 119 } 120 121 125 public String readLine() throws IOException { 126 if (lineNo == 0) { 127 ++lineNo; 129 String line = reader.readLine(); 130 if (line.charAt(0) == '\uFFEF' || 131 line.charAt(0) == '\uFEFF') { 132 line = line.substring(1); 133 } 134 return line; 135 } 136 ++lineNo; 137 return reader.readLine(); 138 } 139 140 145 public String readLineSkippingComments(boolean trim) throws IOException { 146 for (;;) { 147 String line = readLine(); 148 if (line == null) { 149 return line; 150 } 151 int pos = Utility.skipWhitespace(line, 0); 153 if (pos == line.length() || line.charAt(pos) == '#') { 155 continue; 156 } 157 if (trim) line = line.substring(pos); 159 return line; 160 } 161 } 162 163 164 168 public String readLineSkippingComments() throws IOException { 169 return readLineSkippingComments(false); 170 } 171 172 178 public int getLineNumber() { 179 return lineNo; 180 } 181 182 186 public String describePosition() { 187 return resourceName + ':' + lineNo; 188 } 189 190 198 public void reset() { 199 try { 200 _reset(); 201 } catch (UnsupportedEncodingException e) {} 202 } 206 207 214 private void _reset() throws UnsupportedEncodingException { 215 if (lineNo == 0) { 216 return; 217 } 218 InputStream is = ICUData.getStream(root, resourceName); 219 if (is == null) { 220 throw new IllegalArgumentException ("Can't open " + resourceName); 221 } 222 223 InputStreamReader isr = 224 (encoding == null) ? new InputStreamReader(is) : 225 new InputStreamReader(is, encoding); 226 reader = new BufferedReader(isr); 227 lineNo = 0; 228 } 229 } 230 | Popular Tags |