1 /* ConfigFileTokenizer.java -- JAAS Login Configuration default syntax tokenizer
2 Copyright (C) 2006, 2010 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 = Configuration.DEBUG ?
85 Logger.getLogger(ConfigFileParser.class.getName()) : null;
87 /** A constant indicating that the end of the stream has been read. */
88 public static final int TT_EOF = -1;
89 /** A constant indicating that a word token has been read. */
90 public static final int TT_WORD = -3;
91 /** A constant indicating that no tokens have been read yet. */
92 private static final int TT_NONE = -4;
97 private final BufferedReader br;
99 private CPStringBuilder sb;
103 // --------------------------------------------------------------------------
105 /** Trivial constructor. */
106 ConfigFileTokenizer(Reader r)
108 br = r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r);
112 // --------------------------------------------------------------------------
115 // --------------------------------------------------------------------------
117 public int nextToken() throws IOException
122 if (sbNdx >= sb.length())
127 if (sbNdx >= sb.length())
131 if (Character.isJavaIdentifierPart(sb.charAt(sbNdx)))
134 while (Character.isJavaIdentifierPart(sb.charAt(endNdx))
135 || sb.charAt(endNdx) == '.')
139 sval = sb.substring(sbNdx, endNdx);
144 int c = sb.charAt(sbNdx);
145 if (c == '{' || c == '}' || c == ';' || c == '=')
152 if (c == '"' || c == '\'')
155 String quote = sb.substring(sbNdx, sbNdx + 1);
160 endNdx = sb.indexOf(quote, i);
162 abort("Missing closing quote: " + quote);
164 // found one; is it escaped?
165 if (sb.charAt(endNdx - 1) != '\\')
173 sval = sb.substring(sbNdx, endNdx);
178 abort("Unknown character: " + sb.charAt(sbNdx));
179 return Integer.MIN_VALUE;
182 public void pushBack()
184 sbNdx -= ttype != TT_WORD ? 1 : sval.length();
187 private void init() throws IOException
189 sb = new CPStringBuilder();
191 while ((line = br.readLine()) != null)
194 if (line.length() == 0)
197 if (line.startsWith("#") || line.startsWith("//"))
200 sb.append(line).append(" ");
210 private void skipWhitespace() throws IOException
213 while (sbNdx < sb.length())
214 if (Character.isWhitespace(sb.charAt(sbNdx)))
217 while (sbNdx < sb.length() && Character.isWhitespace(sb.charAt(sbNdx)))
222 else if (sb.charAt(sbNdx) == '/' && sb.charAt(sbNdx + 1) == '*')
224 endNdx = sb.indexOf("*/", sbNdx + 2);
226 abort("Missing closing */ sequence");
235 private void abort(String m) throws IOException
237 if (Configuration.DEBUG)
240 log.fine("sb = " + sb);
241 log.fine("sbNdx = " + sbNdx);
243 throw new IOException(m);