KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > CharsetMapper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.util;
19
20
21 import java.io.InputStream JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Properties JavaDoc;
24
25
26
27 /**
28  * Utility class that attempts to map from a Locale to the corresponding
29  * character set to be used for interpreting input text (or generating
30  * output text) when the Content-Type header does not include one. You
31  * can customize the behavior of this class by modifying the mapping data
32  * it loads, or by subclassing it (to change the algorithm) and then using
33  * your own version for a particular web application.
34  *
35  * @author Craig R. McClanahan
36  * @version $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $ $Version$
37  */

38
39 public class CharsetMapper {
40
41
42     // ---------------------------------------------------- Manifest Constants
43

44
45     /**
46      * Default properties resource name.
47      */

48     public static final String JavaDoc DEFAULT_RESOURCE =
49       "/org/apache/catalina/util/CharsetMapperDefault.properties";
50
51
52     // ---------------------------------------------------------- Constructors
53

54
55     /**
56      * Construct a new CharsetMapper using the default properties resource.
57      */

58     public CharsetMapper() {
59         this(DEFAULT_RESOURCE);
60     }
61
62
63     /**
64      * Construct a new CharsetMapper using the specified properties resource.
65      *
66      * @param name Name of a properties resource to be loaded
67      *
68      * @exception IllegalArgumentException if the specified properties
69      * resource could not be loaded for any reason.
70      */

71     public CharsetMapper(String JavaDoc name) {
72         try {
73             InputStream JavaDoc stream =
74               this.getClass().getResourceAsStream(name);
75             map.load(stream);
76             stream.close();
77         } catch (Throwable JavaDoc t) {
78             throw new IllegalArgumentException JavaDoc(t.toString());
79         }
80     }
81
82
83     // ---------------------------------------------------- Instance Variables
84

85
86     /**
87      * The mapping properties that have been initialized from the specified or
88      * default properties resource.
89      */

90     private Properties JavaDoc map = new Properties JavaDoc();
91
92
93     // ------------------------------------------------------- Public Methods
94

95
96     /**
97      * Calculate the name of a character set to be assumed, given the specified
98      * Locale and the absence of a character set specified as part of the
99      * content type header.
100      *
101      * @param locale The locale for which to calculate a character set
102      */

103     public String JavaDoc getCharset(Locale JavaDoc locale) {
104         // Match full language_country_variant first, then language_country,
105
// then language only
106
String JavaDoc charset = map.getProperty(locale.toString());
107         if (charset == null) {
108             charset = map.getProperty(locale.getLanguage() + "_"
109                     + locale.getCountry());
110             if (charset == null) {
111                 charset = map.getProperty(locale.getLanguage());
112             }
113         }
114         return (charset);
115     }
116
117     
118     /**
119      * The deployment descriptor can have a
120      * locale-encoding-mapping-list element which describes the
121      * webapp's desired mapping from locale to charset. This method
122      * gets called when processing the web.xml file for a context
123      *
124      * @param locale The locale for a character set
125      * @param charset The charset to be associated with the locale
126      */

127     public void addCharsetMappingFromDeploymentDescriptor(String JavaDoc locale, String JavaDoc charset) {
128         map.put(locale, charset);
129     }
130
131
132 }
133
Popular Tags