View Javadoc

1   package org.apache.commons.ognl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
23  
24  /**
25   * $Id: ASTTest.java 1194869 2011-10-29 11:10:16Z mcucchiara $
26   * @author Luke Blanshard (blanshlu@netscape.net)
27   * @author Drew Davidson (drew@ognl.org)
28   */
29  class ASTTest
30      extends ExpressionNode
31  {
32      public ASTTest( int id )
33      {
34          super( id );
35      }
36  
37      public ASTTest( OgnlParser p, int id )
38      {
39          super( p, id );
40      }
41  
42      protected Object getValueBody( OgnlContext context, Object source )
43          throws OgnlException
44      {
45          Object test = children[0].getValue( context, source );
46          int branch = OgnlOps.booleanValue( test ) ? 1 : 2;
47          return children[branch].getValue( context, source );
48      }
49  
50      protected void setValueBody( OgnlContext context, Object target, Object value )
51          throws OgnlException
52      {
53          Object test = children[0].getValue( context, target );
54          int branch = OgnlOps.booleanValue( test ) ? 1 : 2;
55          children[branch].setValue( context, target, value );
56      }
57  
58      public String getExpressionOperator( int index )
59      {
60          return ( index == 1 ) ? "?" : ":";
61      }
62  
63      public String toGetSourceString( OgnlContext context, Object target )
64      {
65          if ( target == null )
66          {
67              throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
68          }
69          
70          if ( children.length != 3 )
71          {
72              throw new UnsupportedCompilationException( "Can only compile test expressions with two children."
73                  + children.length );
74          }
75          
76          String result = "";
77  
78          try
79          {
80  
81              String first = OgnlRuntime.getChildSource( context, target, children[0] );
82              if ( !OgnlRuntime.isBoolean( first ) && !context.getCurrentType().isPrimitive() )
83              {
84                  first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, context.getCurrentType() );
85              }
86              
87              if ( ExpressionNode.class.isInstance( children[0] ) )
88              {
89                  first = "(" + first + ")";
90              }
91  
92              String second = OgnlRuntime.getChildSource( context, target, children[1] );
93              Class secondType = context.getCurrentType();
94  
95              if ( !OgnlRuntime.isBoolean( second ) && !context.getCurrentType().isPrimitive() )
96              {
97                  second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() );
98              }
99              
100             if ( ExpressionNode.class.isInstance( children[1] ) )
101             {
102                 second = "(" + second + ")";
103             }
104 
105             String third = OgnlRuntime.getChildSource( context, target, children[2] );
106             Class thirdType = context.getCurrentType();
107 
108             if ( !OgnlRuntime.isBoolean( third ) && !context.getCurrentType().isPrimitive() )
109             {
110                 third = OgnlRuntime.getCompiler( context ).createLocalReference( context, third, context.getCurrentType() );
111             
112             }
113             
114             if ( ExpressionNode.class.isInstance( children[2] ) )
115             {
116                 third = "(" + third + ")";
117             }
118 
119             boolean mismatched =
120                 ( secondType.isPrimitive( ) && !thirdType.isPrimitive( ) ) || ( !secondType.isPrimitive( )
121                     && thirdType.isPrimitive( ) );
122 
123             result += "org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")";
124 
125             result += " ? ";
126 
127             result += ( mismatched ? " ($w) " : "" ) + second;
128             result += " : ";
129 
130             result += ( mismatched ? " ($w) " : "" ) + third;
131 
132             context.setCurrentObject( target );
133             context.setCurrentType( mismatched ? Object.class : secondType );
134 
135             return result;
136 
137         }
138         catch ( NullPointerException e )
139         {
140 
141             // expected to happen in some instances
142             throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
143         }
144         catch ( Throwable t )
145         {
146             throw OgnlOps.castToRuntime( t );
147         }
148     }
149     
150     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
151         throws OgnlException
152     {
153         return visitor.visit( this, data );
154     }
155 }