1 package org.apache.commons.ognl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
23
24
25
26
27
28
29 public class ASTConst
30 extends SimpleNode
31 implements NodeType
32 {
33
34 private Object value;
35
36 private Class getterClass;
37
38 public ASTConst( int id )
39 {
40 super( id );
41 }
42
43 public ASTConst( OgnlParser p, int id )
44 {
45 super( p, id );
46 }
47
48
49
50
51
52 public void setValue( Object value )
53 {
54 this.value = value;
55 }
56
57 public Object getValue()
58 {
59 return value;
60 }
61
62 protected Object getValueBody( OgnlContext context, Object source )
63 throws OgnlException
64 {
65 return this.value;
66 }
67
68 public boolean isNodeConstant( OgnlContext context )
69 throws OgnlException
70 {
71 return true;
72 }
73
74 public Class getGetterClass()
75 {
76 if ( getterClass == null )
77 {
78 return null;
79 }
80
81 return getterClass;
82 }
83
84 public Class getSetterClass()
85 {
86 return null;
87 }
88
89 public String toGetSourceString( OgnlContext context, Object target )
90 {
91 if ( value == null && parent != null && ExpressionNode.class.isInstance( parent ) )
92 {
93 context.setCurrentType( null );
94 return "null";
95 }
96 else if ( value == null )
97 {
98 context.setCurrentType( null );
99 return "";
100 }
101
102 getterClass = value.getClass();
103
104 Object retval = value;
105 if ( parent != null && ASTProperty.class.isInstance( parent ) )
106 {
107 context.setCurrentObject( value );
108
109 return value.toString();
110 }
111 else if ( value != null && Number.class.isAssignableFrom( value.getClass() ) )
112 {
113 context.setCurrentType( OgnlRuntime.getPrimitiveWrapperClass( value.getClass() ) );
114 context.setCurrentObject( value );
115
116 return value.toString();
117 }
118 else if ( !( parent != null
119 && value != null
120 && NumericExpression.class.isAssignableFrom( parent.getClass() ) )
121 && String.class.isAssignableFrom( value.getClass() ) )
122 {
123 context.setCurrentType( String.class );
124
125 retval = '\"' + OgnlOps.getEscapeString( value.toString() ) + '\"';
126
127 context.setCurrentObject( retval.toString() );
128
129 return retval.toString();
130 }
131 else if ( Character.class.isInstance( value ) )
132 {
133 Character val = (Character) value;
134
135 context.setCurrentType( Character.class );
136
137 if ( Character.isLetterOrDigit( val.charValue() ) )
138 {
139 retval = "'" + ( (Character) value ).charValue() + "'";
140 }
141 else
142 {
143 retval = "'" + OgnlOps.getEscapedChar( ( (Character) value ).charValue() ) + "'";
144 }
145
146 context.setCurrentObject( retval );
147 return retval.toString();
148 }
149
150 if ( Boolean.class.isAssignableFrom( value.getClass() ) )
151 {
152 getterClass = Boolean.TYPE;
153
154 context.setCurrentType( Boolean.TYPE );
155 context.setCurrentObject( value );
156
157 return value.toString();
158 }
159
160 return value.toString();
161 }
162
163 public String toSetSourceString( OgnlContext context, Object target )
164 {
165 if ( parent == null )
166 {
167 throw new UnsupportedCompilationException( "Can't modify constant values." );
168 }
169
170 return toGetSourceString( context, target );
171 }
172
173 public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
174 throws OgnlException
175 {
176 return visitor.visit( this, data );
177 }
178 }