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 GmbH & Co. KG, 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.flex.CmsFlexController; 031import org.opencms.loader.I_CmsResourceLoader; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsLog; 034import org.opencms.main.OpenCms; 035import org.opencms.util.CmsStringUtil; 036import org.opencms.xml.I_CmsXmlDocument; 037import org.opencms.xml.page.CmsXmlPageFactory; 038 039import java.util.List; 040import java.util.Locale; 041 042import javax.servlet.ServletRequest; 043import javax.servlet.jsp.tagext.BodyTagSupport; 044 045import org.apache.commons.logging.Log; 046 047/** 048 * Used to select various template elements form a JSP template that 049 * is included in another file.<p> 050 * 051 * @since 6.0.0 052 */ 053public class CmsJspTagTemplate extends BodyTagSupport { 054 055 /** Serial version UID required for safe serialization. */ 056 private static final long serialVersionUID = -3773247710025810438L; 057 058 /** The log object for this class. */ 059 private static final Log LOG = CmsLog.getLog(CmsJspTagTemplate.class); 060 061 /** Condition for element check. */ 062 private boolean m_checkall; 063 064 /** Condition for negative element check. */ 065 private boolean m_checknone; 066 067 /** Name of element. */ 068 private String m_element; 069 070 /** List of elements for element check. */ 071 private String m_elementlist; 072 073 /** 074 * Internal action method.<p> 075 * 076 * @param element the selected element 077 * @param elementlist list the list of elements to check 078 * @param checkall flag to indicate that all elements should be checked 079 * @param checknone flag to indicate that the check is done for nonexisting elements 080 * @param req the current request 081 * @return boolean <code>true</code> if this element should be inclued, <code>false</code> 082 * otherwise 083 */ 084 public static boolean templateTagAction( 085 String element, 086 String elementlist, 087 boolean checkall, 088 boolean checknone, 089 ServletRequest req) { 090 091 if (elementlist != null) { 092 093 CmsFlexController controller = CmsFlexController.getController(req); 094 String filename = controller.getCmsObject().getRequestContext().getUri(); 095 096 I_CmsXmlDocument content = null; 097 try { 098 content = CmsXmlPageFactory.unmarshal(controller.getCmsObject(), filename, req); 099 } catch (CmsException e) { 100 LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_DOCUMENT_UNMARSHAL_1, filename), e); 101 } 102 103 if (content != null) { 104 String absolutePath = controller.getCmsObject().getSitePath(content.getFile()); 105 // check the elements in the elementlist, if the check fails don't render the element 106 String[] elements = CmsStringUtil.splitAsArray(elementlist, ','); 107 boolean found = false; 108 for (int i = 0; i < elements.length; i++) { 109 String el = elements[i].trim(); 110 List<Locale> locales = content.getLocales(el); 111 Locale locale = null; 112 if ((locales != null) && (locales.size() != 0)) { 113 locale = OpenCms.getLocaleManager().getBestMatchingLocale( 114 controller.getCmsObject().getRequestContext().getLocale(), 115 OpenCms.getLocaleManager().getDefaultLocales(controller.getCmsObject(), absolutePath), 116 locales); 117 } 118 if ((locale != null) && content.hasValue(el, locale) && content.isEnabled(el, locale)) { 119 120 found = true; 121 if (!checkall) { 122 // found at least an element that is available 123 break; 124 } 125 } else { 126 if (checkall) { 127 // found at least an element that is not available 128 return false; 129 } 130 } 131 } 132 133 if (!found && !checknone) { 134 // no element found while checking for existing elements 135 return false; 136 } else if (found && checknone) { 137 // element found while checking for nonexisting elements 138 return false; 139 } 140 } 141 } 142 143 // otherwise, check if an element was defined and if its equal to the desired element 144 String param = req.getParameter(I_CmsResourceLoader.PARAMETER_ELEMENT); 145 return ((element == null) || (param == null) || (param.equals(element))); 146 } 147 148 /** 149 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 150 */ 151 @Override 152 public int doStartTag() { 153 154 if (templateTagAction(m_element, m_elementlist, m_checkall, m_checknone, pageContext.getRequest())) { 155 return EVAL_BODY_INCLUDE; 156 } else { 157 return SKIP_BODY; 158 } 159 } 160 161 /** 162 * Returns the selected element.<p> 163 * 164 * @return the selected element 165 */ 166 public String getElement() { 167 168 return m_element != null ? m_element : ""; 169 } 170 171 /** 172 * Returns the list of elements to check.<p> 173 * 174 * @return the list of elements 175 */ 176 public String getIfexists() { 177 178 return m_elementlist != null ? m_elementlist : ""; 179 } 180 181 /** 182 * Returns the list of elements to check.<p> 183 * 184 * @return the list of elements 185 */ 186 public String getIfexistsall() { 187 188 return m_elementlist != null ? m_elementlist : ""; 189 } 190 191 /** 192 * Returns the list of elements to check.<p> 193 * 194 * @return the list of elements 195 */ 196 public String getIfexistsnone() { 197 198 return m_elementlist != null ? m_elementlist : ""; 199 } 200 201 /** 202 * Returns the list of elements to check.<p> 203 * 204 * @return the list of elements 205 */ 206 public String getIfexistsone() { 207 208 return m_elementlist != null ? m_elementlist : ""; 209 } 210 211 /** 212 * @see javax.servlet.jsp.tagext.Tag#release() 213 */ 214 @Override 215 public void release() { 216 217 super.release(); 218 m_element = null; 219 } 220 221 /** 222 * Sets the element target.<p> 223 * 224 * @param element the target to set 225 */ 226 public void setElement(String element) { 227 228 if (element != null) { 229 m_element = element.toLowerCase(); 230 } 231 } 232 233 /** 234 * Sets the list of elements to check.<p> 235 * 236 * @param elements the list of elements 237 */ 238 public void setIfexists(String elements) { 239 240 if (elements != null) { 241 m_elementlist = elements; 242 m_checkall = false; 243 m_checknone = false; 244 } 245 } 246 247 /** 248 * Sets the list of elements to check.<p> 249 * 250 * @param elements the list of elements 251 */ 252 public void setIfexistsall(String elements) { 253 254 if (elements != null) { 255 m_elementlist = elements; 256 m_checkall = true; 257 m_checknone = false; 258 } 259 } 260 261 /** 262 * Sets the list of elements to check.<p> 263 * 264 * @param elements the list of elements 265 */ 266 public void setIfexistsnone(String elements) { 267 268 if (elements != null) { 269 m_elementlist = elements; 270 m_checkall = false; 271 m_checknone = true; 272 } 273 } 274 275 /** 276 * Sets the list of elements to check.<p> 277 * 278 * @param elements the list of elements 279 */ 280 public void setIfexistsone(String elements) { 281 282 if (elements != null) { 283 m_elementlist = elements; 284 m_checkall = false; 285 m_checknone = false; 286 } 287 } 288}