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.7KB

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 handlers = new java.util.HashMap();
  35. /**
  36. * Default constructor.
  37. */
  38. public XMLHandlerRegistry() {
  39. discoverXMLHandlers();
  40. }
  41. /**
  42. * Add a default XML handler which is able to handle any namespace.
  43. * @param handler XMLHandler to use
  44. */
  45. private void setDefaultXMLHandler(XMLHandler handler) {
  46. addXMLHandler(XMLHandler.HANDLE_ALL, handler);
  47. }
  48. /**
  49. * Add an XML handler. The handler itself is inspected to find out what it supports.
  50. * @param classname the fully qualified class name
  51. */
  52. public void addXMLHandler(String classname) {
  53. try {
  54. XMLHandler handlerInstance = (XMLHandler)Class.forName(classname).newInstance();
  55. addXMLHandler(handlerInstance);
  56. } catch (ClassNotFoundException e) {
  57. throw new IllegalArgumentException("Could not find "
  58. + classname);
  59. } catch (InstantiationException e) {
  60. throw new IllegalArgumentException("Could not instantiate "
  61. + classname);
  62. } catch (IllegalAccessException e) {
  63. throw new IllegalArgumentException("Could not access "
  64. + classname);
  65. } catch (ClassCastException e) {
  66. throw new IllegalArgumentException(classname
  67. + " is not an "
  68. + XMLHandler.class.getName());
  69. }
  70. }
  71. /**
  72. * Add an XML handler. The handler itself is inspected to find out what it supports.
  73. * @param handler the XMLHandler instance
  74. */
  75. public void addXMLHandler(XMLHandler handler) {
  76. String ns = handler.getNameSpace();
  77. if (ns == null) {
  78. setDefaultXMLHandler(handler);
  79. } else {
  80. addXMLHandler(ns, handler);
  81. }
  82. }
  83. /**
  84. * Add an XML handler for the given MIME type and XML namespace.
  85. * @param ns Namespace URI
  86. * @param handler XMLHandler to use
  87. */
  88. private void addXMLHandler(String ns,
  89. XMLHandler handler) {
  90. List lst = (List)handlers.get(ns);
  91. if (lst == null) {
  92. lst = new java.util.ArrayList();
  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 lst = (List)handlers.get(ns);
  107. handler = getXMLHandler(renderer, lst);
  108. if (handler == null) {
  109. lst = (List)handlers.get(XMLHandler.HANDLE_ALL);
  110. handler = getXMLHandler(renderer, lst);
  111. }
  112. return handler;
  113. }
  114. private XMLHandler getXMLHandler(Renderer renderer, List lst) {
  115. XMLHandler handler;
  116. if (lst != null) {
  117. for (int i = 0, c = lst.size(); i < c; i++) {
  118. //TODO Maybe add priorities later
  119. handler = (XMLHandler)lst.get(i);
  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 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. }