skinutils.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,157 行 · 第 1/3 页
JAVA
1,157 行
* display name.<p> * * Normally, there are many ID's that correspond to a single display name. * However, the list has been paired down so that a display name only * appears once. Normally, the time zones will be returned in order: * -12 GMT,..., +0GMT,... +12GMT..., etc. * * @return a list of time zones, as a tuple of the zime zone ID, and its * display name. */ public static String [][] getTimeZoneList() { synchronized (timeZoneLock) { if (timeZoneList == null) { Date now = new Date(); String[] timeZoneIDs = TimeZone.getAvailableIDs(); Locale jiveLocale = JiveGlobals.getLocale(); // Now, create String[][] using the unique zones. timeZoneList = new String[timeZoneIDs.length][2]; for (int i=0; i<timeZoneList.length; i++) { String zoneID = timeZoneIDs[i]; timeZoneList[i][0] = zoneID; timeZoneList[i][1] = getTimeZoneName(zoneID, now, jiveLocale); } } } return timeZoneList; } /** * Returns the time zone for a user according to their time zone * preferences (if they exist). Timezones preferences for users are stored * in the user property <tt>jiveTimeZoneID</tt>, while timezones for anonymous * users are stored in the cookie named <tt>jiveTimeZoneID</tt>. If * no preferences are found, the default Jive timezone is used * (JiveGlobals.getTimeZone()). * * @param request the servlet request object. * @param user the User that the date is being formatted for, or null if * an anonymous user. * @return a time zone object. */ public static TimeZone getTimeZone(HttpServletRequest request, User user) { TimeZone timeZone = JiveGlobals.getTimeZone(); String timeZoneID = null; if (user != null) { timeZoneID = user.getProperty("jiveTimeZoneID"); } else { HttpSession session = request.getSession(); timeZoneID = (String)session.getAttribute("jiveTimeZoneID"); if (timeZoneID == null) { Cookie cookie = getCookie(request, "jiveTimeZoneID"); if (cookie != null) { timeZoneID = cookie.getValue(); session.setAttribute("jiveTimeZoneID",timeZoneID); } else { session.setAttribute("jiveTimeZoneID",timeZone.getID()); } } } if (timeZoneID != null) { timeZone = TimeZone.getTimeZone(timeZoneID); } return timeZone; } /** * Returns the display name for a time zone. The display name is the name * specified by the Java TimeZone class, with the addition of the GMT offset * for human readability. * * @param zoneID the time zone to get the name for. * @param now the current date. * @param locale the locale to use. * @return the display name for the time zone. */ private static String getTimeZoneName(String zoneID, Date now, Locale locale) { TimeZone zone = TimeZone.getTimeZone(zoneID); StringBuffer buf = new StringBuffer(); // Add in the GMT part to the name. First, figure out the offset. int offset = zone.getRawOffset(); if (zone.inDaylightTime(now) && zone.useDaylightTime()) { offset += JiveGlobals.HOUR; } if (offset < 0) { buf.append("(GMT-"); } else { buf.append("(GMT+"); } offset = Math.abs(offset); int hours = offset/(int)JiveGlobals.HOUR; int minutes = (offset % (int)JiveGlobals.HOUR)/(int)JiveGlobals.MINUTE; if (hours < 10) { buf.append("0").append(hours).append(":"); } else { buf.append(hours).append(":"); } if (minutes < 10) { buf.append("0").append(minutes); } else { buf.append(minutes); } buf.append(") ").append(zoneID).append(" "); buf.append(zone.getDisplayName(true, TimeZone.SHORT, locale)); return buf.toString(); } /** * Converts a locale string like "en", "en_US" or "en_US_win" to a Java * locale object. If the conversion fails, null is returned. * * @param localeCode the locale code for a Java locale. See the java.util.Lcale * class for more details. */ public static Locale localeCodeToLocale(String localeCode) { Locale locale = null; if (localeCode != null) { String language = null; String country = null; String variant = null; if (localeCode != null) { StringTokenizer tokenizer = new StringTokenizer(localeCode, "_"); if (tokenizer.hasMoreTokens()) { language = tokenizer.nextToken(); if (tokenizer.hasMoreTokens()) { country = tokenizer.nextToken(); if (tokenizer.hasMoreTokens()) { variant = tokenizer.nextToken(); } } } } locale = new Locale( language, ((country!=null)?country:""), ((variant!=null)?variant:"") ); } return locale; } /** * Formats the unfiltered body of a message to make it appear in the "quote * original" format. This is simply the body of the message with the * delimiter appended to the beginning of each line. The delimiter * is most often "> " by convention. A desired length for each line in the * returned String can be specified to aid in formatting.<p> * * This method uses message.getUnfilteredBody() in order to get the body of * the message. This usually yields better results for the formatting * required by this method. However, it also has the potential of being * a security risk if malicious HTML code is embedded in the body. Therefore, * you should always filter HTML from the result of this method before * showing it in an environment where HTML is interpreted. If you are * showing the results of this method in an HTML <textarea>, there is * no need to worry about malicious HTML. * * @param body the message body to quote. * @param delimiter a String that will start each line of the quoted * message. For example, "> "; * @param lineLength the desired length of each line in the quoted message. * @return the unfiltered body of the message in the "quote original" format. */ public static String quoteOriginal(String body, String delimiter, int lineLength) { if (body == null || body.length() == 0) { return ""; } if (body.trim().length() == 0) { return ""; } int length = body.length(); //Create a StringBuffer to hold the quoted body; approximate size. StringBuffer buf = new StringBuffer(body.length()); //i maintains the current position in the String. for (int i=0; i<length; ) { String partialString = StringUtils.chopAtWord( body.substring(i), lineLength ); i += partialString.length()+1; buf.append(delimiter).append(partialString).append("\n"); } return buf.toString(); } /** * Returns true if the forum has been modified since the specified time. * * @param forum the forum to check. * @param time the time to reference the forum against. * @return true if the forum has been modified since the specified time. */ public static boolean isNew(Forum forum, Date time) { return (forum.getModifiedDate().getTime() > time.getTime()); } /** * Returns true if the forum thread has been modified since the specified * time. * * @param thread the thread to check. * @param time the time to reference the thread against. * @return true if the thread has been modified since the specified time. */ public static boolean isNew(ForumThread thread, Date time) { return (thread.getModifiedDate().getTime() > time.getTime()); } /** * Returns true if the forum message has been modified since the specified * time. * * @param message the message to check. * @param time the time to reference the message against. * @return true if the message has been modified since the specified time. */ public static boolean isNew(ForumMessage message, Date time) { return (message.getModifiedDate().getTime() > time.getTime()); } /** * Returns the newest message in the given forum or <tt>null</tt> if there * are no messages in the forum. * * @param forum the forum where we want the last message * @return the newest message in the given forum or <tt>null</tt> if there * are not messages in the forum. */ public static ForumMessage getLastPost(Forum forum) { if (forum.getMessageCount() == 0) { return null; } ResultFilter filter = new ResultFilter(); filter.setSortOrder(ResultFilter.DESCENDING); filter.setSortField(JiveGlobals.CREATION_DATE); // Round down the lower date boundary by 30 mins. filter.setCreationDateRangeMin(ResultFilter.roundDate(forum.getModifiedDate(), 30*60)); filter.setNumResults(1); Iterator messages = forum.messages(filter); ForumMessage lastPost = null; if (messages.hasNext()) { lastPost = (ForumMessage)messages.next(); } return lastPost; } /** * Returns the newest message in the given thread. Note, if there are no * replies to a thread the most recent message in the thread will be the * root message. * * @param thread the thread where we want the last message * @return the newest message in the given thread (may be the root message * of the thread). */ public static ForumMessage getLastPost(ForumThread thread) { ResultFilter filter = new ResultFilter(); filter.setSortOrder(ResultFilter.DESCENDING); filter.setSortField(JiveGlobals.CREATION_DATE); // Round down the lower date boundary by 30 mins. filter.setCreationDateRangeMin(ResultFilter.roundDate(thread.getModifiedDate(), 30*60)); filter.setNumResults(1); Iterator messages = thread.messages(filter); ForumMessage lastPost = null; if (messages.hasNext()) { lastPost = (ForumMessage)messages.next(); } if (lastPost == null) { lastPost = thread.getRootMessage(); } return lastPost; } /** * Returns the Date the user last visited. If no date was found (stored * in a cookie) the current time is used.<p> * * @param request the HttpServletRequest object, known as "request" on * a JSP page. * @param response the HttpServletRequest object, known as "response" on * a JSP page. * @return the time of the last visit in milliseconds since Jan 1st, 1970. */ public static long getLastVisited(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); // The current instant in time. We use the CacheFactory field because // this method will be called often and this will scale better than // calling System.currentTimeMillis(). long now = CacheFactory.currentTime; // First, try to retrieve the value from the session String lastTime = (String)session.getAttribute(JIVE_LASTVISITED_TOKEN); // Found a value in the session, so return it if (lastTime != null) { try { long time = Long.parseLong(lastTime); // update the last visited cookie time to now, but don't update // the last visited time in the session: setCookie(response, JIVE_LASTVISITED_TOKEN, Long.toString(now)); // return the time value return time; } catch(NumberFormatException e) { e.printStackTrace(); } } // Getting to this point means no time value was found in the session, // so look for it in the cookie: long time = now; Cookie cookie = getCookie(request, JIVE_LASTVISITED_TOKEN); if (cookie != null) { lastTime = cookie.getValue(); } if (lastTime != null) { try { time = Long.parseLong(lastTime); } catch (NumberFormatException e) {} } // Set the value in the session & cookie session.setAttribute(JIVE_LASTVISITED_TOKEN, Long.toString(time)); setCookie(response, JIVE_LASTVISITED_TOKEN, Long.toString(now)); return time; } /** * This method will send the user specified an email with instructions * on how to reset their password. Included in the instructions is a 'token' * string which is good for 72 hours. By following the instructions given in * the email the user will be able to reset their password.<p> * * If the user has already been sent a password reset email in the last 72 * hours, the token will not be regenerated but will instead be reused in the * instructional email. Conversely, if the user has been sent an email prior * to the last 72 hours, the token will be regenerated and saved. In either * case, no more than 1 email every 24 hours will be sent. Attempts to send * more than 1 email per 24 hours will result in this method returning * false.<p> * * You can insert the following tokens into the subject or body of the email * message and they'll be dynamically replaced with the appropriate values * when the email is sent: {userID}, {username}, {name}, {email}, {token}, * {requestIP}, {host}, {port} and {webAppDir}.<p> * * The details of the email can be changed using the Jive admin skin, the * default email is below:<p> * * {host} - Jive Forums reset password request.<p> * * We have received a request from {requestIP} to reset your password. If * you have requested to reset your password, please follow the instructions * below. If you do not wish to reset your password, please simply disregard * this message.<p> * * To reset your Jive password at {host} you can either:<p> * * - Click the following url:<p> * http://{host}:{port}{webAppDir}/resetPassword.jsp?userid={userID}&token={token}<p> * * - Goto http://{host}:{port}{webAppDir}/resetPassword.jsp and enter the * following information into the fields provided:<p> * UserID: {userID}<p> * Token: {token}<p> * * @param username the usename of the user for whom we wish to send an email * with instructions on how to reset their password
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?