KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > resourcebundle > ResourceBundlesDigester


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
package org.jahia.resourcebundle;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.Vector JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 import org.apache.commons.digester.Digester;
21 import org.apache.commons.digester.Rule;
22
23 /**
24  * Load Resource Bundle Definitions from XML configuration file.
25  * Implementation based on Apache Digester.
26  *
27  * @author Khue Nguyen
28  */

29 class ResourceBundlesDigester
30 {
31     private Digester digester;
32     private Vector JavaDoc registry;
33
34     public ResourceBundlesDigester(){
35         this.initDigester();
36         this.registry = new Vector JavaDoc();
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     /**
50      * Full path to the XML source file, from which to load the Resource Bundle Definition.
51      * The internal list of Resoure Bundle Definitions will be cleared before loading again.
52      *
53      * @param sourceFile
54      */

55     public boolean loadResourceBundleDefinitions(String JavaDoc sourceFile){
56         boolean success = false;
57         try {
58             File JavaDoc bundleFile = new File JavaDoc(sourceFile);
59             this.digester.parse(bundleFile);
60             success = true;
61         } catch (org.xml.sax.SAXException JavaDoc saxe) {
62             saxe.printStackTrace();
63         } catch ( IOException JavaDoc ioe ){
64             ioe.printStackTrace();
65         }
66         return success;
67     }
68
69     /**
70      * Returns the vector of Definitions
71      * You must call loadResourceBundleDefinitions once to load them first
72      *
73      * @return
74      */

75     public Vector JavaDoc getResourceBundleDefinitions(){
76         return this.registry;
77     }
78
79
80     final class AddResourceBundleRule extends Rule {
81         private HashMap JavaDoc params = new HashMap JavaDoc();
82         SetParamRule setParamRule = new SetParamRule();
83
84         public void end(String JavaDoc namespace, String JavaDoc name)
85                 throws Exception JavaDoc {
86             ResourceBundleDefinition rbDef = new ResourceBundleDefinition((String JavaDoc) params.get("key"), (String JavaDoc) params.get("file"));
87             params.clear();
88             registry.add(rbDef);
89         }
90
91         final class SetParamRule extends Rule {
92             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
93                     throws Exception JavaDoc {
94                 params.put(name,text);
95             }
96         }
97     }
98
99 }
100
101
Popular Tags