You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLHandlerRegistry.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.xmlgraphics.util.Service;
  25. /**
  26. * This class holds references to various XML handlers used by FOP. It also
  27. * supports automatic discovery of additional XML handlers available through
  28. * the class path.
  29. */
  30. public class XMLHandlerRegistry {
  31. /** the logger */
  32. private static Log log = LogFactory.getLog(XMLHandlerRegistry.class);
  33. /** Map containing XML handlers for various document types */
  34. private Map<String, List<XMLHandler>> handlers
  35. = new java.util.HashMap<String, List<XMLHandler>>();
  36. /**
  37. * Default constructor.
  38. */
  39. public XMLHandlerRegistry() {
  40. discoverXMLHandlers();
  41. }
  42. /**
  43. * Add a default XML handler which is able to handle any namespace.
  44. * @param handler XMLHandler to use
  45. */
  46. private void setDefaultXMLHandler(XMLHandler handler) {
  47. addXMLHandler(XMLHandler.HANDLE_ALL, handler);
  48. }
  49. /**
  50. * Add an XML handler. The handler itself is inspected to find out what it supports.
  51. * @param classname the fully qualified class name
  52. */
  53. public void addXMLHandler(String classname) {
  54. try {
  55. XMLHandler handlerInstance = (XMLHandler)Class.forName(classname).newInstance();
  56. addXMLHandler(handlerInstance);
  57. } catch (ClassNotFoundException e) {
  58. throw new IllegalArgumentException("Could not find "
  59. + classname);
  60. } catch (InstantiationException e) {
  61. throw new IllegalArgumentException("Could not instantiate "
  62. + classname);
  63. } catch (IllegalAccessException e) {
  64. throw new IllegalArgumentException("Could not access "
  65. + classname);
  66. } catch (ClassCastException e) {
  67. throw new IllegalArgumentException(classname
  68. + " is not an "
  69. + XMLHandler.class.getName());
  70. }
  71. }
  72. /**
  73. * Add an XML handler. The handler itself is inspected to find out what it supports.
  74. * @param handler the XMLHandler instance
  75. */
  76. public void addXMLHandler(XMLHandler handler) {
  77. String ns = handler.getNamespace();
  78. if (ns == null) {
  79. setDefaultXMLHandler(handler);
  80. } else {
  81. addXMLHandler(ns, handler);
  82. }
  83. }
  84. /**
  85. * Add an XML handler for the given MIME type and XML namespace.
  86. * @param ns Namespace URI
  87. * @param handler XMLHandler to use
  88. */
  89. private void addXMLHandler(String ns, XMLHandler handler) {
  90. List<XMLHandler> lst = handlers.get(ns);
  91. if (lst == null) {
  92. lst = new java.util.ArrayList<XMLHandler>();
  93. handlers.put(ns, lst);
  94. }
  95. lst.add(handler);
  96. }
  97. /**
  98. * Returns an XMLHandler which handles an XML dialect of the given namespace and for
  99. * a specified output format defined by its MIME type.
  100. * @param renderer the Renderer for which to retrieve a Renderer
  101. * @param ns the XML namespace associated with the XML to be rendered
  102. * @return the XMLHandler responsible for handling the XML or null if none is available
  103. */
  104. public XMLHandler getXMLHandler(Renderer renderer, String ns) {
  105. XMLHandler handler;
  106. List<XMLHandler> lst = handlers.get(ns);
  107. handler = getXMLHandler(renderer, lst);
  108. if (handler == null) {
  109. lst = handlers.get(XMLHandler.HANDLE_ALL);
  110. handler = getXMLHandler(renderer, lst);
  111. }
  112. return handler;
  113. }
  114. private XMLHandler getXMLHandler(Renderer renderer, List<XMLHandler> lst) {
  115. XMLHandler handler;
  116. if (lst != null) {
  117. for (XMLHandler aLst : lst) {
  118. //TODO Maybe add priorities later
  119. handler = aLst;
  120. if (handler.supportsRenderer(renderer)) {
  121. return handler;
  122. }
  123. }
  124. }
  125. return null; //No handler found
  126. }
  127. /**
  128. * Discovers XMLHandler implementations through the classpath and dynamically
  129. * registers them.
  130. */
  131. private void discoverXMLHandlers() {
  132. // add mappings from available services
  133. Iterator<Object> providers = Service.providers(XMLHandler.class);
  134. if (providers != null) {
  135. while (providers.hasNext()) {
  136. XMLHandler handler = (XMLHandler)providers.next();
  137. try {
  138. if (log.isDebugEnabled()) {
  139. log.debug("Dynamically adding XMLHandler: " + handler.getClass().getName());
  140. }
  141. addXMLHandler(handler);
  142. } catch (IllegalArgumentException e) {
  143. log.error("Error while adding XMLHandler", e);
  144. }
  145. }
  146. }
  147. }
  148. }