001 package org.apache.commons.ognl; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.commons.ognl.enhance.UnsupportedCompilationException; 023 024 /** 025 * $Id: ASTConst.java 1194869 2011-10-29 11:10:16Z mcucchiara $ 026 * @author Luke Blanshard (blanshlu@netscape.net) 027 * @author Drew Davidson (drew@ognl.org) 028 */ 029 public class ASTConst 030 extends SimpleNode 031 implements NodeType 032 { 033 034 private Object value; 035 036 private Class getterClass; 037 038 public ASTConst( int id ) 039 { 040 super( id ); 041 } 042 043 public ASTConst( OgnlParser p, int id ) 044 { 045 super( p, id ); 046 } 047 048 /** 049 * Called from parser actions. 050 * @param value the value to set 051 */ 052 public void setValue( Object value ) 053 { 054 this.value = value; 055 } 056 057 public Object getValue() 058 { 059 return value; 060 } 061 062 protected Object getValueBody( OgnlContext context, Object source ) 063 throws OgnlException 064 { 065 return this.value; 066 } 067 068 public boolean isNodeConstant( OgnlContext context ) 069 throws OgnlException 070 { 071 return true; 072 } 073 074 public Class getGetterClass() 075 { 076 if ( getterClass == null ) 077 { 078 return null; 079 } 080 081 return getterClass; 082 } 083 084 public Class getSetterClass() 085 { 086 return null; 087 } 088 089 public String toGetSourceString( OgnlContext context, Object target ) 090 { 091 if ( value == null && parent != null && ExpressionNode.class.isInstance( parent ) ) 092 { 093 context.setCurrentType( null ); 094 return "null"; 095 } 096 else if ( value == null ) 097 { 098 context.setCurrentType( null ); 099 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 }