1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.IOException ; 23 import java.util.*; 24 import org.apache.log4j.Logger; 25 import org.w3c.dom.Document ; 26 27 28 33 public class DefaultDOMLoader implements DOMLoader { 34 35 protected static final Logger logger = Logger.getLogger(DefaultDOMLoader.class.getName()); 36 37 40 protected static DefaultDOMLoader globalLoader = null; 41 42 45 protected static final Object sync = new Object (); 46 47 51 protected static boolean initialized = false; 52 53 56 protected DOMFactory defaultDOMFactory = null; 57 58 62 protected Map factories = null; 63 64 67 protected Map classmap = null; 68 69 74 private DefaultDOMLoader() { 75 defaultDOMFactory = new XMLCDeferredParsingDOMFactory(); 76 factories = new HashMap(); 77 classmap = new HashMap(); 78 } 79 80 83 public Document getDOM(Class clazz) throws IOException { 84 return _getDOM(clazz, null); 85 } 86 87 90 public Document getDOM(Class clazz, Locale locale) throws IOException { 91 return _getDOM(clazz, locale); 92 } 93 94 99 public Document getDOM(String docPath) throws IOException { 100 return _getDOMFromFile(docPath, null); 101 } 102 103 108 public Document getDOM(String docPath, Locale locale) throws IOException { 109 return _getDOMFromFile(docPath, locale); 110 } 111 112 117 private Document _getDOM(Class clazz, Locale locale) throws IOException { 118 if (clazz==null) throw new IOException ("Invalid class: class is null"); 120 return _getDOM(clazz.getName(), doGetLocaleCheck(locale)); 121 } 122 123 128 private Document _getDOM(String className, Locale locale) throws IOException { 129 138 String baseName = className; 139 Class targetClass = null; 140 String targetName = null; 141 String language = locale.getLanguage(); 142 String country = locale.getCountry(); 143 String variant = locale.getVariant(); 144 targetName = baseName+"_"+language+"_"+country+"_"+variant; 145 targetClass = lookupClass(targetName); 146 if (targetClass==null) { 147 targetName = baseName+"_"+language+"_"+country; 148 targetClass = lookupClass(targetName); 149 } 150 if (targetClass==null) { 151 targetName = baseName+"_"+language; 152 targetClass = lookupClass(targetName); 153 } 154 if (targetClass==null) { 155 targetName = baseName; 156 targetClass = lookupClass(targetName); 157 } 158 if (logger.isDebugEnabled()) logger.debug("Target class: "+targetClass); 159 if (targetClass==null) throw new IOException ("Unexpected Invalid class: class is null"); 161 synchronized (factories) { 164 if (logger.isDebugEnabled()) logger.debug("Loading DOM"); 165 DOMFactory df = (DOMFactory) factories.get(targetName); 166 if (df!=null) { 167 return df.getInstance(targetClass); 168 } 169 return defaultDOMFactory.getInstance(targetClass); 170 } 171 } 172 173 178 private Document _getDOMFromFile(String docPath, Locale locale) throws IOException { 179 if (docPath==null) throw new IOException ("Invalid document path:"+docPath); 181 182 synchronized (factories) { 184 if (logger.isDebugEnabled()) logger.debug("Loading DOM"); 185 DOMFactory df = (DOMFactory) factories.get(docPath); 186 if (df!=null) { 187 return df.getInstance(docPath); 188 } 189 return defaultDOMFactory.getInstance(docPath); 190 } 191 } 192 193 197 private static Locale doGetLocaleCheck(Locale locale) throws IOException { 198 if (locale==null) { 200 locale = Locale.getDefault(); 201 if (logger.isDebugEnabled()) logger.debug("Using default locale: "+locale); 202 } 203 return locale; 204 } 205 206 213 protected Class lookupClass(String className) { 214 synchronized (classmap) { 215 Class clazz = (Class ) classmap.get(className); 217 218 if (clazz==null) { 220 try { 221 clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); 222 classmap.put(className, clazz); 223 } catch (Exception e) {} 224 } 225 return clazz; 226 } 227 } 228 229 234 public void setDefaultDOMFactory(DOMFactory df) { 235 if (logger.isDebugEnabled()) logger.debug("Setting default DOM factory:"+df); 236 synchronized (factories) { 237 defaultDOMFactory = df; 238 } 239 } 240 241 248 public void registerDOMFactory(DOMFactory df, String key) { 249 if (logger.isDebugEnabled()) logger.debug("Registering DOM factory:"+df+" for key:"+key); 250 synchronized (factories) { 251 factories.put(key, df); 252 } 253 } 254 255 261 public void deregisterDOMFactory(String key) { 262 if (logger.isDebugEnabled()) logger.debug("Deregistering DOM factory for key:"+key); 263 synchronized (factories) { 264 factories.remove(key); 265 } 266 } 267 268 273 public static DefaultDOMLoader getGlobalInstance() { 274 if (initialized == false) { 275 synchronized (sync) { 276 logger.info("initializing global instance of DefaultDOMLoader"); 277 globalLoader = new DefaultDOMLoader(); 278 initialized = true; 279 } 280 } 281 return globalLoader; 282 } 283 284 } 285 | Popular Tags |