View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jexl.parser;
18  
19  import org.apache.commons.jexl.JexlContext;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  
24  /***
25   *  function to see if reference doesn't exist in context
26   *
27   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
28   *  @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
29   *  @version $Id: ASTEmptyFunction.java,v 1.6 2004/08/23 13:30:36 dion Exp $
30   */
31  public class ASTEmptyFunction extends SimpleNode
32  {
33      public ASTEmptyFunction(int id)
34      {
35          super(id);
36      }
37  
38      public ASTEmptyFunction(Parser p, int id)
39      {
40          super(p, id);
41      }
42  
43      /*** Accept the visitor. **/
44      public Object jjtAccept(ParserVisitor visitor, Object data)
45      {
46          return visitor.visit(this, data);
47      }
48  
49      public Object value(JexlContext jc)
50          throws Exception
51      {
52          SimpleNode sn = (SimpleNode) jjtGetChild(0);
53  
54          /*
55           * I can't believe this
56           */
57  
58          Object o = sn.value(jc);
59  
60          if (o == null)
61              return Boolean.TRUE;
62  
63          if (o instanceof String && "".equals((String) o))
64              return Boolean.TRUE;
65  
66          if (o.getClass().isArray() && ((Object[])o).length == 0)
67              return Boolean.TRUE;
68  
69          if (o instanceof Collection && ((Collection)o).isEmpty())
70              return Boolean.TRUE;
71  
72          /*
73           *  Map isn't a collection
74           */
75          if (o instanceof Map && ((Map)o).isEmpty())
76              return Boolean.TRUE;
77  
78          return Boolean.FALSE;
79      }
80  }