001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.ESBase;
032: import com.caucho.es.ESBoolean;
033: import com.caucho.es.ESNumber;
034: import com.caucho.es.ESString;
035:
036: import java.io.IOException;
037:
038: /**
039: * Represents a java literal.
040: */
041: class LiteralExpr extends Expr {
042: private ESBase value;
043:
044: LiteralExpr(Block block, ESBase value) {
045: super (block);
046:
047: if (value == null)
048: value = ESBase.esNull;
049:
050: this .value = value;
051:
052: if (value instanceof ESNumber) {
053: double dv = 0;
054:
055: try {
056: dv = value.toNum();
057: } catch (Throwable e) {
058: }
059:
060: if ((double) ((int) dv) == dv)
061: type = TYPE_INTEGER;
062: else if ((double) ((long) dv) == dv)
063: type = TYPE_LONG;
064: else
065: type = TYPE_NUMBER;
066: } else if (value instanceof ESBoolean)
067: type = TYPE_BOOLEAN;
068: else if (value instanceof ESString)
069: type = TYPE_STRING;
070: else
071: type = TYPE_ES;
072:
073: if (this .value == null)
074: throw new RuntimeException();
075: }
076:
077: /**
078: * Return the literal value.
079: */
080: ESBase getLiteral() {
081: return value;
082: }
083:
084: boolean isSimple() {
085: return true;
086: }
087:
088: void printInt32Impl() throws IOException {
089: try {
090: cl.print(value.toInt32());
091: } catch (Throwable e) {
092: }
093: }
094:
095: void printNumImpl() throws IOException {
096: try {
097: double v = value.toNum();
098:
099: if (Double.isInfinite(v))
100: cl.print("Double.POSITIVE_INFINITY");
101: else if (Double.isInfinite(-v))
102: cl.print("Double.NEGATIVE_INFINITY");
103: else if (Double.isNaN(v))
104: cl.print("Double.NaN");
105: else if ((long) v == v)
106: cl.print("(" + (long) v + "L)");
107: else
108: cl.print("(" + v + "D)");
109: } catch (Throwable e) {
110: e.printStackTrace();
111: }
112: }
113:
114: void printBooleanImpl() throws IOException {
115: cl.print(value.toBoolean());
116: }
117:
118: /**
119: * Prints the literal as a string.
120: */
121: void printStringImpl() throws IOException {
122: try {
123: cl.print("\"");
124: String s = value.toStr().toString();
125: for (int i = 0; i < s.length(); i++) {
126: char ch = s.charAt(i);
127: switch (ch) {
128: case '"':
129: cl.print("\\\"");
130: break;
131: case '\\':
132: cl.print("\\\\");
133: break;
134: case '\n':
135: cl.print("\\n");
136: break;
137: case '\r':
138: cl.print("\\r");
139: break;
140: default:
141: cl.print(ch);
142: break;
143: }
144: }
145: cl.print("\"");
146: } catch (Throwable e) {
147: e.printStackTrace();
148: }
149: }
150:
151: void printStr() throws IOException {
152: try {
153: printLiteral(value.toStr());
154: } catch (Throwable e) {
155: e.printStackTrace();
156: }
157: }
158:
159: void print() throws IOException {
160: printLiteral(value);
161: }
162:
163: void printImpl() throws IOException {
164: printLiteral(value);
165: }
166:
167: public String toString() {
168: return "[LiteralExpr " + value.toString() + "]";
169: }
170: }
|