1 17 18 19 20 package org.apache.fop.util; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.xmlgraphics.util.Service; 29 30 35 public class ContentHandlerFactoryRegistry { 36 37 38 private static Log log = LogFactory.getLog(ContentHandlerFactoryRegistry.class); 39 40 41 private Map factories = new java.util.HashMap (); 42 43 46 public ContentHandlerFactoryRegistry() { 47 discover(); 48 } 49 50 54 public void addContentHandlerFactory(String classname) { 55 try { 56 ContentHandlerFactory factory 57 = (ContentHandlerFactory)Class.forName(classname).newInstance(); 58 addContentHandlerFactory(factory); 59 } catch (ClassNotFoundException e) { 60 throw new IllegalArgumentException ("Could not find " 61 + classname); 62 } catch (InstantiationException e) { 63 throw new IllegalArgumentException ("Could not instantiate " 64 + classname); 65 } catch (IllegalAccessException e) { 66 throw new IllegalArgumentException ("Could not access " 67 + classname); 68 } catch (ClassCastException e) { 69 throw new IllegalArgumentException (classname 70 + " is not an " 71 + ContentHandlerFactory.class.getName()); 72 } 73 } 74 75 79 public void addContentHandlerFactory(ContentHandlerFactory factory) { 80 String [] ns = factory.getSupportedNamespaces(); 81 for (int i = 0; i < ns.length; i++) { 82 factories.put(ns[i], factory); 83 } 84 } 85 86 91 public ContentHandlerFactory getFactory(String namespaceURI) { 92 ContentHandlerFactory factory = (ContentHandlerFactory)factories.get(namespaceURI); 93 return factory; 94 } 95 96 100 private void discover() { 101 Iterator providers = Service.providers(ContentHandlerFactory.class); 103 if (providers != null) { 104 while (providers.hasNext()) { 105 ContentHandlerFactory factory = (ContentHandlerFactory)providers.next(); 106 try { 107 if (log.isDebugEnabled()) { 108 log.debug("Dynamically adding ContentHandlerFactory: " 109 + factory.getClass().getName()); 110 } 111 addContentHandlerFactory(factory); 112 } catch (IllegalArgumentException e) { 113 log.error("Error while adding ContentHandlerFactory", e); 114 } 115 116 } 117 } 118 } 119 } 120 | Popular Tags |