📄 configfiletokenizer.java
字号:
/* ConfigFileTokenizer.java -- JAAS Login Configuration default syntax tokenizer Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.security.auth.login;import java.io.BufferedReader;import java.io.IOException;import java.io.Reader;/** * A UTF-8 friendly, JAAS Login Module Configuration file tokenizer written in * the deault syntax. This class emulates, to a certain extent, the behavior of * a {@link java.io.SrteamTokenizer} instance <code>st</code>, when set as * follows: * * <pre> * st.resetSyntax(); * st.lowerCaseMode(false); * st.slashSlashComments(true); * st.slashStarComments(true); * st.eolIsSignificant(false); * st.wordChars('_', '_'); * st.wordChars('$', '$'); * st.wordChars('A', 'Z'); * st.wordChars('a', 'z'); * st.wordChars('0', '9'); * st.wordChars('.', '.'); * st.whitespaceChars(' ', ' '); * st.whitespaceChars('\t', '\t'); * st.whitespaceChars('\f', '\f'); * st.whitespaceChars('\r', '\r'); * st.whitespaceChars('\n', '\n'); * st.quoteChar('"'); * st.quoteChar('\''); * </pre> * * <p>The most important (negative) difference with a * {@link java.io.StreamTokenizer} is that this tokenizer does not properly * handle C++ and Java // style comments in the middle of the line. It only * ignores them if/when found at the start of the line.</p> */public class ConfigFileTokenizer{ // Constants and fields // -------------------------------------------------------------------------- private static final boolean DEBUG = false; private static final void debug(String m) {if (DEBUG) System.err.println(m);}; /** A constant indicating that the end of the stream has been read. */ public static final int TT_EOF = -1; /** A constant indicating that a word token has been read. */ public static final int TT_WORD = -3; /** A constant indicating that no tokens have been read yet. */ private static final int TT_NONE = -4; public String sval; public int ttype; private BufferedReader br; boolean initialised; private StringBuffer sb; private int sbNdx; // Constructor(s) // -------------------------------------------------------------------------- /** Trivial constructor. */ ConfigFileTokenizer(Reader r) { super(); br = r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r); initialised = false; } // Class methods // -------------------------------------------------------------------------- // Instance methods // -------------------------------------------------------------------------- public int nextToken() throws IOException { if (!initialised) init(); if (sbNdx >= sb.length()) return TT_EOF; skipWhitespace(); if (sbNdx >= sb.length()) return TT_EOF; int endNdx; if (Character.isJavaIdentifierPart(sb.charAt(sbNdx))) { endNdx = sbNdx + 1; while (Character.isJavaIdentifierPart(sb.charAt(endNdx)) || sb.charAt(endNdx) == '.') endNdx++; ttype = TT_WORD; sval = sb.substring(sbNdx, endNdx); sbNdx = endNdx; return ttype; } int c = sb.charAt(sbNdx); if (c == '{' || c == '}' || c == ';' || c == '=') { ttype = c; sbNdx++; return ttype; } if (c == '"' || c == '\'') { ttype = c; String quote = sb.substring(sbNdx, sbNdx + 1); int i = sbNdx + 1; while (true) { // find a candidate endNdx = sb.indexOf(quote, i); if (endNdx == -1) abort("Missing closing quote: " + quote); // found one; is it escaped? if (sb.charAt(endNdx - 1) != '\\') break; i++; continue; } endNdx++; sval = sb.substring(sbNdx, endNdx); sbNdx = endNdx; return ttype; } abort("Unknown character: " + sb.charAt(sbNdx)); return Integer.MIN_VALUE; } public void pushBack() { sbNdx -= ttype != TT_WORD ? 1 : sval.length(); } private void init() throws IOException { sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { line = line.trim(); if (line.length() == 0) continue; if (line.startsWith("#") || line.startsWith("//")) continue; sb.append(line).append(" "); } sbNdx = 0; sval = null; ttype = TT_NONE; initialised = true; } private void skipWhitespace() throws IOException { int endNdx; while (sbNdx < sb.length()) if (Character.isWhitespace(sb.charAt(sbNdx))) { sbNdx++; while (sbNdx < sb.length() && Character.isWhitespace(sb.charAt(sbNdx))) sbNdx++; continue; } else if (sb.charAt(sbNdx) == '/' && sb.charAt(sbNdx + 1) == '*') { endNdx = sb.indexOf("*/", sbNdx + 2); if (endNdx == -1) abort("Missing closing */ sequence"); sbNdx = endNdx + 2; continue; } else break; } private void abort(String m) throws IOException { debug("DEBUG: " + m); debug("DEBUG: sb = " + sb); debug("DEBUG: sbNdx = " + sbNdx); throw new IOException(m); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -