001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.jsp; 029 030import org.opencms.util.CmsStringUtil; 031 032import java.util.Arrays; 033import java.util.Collections; 034import java.util.List; 035 036import javax.servlet.jsp.PageContext; 037import javax.servlet.jsp.tagext.BodyTagSupport; 038 039/** 040 * Parent for body tags that require support for setting scoped variables to the JSP page context.<p> 041 * 042 * @since 7.0.2 043 */ 044public class CmsJspScopedVarBodyTagSuport extends BodyTagSupport { 045 046 /** The scopes supported by the page context. */ 047 private static final String[] SCOPES = {"page", "request", "session", "application"}; 048 049 /** The scopes supported by the page context as a list. */ 050 private static final List<String> SCOPES_LIST = Collections.unmodifiableList(Arrays.asList(SCOPES)); 051 052 /** Serial version UID required for safe serialization. */ 053 private static final long serialVersionUID = 6521418315921327927L; 054 055 /** The scope under which the content info is saved in the page context. */ 056 private int m_scope = PageContext.PAGE_SCOPE; 057 058 /** The name of the variable under which the content info bean should be saved in the page context. */ 059 private String m_variable; 060 061 /** 062 * Returns the int value of the specified scope string.<p> 063 * 064 * The default value is {@link PageContext#PAGE_SCOPE}.<p> 065 * 066 * @param scope the string name of the desired scope, e.g. "application", "request" 067 * @return the int value of the specified scope string 068 */ 069 protected static int getScopeAsInt(String scope) { 070 071 int scopeValue; 072 switch (SCOPES_LIST.indexOf(scope)) { 073 case 3: 074 // application 075 scopeValue = PageContext.APPLICATION_SCOPE; 076 break; 077 case 2: 078 // session 079 scopeValue = PageContext.SESSION_SCOPE; 080 break; 081 case 1: 082 // request 083 scopeValue = PageContext.REQUEST_SCOPE; 084 break; 085 default: 086 // page 087 scopeValue = PageContext.PAGE_SCOPE; 088 break; 089 } 090 return scopeValue; 091 } 092 093 /** 094 * Returns the String value of the specified scope integer.<p> 095 * 096 * Valid values for the scope int parameter are 1 to 4 only.<p> 097 * 098 * @param scope integer that describes the scope according to {@link #getScopeInt()}.<p> 099 * 100 * @return the String value of the specified scope integer 101 */ 102 protected static String getScopeAsString(int scope) { 103 104 if ((scope <= 0) || (scope > 4)) { 105 // return default scope if int is outside valid rage 106 return SCOPES[0]; 107 } 108 return SCOPES[scope - 1]; 109 } 110 111 /** 112 * Returns the scope under which the content access bean is saved in the page context.<p> 113 * 114 * @return the scope under which the content access bean is saved in the page context 115 */ 116 public String getScope() { 117 118 return getScopeAsString(m_scope); 119 } 120 121 /** 122 * Returns the name of the variable under which the content access bean is saved in the page context.<p> 123 * 124 * @return the name of the variable under which the content access bean is saved in the page context 125 */ 126 public String getVar() { 127 128 return m_variable; 129 } 130 131 /** 132 * @see javax.servlet.jsp.tagext.Tag#release() 133 */ 134 @Override 135 public void release() { 136 137 m_variable = null; 138 m_scope = PageContext.PAGE_SCOPE; 139 super.release(); 140 } 141 142 /** 143 * Sets the scope under which the content access bean is saved in the page context.<p> 144 * 145 * @param scope the scope under which the content access bean is saved in the page context 146 */ 147 public void setScope(String scope) { 148 149 if (CmsStringUtil.isNotEmpty(scope)) { 150 scope = scope.trim().toLowerCase(); 151 m_scope = getScopeAsInt(scope); 152 } else { 153 // empty scope, use default "page" scope 154 m_scope = PageContext.PAGE_SCOPE; 155 } 156 } 157 158 /** 159 * Sets the name of the variable under which the content access bean is saved in the page context.<p> 160 * 161 * @param var the name of the variable under which the content access bean is saved in the page context 162 */ 163 public void setVar(String var) { 164 165 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(var)) { 166 m_variable = var.trim(); 167 } 168 } 169 170 /** 171 * Returns the scope as int usable for setting the JSP page context 172 * with {@link PageContext#setAttribute(String, Object, int)}.<p> 173 * 174 * @return the scope as int usable for setting the JSP page context 175 */ 176 protected int getScopeInt() { 177 178 return m_scope; 179 } 180 181 /** 182 * Returns <code>true</code> in case the scoped variable has been set for this Tag.<p> 183 * 184 * @return <code>true</code> in case the scoped variable has been set for this Tag 185 */ 186 protected boolean isScopeVarSet() { 187 188 return CmsStringUtil.isNotEmpty(getVar()); 189 } 190 191 /** 192 * Stores the provided Object as attribute in the JSP page context.<p> 193 * 194 * The values of {@link #getVar()} and {@link #getScope()} are used to determine how the Object is stored.<p> 195 * 196 * @param obj the Object to store in the JSP page context 197 */ 198 protected void storeAttribute(Object obj) { 199 200 storeAttribute(getVar(), obj); 201 } 202 203 /** 204 * Stores the provided Object as attribute with the provided name in the JSP page context.<p> 205 * 206 * The value of {@link #getScope()} is used to determine how the Object is stored.<p> 207 * 208 * @param name the name of the attribute to store the Object in 209 * @param obj the Object to store in the JSP page context 210 */ 211 protected void storeAttribute(String name, Object obj) { 212 213 pageContext.setAttribute(name, obj, getScopeInt()); 214 } 215}