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.file.CmsObject; 031import org.opencms.flex.CmsFlexController; 032import org.opencms.i18n.CmsLocaleManager; 033import org.opencms.i18n.CmsMessageContainer; 034import org.opencms.jsp.util.CmsJspContentAccessBean; 035import org.opencms.main.OpenCms; 036import org.opencms.util.CmsStringUtil; 037import org.opencms.xml.I_CmsXmlDocument; 038 039import java.util.Locale; 040 041import javax.servlet.jsp.JspException; 042import javax.servlet.jsp.JspTagException; 043import javax.servlet.jsp.tagext.Tag; 044 045/** 046 * Used to access XML content item information from the current open <code><cms:contentload></code> 047 * tag using JSP page context and the JSP EL.<p> 048 * 049 * The tag will create an instance of a {@link CmsJspContentAccessBean} that is stored in the selected context. 050 * Use the options provided by the bean to access the XML content directly.<p> 051 * 052 * For example together with the JSTL, use this tag inside an open tag like this:<pre> 053 * <cms:contentload ... > 054 * <cms:contentaccess var="myVarName" val="myValueVarName" scope="page" /> 055 * ... other code ... 056 * </cms:contentload></pre> 057 * 058 * @since 7.0.2 059 */ 060public class CmsJspTagContentAccess extends CmsJspScopedVarBodyTagSuport { 061 062 /** Serial version UID required for safe serialization. */ 063 private static final long serialVersionUID = -9015874900596113856L; 064 065 /** Locale of the content node element to show. */ 066 private Locale m_locale; 067 068 /** Optional name for the attribute that provides direct access to the content value map. */ 069 private String m_value; 070 071 /** 072 * @see javax.servlet.jsp.tagext.Tag#doEndTag() 073 */ 074 @Override 075 public int doEndTag() { 076 077 if (OpenCms.getSystemInfo().getServletContainerSettings().isReleaseTagsAfterEnd()) { 078 // need to release manually, JSP container may not call release as required (happens with Tomcat) 079 release(); 080 } 081 return EVAL_PAGE; 082 } 083 084 /** 085 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 086 */ 087 @Override 088 public int doStartTag() throws JspException { 089 090 // get a reference to the parent "content container" class 091 Tag ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class); 092 if (ancestor == null) { 093 CmsMessageContainer errMsgContainer = Messages.get().container( 094 Messages.ERR_PARENTLESS_TAG_1, 095 "contentaccess"); 096 String msg = Messages.getLocalizedMessage(errMsgContainer, pageContext); 097 throw new JspTagException(msg); 098 } 099 // get the currently open content container 100 I_CmsXmlContentContainer contentContainer = (I_CmsXmlContentContainer)ancestor; 101 102 // get loaded content from content container 103 I_CmsXmlDocument xmlContent = contentContainer.getXmlDocument(); 104 105 // get the current users OpenCms context 106 CmsObject cms = CmsFlexController.getCmsObject(pageContext.getRequest()); 107 108 // get the selected Locale or use the default from the OpenCms request context 109 Locale locale = m_locale == null ? cms.getRequestContext().getLocale() : m_locale; 110 111 // initialize a new instance of a content access bean 112 CmsJspContentAccessBean bean = new CmsJspContentAccessBean(cms, locale, xmlContent); 113 114 // store the content access bean in the selected page context scope 115 storeAttribute(bean); 116 117 if (m_value != null) { 118 // if the optional "val" parameter has been set, store the value map of the content in the page context scope 119 storeAttribute(getVal(), bean.getValue()); 120 } 121 122 return SKIP_BODY; 123 } 124 125 /** 126 * Returns the locale.<p> 127 * 128 * @return the locale 129 */ 130 public String getLocale() { 131 132 return (m_locale != null) ? m_locale.toString() : ""; 133 } 134 135 /** 136 * Returns the name for the optional attribute that provides direct access to the content value map.<p> 137 * 138 * @return the name for the optional attribute that provides direct access to the content value map 139 */ 140 public String getVal() { 141 142 return m_value; 143 } 144 145 /** 146 * @see javax.servlet.jsp.tagext.Tag#release() 147 */ 148 @Override 149 public void release() { 150 151 m_locale = null; 152 m_value = null; 153 super.release(); 154 } 155 156 /** 157 * Sets the locale.<p> 158 * 159 * @param locale the locale to set 160 */ 161 public void setLocale(String locale) { 162 163 if (CmsStringUtil.isEmpty(locale)) { 164 m_locale = null; 165 } else { 166 m_locale = CmsLocaleManager.getLocale(locale); 167 } 168 } 169 170 /** 171 * Sets the name for the optional attribute that provides direct access to the content value map.<p> 172 * 173 * @param val the name for the optional attribute that provides direct access to the content value map 174 */ 175 public void setVal(String val) { 176 177 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) { 178 m_value = val.trim(); 179 } 180 } 181}