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    /**
023     * $Id: ASTInstanceof.java 1194869 2011-10-29 11:10:16Z mcucchiara $
024     * @author Luke Blanshard (blanshlu@netscape.net)
025     * @author Drew Davidson (drew@ognl.org)
026     */
027    public class ASTInstanceof
028        extends SimpleNode
029        implements NodeType
030    {
031        private String targetType;
032    
033        public ASTInstanceof( int id )
034        {
035            super( id );
036        }
037    
038        public ASTInstanceof( OgnlParser p, int id )
039        {
040            super( p, id );
041        }
042    
043        void setTargetType( String targetType )
044        {
045            this.targetType = targetType;
046        }
047    
048        String getTargetType() 
049        {
050            return targetType;
051        }
052    
053        protected Object getValueBody( OgnlContext context, Object source )
054            throws OgnlException
055        {
056            Object value = children[0].getValue( context, source );
057            return OgnlRuntime.isInstance( context, value, targetType ) ? Boolean.TRUE : Boolean.FALSE;
058        }
059    
060        public Class getGetterClass()
061        {
062            return boolean.class;
063        }
064    
065        public Class getSetterClass()
066        {
067            return null;
068        }
069    
070        public String toGetSourceString( OgnlContext context, Object target )
071        {
072            try
073            {
074    
075                String ret = "";
076    
077                if ( ASTConst.class.isInstance( children[0] ) )
078                {
079                    ret = ( (Boolean) getValueBody( context, target ) ).toString();
080                }
081                else
082                {
083                    ret = children[0].toGetSourceString( context, target ) + " instanceof " + targetType;
084                }
085                context.setCurrentType( Boolean.TYPE );
086    
087                return ret;
088    
089            }
090            catch ( Throwable t )
091            {
092                throw OgnlOps.castToRuntime( t );
093            }
094        }
095    
096        public String toSetSourceString( OgnlContext context, Object target )
097        {
098            return toGetSourceString( context, target );
099        }
100        
101        public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
102            throws OgnlException
103        {
104            return visitor.visit( this, data );
105        }
106    }