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 public abstract class ComparisonExpression
28 extends BooleanExpression
29 {
30
31 private static final long serialVersionUID = -5945855000509930682L;
32
33 public ComparisonExpression( int id )
34 {
35 super( id );
36 }
37
38 public ComparisonExpression( OgnlParser p, int id )
39 {
40 super( p, id );
41 }
42
43 public abstract String getComparisonFunction();
44
45
46
47
48 @Override
49 public String toGetSourceString( OgnlContext context, Object target )
50 {
51 if ( target == null )
52 {
53 throw new UnsupportedCompilationException( "Current target is null, can't compile." );
54 }
55
56 try
57 {
58
59 Object value = getValueBody( context, target );
60
61 if ( value != null && Boolean.class.isAssignableFrom( value.getClass() ) )
62 {
63 getterClass = Boolean.TYPE;
64 }
65 else if ( value != null )
66 {
67 getterClass = value.getClass();
68 }
69 else
70 {
71 getterClass = Boolean.TYPE;
72 }
73
74
75
76 OgnlRuntime.getChildSource( context, target, children[0] );
77 OgnlRuntime.getChildSource( context, target, children[1] );
78
79
80
81
82 boolean conversion = OgnlRuntime.shouldConvertNumericTypes( context );
83
84 StringBuilder result = new StringBuilder( "(" );
85 if ( conversion )
86 {
87 result.append( getComparisonFunction() ).append( "( ($w) (" );
88 }
89
90 result.append( OgnlRuntime.getChildSource( context, target, children[0], conversion ) )
91 .append( " " );
92
93 if ( conversion )
94 {
95 result.append( "), ($w) " );
96 }
97 else
98 {
99 result.append( getExpressionOperator( 0 ) );
100 }
101
102 result.append( "" ).append( OgnlRuntime.getChildSource( context, target, children[1], conversion ) );
103
104 if ( conversion )
105 {
106 result.append( ")" );
107 }
108
109 context.setCurrentType( Boolean.TYPE );
110
111 result.append( ")" );
112
113 return result.toString();
114 }
115 catch ( NullPointerException e )
116 {
117
118
119
120 throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
121 }
122 catch ( Throwable t )
123 {
124 throw OgnlOps.castToRuntime( t );
125 }
126 }
127 }