KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > data > ResourceReader


1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2004, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

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 /**
15  * A reader for text resource data in the current package or the package
16  * of a given class object. The
17  * resource data is loaded through the class loader, so it will
18  * typically be a file in the same directory as the *.class files, or
19  * a file within a JAR file in the corresponding subdirectory. The
20  * file must be a text file in one of the supported encodings; when the
21  * resource is opened by constructing a <code>ResourceReader</code>
22  * object the encoding is specified.
23  *
24  * @author Alan Liu
25  */

26 public class ResourceReader {
27     private BufferedReader reader;
28     private String JavaDoc resourceName;
29     private String JavaDoc encoding; // null for default encoding
30
private Class JavaDoc root;
31     
32     /**
33      * The one-based line number. Has the special value -1 before the
34      * object is initialized. Has the special value 0 after initialization
35      * but before the first line is read.
36      */

37     private int lineNo;
38
39     /**
40      * Construct a reader object for the text file of the given name
41      * in this package, using the given encoding.
42      * @param resourceName the name of the text file located in this
43      * package's ".data" subpackage.
44      * @param encoding the encoding of the text file; if unsupported
45      * an exception is thrown
46      * @exception UnsupportedEncodingException if
47      * <code>encoding</code> is not supported by the JDK.
48      */

49     public ResourceReader(String JavaDoc resourceName, String JavaDoc encoding)
50         throws UnsupportedEncodingException {
51         this(ICUData.class, "data/" + resourceName, encoding);
52     }
53
54     /**
55      * Construct a reader object for the text file of the given name
56      * in this package, using the default encoding.
57      * @param resourceName the name of the text file located in this
58      * package's ".data" subpackage.
59      */

60     public ResourceReader(String JavaDoc resourceName) {
61         this(ICUData.class, "data/" + resourceName);
62     }
63
64     /**
65      * Construct a reader object for the text file of the given name
66      * in the given class's package, using the given encoding.
67      * @param resourceName the name of the text file located in the
68      * given class's package.
69      * @param encoding the encoding of the text file; if unsupported
70      * an exception is thrown
71      * @exception UnsupportedEncodingException if
72      * <code>encoding</code> is not supported by the JDK.
73      */

74     public ResourceReader(Class JavaDoc rootClass, String JavaDoc resourceName, String JavaDoc encoding)
75         throws UnsupportedEncodingException {
76         this.root = rootClass;
77         this.resourceName = resourceName;
78         this.encoding = encoding;
79         lineNo = -1;
80         _reset();
81     }
82
83     /**
84      * @internal
85      * @deprecated This API is ICU internal only.
86      */

87      public ResourceReader(InputStream is, String JavaDoc 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     /**
106      * Construct a reader object for the text file of the given name
107      * in the given class's package, using the default encoding.
108      * @param resourceName the name of the text file located in the
109      * given class's package.
110      */

111     public ResourceReader(Class JavaDoc rootClass, String JavaDoc 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     /**
122      * Read and return the next line of the file or <code>null</code>
123      * if the end of the file has been reached.
124      */

125     public String JavaDoc readLine() throws IOException {
126         if (lineNo == 0) {
127             // Remove BOMs
128
++lineNo;
129             String JavaDoc 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     /**
141      * Read a line, ignoring blank lines and lines that start with
142      * '#'.
143      * @param trim if true then trim leading rule white space.
144      */

145     public String JavaDoc readLineSkippingComments(boolean trim) throws IOException {
146         for (;;) {
147             String JavaDoc line = readLine();
148             if (line == null) {
149                 return line;
150             }
151             // Skip over white space
152
int pos = Utility.skipWhitespace(line, 0);
153             // Ignore blank lines and comment lines
154
if (pos == line.length() || line.charAt(pos) == '#') {
155                 continue;
156             }
157             // Process line
158
if (trim) line = line.substring(pos);
159             return line;
160         }
161     }
162
163
164     /**
165      * Read a line, ignoring blank lines and lines that start with
166      * '#'. Do not trim leading rule white space.
167      */

168     public String JavaDoc readLineSkippingComments() throws IOException {
169         return readLineSkippingComments(false);
170     }
171
172     /**
173      * Return the one-based line number of the last line returned by
174      * readLine() or readLineSkippingComments(). Should only be called
175      * after a call to one of these methods; otherwise the return
176      * value is undefined.
177      */

178     public int getLineNumber() {
179         return lineNo;
180     }
181     
182     /**
183      * Return a string description of the position of the last line
184      * returned by readLine() or readLineSkippingComments().
185      */

186     public String JavaDoc describePosition() {
187         return resourceName + ':' + lineNo;
188     }
189     
190     /**
191      * Reset this reader so that the next call to
192      * <code>readLine()</code> returns the first line of the file
193      * again. This is a somewhat expensive call, however, calling
194      * <code>reset()</code> after calling it the first time does
195      * nothing if <code>readLine()</code> has not been called in
196      * between.
197      */

198     public void reset() {
199         try {
200             _reset();
201         } catch (UnsupportedEncodingException e) {}
202         // We swallow this exception, if there is one. If the encoding is
203
// invalid, the constructor will have thrown this exception already and
204
// the caller shouldn't use the object afterwards.
205
}
206
207     /**
208      * Reset to the start by reconstructing the stream and readers.
209      * We could also use mark() and reset() on the stream or reader,
210      * but that would cause them to keep the stream data around in
211      * memory. We don't want that because some of the resource files
212      * are large, e.g., 400k.
213      */

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 JavaDoc("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