📄 xpathtokenizer.java
字号:
/* XPathTokenizer.java -- Copyright (C) 2004 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.xml.xpath;import java.io.BufferedReader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.util.Map;import java.util.TreeMap;/*import antlr.Token;import antlr.TokenStream;import antlr.TokenStreamException;import antlr.TokenStreamIOException;*//** * XPath 1.0 expression tokenizer. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public class XPathTokenizerimplements XPathParser.yyInput//implements TokenStream{ static class XPathToken //extends Token { int type; String val; XPathToken (int type) { this (type, null); } XPathToken (int type, String val) { //super (type); this.type = type; this.val = val; } public String getText () { return val; } public String toString () { return val; } } static final Map keywords = new TreeMap (); static { keywords.put ("ancestor", new Integer (XPathParser.ANCESTOR)); keywords.put ("ancestor-or-self", new Integer (XPathParser.ANCESTOR_OR_SELF)); keywords.put ("attribute", new Integer (XPathParser.ATTRIBUTE)); keywords.put ("child", new Integer (XPathParser.CHILD)); keywords.put ("descendant", new Integer (XPathParser.DESCENDANT)); keywords.put ("descendant-or-self", new Integer (XPathParser.DESCENDANT_OR_SELF)); keywords.put ("following", new Integer (XPathParser.FOLLOWING)); keywords.put ("following-sibling", new Integer (XPathParser.FOLLOWING_SIBLING)); keywords.put ("namespace", new Integer (XPathParser.NAMESPACE)); keywords.put ("parent", new Integer (XPathParser.PARENT)); keywords.put ("preceding", new Integer (XPathParser.PRECEDING)); keywords.put ("preceding-sibling", new Integer (XPathParser.PRECEDING_SIBLING)); keywords.put ("self", new Integer (XPathParser.SELF)); keywords.put ("div", new Integer (XPathParser.DIV)); keywords.put ("mod", new Integer (XPathParser.MOD)); keywords.put ("or", new Integer (XPathParser.OR)); keywords.put ("and", new Integer (XPathParser.AND)); keywords.put ("comment", new Integer (XPathParser.COMMENT)); keywords.put ("processing-instruction", new Integer (XPathParser.PROCESSING_INSTRUCTION)); keywords.put ("text", new Integer (XPathParser.TEXT)); keywords.put ("node", new Integer (XPathParser.NODE)); } Reader in; XPathToken token; XPathToken lastToken; public XPathTokenizer (String expr) { this (new StringReader (expr)); } XPathTokenizer (Reader in) { this.in = in.markSupported () ? in : new BufferedReader (in); } /* Begin ANTLR specific * public Token nextToken () throws TokenStreamException { try { if (!advance ()) { throw new TokenStreamException ("eof"); } token (); return token; } catch (IOException e) { throw new TokenStreamIOException (e); } } * End ANTLR specific */ public boolean advance () throws IOException { lastToken = token; int c = in.read (); switch (c) { case -1: // eof return false; case 0x20: case 0x09: case 0x0d: case 0x0a: // skip whitespace return advance (); case 0x22: // " case 0x27: // ' token = consume_literal (c); break; case 0x28: // ( token = new XPathToken (XPathParser.LP); break; case 0x29: // ) token = new XPathToken (XPathParser.RP); break; case 0x5b: // [ token = new XPathToken (XPathParser.LB); break; case 0x5d: // ] token = new XPathToken (XPathParser.RB); break; case 0x2c: // , token = new XPathToken (XPathParser.COMMA); break; case 0x7c: // | token = new XPathToken (XPathParser.PIPE); break; case 0x2f: // / in.mark (1); int d1 = in.read (); if (d1 == 0x2f) { token = new XPathToken (XPathParser.DOUBLE_SLASH); } else { in.reset (); token = new XPathToken (XPathParser.SLASH); } break; case 0x3d: // = token = new XPathToken (XPathParser.EQ); break; case 0x21: // ! in.mark (1); int d2 = in.read (); if (d2 == 0x3d) // = { token = new XPathToken (XPathParser.NE); } else { in.reset (); token = new XPathToken (XPathParser.yyErrorCode); } break; case 0x3e: // > in.mark (1); int d3 = in.read (); if (d3 == 0x3d) // = { token = new XPathToken (XPathParser.GTE); } else { in.reset (); token = new XPathToken (XPathParser.GT); } break; case 0x3c: // < in.mark (1); int d4 = in.read (); if (d4 == 0x3d) // = { token = new XPathToken (XPathParser.LTE); } else { in.reset (); token = new XPathToken (XPathParser.LT); } break; case 0x2b: // + token = new XPathToken (XPathParser.PLUS); break; case 0x2d: // - token = new XPathToken (XPathParser.MINUS); break; case 0x40: // @ token = new XPathToken (XPathParser.AT); break; case 0x2a: // * token = new XPathToken (XPathParser.STAR); break; case 0x24: // $ token = new XPathToken (XPathParser.DOLLAR); break; case 0x3a: // : in.mark (1); int d5 = in.read (); if (d5 == 0x3a) { token = new XPathToken (XPathParser.DOUBLE_COLON); } else { in.reset (); token = new XPathToken (XPathParser.COLON); } break; case 0x2e: // . in.mark (1); int d6 = in.read (); if (d6 == 0x2e) { token = new XPathToken (XPathParser.DOUBLE_DOT); } else { in.reset (); token = new XPathToken (XPathParser.DOT); } break; default: if (c >= 0x30 && c <= 0x39) { token = consume_digits (c); } else if (c == 0x5f || Character.isLetter ((char) c)) { token = consume_name (c); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -