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  import org.apache.commons.ognl.enhance.OrderedReturn;
24  
25  /**
26   * $Id: ASTSequence.java 1194869 2011-10-29 11:10:16Z mcucchiara $
27   * @author Luke Blanshard (blanshlu@netscape.net)
28   * @author Drew Davidson (drew@ognl.org)
29   */
30  public class ASTSequence
31      extends SimpleNode
32      implements NodeType, OrderedReturn
33  {
34      private Class getterClass;
35  
36      private String lastExpression;
37  
38      private String coreExpression;
39  
40      public ASTSequence( int id )
41      {
42          super( id );
43      }
44  
45      public ASTSequence( OgnlParser p, int id )
46      {
47          super( p, id );
48      }
49  
50      public void jjtClose()
51      {
52          flattenTree();
53      }
54  
55      protected Object getValueBody( OgnlContext context, Object source )
56          throws OgnlException
57      {
58          Object result = null;
59          for ( int i = 0; i < children.length; ++i )
60          {
61              result = children[i].getValue( context, source );
62          }
63  
64          return result; // The result is just the last one we saw.
65      }
66  
67      protected void setValueBody( OgnlContext context, Object target, Object value )
68          throws OgnlException
69      {
70          int last = children.length - 1;
71          for ( int i = 0; i < last; ++i )
72          {
73              children[i].getValue( context, target );
74          }
75          children[last].setValue( context, target, value );
76      }
77  
78      public Class getGetterClass()
79      {
80          return getterClass;
81      }
82  
83      public Class getSetterClass()
84      {
85          return null;
86      }
87  
88      public String getLastExpression()
89      {
90          return lastExpression;
91      }
92  
93      public String getCoreExpression()
94      {
95          return coreExpression;
96      }
97  
98      public String toSetSourceString( OgnlContext context, Object target )
99      {
100         return "";
101     }
102 
103     public String toGetSourceString( OgnlContext context, Object target )
104     {
105         String result = "";
106 
107         NodeType lastType = null;
108 
109         for ( int i = 0; i < children.length; ++i )
110         {
111             // System.out.println("astsequence child : " + _children[i].getClass().getName());
112             String seqValue = children[i].toGetSourceString( context, target );
113 
114             if ( ( i + 1 ) < children.length && ASTOr.class.isInstance( children[i] ) )
115             {
116                 seqValue = "(" + seqValue + ")";
117             }
118 
119             if ( i > 0 && ASTProperty.class.isInstance( children[i] ) && seqValue != null
120                 && seqValue.trim().length() > 0 )
121             {
122                 String pre = (String) context.get( "_currentChain" );
123                 if ( pre == null )
124                 {
125                     pre = "";
126                 }
127                 
128                 seqValue =
129                     ExpressionCompiler.getRootExpression( children[i], context.getRoot(), context ) + pre + seqValue;
130                 context.setCurrentAccessor( context.getRoot().getClass() );
131             }
132 
133             if ( ( i + 1 ) >= children.length )
134             {
135                 coreExpression = result;
136                 lastExpression = seqValue;
137             }
138 
139             if ( seqValue != null && seqValue.trim().length() > 0 && ( i + 1 ) < children.length )
140             {
141                 result += seqValue + ";";
142             }
143             else if ( seqValue != null && seqValue.trim().length() > 0 )
144             {
145                 result += seqValue;
146             }
147             // set last known type from last child with a type
148 
149             if ( NodeType.class.isInstance( children[i] ) && ( (NodeType) children[i] ).getGetterClass() != null )
150             {
151                 lastType = (NodeType) children[i];
152             }
153         }
154 
155         if ( lastType != null )
156         {
157             getterClass = lastType.getGetterClass();
158         }
159 
160         return result;
161     }
162     
163     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
164         throws OgnlException
165     {
166         return visitor.visit( this, data );
167     }
168 }