1 23 24 package org.enhydra.xml.xmlc.deferredparsing; 25 26 import java.io.File ; 27 import java.lang.reflect.Constructor ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 33 import org.enhydra.xml.xmlc.XMLCError; 34 import org.enhydra.xml.xmlc.XMLCLogger; 35 import org.enhydra.xml.xmlc.XMLCRuntimeException; 36 import org.enhydra.xml.xmlc.XMLCStdFactory; 37 import org.enhydra.xml.xmlc.XMLObject; 38 39 45 public class XMLCDeferredParsingFactory extends XMLCStdFactory { 46 49 private static final Class [] CONSTRUCTOR_ARG_TYPES = { 50 DocumentLoader.class 51 }; 52 53 56 private final Object [] CONSTRUCTOR_ARGS; 57 58 61 private List fResourceDirList = new LinkedList (); 62 63 67 private List fPackagePrefixList = new LinkedList (); 68 69 73 private String defaultMetaDataFile = null; 74 75 79 private DynamicClassLoader dynamicClassLoader; 80 81 90 public XMLCDeferredParsingFactory(DocumentLoader docLoader, 91 ClassLoader classLoader, 92 XMLCLogger logger) { 93 super(classLoader, logger); 94 95 if (docLoader == null) { 96 docLoader = new StandardDocumentLoader(Collections.synchronizedMap(new HashMap ())); 97 } 98 CONSTRUCTOR_ARGS = new Object [] { docLoader }; 99 100 dynamicClassLoader = new DynamicClassLoader(classLoader, logger); 101 docLoader.init(this); 103 } 104 105 109 private XMLObject createObject(Class xmlcBasedClass) { 110 try { 111 try { 112 Constructor constructor = xmlcBasedClass.getConstructor(CONSTRUCTOR_ARG_TYPES); 113 return (XMLObject)constructor.newInstance(CONSTRUCTOR_ARGS); 114 } catch (NoSuchMethodException e) { 115 return (XMLObject)xmlcBasedClass.newInstance(); 117 } 118 } catch (Exception except) { 119 throw new XMLCRuntimeException(except); 120 } 121 } 122 123 126 protected XMLObject doCreate(Class xmlcBasedClass) 127 throws ClassNotFoundException , IllegalAccessException , InstantiationException { 128 if (xmlcBasedClass.isInterface()) { 129 return doCreate(xmlcBasedClass.getName() + "Impl", 131 getDefaultClassLoader()); 132 } else { 133 if (!XMLObject.class.isAssignableFrom(xmlcBasedClass)) { 134 throw new XMLCError("class \"" + xmlcBasedClass.getName() 135 + "\" does not implement \"" 136 + XMLObject.class.getName() + "\""); 137 } 138 139 return createObject(xmlcBasedClass); 140 } 141 } 142 143 146 protected XMLObject doCreate(String xmlcClassName, 147 ClassLoader classLoader) 148 throws ClassNotFoundException , IllegalAccessException , InstantiationException { 149 150 if (classLoader != null) { 151 dynamicClassLoader.setDelegate(classLoader); 152 } else { 153 dynamicClassLoader.setDelegate(getClass().getClassLoader()); 154 } 155 156 return doCreate(dynamicClassLoader.loadClass(xmlcClassName)); 157 } 158 159 162 public XMLObject createFromFile (String filename) { 163 String classname = filename.replace('/', '.'); 164 classname = classname.replace('\\', '.'); 165 classname = classname.replace(' ', '_'); 166 classname = "$$XMLC_GENERATED$$." + classname; 167 168 return create(dynamicClassLoader.createClass(classname, filename)); 169 } 170 171 175 public void addResourceDir (String path) { 176 synchronized(fResourceDirList) { 177 File dir = new File (path); 178 if (!fResourceDirList.contains(dir)) { 179 if (dir.exists() && dir.isDirectory()) { 180 fResourceDirList.add(dir); 181 } 182 } 183 } 184 } 185 186 190 public void removeResourceDir (String path) { 191 synchronized(fResourceDirList) { 192 fResourceDirList.remove(path); 193 } 194 } 195 196 199 List getResourceDirList () { 200 return fResourceDirList; 201 } 202 203 207 public void addPackagePrefix (String prefix) { 208 synchronized (fPackagePrefixList) { 209 prefix = prefix.replace('.', File.pathSeparatorChar); 210 if (!fPackagePrefixList.contains(prefix)) { 211 fPackagePrefixList.add(prefix); 212 } 213 } 214 } 215 216 220 public void remove (String prefix) { 221 synchronized (fPackagePrefixList) { 222 prefix = prefix.replace('.', File.pathSeparatorChar); 223 fPackagePrefixList.remove(prefix); 224 } 225 } 226 227 231 List getPackagePrefixList () { 232 return fPackagePrefixList; 233 } 234 235 240 public void setDefaultMetaDataPath(String path) { 241 defaultMetaDataFile = path; 242 } 243 244 248 public String getDefaultMetaDataFile() { 249 return defaultMetaDataFile; 250 } 251 } 252 | Popular Tags |