KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > form > FormDocument


1 package com.tonbeller.wcf.form;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.Locale JavaDoc;
6 import java.util.MissingResourceException JavaDoc;
7 import java.util.ResourceBundle JavaDoc;
8
9 import org.w3c.dom.Document JavaDoc;
10
11 import com.tonbeller.tbutils.res.Resources;
12 import com.tonbeller.wcf.controller.RequestContext;
13 import com.tonbeller.wcf.utils.I18nReplacer;
14 import com.tonbeller.wcf.utils.ResourceLocator;
15 import com.tonbeller.wcf.utils.XmlUtils;
16
17 /**
18  * Loads an xml document and replaces locale dependant labels
19  */

20 public class FormDocument {
21
22   /**
23    * Shortcut for replaceI18N(parseDocument())
24    */

25   public static Document JavaDoc loadDocument(RequestContext context, String JavaDoc bundle, String JavaDoc name) throws MalformedURLException JavaDoc, MissingResourceException JavaDoc {
26     Document JavaDoc doc = parseDocument(context, name);
27     replaceI18n(context, doc, bundle);
28     return doc;
29   }
30   
31   /**
32    * replaces attr value "fmt:xxx" with string for key "xxx" from resource bundle.
33    */

34   public static void replaceI18n(RequestContext context, Document JavaDoc dom, String JavaDoc bundle)
35       throws MissingResourceException JavaDoc {
36
37     String JavaDoc bundleAttr = dom.getDocumentElement().getAttribute("bundle");
38     if (bundleAttr.length() > 0) {
39       bundle = bundleAttr;
40     }
41
42     if (bundle != null) {
43       Locale JavaDoc loc = context.getLocale();
44       ResourceBundle JavaDoc resb = ResourceBundle.getBundle(bundle, loc);
45       I18nReplacer replacer = I18nReplacer.instance(resb);
46       replacer.replaceAll(dom);
47     } else {
48       Resources res = context.getResources();
49       I18nReplacer replacer = I18nReplacer.instance(res);
50       replacer.replaceAll(dom);
51     }
52   }
53
54   public static Document JavaDoc parseDocument(RequestContext context, String JavaDoc name) throws MalformedURLException JavaDoc,
55       MissingResourceException JavaDoc {
56
57     Locale JavaDoc loc = context.getLocale(); // Default: browser setting
58
URL JavaDoc url = ResourceLocator.getResource(context.getServletContext(), loc, name);
59     return XmlUtils.parse(url);
60   }
61
62 }
Popular Tags