⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skinutils.java

📁 Jive论坛2.5版本的源程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }
        // Time difference between now and the parameter object's time. Using
        // the cache timer means the resolution is no better than a seconds,
        // for for our purposes, we don't need better.
        long delta = com.jivesoftware.util.CacheTimer.currentTime - date.getTime();

        // Within the last hour
        if ((delta / JiveGlobals.HOUR) < 1) {
            long minutes = (delta/JiveGlobals.MINUTE);
            if (minutes == 0) {
                return "Less than 1 min ago";
            }
            else if (minutes == 1) {
                return "1 minute ago";
            }
            else {
                return (minutes + " minutes ago");
            }
        }

        // Sometime today
        if ((delta / JiveGlobals.DAY) < 1) {
            long hours = (delta/JiveGlobals.HOUR);
            if(hours <= 1) {
                return "1 hour ago";
            }
            else {
                return (hours + " hours ago");
            }
        }

        int hour = -1;
        int minute = -1;
        int am_pm = -1;
        int day_of_week = -1;
        int day_of_month = -1;
        int month = -1;
        int day_of_the_week = -1;

        synchronized (globalCal) {
            globalCal.setTime(date);
            globalCal.setTimeZone(getTimeZone(request, response, user));
            hour = globalCal.get(Calendar.HOUR);
            minute = globalCal.get(Calendar.MINUTE);
            am_pm = globalCal.get(Calendar.AM_PM);
            day_of_the_week = globalCal.get(Calendar.DAY_OF_WEEK);
            day_of_month = globalCal.get(Calendar.DAY_OF_MONTH);
            month = globalCal.get(Calendar.MONTH);
            day_of_week = globalCal.get(Calendar.DAY_OF_WEEK);
        }

        // Within the last week
        if ((delta / JiveGlobals.WEEK) < 1) {
            long days = (delta/JiveGlobals.DAY);
            if (days <= 1) {
                StringBuffer buf = new StringBuffer("Yesterday, ");
                if (am_pm == 1 && hour == 0) {
                    buf.append(12).append(":");
                } else {
                    buf.append(hour).append(":");
                }
                if (minute < 10) {
                    buf.append("0").append(minute);
                } else {
                    buf.append(minute);
                }
                buf.append(" ");
                if (am_pm == 0) {
                    buf.append("AM");
                } else {
                    buf.append("PM");
                }
                return buf.toString();
            }
            else {
                StringBuffer buf = new StringBuffer();
                buf.append(DAYS_OF_WEEK[day_of_the_week-1]);
                buf.append(", ").append(MONTHS_OF_YEAR[month]);
                buf.append(" ").append(day_of_month).append(" ");
                if (am_pm == 1 && hour == 0) {
                    buf.append(12).append(":");
                } else {
                    buf.append(hour).append(":");
                }
                if (minute < 10) {
                    buf.append("0").append(minute);
                } else {
                    buf.append(minute);
                }
                buf.append(" ");
                if (am_pm == 0) {
                    buf.append("AM");
                } else {
                    buf.append("PM");
                }
                return buf.toString();
            }
        }

        // More than a week ago.
        else {
            return formatDate(request, response, user, date);
        }
    }

    /**
     * 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 "jive.timeZoneID", while timezones for anonymous
     * users are stored in the cookie/session attribute "jive.timeZoneID". If
     * no preferences are found, the default Jive timezone is used
     * (JiveGlobals.getTimeZone()).
     *
     * @param request the servlet request object.
     * @param response the servlet response object.
     * @param user the User that the date is being formatted for, or null if
     *      an anonymous user.
     * @param date the Date object we're comparing the current time with.
     */
    public static TimeZone getTimeZone(HttpServletRequest request,
            HttpServletResponse response, 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;
    }

    private static String [][] timeZoneList = null;
    private static Object timeZoneLock = new Object();

    /**
     * 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
     * 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();
                ArrayList timeZones = new ArrayList(timeZoneIDs.length);
                Map uniqueNames = new HashMap(timeZoneIDs.length/2);
                Locale jiveLocale = JiveGlobals.getLocale();

                // Loop through all time zones, and create a list of those that
                // have unique names.
                for (int i=0; i<timeZoneIDs.length; i++) {
                    TimeZone zone = TimeZone.getTimeZone(timeZoneIDs[i]);
                    String zoneName = getTimeZoneName(zone, now, jiveLocale);

                    // If we don't already have this name, add it.
                    if (!uniqueNames.containsKey(zoneName)) {
                        uniqueNames.put(zoneName, null);
                        timeZones.add(zone);
                    }
                }
                // Now, create String[][] using the unique zones.
                timeZoneList = new String[uniqueNames.size()][2];
                for (int i=0; i<timeZoneList.length; i++) {
                    TimeZone zone = (TimeZone)timeZones.get(i);
                    timeZoneList[i][0] = zone.getID();
                    timeZoneList[i][1] = getTimeZoneName(zone, now, jiveLocale);
                }
            }
        }
        return timeZoneList;
    }

    /**
     * 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 zone 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(TimeZone zone, Date now, Locale locale) {
        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;
        }
        String gmt;
        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(zone.getDisplayName(true, TimeZone.LONG, locale));
        return buf.toString();
    }

    /**
     * Builds a cookie string containing a username and password.<p>
     *
     * Note: with open source this is not really secure, but it prevents users
     * from snooping the cookie file of others and by changing the XOR mask and
     * character offsets, you can easily tweak results.
     *
     * @param username The username.
     * @param password The password.
     * @return String encoding the input parameters, an empty string if one of
     *      the arguments equals <code>null</code>.
     */
    private static String encodePasswordCookie(String username, String password)
    {
        StringBuffer buf = new StringBuffer();
        if (username != null && password != null) {
            byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes();
            int b;

            for (int n = 0; n < bytes.length; n++) {
                b = bytes[n] ^ (ENCODE_XORMASK + n);
                buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
                buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
            }
        }
        return buf.toString();
    }

    /**
     * Unrafels a cookie string containing a username and password.
     * @param value The cookie value.
     * @return String[] containing the username at index 0 and the password at
     *      index 1, or <code>{ null, null }</code> if cookieVal equals
     *      <code>null</code> or the empty string.
     */
    private static String[] decodePasswordCookie( String cookieVal ) {

        // check that the cookie value isn't null or zero-length
        if( cookieVal == null || cookieVal.length() <= 0 ) {
            return null;
        }

        // unrafel the cookie value
        char[] chars = cookieVal.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        int b;
        for (int n = 0, m = 0; n < bytes.length; n++) {
            b = chars[m++] - ENCODE_CHAR_OFFSET1;
            b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
            bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n));
        }
        cookieVal = new String(bytes);
        int	pos = cookieVal.indexOf(ENCODE_DELIMETER);
        String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
        String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);

        return new String[] {username, password};
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -