blob: 0aa1511f91e2945d207b928e00cf3371a38cc317 [file] [log] [blame]
Richard S. Hall930fecc2005-08-16 18:33:34 +00001/*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
Richard S. Hall5a031592005-08-19 19:53:58 +000017package org.apache.felix.framework.util.ldap;
Richard S. Hall930fecc2005-08-16 18:33:34 +000018
19import java.io.IOException;
20import java.io.Reader;
21
22public class LdapLexer {
23
24 static final int EOF = -1;
25 static final int NOCHAR = 0; // signal no peeked char; different from EOF
26
27 public static final String WHITESPACE = " \t\n\r";
28
29 Reader reader = null;
30
31 int nextChar = NOCHAR; // last peeked character
32
33 public LdapLexer() {}
34
35 public LdapLexer(Reader r)
36 {
37 setReader(r);
38 charno = 1;
39 }
40
41 public void setReader(Reader r)
42 {
43 reader = r;
44 }
45
46 /*
47 The procedures get(),peek(),skipwhitespace(),getnw(), and peeknw()
48 provide the essential LdapLexer interface.
49 */
50
51 public int get() throws IOException // any next char
52 {
53 if(nextChar == NOCHAR) return readChar();
54 int c = nextChar;
55 nextChar = NOCHAR;
56 return c;
57 }
58
59 public int peek() throws IOException
60 {
61 if(nextChar == NOCHAR) {
62 nextChar = readChar();
63 }
64 return nextChar;
65 }
66
67 void skipwhitespace() throws IOException
68 {
69 while(WHITESPACE.indexOf(peek()) >= 0) get();
70 }
71
72 public int getnw() throws IOException // next non-whitespace char
73 { // (note: not essential but useful)
74 skipwhitespace();
75 return get();
76 }
77
78 public int peeknw() throws IOException // next non-whitespace char
79 { // (note: not essential but useful)
80 skipwhitespace();
81 return peek();
82 }
83
84 // Following is for error reporting
85
86 // Pass all character reads thru this so we can track char count
87
88 int charno; // 1-based
89
90 public int charno() {return charno;}
91
92 int readChar() throws IOException
93 {
94 charno++;
95 return reader.read();
96 }
97
98}