/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.anodyneos.xp.tag; import java.text.DateFormat; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.anodyneos.xp.XpException; /** * <p> * Utilities in support of tag-handler classes. * </p> * * @author Jan Luehe */ public class Util { private static final String DEFAULT = "default"; private static final String SHORT = "short"; private static final String MEDIUM = "medium"; private static final String LONG = "long"; private static final String FULL = "full"; /* * Converts the given string description of a formatting style for dates and * times to the corresponding java.util.DateFormat constant. * * @param style String description of formatting style for dates and times * @param errCode Error code to throw if given style is invalid * * @return java.util.DateFormat constant corresponding to given style * * @throws JspException if the given style is invalid */ public static int getStyle(String style, String errCode) throws XpException { int ret = DateFormat.DEFAULT; if (style != null) { if (DEFAULT.equalsIgnoreCase(style)) { ret = DateFormat.DEFAULT; } else if (SHORT.equalsIgnoreCase(style)) { ret = DateFormat.SHORT; } else if (MEDIUM.equalsIgnoreCase(style)) { ret = DateFormat.MEDIUM; } else if (LONG.equalsIgnoreCase(style)) { ret = DateFormat.LONG; } else if (FULL.equalsIgnoreCase(style)) { ret = DateFormat.FULL; } else { throw new XpException("Invalid style " + errCode); } } return ret; } /** * HttpServletRequest.getLocales() returns the server's default locale if * the request did not specify a preferred language. We do not want this * behavior, because it prevents us from using the fallback locale. We * therefore need to return an empty Enumeration if no preferred locale has * been specified. This way, the logic for the fallback locale will be able * to kick in. */ public static Enumeration getRequestLocales(HttpServletRequest request) { Enumeration values = request.getHeaders("accept-language"); if (values.hasMoreElements()) { // At least one "accept-language". Simply return // the enumeration returned by request.getLocales(). // System.out.println("At least one accept-language"); return request.getLocales(); } else { // No header for "accept-language". Simply return // the empty enumeration. // System.out.println("No accept-language"); return values; } } }