1 package cintoo.messages.bundle.xml; 2 3 import api.cintoo.messages.bundle.BaseBundle; 4 import org.apache.commons.digester.Digester; 5 import org.xml.sax.SAXException ; 6 7 import java.io.FileNotFoundException ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.util.*; 11 12 public class XmlResourceBundle extends BaseBundle { 13 14 protected Locale locale; 15 16 protected Map<String , String > properties; 17 18 public XmlResourceBundle(InputStream input, Locale locale) throws IOException { 19 super(); 20 this.locale = locale; 21 22 properties = new HashMap<String , String >(); 23 24 Digester digester = new Digester(); 25 configure(digester); 26 digester.push(this); 27 28 try { 30 digester.parse(input); 31 } catch (FileNotFoundException e) { 32 throw new IOException (e.getMessage()); 33 } catch (IOException e) { 34 throw new IOException (e.getMessage()); 35 } catch (SAXException e) { 36 throw new IOException (e.getMessage()); 37 } 38 } 39 40 public Object handleGetObject(String key) { 41 Object object = null; 42 if (properties != null) { 43 object = properties.get(key); 44 } 45 return object; 46 } 47 48 public void addProperty(Property property) { 49 properties.put(property.getKey(), property.getValue()); 50 } 51 52 public Enumeration<String > getKeys() { 53 return Collections.enumeration(properties.keySet()); 54 } 55 56 public Locale getLocale() { 57 return locale; 58 } 59 60 protected Digester configure(Digester digester) { 61 digester.addObjectCreate("properties/property", Property.class); 62 digester.addSetNext("properties/property", "addProperty", "cintoo.messages.bundle.xml.Property"); 63 digester.addBeanPropertySetter("properties/property", "value"); 64 digester.addSetProperties("properties/property", "key", "key"); 65 return digester; 66 } 67 } | Popular Tags |