001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.io.StreamTokenizer;
029:
030: /**
031: * A class for encapsulating tokens returned from StreamTokenizer primarily
032: * for output formatting purposes.
033: *
034: * @author Brian Doherty
035: * @version 1.8, 05/05/07
036: * @since 1.5
037: */
038: public class Token {
039: public String sval;
040: public double nval;
041: public int ttype;
042:
043: public Token(int ttype, String sval, double nval) {
044: this .ttype = ttype;
045: this .sval = sval;
046: this .nval = nval;
047: }
048:
049: public Token(int ttype, String sval) {
050: this (ttype, sval, 0);
051: }
052:
053: public Token(int ttype) {
054: this (ttype, null, 0);
055: }
056:
057: public String toMessage() {
058: switch (ttype) {
059: case StreamTokenizer.TT_EOL:
060: return "\"EOL\"";
061: case StreamTokenizer.TT_EOF:
062: return "\"EOF\"";
063: case StreamTokenizer.TT_NUMBER:
064: return "NUMBER";
065: case StreamTokenizer.TT_WORD:
066: if (sval == null) {
067: return "IDENTIFIER";
068: } else {
069: return "IDENTIFIER " + sval;
070: }
071: default:
072: if (ttype == (int) '"') {
073: String msg = "QUOTED STRING";
074: if (sval != null)
075: msg = msg + " \"" + sval + "\"";
076: return msg;
077: } else {
078: return "CHARACTER \'" + (char) ttype + "\'";
079: }
080: }
081: }
082:
083: public String toString() {
084: StringBuilder sb = new StringBuilder();
085: switch (ttype) {
086: case StreamTokenizer.TT_EOL:
087: sb.append("ttype=TT_EOL");
088: break;
089: case StreamTokenizer.TT_EOF:
090: sb.append("ttype=TT_EOF");
091: break;
092: case StreamTokenizer.TT_NUMBER:
093: sb.append("ttype=TT_NUM,").append("nval=" + nval);
094: break;
095: case StreamTokenizer.TT_WORD:
096: if (sval == null) {
097: sb.append("ttype=TT_WORD:IDENTIFIER");
098: } else {
099: sb.append("ttype=TT_WORD:").append("sval=" + sval);
100: }
101: break;
102: default:
103: if (ttype == (int) '"') {
104: sb.append("ttype=TT_STRING:").append("sval=" + sval);
105: } else {
106: sb.append("ttype=TT_CHAR:").append((char) ttype);
107: }
108: break;
109: }
110: return sb.toString();
111: }
112: }
|