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

📄 uiutil.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * UIUtil.java * * Version: $Revision: 1.18 $ * * Date: $Date: 2005/12/08 00:17:59 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.app.webui.util;import java.io.PrintWriter;import java.io.StringWriter;import java.sql.SQLException;import java.util.Date;import java.util.Enumeration;import java.net.URLEncoder;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;import org.dspace.content.Collection;import org.dspace.content.Community;import org.dspace.content.DCDate;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.Email;import org.dspace.eperson.EPerson;import org.dspace.eperson.AuthenticationManager;/** * Miscellaneous UI utility methods *  * @author Robert Tansley * @version $Revision: 1.18 $ */public class UIUtil{    /** log4j category */    private static Logger log = Logger.getLogger(UIUtil.class);    /**     * Obtain a new context object. If a context object has already been created     * for this HTTP request, it is re-used, otherwise it is created. If a user     * has authenticated with the system, the current user of the context is set     * appropriately.     *      * @param request     *            the HTTP request     *      * @return a context object     */    public static Context obtainContext(HttpServletRequest request)            throws SQLException    {        Context c = (Context) request.getAttribute("dspace.context");        if (c == null)        {            // No context for this request yet            c = new Context();            // See if a user has authentication            Integer userID = (Integer) request.getSession().getAttribute(                    "dspace.current.user.id");            if (userID != null)            {                EPerson e = EPerson.find(c, userID.intValue());                Authenticate.loggedIn(c, request, e);            }            // Set any special groups - invoke the authentication mgr.            int[] groupIDs = AuthenticationManager.getSpecialGroups(c, request);            for (int i = 0; i < groupIDs.length; i++)            {                c.setSpecialGroup(groupIDs[i]);                log.debug("Adding Special Group id="+String.valueOf(groupIDs[i]));            }            // Set the session ID and IP address            c.setExtraLogInfo("session_id=" + request.getSession().getId() + ":ip_addr=" + request.getRemoteAddr());            // Store the context in the request            request.setAttribute("dspace.context", c);        }        return c;    }    /**     * Get the current community location, that is, where the user "is". This     * returns <code>null</code> if there is no location, i.e. "all of DSpace"     * is the location.     *      * @param request     *            current HTTP request     *      * @return the current community location, or null     */    public static Community getCommunityLocation(HttpServletRequest request)    {        return ((Community) request.getAttribute("dspace.community"));    }    /**     * Get the current collection location, that is, where the user "is". This     * returns null if there is no collection location, i.e. the location is     * "all of DSpace" or a community.     *      * @param request     *            current HTTP request     *      * @return the current collection location, or null     */    public static Collection getCollectionLocation(HttpServletRequest request)    {        return ((Collection) request.getAttribute("dspace.collection"));    }    /**     * Put the original request URL into the request object as an attribute for     * later use. This is necessary because forwarding a request removes this     * information. The attribute is only written if it hasn't been before; thus     * it can be called after a forward safely.     *      * @param request     *            the HTTP request     */    public static void storeOriginalURL(HttpServletRequest request)    {        String orig = (String) request.getAttribute("dspace.original.url");        if (orig == null)        {            String fullURL = request.getRequestURL().toString();            if (request.getQueryString() != null)            {                fullURL = fullURL + "?" + request.getQueryString();            }            request.setAttribute("dspace.original.url", fullURL);        }    }    /**     * Get the original request URL.     *      * @param request     *            the HTTP request     *      * @return the original request URL     */    public static String getOriginalURL(HttpServletRequest request)    {        // Make sure there's a URL in the attribute        storeOriginalURL(request);        return ((String) request.getAttribute("dspace.original.url"));    }    /**     * Utility method to convert spaces in a string to HTML non-break space     * elements.     *      * @param s     *            string to change spaces in     * @return the string passed in with spaces converted to HTML non-break     *         spaces     */    public static String nonBreakSpace(String s)    {        StringBuffer newString = new StringBuffer();        for (int i = 0; i < s.length(); i++)        {            char ch = s.charAt(i);            if (ch == ' ')            {                newString.append("&nbsp;");            }            else            {                newString.append(ch);            }        }        return newString.toString();    }    /**     * Write a human-readable version of a DCDate.     *      * @param d     *            the date     * @param time     *            if true, display the time with the date     * @param localTime     *            if true, adjust for local timezone, otherwise GMT     *      * @return the date in a human-readable form.     */    public static String displayDate(DCDate d, boolean time, boolean localTime)    {        StringBuffer sb = new StringBuffer();        if (d != null)        {            int year;            int month;            int day;            int hour;            int minute;            int second;            if (localTime)            {                year = d.getYear();                month = d.getMonth();                day = d.getDay();                hour = d.getHour();                minute = d.getMinute();                second = d.getSecond();            }            else            {                year = d.getYearGMT();                month = d.getMonthGMT();                day = d.getDayGMT();                hour = d.getHourGMT();                minute = d.getMinuteGMT();                second = d.getSecondGMT();            }            if (year > -1)            {                if (month > -1)                {                    if (day > -1)                    {                        sb.append(day + "-");                    }                    sb.append(DCDate.getMonthName(month).substring(0, 3) + "-");                }                sb.append(year + " ");            }            if (time && (hour > -1))            {                String hr = String.valueOf(hour);                while (hr.length() < 2)                {                    hr = "0" + hr;                }

⌨️ 快捷键说明

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