001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.math.*;
034:
035: /**
036: * @author Luke Blanshard (blanshlu@netscape.net)
037: * @author Drew Davidson (drew@ognl.org)
038: */
039: class ASTConst extends SimpleNode {
040: private Object value;
041:
042: public ASTConst(int id) {
043: super (id);
044: }
045:
046: public ASTConst(OgnlParser p, int id) {
047: super (p, id);
048: }
049:
050: /** Called from parser actions. */
051: void setValue(Object value) {
052: this .value = value;
053: }
054:
055: public Object getValue() {
056: return value;
057: }
058:
059: protected Object getValueBody(OgnlContext context, Object source)
060: throws OgnlException {
061: return this .value;
062: }
063:
064: public boolean isNodeConstant(OgnlContext context)
065: throws OgnlException {
066: return true;
067: }
068:
069: public String getEscapedChar(char ch) {
070: String result;
071:
072: switch (ch) {
073: case '\b':
074: result = "\b";
075: break;
076: case '\t':
077: result = "\\t";
078: break;
079: case '\n':
080: result = "\\n";
081: break;
082: case '\f':
083: result = "\\f";
084: break;
085: case '\r':
086: result = "\\r";
087: break;
088: case '\"':
089: result = "\\\"";
090: break;
091: case '\'':
092: result = "\\\'";
093: break;
094: case '\\':
095: result = "\\\\";
096: break;
097: default:
098: if (Character.isISOControl(ch) || (ch > 255)) {
099: String hc = Integer.toString((int) ch, 16);
100: int hcl = hc.length();
101:
102: result = "\\u";
103: if (hcl < 4) {
104: if (hcl == 3) {
105: result = result + "0";
106: } else {
107: if (hcl == 2) {
108: result = result + "00";
109: } else {
110: result = result + "000";
111: }
112: }
113: }
114:
115: result = result + hc;
116: } else {
117: result = new String(ch + "");
118: }
119: break;
120: }
121: return result;
122: }
123:
124: public String getEscapedString(String value) {
125: StringBuffer result = new StringBuffer();
126:
127: for (int i = 0, icount = value.length(); i < icount; i++) {
128: result.append(getEscapedChar(value.charAt(i)));
129: }
130: return new String(result);
131: }
132:
133: public String toString() {
134: String result;
135:
136: if (value == null) {
137: result = "null";
138: } else {
139: if (value instanceof String) {
140: result = '\"' + getEscapedString(value.toString()) + '\"';
141: } else {
142: if (value instanceof Character) {
143: result = '\'' + getEscapedChar(((Character) value)
144: .charValue()) + '\'';
145: } else {
146: result = value.toString();
147: if (value instanceof Long) {
148: result = result + "L";
149: } else {
150: if (value instanceof BigDecimal) {
151: result = result + "B";
152: } else {
153: if (value instanceof BigInteger) {
154: result = result + "H";
155: } else {
156: if (value instanceof Node) {
157: result = ":[ " + result + " ]";
158: }
159: }
160: }
161: }
162: }
163: }
164: }
165: return result;
166: }
167: }
|