1. /*
  2. * Copyright 2002-2004 the original author or authors.
  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.springframework.web.util;
  17. import javax.servlet.jsp.JspException;
  18. import javax.servlet.jsp.PageContext;
  19. import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
  20. /**
  21. * Convenience methods for easy access to the JSP Expression Language
  22. * evaluator of Jakarta's JSTL implementation.
  23. *
  24. * <p>The evaluation methods check if the value contains "${"
  25. * before invoking the EL evaluator, treating the value as "normal"
  26. * expression (i.e. conventional String) else.
  27. *
  28. * <p>Note: The evaluation methods do not have a runtime dependency
  29. * on Jakarta's JSTL implementation, as long as they don't receive
  30. * actual EL expressions.
  31. *
  32. * @author Juergen Hoeller
  33. * @author Alef Arendsen
  34. * @since 11.07.2003
  35. */
  36. public abstract class ExpressionEvaluationUtils {
  37. /**
  38. * Check if the given expression value is an EL expression.
  39. * @param value the expression to check
  40. * @return <code>true</code> if the expression is an EL expression,
  41. * <code>false</code> otherwise
  42. */
  43. public static boolean isExpressionLanguage(String value) {
  44. return (value != null && value.indexOf("${") != -1);
  45. }
  46. /**
  47. * Evaluate the given expression to an Object, be it EL or a conventional String.
  48. * @param attrName name of the attribute (typically a JSP tag attribute)
  49. * @param attrValue value of the attribute
  50. * @param resultClass class that the result should have (String, Integer, Boolean)
  51. * @param pageContext current JSP PageContext
  52. * @return the result of the evaluation
  53. * @throws JspException in case of parsing errors
  54. */
  55. public static Object evaluate(String attrName, String attrValue, Class resultClass, PageContext pageContext)
  56. throws JspException {
  57. if (isExpressionLanguage(attrValue))
  58. return ExpressionEvaluationHelper.evaluate(attrName, attrValue, resultClass, pageContext);
  59. else
  60. return attrValue;
  61. }
  62. /**
  63. * Evaluate the given expression to a String, be it EL or a conventional String.
  64. * @param attrName name of the attribute (typically a JSP tag attribute)
  65. * @param attrValue value of the attribute
  66. * @param pageContext current JSP PageContext
  67. * @return the result of the evaluation
  68. * @throws JspException in case of parsing errors
  69. */
  70. public static String evaluateString(String attrName, String attrValue, PageContext pageContext)
  71. throws JspException {
  72. if (isExpressionLanguage(attrValue))
  73. return (String) ExpressionEvaluationHelper.evaluate(attrName, attrValue, String.class, pageContext);
  74. else
  75. return attrValue;
  76. }
  77. /**
  78. * Evaluate the given expression to an integer, be it EL or a conventional String.
  79. * @param attrName name of the attribute (typically a JSP tag attribute)
  80. * @param attrValue value of the attribute
  81. * @param pageContext current JSP PageContext
  82. * @return the result of the evaluation
  83. * @throws JspException in case of parsing errors
  84. */
  85. public static int evaluateInteger(String attrName, String attrValue, PageContext pageContext)
  86. throws JspException {
  87. if (isExpressionLanguage(attrValue))
  88. return ((Integer) ExpressionEvaluationHelper.evaluate(attrName, attrValue, Integer.class, pageContext)).intValue();
  89. else
  90. return Integer.parseInt(attrValue);
  91. }
  92. /**
  93. * Evaluate the given expression to a boolean, be it EL or a conventional String.
  94. * @param attrName name of the attribute (typically a JSP tag attribute)
  95. * @param attrValue value of the attribute
  96. * @param pageContext current JSP PageContext
  97. * @return the result of the evaluation
  98. * @throws JspException in case of parsing errors
  99. */
  100. public static boolean evaluateBoolean(String attrName, String attrValue, PageContext pageContext)
  101. throws JspException {
  102. if (isExpressionLanguage(attrValue))
  103. return ((Boolean) ExpressionEvaluationHelper.evaluate(attrName, attrValue, Boolean.class, pageContext)).booleanValue();
  104. else
  105. return Boolean.valueOf(attrValue).booleanValue();
  106. }
  107. /**
  108. * Actual invocation of the Jakarta ExpressionEvaluatorManager.
  109. * In separate inner class to avoid runtime dependency on Jakarta's
  110. * JSTL implementation, for evaluation of non-EL expressions.
  111. */
  112. private static class ExpressionEvaluationHelper {
  113. private static Object evaluate(String attrName, String attrValue, Class resultClass, PageContext pageContext)
  114. throws JspException {
  115. return ExpressionEvaluatorManager.evaluate(attrName, attrValue, resultClass, pageContext);
  116. }
  117. }
  118. }