001: /*
002: * Copyright (c) 1998-2008 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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import javax.el.MethodInfo;
037: import java.io.IOException;
038:
039: /**
040: * Represents a string literal expression
041: */
042: public class StringLiteral extends Expr {
043: private String _value;
044:
045: public StringLiteral(String value) {
046: _value = value;
047: }
048:
049: /**
050: * Returns true if the expression is constant.
051: */
052: @Override
053: public boolean isConstant() {
054: return true;
055: }
056:
057: /**
058: * Returns true if the expression is literal text
059: */
060: @Override
061: public boolean isLiteralText() {
062: return true;
063: }
064:
065: /**
066: * Evaluates the expression, returning an object.
067: *
068: * @param env the variable environment
069: *
070: * @return the value of the expression as an object
071: */
072: @Override
073: public MethodInfo getMethodInfo(ELContext env, Class<?> returnType,
074: Class<?>[] argTypes) throws ELException {
075: return new MethodInfo(_value, returnType, argTypes);
076: }
077:
078: /**
079: * Evaluates the expression, returning an object.
080: *
081: * @param env the variable environment
082: *
083: * @return the value of the expression as an object
084: */
085: @Override
086: public Object invoke(ELContext env, Class<?>[] argTypes,
087: Object[] args) throws ELException {
088: return _value;
089: }
090:
091: /**
092: * Returns the value of the literal.
093: */
094: public String getValue() {
095: return _value;
096: }
097:
098: /**
099: * Evaluate the expr as an object.
100: *
101: * @param env the variable environment
102: */
103: @Override
104: public Object getValue(ELContext env) throws ELException {
105: return _value;
106: }
107:
108: /**
109: * Evaluate the expr as a string
110: *
111: * @param env the variable environment
112: */
113: @Override
114: public String evalString(ELContext env) throws ELException {
115: return _value;
116: }
117:
118: /**
119: * Evalutes directly to the output.
120: */
121: @Override
122: public boolean print(WriteStream out, ELContext env,
123: boolean isEscape) throws IOException, ELException {
124: if (isEscape)
125: toStreamEscaped(out, _value);
126: else
127: out.print(_value);
128:
129: return false;
130: }
131:
132: /**
133: * Prints the code to create an LongLiteral.
134: */
135: @Override
136: public void printCreate(WriteStream os) throws IOException {
137: os.print("new com.caucho.el.StringLiteral(\"");
138: printEscapedString(os, _value);
139: os.print("\")");
140: }
141:
142: /**
143: * Returns true for equal strings.
144: */
145: public boolean equals(Object o) {
146: if (!(o instanceof StringLiteral))
147: return false;
148:
149: StringLiteral literal = (StringLiteral) o;
150:
151: return _value.equals(literal._value);
152: }
153:
154: /**
155: * Returns a printable version.
156: */
157: public String toString() {
158: return "\"" + _value + "\"";
159: }
160: }
|