Source Code Cross Referenced for ResourceReader.java in  » Internationalization-Localization » icu4j » com » ibm » icu » impl » data » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.impl.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *******************************************************************************
003:         * Copyright (C) 2001-2004, International Business Machines Corporation and    *
004:         * others. All Rights Reserved.                                                *
005:         *******************************************************************************
006:         */package com.ibm.icu.impl.data;
007:
008:        import java.io.*;
009:
010:        import com.ibm.icu.impl.ICUData;
011:        import com.ibm.icu.impl.Utility;
012:
013:        /**
014:         * A reader for text resource data in the current package or the package
015:         * of a given class object.  The
016:         * resource data is loaded through the class loader, so it will
017:         * typically be a file in the same directory as the *.class files, or
018:         * a file within a JAR file in the corresponding subdirectory.  The
019:         * file must be a text file in one of the supported encodings; when the
020:         * resource is opened by constructing a <code>ResourceReader</code>
021:         * object the encoding is specified.
022:         *
023:         * @author Alan Liu
024:         */
025:        public class ResourceReader {
026:            private BufferedReader reader;
027:            private String resourceName;
028:            private String encoding; // null for default encoding
029:            private Class root;
030:
031:            /**
032:             * The one-based line number. Has the special value -1 before the
033:             * object is initialized. Has the special value 0 after initialization
034:             * but before the first line is read.
035:             */
036:            private int lineNo;
037:
038:            /**
039:             * Construct a reader object for the text file of the given name
040:             * in this package, using the given encoding.
041:             * @param resourceName the name of the text file located in this
042:             * package's ".data" subpackage.
043:             * @param encoding the encoding of the text file; if unsupported
044:             * an exception is thrown
045:             * @exception UnsupportedEncodingException if
046:             * <code>encoding</code> is not supported by the JDK.
047:             */
048:            public ResourceReader(String resourceName, String encoding)
049:                    throws UnsupportedEncodingException {
050:                this (ICUData.class, "data/" + resourceName, encoding);
051:            }
052:
053:            /**
054:             * Construct a reader object for the text file of the given name
055:             * in this package, using the default encoding.
056:             * @param resourceName the name of the text file located in this
057:             * package's ".data" subpackage.
058:             */
059:            public ResourceReader(String resourceName) {
060:                this (ICUData.class, "data/" + resourceName);
061:            }
062:
063:            /**
064:             * Construct a reader object for the text file of the given name
065:             * in the given class's package, using the given encoding.
066:             * @param resourceName the name of the text file located in the
067:             * given class's package.
068:             * @param encoding the encoding of the text file; if unsupported
069:             * an exception is thrown
070:             * @exception UnsupportedEncodingException if
071:             * <code>encoding</code> is not supported by the JDK.
072:             */
073:            public ResourceReader(Class rootClass, String resourceName,
074:                    String encoding) throws UnsupportedEncodingException {
075:                this .root = rootClass;
076:                this .resourceName = resourceName;
077:                this .encoding = encoding;
078:                lineNo = -1;
079:                _reset();
080:            }
081:
082:            /**
083:             * @internal
084:             * @deprecated This API is ICU internal only.
085:             */
086:            public ResourceReader(InputStream is, String resourceName) {
087:                this .root = null;
088:                this .resourceName = resourceName;
089:                this .encoding = null;
090:
091:                this .lineNo = -1;
092:                try {
093:                    InputStreamReader isr = (encoding == null) ? new InputStreamReader(
094:                            is)
095:                            : new InputStreamReader(is, encoding);
096:
097:                    this .reader = new BufferedReader(isr);
098:                    this .lineNo = 0;
099:                } catch (UnsupportedEncodingException e) {
100:                }
101:            }
102:
103:            /**
104:             * Construct a reader object for the text file of the given name
105:             * in the given class's package, using the default encoding.
106:             * @param resourceName the name of the text file located in the
107:             * given class's package.
108:             */
109:            public ResourceReader(Class rootClass, String resourceName) {
110:                this .root = rootClass;
111:                this .resourceName = resourceName;
112:                this .encoding = null;
113:                lineNo = -1;
114:                try {
115:                    _reset();
116:                } catch (UnsupportedEncodingException e) {
117:                }
118:            }
119:
120:            /**
121:             * Read and return the next line of the file or <code>null</code>
122:             * if the end of the file has been reached.
123:             */
124:            public String readLine() throws IOException {
125:                if (lineNo == 0) {
126:                    // Remove BOMs
127:                    ++lineNo;
128:                    String line = reader.readLine();
129:                    if (line.charAt(0) == '\uFFEF'
130:                            || line.charAt(0) == '\uFEFF') {
131:                        line = line.substring(1);
132:                    }
133:                    return line;
134:                }
135:                ++lineNo;
136:                return reader.readLine();
137:            }
138:
139:            /**
140:             * Read a line, ignoring blank lines and lines that start with
141:             * '#'.
142:             * @param trim if true then trim leading rule white space.
143:             */
144:            public String readLineSkippingComments(boolean trim)
145:                    throws IOException {
146:                for (;;) {
147:                    String 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)
159:                        line = line.substring(pos);
160:                    return line;
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 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 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:                }
203:                // We swallow this exception, if there is one.  If the encoding is
204:                // invalid, the constructor will have thrown this exception already and
205:                // the caller shouldn't use the object afterwards.
206:            }
207:
208:            /**
209:             * Reset to the start by reconstructing the stream and readers.
210:             * We could also use mark() and reset() on the stream or reader,
211:             * but that would cause them to keep the stream data around in
212:             * memory.  We don't want that because some of the resource files
213:             * are large, e.g., 400k.
214:             */
215:            private void _reset() throws UnsupportedEncodingException {
216:                if (lineNo == 0) {
217:                    return;
218:                }
219:                InputStream is = ICUData.getStream(root, resourceName);
220:                if (is == null) {
221:                    throw new IllegalArgumentException("Can't open "
222:                            + resourceName);
223:                }
224:
225:                InputStreamReader isr = (encoding == null) ? new InputStreamReader(
226:                        is)
227:                        : new InputStreamReader(is, encoding);
228:                reader = new BufferedReader(isr);
229:                lineNo = 0;
230:            }
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.