1 23 24 package org.enhydra.xml.xmlc.dom; 25 26 import java.util.HashMap ; 27 28 import org.enhydra.xml.xmlc.XMLCError; 29 import org.w3c.dom.DocumentType ; 30 import org.w3c.dom.Element ; 31 32 39 public class XMLCDomFactoryCache { 40 43 private static final String DEFAULT_XML_DOM_FACTORY_CLASS 44 = "org.enhydra.xml.xmlc.dom.lazydom.LazyDomFactory"; 45 46 49 private static final String DEFAULT_HTML_DOM_FACTORY_CLASS 50 = "org.enhydra.xml.xmlc.dom.lazydom.LazyHTMLDomFactory"; 51 52 55 private static HashMap cache = new HashMap (); 56 57 60 private XMLCDomFactoryCache() { 61 } 62 63 67 private static void handleCreateError(Class factoryClass, 68 Exception except) { 69 throw new XMLCError("Can't create an instance of \"" 70 + factoryClass.getName() + "\": " 71 + except.getClass().getName(), 72 except); 73 } 74 75 79 private static XMLCDomFactory createFactory(Class factoryClass) { 80 try { 81 return (XMLCDomFactory)factoryClass.newInstance(); 82 } catch (IllegalAccessException except) { 83 handleCreateError(factoryClass, except); 84 } catch (InstantiationException except) { 85 handleCreateError(factoryClass, except); 86 } catch (ClassCastException except) { 87 handleCreateError(factoryClass, except); 88 } 89 return null; } 91 92 100 public static XMLCDomFactory createFactory(String factoryClassName, 101 boolean isHtmlDocument) { 102 if (factoryClassName == null) { 103 if (isHtmlDocument) { 104 factoryClassName = DEFAULT_HTML_DOM_FACTORY_CLASS; 105 } else { 106 factoryClassName = DEFAULT_XML_DOM_FACTORY_CLASS; 107 } 108 } 109 try { 110 return createFactory(Class.forName(factoryClassName)); 111 } catch (Exception except) { 112 throw new XMLCError("Can't load class \"" 113 + factoryClassName + "\": " 114 + except.getClass().getName(), 115 except); 116 } 117 } 118 119 122 public static XMLCDomFactory getFactory(Class factoryClass) { 123 XMLCDomFactory factory = (XMLCDomFactory)cache.get(factoryClass); 124 if (factory == null) { 125 factory = createFactory(factoryClass); 126 cache.put(factoryClass, factory); 127 } 128 return factory; 129 } 130 131 134 private static void checkForOutdateMethod(Class factoryClass, 135 String methodName, 136 Class [] methodArgTypes, 137 String methodDesc) { 138 try { 139 factoryClass.getMethod(methodName, methodArgTypes); 140 throw new XMLCError("XMLCDomFactory implementation " 141 + factoryClass.getName() 142 + " contains outdated method " 143 + methodDesc 144 + "; please update your code"); 145 } catch (NoSuchMethodException except) { 146 } 148 } 149 150 159 public static void checkForOutdatedClass(XMLCDomFactory domFactory) { 160 Class factoryClass = domFactory.getClass(); 162 163 checkForOutdateMethod(factoryClass, "createDocument", 165 new Class [] {String .class, DocumentType .class}, 166 "createDocument(String,DocumentType)"); 167 168 checkForOutdateMethod(factoryClass, "getElementURLAttrs", 170 new Class [] {Element .class}, 171 "getElementURLAttrs(Element)"); 172 173 checkForOutdateMethod(factoryClass, "getBaseInterfaceName", 175 null, "getBaseInterfaceName()"); 176 177 checkForOutdateMethod(factoryClass, "needsCreateTextSetMethod", 179 new Class [] {Element .class}, 180 "needsCreateTextSetMethod(Element)"); 181 } 182 } 183 | Popular Tags |