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     * Base class for types that compare values.
026     */
027    public abstract class ComparisonExpression
028        extends BooleanExpression
029    {
030    
031        private static final long serialVersionUID = -5945855000509930682L;
032    
033        public ComparisonExpression( int id )
034        {
035            super( id );
036        }
037    
038        public ComparisonExpression( OgnlParser p, int id )
039        {
040            super( p, id );
041        }
042    
043        public abstract String getComparisonFunction();
044    
045        /**
046         * {@inheritDoc}
047         */
048        @Override
049        public String toGetSourceString( OgnlContext context, Object target )
050        {
051            if ( target == null )
052            {
053                throw new UnsupportedCompilationException( "Current target is null, can't compile." );
054            }
055    
056            try
057            {
058    
059                Object value = getValueBody( context, target );
060    
061                if ( value != null && Boolean.class.isAssignableFrom( value.getClass() ) )
062                {
063                    getterClass = Boolean.TYPE;
064                }
065                else if ( value != null )
066                {
067                    getterClass = value.getClass();
068                }
069                else
070                {
071                    getterClass = Boolean.TYPE;
072                }
073    
074                // iterate over children to make numeric type detection work properly
075    
076                OgnlRuntime.getChildSource( context, target, children[0] );
077                OgnlRuntime.getChildSource( context, target, children[1] );
078    
079                // System.out.println("comparison expression currentType: " + context.getCurrentType() + " previousType: " +
080                // context.getPreviousType());
081    
082                boolean conversion = OgnlRuntime.shouldConvertNumericTypes( context );
083    
084                StringBuilder result = new StringBuilder( "(" );
085                if ( conversion )
086                {
087                    result.append( getComparisonFunction() ).append( "( ($w) (" );
088                }
089    
090                result.append( OgnlRuntime.getChildSource( context, target, children[0], conversion ) )
091                        .append( " " );
092    
093                if ( conversion )
094                {
095                    result.append( "), ($w) " );
096                }
097                else
098                {
099                    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                // expected to happen in some instances
119    
120                throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
121            }
122            catch ( Throwable t )
123            {
124                throw OgnlOps.castToRuntime( t );
125            }
126        }
127    }