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

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