1 package org.jahia.resourcebundle; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.Vector ; 18 import java.util.HashMap ; 19 20 import org.apache.commons.digester.Digester; 21 import org.apache.commons.digester.Rule; 22 23 29 class ResourceBundlesDigester 30 { 31 private Digester digester; 32 private Vector registry; 33 34 public ResourceBundlesDigester(){ 35 this.initDigester(); 36 this.registry = new Vector (); 37 } 38 39 private void initDigester(){ 40 41 this.digester = new Digester(); 42 43 AddResourceBundleRule rule = new AddResourceBundleRule(); 44 digester.addRule("registry/resource-bundle", rule); 45 digester.addRule("registry/resource-bundle/file", rule.setParamRule); 46 digester.addRule("registry/resource-bundle/key", rule.setParamRule); 47 } 48 49 55 public boolean loadResourceBundleDefinitions(String sourceFile){ 56 boolean success = false; 57 try { 58 File bundleFile = new File (sourceFile); 59 this.digester.parse(bundleFile); 60 success = true; 61 } catch (org.xml.sax.SAXException saxe) { 62 saxe.printStackTrace(); 63 } catch ( IOException ioe ){ 64 ioe.printStackTrace(); 65 } 66 return success; 67 } 68 69 75 public Vector getResourceBundleDefinitions(){ 76 return this.registry; 77 } 78 79 80 final class AddResourceBundleRule extends Rule { 81 private HashMap params = new HashMap (); 82 SetParamRule setParamRule = new SetParamRule(); 83 84 public void end(String namespace, String name) 85 throws Exception { 86 ResourceBundleDefinition rbDef = new ResourceBundleDefinition((String ) params.get("key"), (String ) params.get("file")); 87 params.clear(); 88 registry.add(rbDef); 89 } 90 91 final class SetParamRule extends Rule { 92 public void body(String namespace, String name, String text) 93 throws Exception { 94 params.put(name,text); 95 } 96 } 97 } 98 99 } 100 101 | Popular Tags |