1 package com.tonbeller.wcf.form; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.util.Locale ; 6 import java.util.MissingResourceException ; 7 import java.util.ResourceBundle ; 8 9 import org.w3c.dom.Document ; 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 20 public class FormDocument { 21 22 25 public static Document loadDocument(RequestContext context, String bundle, String name) throws MalformedURLException , MissingResourceException { 26 Document doc = parseDocument(context, name); 27 replaceI18n(context, doc, bundle); 28 return doc; 29 } 30 31 34 public static void replaceI18n(RequestContext context, Document dom, String bundle) 35 throws MissingResourceException { 36 37 String bundleAttr = dom.getDocumentElement().getAttribute("bundle"); 38 if (bundleAttr.length() > 0) { 39 bundle = bundleAttr; 40 } 41 42 if (bundle != null) { 43 Locale loc = context.getLocale(); 44 ResourceBundle 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 parseDocument(RequestContext context, String name) throws MalformedURLException , 55 MissingResourceException { 56 57 Locale loc = context.getLocale(); URL url = ResourceLocator.getResource(context.getServletContext(), loc, name); 59 return XmlUtils.parse(url); 60 } 61 62 } | Popular Tags |