1 5 6 package org.exoplatform.services.resources.impl; 7 8 import java.io.InputStream ; 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import javax.xml.parsers.DocumentBuilder ; 14 import javax.xml.parsers.DocumentBuilderFactory ; 15 16 import org.exoplatform.container.configuration.ConfigurationManager; 17 import org.exoplatform.container.configuration.ServiceConfiguration; 18 import org.exoplatform.services.resources.LocaleConfig; 19 import org.exoplatform.services.resources.LocaleConfigService; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.NodeList ; 23 24 28 public class LocaleConfigServiceImpl implements LocaleConfigService { 29 30 private LocaleConfig defaultConfig_ ; 31 private Map configs_ ; 32 33 public LocaleConfigServiceImpl(ConfigurationManager confService) throws Exception { 34 configs_ = new HashMap (10) ; 35 ServiceConfiguration sconf = confService.getServiceConfiguration(getClass()) ; 36 String confResource = sconf.getValueParam("locale.config.file").getValue() ; 37 InputStream is = confService.getInputStream(confResource) ; 38 parseConfiguration(is) ; 39 } 40 41 public LocaleConfig getDefaultLocaleConfig() { return defaultConfig_ ; } 42 43 public LocaleConfig getLocaleConfig(String lang) { 44 LocaleConfig config = (LocaleConfig) configs_.get(lang) ; 45 if(config != null) return config ; 46 return defaultConfig_ ; 47 } 48 49 public Collection getLocalConfigs() { return configs_.values() ; } 50 51 private void parseConfiguration(InputStream is) throws Exception { 52 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 53 factory.setIgnoringComments(true); 54 factory.setCoalescing(true); 55 factory.setNamespaceAware(false); 56 factory.setValidating(false); 57 DocumentBuilder parser = factory.newDocumentBuilder(); 58 Document document = parser.parse(is); 59 NodeList nodes = document.getElementsByTagName("locale-config") ; 60 for(int i = 0; i < nodes.getLength() ; i++) { 61 Node node = nodes.item(i) ; 62 NodeList children = node.getChildNodes() ; 63 LocaleConfig config = new LocaleConfigImpl() ; 64 for(int j = 0; j < children.getLength(); j++ ) { 65 Node element = children.item(j) ; 66 if("locale".equals(element.getNodeName())) { 67 config.setLocale(element.getFirstChild().getNodeValue()) ; 68 } else if("output-encoding".equals(element.getNodeName())) { 69 config.setOutputEncoding(element.getFirstChild().getNodeValue()) ; 70 } else if("input-encoding".equals(element.getNodeName())) { 71 config.setInputEncoding(element.getFirstChild().getNodeValue()) ; 72 } else if("description".equals(element.getNodeName())) { 73 config.setDescription(element.getFirstChild().getNodeValue()) ; 74 } 75 } 76 configs_.put(config.getLanguage(), config) ; 77 if(i == 0) defaultConfig_ = config ; 78 } 79 } 80 } 81 | Popular Tags |