View Javadoc

1   /*
2    * Copyright 2002-2006 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 398190 2006-04-29 16:04:10Z dion $
30   */
31  public class ASTEmptyFunction extends SimpleNode {
32      /***
33       * Create the node given an id.
34       * 
35       * @param id node id.
36       */
37      public ASTEmptyFunction(int id) {
38          super(id);
39      }
40  
41      /***
42       * Create a node with the given parser and id.
43       * 
44       * @param p a parser.
45       * @param id node id.
46       */
47      public ASTEmptyFunction(Parser p, int id) {
48          super(p, id);
49      }
50  
51      /*** {@inheritDoc} */
52      public Object jjtAccept(ParserVisitor visitor, Object data) {
53          return visitor.visit(this, data);
54      }
55  
56      /*** {@inheritDoc} */
57      public Object value(JexlContext jc) throws Exception {
58          SimpleNode sn = (SimpleNode) jjtGetChild(0);
59  
60          /*
61           * I can't believe this
62           */
63  
64          Object o = sn.value(jc);
65  
66          if (o == null) {
67              return Boolean.TRUE;
68          }
69  
70          if (o instanceof String && "".equals(o)) {
71              return Boolean.TRUE;
72          }
73  
74          if (o.getClass().isArray() && ((Object[]) o).length == 0) {
75              return Boolean.TRUE;
76          }
77  
78          if (o instanceof Collection && ((Collection) o).isEmpty()) {
79              return Boolean.TRUE;
80          }
81  
82          /*
83           * Map isn't a collection
84           */
85          if (o instanceof Map && ((Map) o).isEmpty()) {
86              return Boolean.TRUE;
87          }
88  
89          return Boolean.FALSE;
90      }
91  }