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.PageContext;
  18. /**
  19. * Utility class to transform Strings to scopes:<br>
  20. * <code>page</code> will be transformed to
  21. * {@link javax.servlet.jsp.PageContext#PAGE_SCOPE PageContext.PAGE_SCOPE}
  22. * <code>request</code> will be transformed to
  23. * {@link javax.servlet.jsp.PageContext#REQUEST_SCOPE PageContext.REQUEST_SCOPE}<br>
  24. * <code>session</code> will be transformed to
  25. * {@link javax.servlet.jsp.PageContext#SESSION_SCOPE PageContext.SESSION_SCOPE}<br>
  26. * <code>application</code> will be transformed to
  27. * {@link javax.servlet.jsp.PageContext#APPLICATION_SCOPE PageContext.APPLICATION_SCOPE}<br>
  28. *
  29. * @author Alef Arendsen
  30. */
  31. public abstract class TagUtils {
  32. /** constant identifying the page scope String */
  33. public static final String SCOPE_PAGE = "page";
  34. /** constant identifying the request scope String */
  35. public static final String SCOPE_REQUEST = "request";
  36. /** constant identifying the session scope String */
  37. public static final String SCOPE_SESSION = "session";
  38. /** constant identifying the application scope String */
  39. public static final String SCOPE_APPLICATION = "application";
  40. /**
  41. * Determines the scope for a given input String. If the string does not match
  42. * 'request', 'session', 'page' or 'application', the method will return
  43. * PageContext.PAGE_SCOPE.
  44. * @param scope the string to inspect
  45. * @return the scope found, or PageContext.PAGE_SCOPE if no scope matched.
  46. * @throws java.lang.IllegalArgumentException if the scope is null
  47. */
  48. public static int getScope(String scope) {
  49. if (scope == null) {
  50. throw new IllegalArgumentException(
  51. "Scope to search for cannot be null");
  52. }
  53. else if (scope.equals(SCOPE_REQUEST)) {
  54. return PageContext.REQUEST_SCOPE;
  55. }
  56. else if (scope.equals(SCOPE_SESSION)) {
  57. return PageContext.SESSION_SCOPE;
  58. }
  59. else if (scope.equals(SCOPE_APPLICATION)) {
  60. return PageContext.APPLICATION_SCOPE;
  61. }
  62. else {
  63. return PageContext.PAGE_SCOPE;
  64. }
  65. }
  66. }