1 /* ConfigFileTokenizer.java -- JAAS Login Configuration default syntax tokenizer
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.javax
.security
.auth
.login
;
41 import gnu
.java
.lang
.CPStringBuilder
;
43 import gnu
.java
.security
.Configuration
;
45 import java
.io
.BufferedReader
;
46 import java
.io
.IOException
;
47 import java
.io
.Reader
;
48 import java
.util
.logging
.Logger
;
51 * A UTF-8 friendly, JAAS Login Module Configuration file tokenizer written in
52 * the deault syntax. This class emulates, to a certain extent, the behavior of
53 * a {@link java.io.StreamTokenizer} instance <code>st</code>, when set as
58 * st.lowerCaseMode(false);
59 * st.slashSlashComments(true);
60 * st.slashStarComments(true);
61 * st.eolIsSignificant(false);
62 * st.wordChars('_', '_');
63 * st.wordChars('$', '$');
64 * st.wordChars('A', 'Z');
65 * st.wordChars('a', 'z');
66 * st.wordChars('0', '9');
67 * st.wordChars('.', '.');
68 * st.whitespaceChars(' ', ' ');
69 * st.whitespaceChars('\t', '\t');
70 * st.whitespaceChars('\f', '\f');
71 * st.whitespaceChars('\r', '\r');
72 * st.whitespaceChars('\n', '\n');
77 * <p>The most important (negative) difference with a
78 * {@link java.io.StreamTokenizer} is that this tokenizer does not properly
79 * handle C++ and Java // style comments in the middle of the line. It only
80 * ignores them if/when found at the start of the line.</p>
82 public class ConfigFileTokenizer
84 private static final Logger log
= Logger
.getLogger(ConfigFileParser
.class.getName());
85 /** A constant indicating that the end of the stream has been read. */
86 public static final int TT_EOF
= -1;
87 /** A constant indicating that a word token has been read. */
88 public static final int TT_WORD
= -3;
89 /** A constant indicating that no tokens have been read yet. */
90 private static final int TT_NONE
= -4;
95 private BufferedReader br
;
97 private CPStringBuilder sb
;
101 // --------------------------------------------------------------------------
103 /** Trivial constructor. */
104 ConfigFileTokenizer(Reader r
)
108 br
= r
instanceof BufferedReader ?
(BufferedReader
) r
: new BufferedReader(r
);
113 // --------------------------------------------------------------------------
116 // --------------------------------------------------------------------------
118 public int nextToken() throws IOException
123 if (sbNdx
>= sb
.length())
128 if (sbNdx
>= sb
.length())
132 if (Character
.isJavaIdentifierPart(sb
.charAt(sbNdx
)))
135 while (Character
.isJavaIdentifierPart(sb
.charAt(endNdx
))
136 || sb
.charAt(endNdx
) == '.')
140 sval
= sb
.substring(sbNdx
, endNdx
);
145 int c
= sb
.charAt(sbNdx
);
146 if (c
== '{' || c
== '}' || c
== ';' || c
== '=')
153 if (c
== '"' || c
== '\'')
156 String quote
= sb
.substring(sbNdx
, sbNdx
+ 1);
161 endNdx
= sb
.indexOf(quote
, i
);
163 abort("Missing closing quote: " + quote
);
165 // found one; is it escaped?
166 if (sb
.charAt(endNdx
- 1) != '\\')
174 sval
= sb
.substring(sbNdx
, endNdx
);
179 abort("Unknown character: " + sb
.charAt(sbNdx
));
180 return Integer
.MIN_VALUE
;
183 public void pushBack()
185 sbNdx
-= ttype
!= TT_WORD ?
1 : sval
.length();
188 private void init() throws IOException
190 sb
= new CPStringBuilder();
192 while ((line
= br
.readLine()) != null)
195 if (line
.length() == 0)
198 if (line
.startsWith("#") || line
.startsWith("//"))
201 sb
.append(line
).append(" ");
211 private void skipWhitespace() throws IOException
214 while (sbNdx
< sb
.length())
215 if (Character
.isWhitespace(sb
.charAt(sbNdx
)))
218 while (sbNdx
< sb
.length() && Character
.isWhitespace(sb
.charAt(sbNdx
)))
223 else if (sb
.charAt(sbNdx
) == '/' && sb
.charAt(sbNdx
+ 1) == '*')
225 endNdx
= sb
.indexOf("*/", sbNdx
+ 2);
227 abort("Missing closing */ sequence");
236 private void abort(String m
) throws IOException
238 if (Configuration
.DEBUG
)
241 log
.fine("sb = " + sb
);
242 log
.fine("sbNdx = " + sbNdx
);
244 throw new IOException(m
);