1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.StringTokenizer ; 25 import javax.servlet.ServletContext ; 26 import org.w3c.dom.Document ; 27 import org.apache.log4j.Logger; 28 import org.enhydra.xml.xmlc.XMLObject; 29 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory; 30 31 32 59 public class XMLCDeferredParsingDOMFactory implements DOMFactory { 60 61 64 protected static final Logger logger = Logger.getLogger(XMLCDeferredParsingDOMFactory.class.getName()); 65 66 69 protected final String PARAM_XMLC_REPARSE_RESOURCE_DIRS = "xmlcReparseResourceDirs"; 70 71 74 protected final String PARAM_XMLC_REPARSE_PACKAGE_PREFIXES = "xmlcReparsePackagePrefixes"; 75 76 79 protected final String PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH = "xmlcReparseDefaultMetaDataPath"; 80 81 86 protected ServletContext servletContext; 87 88 93 protected String xmlcReparseResourceDirs; 94 95 100 protected String xmlcReparsePackagePrefixes; 101 102 107 protected String xmlcReparseDefaultMetaDataPath; 108 109 112 protected final Object sync = new Object (); 113 114 118 protected boolean initialized = false; 119 120 124 protected XMLCDeferredParsingFactory xmlcFactory; 125 126 136 public Document getInstance(Class clazz) throws IOException { 137 if (!((XMLObject.class).isAssignableFrom(clazz))) throw new IOException ("Class "+clazz.getName()+" can not be loaded by this DOMFactory because it does not implement XMLOBject"); 138 initFactory(); 139 return xmlcFactory.create(clazz); 140 } 141 142 154 public Document getInstance(String docPath) throws IOException { 155 initFactory(); 156 return xmlcFactory.createFromFile(docPath); 157 } 158 159 165 public void setServletContext(ServletContext iservletContext) { 166 this.servletContext = iservletContext; 167 } 168 169 178 public void setXMLCReparseResourceDirs(String ireparseResourceDirs) { 179 this.xmlcReparseResourceDirs = ireparseResourceDirs; 180 } 181 182 192 public void setXMLCReparsePackagePrefixes(String ireparsePackagePrefixes) { 193 this.xmlcReparsePackagePrefixes = ireparsePackagePrefixes; 194 } 195 196 205 public void setXMLCReparseDefaultMetaDataPath(String ireparseDefaultMetaDataPath) { 206 this.xmlcReparseDefaultMetaDataPath = ireparseDefaultMetaDataPath; 207 } 208 209 private void initFactory() { 210 if (initialized == false) { 211 synchronized (sync) { 212 logger.info("initializing an XMLC deferred parsing factory for returning XMLC-generated documents"); 213 xmlcFactory = new XMLCDeferredParsingFactory(null, Thread.currentThread().getContextClassLoader(), null); 214 215 String resDirs = null; 216 if (xmlcReparseResourceDirs != null) { 217 resDirs = xmlcReparseResourceDirs; 218 } else { 219 if (servletContext != null) { 220 resDirs = servletContext.getInitParameter(PARAM_XMLC_REPARSE_RESOURCE_DIRS); 221 } 222 } 223 if (resDirs != null) { 224 StringTokenizer st = new StringTokenizer (resDirs, File.pathSeparator); 225 while (st.hasMoreTokens()) { 226 xmlcFactory.addResourceDir(st.nextToken()); 227 } 228 } 229 230 String pkgPrefixes = null; 231 if (xmlcReparsePackagePrefixes != null) { 232 pkgPrefixes = xmlcReparsePackagePrefixes; 233 } else { 234 if (servletContext != null) { 235 pkgPrefixes = servletContext.getInitParameter(PARAM_XMLC_REPARSE_PACKAGE_PREFIXES); 236 } 237 } 238 if (pkgPrefixes != null) { 239 StringTokenizer st = new StringTokenizer (pkgPrefixes, File.pathSeparator); 240 while (st.hasMoreTokens()) { 241 xmlcFactory.addPackagePrefix(st.nextToken()); 242 } 243 } 244 245 String defaultMetaDataPath = null; 246 if (xmlcReparseDefaultMetaDataPath != null) { 247 defaultMetaDataPath = xmlcReparseDefaultMetaDataPath; 248 } else { 249 if (servletContext != null) { 250 defaultMetaDataPath = servletContext.getInitParameter(PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH); 251 } 252 } 253 if (defaultMetaDataPath != null) { 254 xmlcFactory.setDefaultMetaDataPath(defaultMetaDataPath); 255 } 256 initialized = true; 257 } 258 } 259 } 260 261 } 262 | Popular Tags |