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. package org.apache.commons.jexl.parser;
  17. import org.apache.commons.jexl.JexlContext;
  18. import java.util.Collection;
  19. import java.util.Map;
  20. /**
  21. * function to see if reference doesn't exist in context
  22. *
  23. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  24. * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
  25. * @version $Id: ASTEmptyFunction.java,v 1.6 2004/08/23 13:30:36 dion Exp $
  26. */
  27. public class ASTEmptyFunction extends SimpleNode
  28. {
  29. public ASTEmptyFunction(int id)
  30. {
  31. super(id);
  32. }
  33. public ASTEmptyFunction(Parser p, int id)
  34. {
  35. super(p, id);
  36. }
  37. /** Accept the visitor. **/
  38. public Object jjtAccept(ParserVisitor visitor, Object data)
  39. {
  40. return visitor.visit(this, data);
  41. }
  42. public Object value(JexlContext jc)
  43. throws Exception
  44. {
  45. SimpleNode sn = (SimpleNode) jjtGetChild(0);
  46. /*
  47. * I can't believe this
  48. */
  49. Object o = sn.value(jc);
  50. if (o == null)
  51. return Boolean.TRUE;
  52. if (o instanceof String && "".equals((String) o))
  53. return Boolean.TRUE;
  54. if (o.getClass().isArray() && ((Object[])o).length == 0)
  55. return Boolean.TRUE;
  56. if (o instanceof Collection && ((Collection)o).isEmpty())
  57. return Boolean.TRUE;
  58. /*
  59. * Map isn't a collection
  60. */
  61. if (o instanceof Map && ((Map)o).isEmpty())
  62. return Boolean.TRUE;
  63. return Boolean.FALSE;
  64. }
  65. }