1 17 18 19 20 package org.apache.fop.render; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.xmlgraphics.util.Service; 30 31 36 public class XMLHandlerRegistry { 37 38 39 private static Log log = LogFactory.getLog(XMLHandlerRegistry.class); 40 41 42 private Map handlers = new java.util.HashMap (); 43 44 47 public XMLHandlerRegistry() { 48 discoverXMLHandlers(); 49 } 50 51 55 private void setDefaultXMLHandler(XMLHandler handler) { 56 addXMLHandler(XMLHandler.HANDLE_ALL, handler); 57 } 58 59 63 public void addXMLHandler(String classname) { 64 try { 65 XMLHandler handlerInstance = (XMLHandler)Class.forName(classname).newInstance(); 66 addXMLHandler(handlerInstance); 67 } catch (ClassNotFoundException e) { 68 throw new IllegalArgumentException ("Could not find " 69 + classname); 70 } catch (InstantiationException e) { 71 throw new IllegalArgumentException ("Could not instantiate " 72 + classname); 73 } catch (IllegalAccessException e) { 74 throw new IllegalArgumentException ("Could not access " 75 + classname); 76 } catch (ClassCastException e) { 77 throw new IllegalArgumentException (classname 78 + " is not an " 79 + XMLHandler.class.getName()); 80 } 81 } 82 83 87 public void addXMLHandler(XMLHandler handler) { 88 String ns = handler.getNamespace(); 89 if (ns == null) { 90 setDefaultXMLHandler(handler); 91 } else { 92 addXMLHandler(ns, handler); 93 } 94 } 95 96 101 private void addXMLHandler(String ns, 102 XMLHandler handler) { 103 List lst = (List )handlers.get(ns); 104 if (lst == null) { 105 lst = new java.util.ArrayList (); 106 handlers.put(ns, lst); 107 } 108 lst.add(handler); 109 } 110 111 118 public XMLHandler getXMLHandler(Renderer renderer, String ns) { 119 XMLHandler handler; 120 121 List lst = (List )handlers.get(ns); 122 handler = getXMLHandler(renderer, lst); 123 if (handler == null) { 124 lst = (List )handlers.get(XMLHandler.HANDLE_ALL); 125 handler = getXMLHandler(renderer, lst); 126 } 127 return handler; 128 } 129 130 private XMLHandler getXMLHandler(Renderer renderer, List lst) { 131 XMLHandler handler; 132 if (lst != null) { 133 for (int i = 0, c = lst.size(); i < c; i++) { 134 handler = (XMLHandler)lst.get(i); 136 if (handler.supportsRenderer(renderer)) { 137 return handler; 138 } 139 } 140 } 141 return null; } 143 144 148 private void discoverXMLHandlers() { 149 Iterator providers = Service.providers(XMLHandler.class); 151 if (providers != null) { 152 while (providers.hasNext()) { 153 XMLHandler handler = (XMLHandler)providers.next(); 154 try { 155 if (log.isDebugEnabled()) { 156 log.debug("Dynamically adding XMLHandler: " + handler.getClass().getName()); 157 } 158 addXMLHandler(handler); 159 } catch (IllegalArgumentException e) { 160 log.error("Error while adding XMLHandler", e); 161 } 162 163 } 164 } 165 } 166 } 167 | Popular Tags |