1 48 49 package org.jpublish.util.encoding; 50 51 import java.util.ArrayList ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 55 import com.anthonyeden.lib.config.Configuration; 56 import com.anthonyeden.lib.config.ConfigurationException; 57 import org.jpublish.util.PathUtilities; 58 59 64 65 public class CharacterEncodingManager { 66 67 70 public static final String DEFAULT_PAGE_ENCODING = "ISO-8859-1"; 71 72 75 public static final String DEFAULT_TEMPLATE_ENCODING = "ISO-8859-1"; 76 77 80 public static final String DEFAULT_REQUEST_ENCODING = "ISO-8859-1"; 81 82 85 public static final String DEFAULT_RESPONSE_ENCODING = "ISO-8859-1"; 86 87 private List characterEncodingMaps; 88 private CharacterEncodingMap defaultMap; 89 90 93 94 public CharacterEncodingManager() { 95 characterEncodingMaps = new ArrayList (); 96 loadDefaultCharacterEncodingMap(); 97 } 98 99 106 107 public CharacterEncodingMap getMap(String path) { 108 Iterator 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 123 124 public CharacterEncodingMap getDefaultMap() { 125 return defaultMap; 126 } 127 128 133 134 public void setDefaultMap(CharacterEncodingMap defaultMap) { 135 this.defaultMap = defaultMap; 136 } 137 138 144 145 public void loadConfiguration(Configuration configuration) throws ConfigurationException { 146 loadCharacterEncodingMaps(configuration.getChildren("character-encoding-map")); 147 } 148 149 154 155 private void loadCharacterEncodingMaps(List configurationElements) { 156 characterEncodingMaps.clear(); 157 158 Iterator 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 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 |