1 /*
2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.tools.jstat;
27 
28 import java.io.StreamTokenizer;
29 
30 /**
31  * A class for encapsulating tokens returned from StreamTokenizer primarily
32  * for output formatting purposes.
33  *
34  * @author Brian Doherty
35  * @since 1.5
36  */
37 public class Token {
38     public String sval;
39     public double nval;
40     public int ttype;
41 
Token(int ttype, String sval, double nval)42     public Token(int ttype, String sval, double nval) {
43         this.ttype = ttype;
44         this.sval = sval;
45         this.nval = nval;
46     }
47 
Token(int ttype, String sval)48     public Token(int ttype, String sval) {
49         this(ttype, sval, 0);
50     }
51 
Token(int ttype)52     public Token(int ttype) {
53         this(ttype, null, 0);
54     }
55 
toMessage()56     public String toMessage() {
57         switch(ttype) {
58         case StreamTokenizer.TT_EOL:
59             return "\"EOL\"";
60         case StreamTokenizer.TT_EOF:
61             return "\"EOF\"";
62         case StreamTokenizer.TT_NUMBER:
63             return "NUMBER";
64         case StreamTokenizer.TT_WORD:
65             if (sval == null) {
66                 return "IDENTIFIER";
67             } else {
68                 return "IDENTIFIER " + sval;
69             }
70         default:
71             if (ttype == (int)'"') {
72                 String msg = "QUOTED STRING";
73                 if (sval != null)
74                     msg = msg + " \"" + sval + "\"";
75                 return msg;
76             } else {
77                 return "CHARACTER \'" + (char)ttype + "\'";
78             }
79         }
80     }
81 
toString()82     public String toString() {
83         StringBuilder sb = new StringBuilder();
84         switch(ttype) {
85         case StreamTokenizer.TT_EOL:
86             sb.append("ttype=TT_EOL");
87             break;
88         case StreamTokenizer.TT_EOF:
89             sb.append("ttype=TT_EOF");
90             break;
91         case StreamTokenizer.TT_NUMBER:
92             sb.append("ttype=TT_NUM,").append("nval="+nval);
93             break;
94         case StreamTokenizer.TT_WORD:
95             if (sval == null) {
96                 sb.append("ttype=TT_WORD:IDENTIFIER");
97             } else {
98                 sb.append("ttype=TT_WORD:").append("sval="+sval);
99             }
100             break;
101         default:
102             if (ttype == (int)'"') {
103                 sb.append("ttype=TT_STRING:").append("sval="+sval);
104             } else {
105                 sb.append("ttype=TT_CHAR:").append((char)ttype);
106             }
107             break;
108         }
109         return sb.toString();
110     }
111 }
112