Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 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, 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.util;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.flex.CmsFlexController;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.i18n.CmsMessages;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsHtml2TextConverter;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.PropertyUtils;
/**
* Provides utility methods to be used as functions from a JSP with the EL.
*
* @since 7.0.2
*
* @see CmsJspContentAccessBean
*/
public final class CmsJspElFunctions {
/**
* Hide the public constructor.
*/
private CmsJspElFunctions() {
// NOOP
}
/**
* Returns an OpenCms user context created from an Object.
*
*
*
If the input is already a {@link CmsObject}, it is casted and returned unchanged.
*
If the input is a {@link ServletRequest}, the OpenCms user context is read from the request context.
*
If the input is a {@link PageContext}, the OpenCms user context is read from the request of the page context.
*
Otherwise the input is converted to a String which should be a user name, and creation of a OpenCms
* user context with this name is attempted. Please note that this will only work if the user name is
* either the "Guest" user or the "Export" user.
*
If no valid OpenCms user context could be created with all of the above, then a new user context for
* the "Guest" user is created.
*
*
* @param input the input to create an OpenCms user context from
*
* @return an OpenCms user context created from an Object
*/
public static CmsObject convertCmsObject(Object input) {
CmsObject result;
if (input instanceof CmsObject) {
result = (CmsObject)input;
} else if (input instanceof ServletRequest) {
result = CmsFlexController.getCmsObject((ServletRequest)input);
} else if (input instanceof PageContext) {
result = CmsFlexController.getCmsObject(((PageContext)input).getRequest());
} else {
try {
// try to use the given name as user name
result = OpenCms.initCmsObject(String.valueOf(input));
// try to set the right site root
ServletRequest req = convertRequest(input);
if (req instanceof HttpServletRequest) {
result.getRequestContext().setSiteRoot(
OpenCms.getSiteManager().matchRequest((HttpServletRequest)req).getSiteRoot());
}
} catch (@SuppressWarnings("unused") CmsException e) {
result = null;
}
}
if (result == null) {
try {
result = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
// try to set the right site root
ServletRequest req = convertRequest(input);
if (req instanceof HttpServletRequest) {
result.getRequestContext().setSiteRoot(
OpenCms.getSiteManager().matchRequest((HttpServletRequest)req).getSiteRoot());
}
} catch (@SuppressWarnings("unused") CmsException e1) {
// this should never fail since we can always create a "Guest" user
}
}
return result;
}
/**
* Returns a Date created from an Object.
*
*
*
The Object is first checked if it is a {@link Date} already, if so it is casted and returned unchanged.
*
If not, the input is checked if it is a {@link Long}, and if so the Date is created from the Long value.
*
If it's not a Date and not a Long, the Object is transformed to a String and then it's tried
* to parse a Long out of the String.
*
If this fails, it is tried to parse as a Date using the
* default date formatting.
*
If this also fails, a new Date is returned that has been initialized with 0.
*
*
* @param input the Object to create a Date from
*
* @return a Date created from the given Object
*/
public static Date convertDate(Object input) {
Date result;
if (input instanceof Date) {
result = (Date)input;
} else if (input instanceof Long) {
result = new Date(((Long)input).longValue());
} else {
String str = String.valueOf(input);
try {
// treat the input as a String
long l = Long.parseLong(str);
result = new Date(l);
} catch (@SuppressWarnings("unused") NumberFormatException e) {
try {
// try to parse String as a Date
result = DateFormat.getDateInstance().parse(str);
} catch (@SuppressWarnings("unused") ParseException e1) {
result = null;
}
if (result == null) {
// use default date if parsing fails
result = new Date(0);
}
}
}
return result;
}
/**
* Returns a list of attribute values specified by the attribute name of the items of the given list.
*
* @param input the list of objects to obtain the attribute values from
* @param attributeName the name of the attribute to obtain
* @return a list of attributes specified by the attribute name of the items of the given list
*/
public static List