KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > encoding > CharacterEncodingManager


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.util.encoding;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54
55 import com.anthonyeden.lib.config.Configuration;
56 import com.anthonyeden.lib.config.ConfigurationException;
57 import org.jpublish.util.PathUtilities;
58
59 /**
60  * Manager which controls mapping character encodings to request paths.
61  *
62  * @author Anthony Eden
63  */

64
65 public class CharacterEncodingManager {
66
67     /**
68      * The default page encoding (ISO-8859-1).
69      */

70     public static final String JavaDoc DEFAULT_PAGE_ENCODING = "ISO-8859-1";
71
72     /**
73      * The default template encoding (ISO-8859-1).
74      */

75     public static final String JavaDoc DEFAULT_TEMPLATE_ENCODING = "ISO-8859-1";
76
77     /**
78      * The default request encoding (ISO-8859-1).
79      */

80     public static final String JavaDoc DEFAULT_REQUEST_ENCODING = "ISO-8859-1";
81
82     /**
83      * The default response encoding (ISO-8859-1).
84      */

85     public static final String JavaDoc DEFAULT_RESPONSE_ENCODING = "ISO-8859-1";
86
87     private List JavaDoc characterEncodingMaps;
88     private CharacterEncodingMap defaultMap;
89
90     /**
91      * Construct a new CharacterEncodingManager.
92      */

93
94     public CharacterEncodingManager() {
95         characterEncodingMaps = new ArrayList JavaDoc();
96         loadDefaultCharacterEncodingMap();
97     }
98
99     /**
100      * Get the CharacterEncodingMap for the given path. If the no map is found for that path then the default map is
101      * used.
102      *
103      * @param path The path
104      * @return The CharacterEncodingMap
105      */

106
107     public CharacterEncodingMap getMap(String JavaDoc path) {
108         Iterator JavaDoc iter = characterEncodingMaps.iterator();
109         while (iter.hasNext()) {
110             CharacterEncodingMap map = (CharacterEncodingMap) iter.next();
111             if (PathUtilities.match(path, map.getPath())) {
112                 return map;
113             }
114         }
115         return getDefaultMap();
116     }
117
118     /**
119      * Get the default character encoding map.
120      *
121      * @return The default character encoding map
122      */

123
124     public CharacterEncodingMap getDefaultMap() {
125         return defaultMap;
126     }
127
128     /**
129      * Set the default character encoding map.
130      *
131      * @param defaultMap The default character encoding map
132      */

133
134     public void setDefaultMap(CharacterEncodingMap defaultMap) {
135         this.defaultMap = defaultMap;
136     }
137
138     /**
139      * Load the character encoding configuration.
140      *
141      * @param configuration The configuration object
142      * @throws ConfigurationException
143      */

144
145     public void loadConfiguration(Configuration configuration) throws ConfigurationException {
146         loadCharacterEncodingMaps(configuration.getChildren("character-encoding-map"));
147     }
148
149     /**
150      * Load all character encodings.
151      *
152      * @param configurationElements The Configuration element list
153      */

154
155     private void loadCharacterEncodingMaps(List JavaDoc configurationElements) {
156         characterEncodingMaps.clear();
157
158         Iterator JavaDoc iter = configurationElements.iterator();
159         while (iter.hasNext()) {
160             CharacterEncodingMap characterEncodingMap = new CharacterEncodingMap();
161             Configuration mapElement = (Configuration) iter.next();
162             characterEncodingMap.setPath(mapElement.getAttribute("path"));
163             characterEncodingMap.setPageEncoding(mapElement.getChildValue("page-encoding"));
164             characterEncodingMap.setTemplateEncoding(mapElement.getChildValue("template-encoding"));
165             characterEncodingMap.setRequestEncoding(mapElement.getChildValue("request-encoding"));
166             characterEncodingMap.setResponseEncoding(mapElement.getChildValue("response-encoding"));
167             characterEncodingMaps.add(characterEncodingMap);
168         }
169     }
170
171     /**
172      * Load the default character encoding map. The default map uses ISO-8859-1 as the encoding for all data.
173      */

174
175     private void loadDefaultCharacterEncodingMap() {
176         defaultMap = new CharacterEncodingMap();
177         defaultMap.setPageEncoding(DEFAULT_PAGE_ENCODING);
178         defaultMap.setTemplateEncoding(DEFAULT_TEMPLATE_ENCODING);
179         defaultMap.setRequestEncoding(DEFAULT_REQUEST_ENCODING);
180         defaultMap.setResponseEncoding(DEFAULT_RESPONSE_ENCODING);
181     }
182
183 }
184
Popular Tags