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

📄 parambean.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
         * Constructor for the Language entry object.         * @param language the ISO-639 language abbreviation (ie : fr, en, ...)         * @param country the ISO-3166 country code (ie : US, CH, ...)         * @param qValue a double indicating the priority of the language in         * order to indicate preference (min=0.0, max=1.0)         */        LanguageEntry (String language, String country, double qValue) {            this.language = language;            this.country = country;            if ((qValue >= 0.0) && (qValue <= 1.0)) {                this.qValue = qValue;            } else {                this.qValue = 1.0;            }        }        public int compareTo (Object o)                throws ClassCastException {            LanguageEntry right = (LanguageEntry)o;            Double tempDouble = new Double (this.qValue);            return tempDouble.compareTo (new Double (right.getQValue ()));        }        public boolean equals (Object obj) {            if (obj instanceof LanguageEntry) {                LanguageEntry right = (LanguageEntry)obj;                if (this.qValue == right.getQValue ()) {                    return true;                } else {                    return false;                }            } else {                return false;            }        }        public String getLanguage () {            return language;        }        public String getCountry () {            return country;        }        public double getQValue () {            return qValue;        }    }    /**     * Actually use the client preferred locale if found, else     * returns the Locale.getDefault() value.     *     * This function provides a full accept-language implementation, but caches     * the result. Therefore it assumes that the language is NOT changed     * during a request other by methods provided by the ParamBean class.     *     * @return Locale the current locale in the current request     */    public Locale getLocale () {        // first we check if we had previously determined the value of the        // locale in order to avoid doing unnecessary processing.        if (this.currentLocale != null) {            // we have a Locale already determined, let's return it...            return currentLocale;        }        // no currently defined, locale, let's start by retrieving the default        // locale for the system.        Locale locale = Locale.getDefault ();        // now let's evaluate the accepted language sent by the browser.        // something like : fr-ch,en-us;q=0.7,ar-eg;q=0.3        String acceptLanguage = mRequest.getHeader ("Accept-Language");        /* this data structure is essential to the implementation of this method.         * Basically it's a sorted map of lists, such as :         *     1.0    en-US,en-GB,fr-CH         *     0.5    fr,en         * This way we can keep order of preference even for LanguageEntry objects         * that have equal priorities. So each entry of our Map is an ArrayList         * of LanguageEntry objects.         */        TreeMap languageSortedMap = new TreeMap ();        JahiaConsole.println (CLASS_NAME + ".getLocale",                "Client Accept-Language is [" + acceptLanguage + "]");        if (acceptLanguage != null) {            // build string array from tokens separated by , : fr-ch,en-us;q=0.7,ar-eg;q=0.3            String[] languages = JahiaTools.getTokens (acceptLanguage, ",");            // actually we return the locale of the first available language            for (int i = 0; i < languages.length; i++) {                String languageRangeEntry = languages[i];                int posQValue = languageRangeEntry.indexOf (";q=");                String languageRange = null;                String languageQValueStr = null;                double languageQValue = 1.0;                if (posQValue != -1) {                    languageRange = languageRangeEntry.substring (0, posQValue);                    languageQValueStr = languageRangeEntry.substring (posQValue + 3, languageRangeEntry.length ());                    // JahiaConsole.println(CLASS_NAME + ".getLocale", "Attempting to convert qvalue string [" + languageQValueStr + "]");                    try {                        languageQValue = Double.parseDouble (languageQValueStr);                    } catch (NumberFormatException nfe) {                        JahiaConsole.println (CLASS_NAME + ".getLocale",                                "Unable to convert qvalue, defaulting to 1...");                        languageQValue = 1.0;                    }                } else {                    languageRange = languageRangeEntry;                }                // JahiaConsole.println(CLASS_NAME + ".getLocale",                //                     "languageRange=" + languageRange +                //                     " languageQValue=" + languageQValue);                // currently we assume we use languageRange is a language entry                String language = "";                String country = "";                int pos = languageRange.indexOf ("-");                if (pos != -1) {                    language = languageRange.substring (0, pos);                    country = languageRange.substring (pos + 1, languageRange.length ());                    country = country.toUpperCase ();                } else {                    language = languageRange;                    country = "";                }                LanguageEntry curLanguageEntry = new LanguageEntry (language, country, languageQValue);                Double languageQValueDouble = new Double (languageQValue);                // let's retrieve the ArrayList if it exists for the qvalue and                // append to it the new entry.                ArrayList mapEntry = (ArrayList)languageSortedMap.get (languageQValueDouble);                if (mapEntry == null) {                    // the array list doesn't exist yet, let's create one...                    mapEntry = new ArrayList ();                }                mapEntry.add (curLanguageEntry);                // this insertion will automatically sort by qvalue since we                // are using a TreeMap object.                languageSortedMap.put (languageQValueDouble, mapEntry);            }            try {                // now let's retrieve the highest qvalue and take the FIRST language                // entry of that list.                Double highestQValue = (Double)languageSortedMap.lastKey ();                ArrayList highestQValueLanguageList = (ArrayList)languageSortedMap.get (highestQValue);                if (highestQValueLanguageList != null) {                    if (highestQValueLanguageList.size () > 0) {                        LanguageEntry chosenLanguage = (LanguageEntry)highestQValueLanguageList.get (0);                        if (chosenLanguage != null) {                            JahiaConsole.println ("ParamBean.getLocale",                                    "Using language=" +                                    chosenLanguage.getLanguage () +                                    " country=" + chosenLanguage.getCountry () +                                    " qvalue=" + chosenLanguage.getQValue ());                            locale = new Locale (chosenLanguage.getLanguage (), chosenLanguage.getCountry ());                        }                    }                }                currentLocale = locale;            } catch (NoSuchElementException nsee) {                JahiaConsole.printe ("ParamBean.getLocale", nsee);            }        }        return locale;    }    /**     * Change the page     *     * Used ind Logout_Engine.java     */    public void changePage (JahiaPage page) {        if (page == null)            return;        if (thePage == null) {            lastRequestedPageID = -1;            newPageRequest = true;        } else if (thePage.getID () != page.getID ()) {            lastRequestedPageID = thePage.getID ();            newPageRequest = true;        }        thePage = page;    }    /**     * get the parameter from the request object specified with specifyrequestObj     */    public String getParameter (String sParameter) {        if (customParameters.get (sParameter) != null) {            return (String)customParameters.get (sParameter);        } else {            return mRequest.getParameter (sParameter);        }    }    /**     * Insert a new parameter into Jahia's paramBean internal parameters. This     * is used for internal in-request communication.     * @param parameterName name of the parameter. If this is an existing     * parameter name-value pair, the value specified will replace the old one     * so be careful.     * @param parameterValue a String value for the parameter name specified.     */    public void setParameter (String parameterName, String parameterValue) {        customParameters.put (parameterName, parameterValue);    }    //-------------------------------------------------------------------------    // MJ   20.03.2001    // NK   18.05.2001 Catch malformed pathinfo exception. Stop parsing it.    /**     * parse the PathInfo elements and convert them to emultated request parameters.     * the parameters are stored in a HashMap (customParameters) where they can     * be looked up by a customized version of this.getParameter().     *     * @todo we might want to extend this in order to store parameter info either     * in the session or in the URL. Session for shorter URLs and URL for     * bookmarking. This should be configurable in the properties, or maybe     * even for the page.     *     * @param   mRequest    the HttpRequest object     */    private void buildCustomParameters (HttpServletRequest mRequest) {        // Parse the PathInfo and build a custom parameter map        String pathInfo = mRequest.getPathInfo ();        if (pathInfo != null) {            if (pathInfo.endsWith (".html")) {                // let's remove false static ending.                int lastSlash = pathInfo.lastIndexOf ("/");                if (lastSlash != -1) {                    String fakeStaticName = pathInfo.substring (lastSlash + 1);                    pathInfo = pathInfo.substring (0, lastSlash);                    JahiaConsole.println ("ParamBean.buildCustomParameters",                            "Removed fake static ending. pathInfo=[" +                            pathInfo + "] fakeEnding=[" +                            fakeStaticName + "]");                }            }            try {                StringTokenizer st = new StringTokenizer (pathInfo, "/");                while (st.hasMoreTokens ()) {                    String token = st.nextToken ();                    if (ENGINE_NAME_PARAMETER.equals (token) ||                            SITE_KEY_PARAMETER.equals (token) ||                            PAGE_ID_PARAMETER.equals (token) ||                            FIELD_ID_PARAMETER.equals (token) ||                            OPERATION_MODE_PARAMETER.equals (token) ||                            CACHE_MODE_PARAMETER.equals (token)) {                        customParameters.put (token, st.nextToken ());                    }                    if (token.startsWith (CONTAINER_SCROLL_PREFIX_PARAMETER)) {                        customParameters.put (token, st.nextToken ());                    }                }            } catch (NoSuchElementException nee) {                // stop parsing token            }        }        // Hollis : In case we have Multipart request        // Append other parameters parsed from query string        if (isMultipartRequest (mRequest)) {            Map queryStringParams = new HashMap ();            ServletIncludeRequestWrapper.parseStringParameters (                    queryStringParams, mRequest.getQueryString (), true);            customParameters.putAll (queryStringParams);        }    }    /**     * Get requested site info     *     * @param session     */    protected void getSiteInfos (HttpSession session) throws JahiaException {        String siteKey = getParameter (SITE_KEY_PARAMETER);        if (siteKey != null) {            JahiaConsole.println ("ParamBean.getSiteInfos()",                    "Found site info in parameters...");            // Try to get the site by its sitekey            this.site = ServicesRegistry.getInstance ()                    .getJahiaSitesService ()                    .getSiteByKey (siteKey);            if (site != null) {                this.siteID = site.getID ();            }            this.siteKey = siteKey;        } else {            // try to get the site by the servername            this.site = ServicesRegistry.getInstance ()                    .getJahiaSitesService ()                    .getSite (mRequest.getServerName ());            if (this.site == null) {                JahiaConsole.println ("ParamBean.getSiteInfos()",                        "Can't find site in serverName, trying to reverse from page ID...");                // try to get the site by the requested page id                // Get the page information, if no page info is specified, then                // load the default page.                String pageIDStr = getParameter (PAGE_ID_PARAMETER);

⌨️ 快捷键说明

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