📄 cmsjspactionelement.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspActionElement.java,v $
* Date : $Date: 2006/10/26 12:25:35 $
* Version: $Revision: 1.27 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.jsp;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsProperty;
import org.opencms.flex.CmsFlexController;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.i18n.CmsMessages;
import org.opencms.loader.CmsImageScaler;
import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsSecurityException;
import org.opencms.staticexport.CmsLinkManager;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.editors.directedit.CmsDirectEditJspIncludeProvider;
import org.opencms.workplace.editors.directedit.CmsDirectEditMode;
import org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
/**
* Bean to be used in JSP scriptlet code that provides direct
* access to the functionality offered by the opencms taglib.<p>
*
* By instanciating a bean of this type and accessing the methods provided by
* the instance, all functionality of the OpenCms JSP taglib can be easily
* used from within JSP scriplet code.<p>
*
* Initialize this bean at the beginning of your JSP like this:
* <pre>
* <jsp:useBean id="cms" class="org.opencms.jsp.CmsJspActionElement">
* <% cms.init(pageContext, request, response); %>
* </jsp:useBean>
* </pre>
*
* You can also access the current users <code>{@link org.opencms.file.CmsObject}</code>
* by using <code>{@link org.opencms.jsp.CmsJspBean#getCmsObject()}</code>.<p>
*
* All exceptions that occur when calling any method of this class are catched
* and written to the log output only, so that a template still has a chance of
* working at least in some elements.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.27 $
*
* @since 6.0.0
*/
public class CmsJspActionElement extends CmsJspBean {
/** Error message in case bean was not properly initialized. */
// cannot use a string: At class-loading time the
// user request context for localization is not at hand.
public static final CmsMessageContainer NOT_INITIALIZED = Messages.get().container(
Messages.GUI_ERR_ACTIONELEM_NOT_INIT_0);
/** JSP navigation builder. */
private CmsJspNavBuilder m_navigation;
/**
* Empty constructor, required for every JavaBean.
*/
public CmsJspActionElement() {
super();
}
/**
* Constructor, with parameters.
*
* @param context the JSP page context object
* @param req the JSP request
* @param res the JSP response
*/
public CmsJspActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) {
super();
init(context, req, res);
}
/**
* Includes direct edit scriptlets, same as
* using the <code><cms:editable /></code> tag.<p>
*
* The configured default direct edit provider is used.<p>
*
* @param isEditable include scriptlets only if true
*
* @throws JspException if something goes wrong
*/
public void editable(boolean isEditable) throws JspException {
if (isEditable) {
CmsJspTagEditable.editableTagAction(getJspContext(), null, CmsDirectEditMode.AUTO, null);
}
}
/**
* Includes direct edit scriptlets, same as
* using the <code><cms:editable file="..." /></code>tag.<p>
*
* For backward compatibilty, this always uses the JSP include based direct edit provider<p>.
*
* @param isEditable include scriptlets only if true
* @param filename file with scriptlets
*
* @throws JspException if something goes wrong
*/
public void editable(boolean isEditable, String filename) throws JspException {
if (isEditable) {
CmsJspTagEditable.editableTagAction(
getJspContext(),
CmsDirectEditJspIncludeProvider.class.getName(),
CmsDirectEditMode.AUTO,
filename);
}
}
/**
* Includes direct edit scriptlets, same as
* using the <code><cms:editable provider="..." mode="..." file="..." /></code>tag.<p>
*
* @param provider the direct edit provider class name
* @param mode the direct edit mode to use
* @param filename file with scriptlets (may be <code>null</code>)
*
* @throws JspException if something goes wrong
*/
public void editable(String provider, String mode, String filename) throws JspException {
CmsJspTagEditable.editableTagAction(getJspContext(), provider, CmsDirectEditMode.valueOf(mode), filename);
}
/**
* Insert the end HTML for the direct edit buttons in manual mode (if required).<p>
*
* Same as closing the <code></cms:editable</code> tag after opening one in manual mode.<p>
*
* @param needsClose result of {@link #editableManualOpen()} should be the value for this parameter
*
* @throws JspException if something goes wrong
*/
public void editableManualClose(boolean needsClose) throws JspException {
if (needsClose) {
CmsJspTagEditable.editableTagAction(getJspContext(), null, CmsDirectEditMode.MANUAL, null);
}
}
/**
* Insert the start HTML for the direct edit buttons in manual mode.<p>
*
* Same as opening the <code><cms:editable mode="manual"></code> tag.<p>
*
* @return <code>true</code> if HTML was inserted that needs to be closed
*
* @throws JspException if something goes wrong
*/
public boolean editableManualOpen() throws JspException {
boolean result = false;
if (!CmsFlexController.isCmsOnlineRequest(getJspContext().getRequest())) {
// all this does NOT apply to the "online" project
I_CmsDirectEditProvider eb = CmsJspTagEditable.getDirectEditProvider(getJspContext());
if ((eb != null)) {
// check if the provider support manual placement of buttons
if (eb.isManual(CmsDirectEditMode.MANUAL)) {
// the provider supports manual placement of buttons
result = true;
CmsJspTagEditable.editableTagAction(getJspContext(), null, CmsDirectEditMode.MANUAL, null);
}
}
}
return result;
}
/**
* Returns the processed output of an OpenCms resource in a String.<p>
*
* @param target the target to process
* @return the processed output of an OpenCms resource in a String
*/
public String getContent(String target) {
return getContent(target, null, null);
}
/**
* Returns the processed output of an element within an OpenCms resource.<p>
*
* @param target the target to process
* @param element name of the element
* @param locale locale of the element
* @return the processed output
*/
public String getContent(String target, String element, Locale locale) {
I_CmsResourceLoader loader;
CmsFile file;
target = toAbsolute(target);
try {
file = getCmsObject().readFile(target);
loader = OpenCms.getResourceManager().getLoader(file);
} catch (ClassCastException e) {
// no loader implementation found
return CmsMessages.formatUnknownKey(e.getMessage());
} catch (CmsException e) {
// file might not exist or no read permissions
return CmsMessages.formatUnknownKey(e.getMessage());
}
try {
byte[] result = loader.dump(getCmsObject(), file, element, locale, getRequest(), getResponse());
return new String(result, getRequestContext().getEncoding());
} catch (UnsupportedEncodingException uee) {
// encoding unsupported
return CmsMessages.formatUnknownKey(uee.getMessage());
} catch (Throwable t) {
// any other exception, check for hidden root cause first
Throwable cause = CmsFlexController.getThrowable(getRequest());
if (cause == null) {
cause = t;
}
handleException(cause);
return CmsMessages.formatUnknownKey(cause.getMessage());
}
}
/**
* Generates an initialized instance of {@link CmsMessages} for
* convenient access to localized resource bundles.<p>
*
* @param bundleName the name of the ResourceBundle to use
* @param locale the locale to use for localization
*
* @return CmsMessages a message bundle initialized with the provided values
*/
public CmsMessages getMessages(String bundleName, Locale locale) {
return new CmsMessages(bundleName, locale);
}
/**
* Generates an initialized instance of {@link CmsMessages} for
* convenient access to localized resource bundles.<p>
*
* @param bundleName the name of the ResourceBundle to use
* @param language language indentificator for the locale of the bundle
* @return CmsMessages a message bundle initialized with the provided values
*/
public CmsMessages getMessages(String bundleName, String language) {
return getMessages(bundleName, language, "", "", null);
}
/**
* Generates an initialized instance of {@link CmsMessages} for
* convenient access to localized resource bundles.<p>
*
* @param bundleName the name of the ResourceBundle to use
* @param language language indentificator for the locale of the bundle
* @param defaultLanguage default for the language, will be used
* if language is null or empty String "", and defaultLanguage is not null
* @return CmsMessages a message bundle initialized with the provided values
*/
public CmsMessages getMessages(String bundleName, String language, String defaultLanguage) {
return getMessages(bundleName, language, "", "", defaultLanguage);
}
/**
* Generates an initialized instance of {@link CmsMessages} for
* convenient access to localized resource bundles.<p>
*
* @param bundleName the name of the ResourceBundle to use
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -