001: /*
002: * ====================================================================
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: * [Additional notices, if required by prior licensing conditions]
057: *
058: */
059:
060: /*
061: * This class is based on a class originally written by Jason Hunter
062: * <jhunter@acm.org> as part of the book "Java Servlet Programming"
063: * (O'Reilly). See http://www.servlets.com/book for more information.
064: * Used by Sun Microsystems with permission.
065: */
066:
067: package org.apache.catalina.util;
068:
069: import java.io.InputStream;
070: import java.util.Locale;
071: import java.util.Properties;
072:
073: /**
074: * Utility class that attempts to map from a Locale to the corresponding
075: * character set to be used for interpreting input text (or generating
076: * output text) when the Content-Type header does not include one. You
077: * can customize the behavior of this class by modifying the mapping data
078: * it loads, or by subclassing it (to change the algorithm) and then using
079: * your own version for a particular web application.
080: *
081: * @author Craig R. McClanahan
082: * @revision $Date: 2001/07/22 20:25:13 $ $Version$
083: */
084:
085: public class CharsetMapper {
086:
087: // ---------------------------------------------------- Manifest Constants
088:
089: /**
090: * Default properties resource name.
091: */
092: public static final String DEFAULT_RESOURCE = "/org/apache/catalina/util/CharsetMapperDefault.properties";
093:
094: // ---------------------------------------------------------- Constructors
095:
096: /**
097: * Construct a new CharsetMapper using the default properties resource.
098: */
099: public CharsetMapper() {
100:
101: this (DEFAULT_RESOURCE);
102:
103: }
104:
105: /**
106: * Construct a new CharsetMapper using the specified properties resource.
107: *
108: * @param name Name of a properties resource to be loaded
109: *
110: * @exception IllegalArgumentException if the specified properties
111: * resource could not be loaded for any reason.
112: */
113: public CharsetMapper(String name) {
114:
115: try {
116: InputStream stream = this .getClass().getResourceAsStream(
117: name);
118: map.load(stream);
119: stream.close();
120: } catch (Throwable t) {
121: throw new IllegalArgumentException(t.toString());
122: }
123:
124: }
125:
126: // ---------------------------------------------------- Instance Variables
127:
128: /**
129: * The mapping properties that have been initialized from the specified or
130: * default properties resource.
131: */
132: private Properties map = new Properties();
133:
134: // ------------------------------------------------------- Public Methods
135:
136: /**
137: * Calculate the name of a character set to be assumed, given the specified
138: * Locale and the absence of a character set specified as part of the
139: * content type header.
140: *
141: * @param locale The locale for which to calculate a character set
142: */
143: public String getCharset(Locale locale) {
144:
145: String charset = null;
146:
147: // First, try a full name match (language and country)
148: charset = map.getProperty(locale.toString());
149: if (charset != null)
150: return (charset);
151:
152: // Second, try to match just the language
153: charset = map.getProperty(locale.getLanguage());
154: return (charset);
155:
156: }
157:
158: }
|