skinutils.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,157 行 · 第 1/3 页
JAVA
1,157 行
/** * $RCSfile: SkinUtils.java,v $ * $Revision: 1.20 $ * $Date: 2002/07/03 02:50:53 $ * * Copyright (C) 1999-2002 Jive Software. All rights reserved. * * This software is the proprietary information of Jive Software. * Use is subject to license terms. */package com.jivesoftware.forum.util;import com.jivesoftware.forum.*;import com.jivesoftware.forum.database.DbForumFactory;import com.jivesoftware.util.CacheFactory;import com.jivesoftware.util.StringUtils;import javax.servlet.http.*;import java.text.*;import java.util.*;/** * A collection of utility methods for use in Jive Skins. Because these * methods make skin development much easier, skin authors should study them * carefully.<p> * * Five major areas of functionality are provided:<p><ol> * <li> Methods that get and set cookie values. * <ul> * <li>{@link #getCookie(HttpServletRequest, String)} * <li>{@link #deleteCookie(HttpServletResponse, Cookie)} * <li>{@link #setCookie(HttpServletResponse, String, String)} * <li>{@link #setCookie(HttpServletResponse, String, String, int)} * </ul> * <p> * <li> Localization methods. * <ul> * <li>{@link #getResourceBundle(String, Locale)} * <li>{@link #getLocalizedString(String)} * <li>{@link #getLocalizedString(String, List)} * <li>{@link #getLocalizedString(String, Locale)} * <li>{@link #getLocalizedString(String, Locale, List)} * <li>{@link #getLocalizedNumber(long)} * <li>{@link #getLocalizedNumber(long, Locale)} * <li>{@link #getLocalizedNumber(double)} * <li>{@link #getLocalizedNumber(double, Locale)} * <li>{@link #formatDate(HttpServletRequest, User, Date)} * </ul> * <p> * <li> TimeZone methods. * <ul> * <li>{@link #getTimeZoneList()} * <li>{@link #getTimeZone(HttpServletRequest, User)} * </ul> * <p> * <li> Password reset methods. * <ul> * <li>{@link #emailResetPasswordInstructions(String, HttpServletRequest)} * <li>{@link #isValidPasswordToken(long, String)} * <li>{@link #resetUserPassword(long, String, String)} * </ul> * <p> * <li> Other methods. * <ul> * <li>{@link #quoteOriginal(String, String, int)} * <li>{@link #isNew(Forum, Date)} * <li>{@link #isNew(ForumThread, Date)} * <li>{@link #isNew(ForumMessage, Date)} * <li>{@link #getLastVisited(HttpServletRequest, HttpServletResponse)} * </ul> * </ol> */public class SkinUtils { /** * Name of the last visited token (stored in the user's session) */ private static final String JIVE_LASTVISITED_TOKEN = "jive.user.lastvisited"; /** * Name of the password reset token (stored as an extended user property). */ private static final String JIVE_PASSWORDRESET_TOKEN = "jive.passwordreset.token"; /** * Name of extended user property holding the timestamp that the password * reset token was created. */ private static final String JIVE_PASSWORDRESET_TIMESTAMP = "jive.passwordreset.timestamp"; /** * Name of extended user property holding the timestamp that the last password * reset email was sent. */ private static final String JIVE_PASSWORDRESET_LAST_SENT = "jive.passwordreset.last_sent"; // Days of the week private static final String[] DAYS_OF_WEEK = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"}; // Months of the year private static final String[] MONTHS_OF_YEAR = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"}; private static String [][] timeZoneList = null; private static Object timeZoneLock = new Object(); // A cache of DateFormat objects private static Map dateFormatCache = new HashMap(); // A reuseable global calendar object private static Calendar globalCal = Calendar.getInstance(); // The basename to use for looking up the appropriate resource bundles private static final String resoureBaseName = "jive_i18n"; /** * Returns the specified cookie, or <code>null</code> if the cookie * does not exist. Note: because of the way that cookies are implemented * it's possible for multiple cookies with the same name to exist (but with * different domain values). This method will return the first cookie that * has a name match. * * @param request the HttpServletRequest object, known as "request" in a * JSP page. * @param name the name of the cookie. * @return the Cookie object if it exists, otherwise <code>null</code>. */ public static Cookie getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); // Return null if there are no cookies or the name is invalid. if(cookies == null || name == null || name.length() == 0) { return null; } // Otherwise, we do a linear scan for the cookie. for (int i = 0; i < cookies.length; i++) { if(cookies[i].getName().equals(name) ) { return cookies[i]; } } return null; } /** * Deletes the specified cookie. * * @param response the HttpServletResponse object in a JSP page * @param cookie the cookie object to be deleted. */ public static void deleteCookie(HttpServletResponse response, Cookie cookie) { if (cookie != null) { // Invalidate the cookie cookie.setPath("/"); cookie.setMaxAge(0); response.addCookie(cookie); } } /** * Stores a value in a cookie. This cookie will persist for 30 days. * * @param response the HttpServletResponse object, known as "response" in a * JSP page. * @param name a name to identify the cookie. * @param value the value to store in the cookie. */ public static void setCookie(HttpServletResponse response, String name, String value) { // Save the cookie value for 1 month setCookie(response, name, value, 60*60*24*30); } /** * Stores a value in a cookie. This cookie will persist for the amount * specified in the <tt>saveTime</tt> parameter. * * @param response the HttpServletResponse object, known as "response" in a * JSP page. * @param name a name to identify the cookie. * @param value the value to store in the cookie. * @param maxAge the time (in seconds) this cookie should live. */ public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) { // Check to make sure the new value is not null (appservers like Tomcat // 4 blow up if the value is null). if (value == null) { value = ""; } Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAge); cookie.setPath("/"); response.addCookie(cookie); } /** * Returns the specified resource bundle, which is a properties file * that aids in localization of skins. This method is handy since it * uses the class loader that other Jive classes are loaded from (hence, * it can load bundles that are stored in jive.jar). * * @param baseName the name of the resource bundle to load. * @param locale the desired Locale. * @return the specified resource bundle, if it exists. */ public static ResourceBundle getResourceBundle(String baseName, Locale locale) { return ResourceBundle.getBundle(baseName, locale); } /** * Returns an internationalized string loaded from a resource bundle. * The locale used will be the locale specified by JiveGlobals.getLocale(). * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @return the localized string. */ public static String getLocalizedString(String key) { return getLocalizedString(key, JiveGlobals.getLocale(), null); } /** * Returns an internationalized string loaded from a resource bundle using * the passed in Locale. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param locale the locale to use for retrieving the appropriate * locale-specific string. * @return the localized string. */ public static String getLocalizedString(String key, Locale locale) { return getLocalizedString(key, locale, null); } /** * Returns an internationalized string loaded from a resource bundle using * the locale specified by JiveGlobals.getLocale() substituting the passed * in arguments. Substitution is handled using the * {@link java.text.MessageFormat} class. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param arguments a list of objects to use which are formatted, then * inserted into the pattern at the appropriate places. * @return the localized string. */ public static String getLocalizedString(String key, List arguments) { return getLocalizedString(key, JiveGlobals.getLocale(), arguments); } /** * Returns an internationalized string loaded from a resource bundle using * the passed in Locale substituting the passed in arguments. Substitution * is handled using the {@link java.text.MessageFormat} class. * * @param key the key to use for retrieving the string from the * appropriate resource bundle. * @param locale the locale to use for retrieving the appropriate * locale-specific string. * @param arguments a list of objects to use which are formatted, then * inserted into the pattern at the appropriate places. * @return the localized string. */ public static String getLocalizedString(String key, Locale locale, List arguments) { if (key == null) { throw new NullPointerException("Key cannot be null"); } if (locale == null) { locale = JiveGlobals.getLocale(); } String value = ""; // The jdk caches resource bundles on it's own, so we won't bother. ResourceBundle bundle = ResourceBundle.getBundle(resoureBaseName, locale); // See if the bundle has a value try { value = bundle.getString(key); } catch (java.util.MissingResourceException mre) { System.err.println("Missing resource for key: " + key + " in locale " + locale.toString()); value = ""; } // perform argument substitutions if (arguments != null) { MessageFormat messageFormat = new MessageFormat(""); messageFormat.setLocale(bundle.getLocale()); messageFormat.applyPattern(value); try { value = messageFormat.format(arguments.toArray()); } catch (IllegalArgumentException e) { System.err.println("Unable to format resource string for key: " + key + ", argument type not supported"); value = ""; } } return value; } /** * */ public static String getLocalizedNumber(long number) { return NumberFormat.getInstance().format(number); } /** * */ public static String getLocalizedNumber(long number, Locale locale) { return NumberFormat.getInstance(locale).format(number); } /** * */ public static String getLocalizedNumber(double number) { return NumberFormat.getInstance().format(number); } /** * */ public static String getLocalizedNumber(double number, Locale locale) { return NumberFormat.getInstance(locale).format(number); } /** * Formats a date for a user, according to their locale and time zone * preferences. * * @param request the servlet request object. * @param user the User that the date is being formatted for. * @param date the Date object we're comparing the current time with. * @return the date formatted for the correct locale and time zone. */ public static String formatDate(HttpServletRequest request, User user, Date date) { Locale locale = JiveGlobals.getLocale(); // Check if the guest or user has a locale that would override the global one: Locale tempLocale = null; if (user != null) { tempLocale = localeCodeToLocale(user.getProperty("jiveLocale")); } else { Cookie cookie = SkinUtils.getCookie(request, "jiveLocale"); if (cookie != null) { tempLocale = localeCodeToLocale(cookie.getValue()); } } if (tempLocale != null) { locale = tempLocale; } TimeZone timeZone = getTimeZone(request, user); // See if the date is today. // Check the cache of DateFormat objects: String key = locale.toString() + timeZone.getID(); DateFormat formatter = (DateFormat)dateFormatCache.get(key); if (formatter == null) { formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale); formatter.setTimeZone(timeZone); dateFormatCache.put(key,formatter); } // DateFormat objects are not thread safe, so we must synchronize on it. synchronized(formatter) { return formatter.format(date); } } /** * Returns a list of all available time zone's as a String [][]. The first * entry in each list item is the timeZoneID, and the second is the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?