KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHLiteral


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34
35 package bsh;
36
37 class BSHLiteral extends SimpleNode
38 {
39     public Object JavaDoc value;
40
41     BSHLiteral(int id) { super(id); }
42
43     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter )
44         throws EvalError
45     {
46         if ( value == null )
47             throw new InterpreterError("Null in bsh literal: "+value);
48
49         return value;
50     }
51
52     private char getEscapeChar(char ch)
53     {
54         switch(ch)
55         {
56             case 'b':
57                 ch = '\b';
58                 break;
59
60             case 't':
61                 ch = '\t';
62                 break;
63
64             case 'n':
65                 ch = '\n';
66                 break;
67
68             case 'f':
69                 ch = '\f';
70                 break;
71
72             case 'r':
73                 ch = '\r';
74                 break;
75
76             // do nothing - ch already contains correct character
77
case '"':
78             case '\'':
79             case '\\':
80                 break;
81         }
82
83         return ch;
84     }
85
86     public void charSetup(String JavaDoc str)
87     {
88         char ch = str.charAt(0);
89         if(ch == '\\')
90         {
91             // get next character
92
ch = str.charAt(1);
93
94             if(Character.isDigit(ch))
95                 ch = (char)Integer.parseInt(str.substring(1), 8);
96             else
97                 ch = getEscapeChar(ch);
98         }
99
100         value = new Primitive(new Character JavaDoc(ch).charValue());
101     }
102
103     void stringSetup(String JavaDoc str)
104     {
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
106         for(int i = 0; i < str.length(); i++)
107         {
108             char ch = str.charAt(i);
109             if(ch == '\\')
110             {
111                 // get next character
112
ch = str.charAt(++i);
113
114                 if(Character.isDigit(ch))
115                 {
116                     int endPos = i;
117
118                     // check the next two characters
119
while(endPos < i + 2)
120                     {
121                         if(Character.isDigit(str.charAt(endPos + 1)))
122                             endPos++;
123                         else
124                             break;
125                     }
126
127                     ch = (char)Integer.parseInt(str.substring(i, endPos + 1), 8);
128                     i = endPos;
129                 }
130                 else
131                     ch = getEscapeChar(ch);
132             }
133
134             buffer.append(ch);
135         }
136
137         value = buffer.toString().intern();
138     }
139 }
140
Popular Tags