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.ExpressionCompiler;
23  
24  /**
25   * @author Luke Blanshard (blanshlu@netscape.net)
26   * @author Drew Davidson (drew@ognl.org)
27   */
28  public abstract class ExpressionNode
29      extends SimpleNode
30  {
31  
32      private static final long serialVersionUID = 9076228016268317598L;
33  
34      public ExpressionNode( int i )
35      {
36          super( i );
37      }
38  
39      public ExpressionNode( OgnlParser p, int i )
40      {
41          super( p, i );
42      }
43  
44      /**
45       * Returns true iff this node is constant without respect to the children.
46       */
47      @Override
48      public boolean isNodeConstant( OgnlContext context )
49          throws OgnlException
50      {
51          return false;
52      }
53  
54      @Override
55      public boolean isConstant( OgnlContext context )
56          throws OgnlException
57      {
58          boolean result = isNodeConstant( context );
59  
60          if ( ( children != null ) && ( children.length > 0 ) )
61          {
62              result = true;
63              for ( int i = 0; result && ( i < children.length ); ++i )
64              {
65                  if ( children[i] instanceof SimpleNode )
66                  {
67                      result = ( (SimpleNode) children[i] ).isConstant( context );
68                  }
69                  else
70                  {
71                      result = false;
72                  }
73              }
74          }
75          return result;
76      }
77  
78      public String getExpressionOperator( int index )
79      {
80          throw new RuntimeException( "unknown operator for " + OgnlParserTreeConstants.jjtNodeName[id] );
81      }
82  
83      @Override
84      public String toGetSourceString( OgnlContext context, Object target )
85      {
86          StringBuilder result =
87              new StringBuilder(
88                  ( parent == null || NumericExpression.class.isAssignableFrom( parent.getClass() ) ) ? "" : "(" );
89  
90          if ( ( children != null ) && ( children.length > 0 ) )
91          {
92              for ( int i = 0; i < children.length; ++i )
93              {
94                  if ( i > 0 )
95                  {
96                      result.append( " " ).append( getExpressionOperator( i ) ).append( " " );
97                  }
98  
99                  String value = children[i].toGetSourceString( context, target );
100 
101                 if ( ( ASTProperty.class.isInstance( children[i] ) || ASTMethod.class.isInstance( children[i] )
102                     || ASTSequence.class.isInstance( children[i] ) || ASTChain.class.isInstance( children[i] ) )
103                     && value != null && value.trim().length() > 0 )
104                 {
105 
106                     String pre = null;
107                     if ( ASTMethod.class.isInstance( children[i] ) )
108                     {
109                         pre = (String) context.get( "_currentChain" );
110                     }
111 
112                     if ( pre == null )
113                     {
114                         pre = "";
115                     }
116 
117                     String cast = (String) context.remove( ExpressionCompiler.PRE_CAST );
118                     if ( cast == null )
119                     {
120                         cast = "";
121                     }
122 
123                     value =
124                         cast + ExpressionCompiler.getRootExpression( children[i], context.getRoot(), context ) + pre
125                             + value;
126                 }
127 
128                 result.append( value );
129             }
130         }
131 
132         if ( parent != null && !NumericExpression.class.isAssignableFrom( parent.getClass() ) )
133         {
134             result.append( ")" );
135         }
136 
137         return result.toString();
138     }
139 
140     @Override
141     public String toSetSourceString( OgnlContext context, Object target )
142     {
143         StringBuilder sourceStringBuilder = new StringBuilder( parent == null ? "" : "(" );
144 
145         if ( ( children != null ) && ( children.length > 0 ) )
146         {
147             for ( int i = 0; i < children.length; ++i )
148             {
149                 if ( i > 0 )
150                 {
151                     sourceStringBuilder.append( " " ).append( getExpressionOperator( i ) ).append( ' ' );
152                 }
153 
154                 sourceStringBuilder.append( children[i].toSetSourceString( context, target ) );
155             }
156         }
157         if ( parent != null )
158         {
159             sourceStringBuilder.append( ")" );
160         }
161 
162         return sourceStringBuilder.toString();
163     }
164 }