KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > I18nReplacer


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.util.MissingResourceException JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.w3c.dom.Attr JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.NamedNodeMap JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23 import org.w3c.dom.Text JavaDoc;
24
25
26 /**
27  * replaces attribute values in a DOM tree with values from a resource bundle
28  * Attribute value must start with "fmt:", e.g. "fmt:akey" will replace
29  * the string "fmt:akey" with the value of found for "akey" in the resource
30  * bundle.
31  *
32  * @author av
33  */

34 public abstract class I18nReplacer {
35   
36   public static final String JavaDoc PREFIX = "fmt:";
37   
38   public static I18nReplacer instance(final ResourceBundle JavaDoc resb) {
39     return new I18nReplacer() {
40       protected String JavaDoc internalReplace(String JavaDoc key) {
41         try {
42           return resb.getString(key);
43         } catch (MissingResourceException JavaDoc e) {
44           return "???" + key + "???";
45         }
46       }
47       
48     };
49   }
50
51   public static I18nReplacer instance(final com.tonbeller.tbutils.res.Resources res) {
52     return new I18nReplacer() {
53       protected String JavaDoc internalReplace(String JavaDoc key) {
54         try {
55           return res.getString(key);
56         } catch (MissingResourceException JavaDoc e) {
57           return "???" + key + "???";
58         }
59       }
60     };
61   }
62   
63   protected abstract String JavaDoc internalReplace(String JavaDoc key);
64   
65   public String JavaDoc replace(String JavaDoc value) {
66     if (value.startsWith(PREFIX)) {
67       return internalReplace(value.substring(4));
68     }
69     return value;
70   }
71   
72   public void replaceAll(Node JavaDoc root) {
73     if (root.getNodeType() == Node.ELEMENT_NODE) {
74       Element JavaDoc e = (Element JavaDoc) root;
75       NamedNodeMap JavaDoc atts = e.getAttributes();
76       final int N = atts.getLength();
77       for (int i = 0; i < N; i++) {
78         Attr JavaDoc attr = (Attr JavaDoc) atts.item(i);
79         String JavaDoc value = attr.getValue();
80         if (value.startsWith(PREFIX)) {
81           value = internalReplace(value.substring(4));
82           attr.setValue(value);
83         }
84       }
85     }
86     if (root.getNodeType() == Node.TEXT_NODE) {
87       Text JavaDoc text = (Text JavaDoc) root;
88       String JavaDoc data = text.getData();
89       if (data.startsWith(PREFIX)) {
90         data = internalReplace(data.substring(4));
91         text.setData(data);
92       }
93     }
94     NodeList JavaDoc list = root.getChildNodes();
95     int N = list.getLength();
96     for (int i = 0; i < N; i++)
97       replaceAll(list.item(i));
98   }
99 }
100
Popular Tags