001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.acl;
028:
029: import java.io.Reader;
030: import java.io.IOException;
031:
032: /**
033: * This class represents file reader for file that describes access control
034: * information.
035: */
036: public class ACLFileReader {
037:
038: /**
039: * Input data reader.
040: */
041: Reader in;
042:
043: /**
044: * Constructor.
045: * @param r input data reader.
046: */
047: public ACLFileReader(Reader r) {
048:
049: lineBuffer = new char[128];
050: in = r;
051: }
052:
053: /**
054: * Temporary data array.
055: */
056: char[] lineBuffer;
057:
058: /** Horizontal Tab - Unicode character 0x09. */
059: protected static final int HT = 0x09;
060:
061: /** Line Feed - Unicode character 0x0A. */
062: protected static final int LF = 0x0A;
063:
064: /** Carrage Return - Unicode character 0x0D. */
065: protected static final int CR = 0x0D;
066:
067: /** End Of File - Unicode character 0x1A. */
068: protected static final int EOF = 0x1A;
069:
070: /** SPace - Unicode character 0x20. */
071: protected static final int SP = 0x20;
072:
073: /**
074: * Current line number.
075: */
076: protected int lineNumber = 1;
077:
078: /**
079: * Temporary variable used in parsing.
080: */
081: protected char savedChar = 0;
082:
083: /**
084: * Read one word.
085: * @return the word.
086: * @throws IOException if I/O error occurs.
087: */
088: public String readWord() throws IOException {
089:
090: if (savedChar == '{') {
091: savedChar = 0;
092: return "{";
093: }
094:
095: if (savedChar == '}') {
096: savedChar = 0;
097: return "}";
098: }
099:
100: int room = lineBuffer.length;
101: int offset = 0;
102: boolean comment = false;
103: int c;
104:
105: for (;;) {
106: c = in.read();
107: if (c == -1) {
108: // LF or CR LF ends a line
109: break;
110: }
111:
112: if (c == '#') {
113: comment = true;
114: }
115:
116: if (c == LF) {
117: lineNumber++;
118: comment = false;
119: }
120:
121: if (comment) {
122: continue;
123: }
124:
125: if (c == LF || c == CR || c == EOF || c == HT || c == SP) {
126: if (offset == 0) {
127: continue;
128: }
129: break;
130: }
131:
132: if (c == '{' || c == '}') {
133: if (offset == 0) {
134: lineBuffer[offset++] = (char) c;
135: } else {
136: savedChar = (char) c;
137: }
138: break;
139: }
140:
141: if (--room < 0) {
142: char[] temp = new char[offset + 128];
143: room = temp.length - offset - 1;
144: System.arraycopy(lineBuffer, 0, temp, 0, offset);
145: lineBuffer = temp;
146: }
147:
148: lineBuffer[offset++] = (char) c;
149: }
150:
151: if ((c == -1) && (offset <= 0)) {
152: return null;
153: }
154:
155: return new String(lineBuffer, 0, offset).trim();
156: }
157:
158: /**
159: * Read current line without any modification.
160: * @return the current line value.
161: * @throws IOException if I/O error occurs.
162: */
163: public String readLine() throws IOException {
164:
165: int room;
166: int offset = 0;
167: int c;
168: char[] temp;
169:
170: room = lineBuffer.length;
171:
172: for (;;) {
173: c = in.read();
174:
175: if (c == LF) {
176: lineNumber++;
177: }
178:
179: if (c == -1 || c == LF) {
180: // LF or CR LF ends a line
181: break;
182: }
183:
184: /*
185: * throw away carrage returns and the end of file character.
186: */
187: if (c == CR || c == EOF) {
188: continue;
189: }
190:
191: if (--room < 0) {
192: temp = new char[offset + 128];
193: room = temp.length - offset - 1;
194: System.arraycopy(lineBuffer, 0, temp, 0, offset);
195: lineBuffer = temp;
196: }
197:
198: lineBuffer[offset++] = (char) c;
199: }
200:
201: if ((c == -1) && (offset <= 0)) {
202: return null;
203: }
204:
205: return new String(lineBuffer, 0, offset).trim();
206: }
207:
208: /**
209: * Reads one word, converts it into unsigned byte value.
210: * @return unsigned byte value.
211: * @throws IOException if I/O error occurs.
212: */
213: public int readByte() throws IOException {
214: return Integer.parseInt(readWord(), 16) & 0xff;
215: }
216:
217: /**
218: * Check that the next word is equal to specified one.
219: * @param s the word value.
220: * @throws IOException if I/O error occurs.
221: */
222: public void checkWord(String s) throws IOException {
223:
224: if (!readWord().equals(s)) {
225: throw new IOException();
226: }
227: }
228:
229: /**
230: * Returns the current line number.
231: * @return the current line number.
232: */
233: public int getLineNumber() {
234: return lineNumber;
235: }
236: }
|