KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > LiteralExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESBase;
32 import com.caucho.es.ESBoolean;
33 import com.caucho.es.ESNumber;
34 import com.caucho.es.ESString;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Represents a java literal.
40  */

41 class LiteralExpr extends Expr {
42   private ESBase value;
43
44   LiteralExpr(Block block, ESBase value)
45   {
46     super(block);
47     
48     if (value == null)
49       value = ESBase.esNull;
50     
51     this.value = value;
52     
53     if (value instanceof ESNumber) {
54       double dv = 0;
55
56       try {
57         dv = value.toNum();
58       } catch (Throwable JavaDoc e) {
59       }
60       
61       if ((double) ((int) dv) == dv)
62         type = TYPE_INTEGER;
63       else if ((double) ((long) dv) == dv)
64         type = TYPE_LONG;
65       else
66         type = TYPE_NUMBER;
67     }
68     else if (value instanceof ESBoolean)
69       type = TYPE_BOOLEAN;
70     else if (value instanceof ESString)
71       type = TYPE_STRING;
72     else
73       type = TYPE_ES;
74
75     if (this.value == null)
76       throw new RuntimeException JavaDoc();
77   }
78
79   /**
80    * Return the literal value.
81    */

82   ESBase getLiteral()
83   {
84     return value;
85   }
86
87   boolean isSimple()
88   {
89     return true;
90   }
91
92   void printInt32Impl() throws IOException JavaDoc
93   {
94     try {
95       cl.print(value.toInt32());
96     } catch (Throwable JavaDoc e) {
97     }
98   }
99
100   void printNumImpl() throws IOException JavaDoc
101   {
102     try {
103       double v = value.toNum();
104
105       if (Double.isInfinite(v))
106         cl.print("Double.POSITIVE_INFINITY");
107       else if (Double.isInfinite(-v))
108         cl.print("Double.NEGATIVE_INFINITY");
109       else if (Double.isNaN(v))
110         cl.print("Double.NaN");
111       else if ((long) v == v)
112         cl.print("(" + (long) v + "L)");
113       else
114         cl.print("(" + v + "D)");
115     } catch (Throwable JavaDoc e) {
116       e.printStackTrace();
117     }
118   }
119
120   void printBooleanImpl() throws IOException JavaDoc
121   {
122     cl.print(value.toBoolean());
123   }
124
125   /**
126    * Prints the literal as a string.
127    */

128   void printStringImpl() throws IOException JavaDoc
129   {
130     try {
131       cl.print("\"");
132       String JavaDoc s = value.toStr().toString();
133       for (int i = 0; i < s.length(); i++) {
134         char ch = s.charAt(i);
135         switch (ch) {
136         case '"':
137           cl.print("\\\"");
138           break;
139         case '\\':
140           cl.print("\\\\");
141           break;
142         case '\n':
143           cl.print("\\n");
144           break;
145         case '\r':
146           cl.print("\\r");
147           break;
148         default:
149           cl.print(ch);
150           break;
151         }
152       }
153       cl.print("\"");
154     } catch (Throwable JavaDoc e) {
155       e.printStackTrace();
156     }
157   }
158
159   void printStr() throws IOException JavaDoc
160   {
161     try {
162       printLiteral(value.toStr());
163     } catch (Throwable JavaDoc e) {
164       e.printStackTrace();
165     }
166   }
167
168   void print() throws IOException JavaDoc
169   {
170     printLiteral(value);
171   }
172
173   void printImpl() throws IOException JavaDoc
174   {
175     printLiteral(value);
176   }
177
178   public String JavaDoc toString()
179   {
180     return "[LiteralExpr " + value.toString() + "]";
181   }
182 }
183
Popular Tags