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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Map;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.fop.util.Service;
  23. /**
  24. * This class holds references to various XML handlers used by FOP. It also
  25. * supports automatic discovery of additional XML handlers available through
  26. * the class path.
  27. */
  28. public class XMLHandlerRegistry {
  29. /** the logger */
  30. private static Log log = LogFactory.getLog(XMLHandlerRegistry.class);
  31. /** Map containing XML handlers for various document types */
  32. private Map handlers = new java.util.HashMap();
  33. /**
  34. * Default constructor.
  35. */
  36. public XMLHandlerRegistry() {
  37. discoverXMLHandlers();
  38. }
  39. /**
  40. * Set the default XML handler for the given MIME type.
  41. * @param mime MIME type
  42. * @param handler XMLHandler to use
  43. */
  44. private void setDefaultXMLHandler(String mime,
  45. XMLHandler handler) {
  46. addXMLHandler(mime, 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 =
  55. (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 mime = handler.getMimeType();
  78. String ns = handler.getNamespace();
  79. if (ns == null) {
  80. setDefaultXMLHandler(mime, handler);
  81. } else {
  82. addXMLHandler(mime, ns, handler);
  83. }
  84. }
  85. /**
  86. * Add an XML handler for the given MIME type and XML namespace.
  87. * @param mime MIME type
  88. * @param ns Namespace URI
  89. * @param handler XMLHandler to use
  90. */
  91. private void addXMLHandler(String mime, String ns,
  92. XMLHandler handler) {
  93. Map mh = (Map)handlers.get(mime);
  94. if (mh == null) {
  95. mh = new java.util.HashMap();
  96. handlers.put(mime, mh);
  97. }
  98. mh.put(ns, handler);
  99. }
  100. /**
  101. * Returns an XMLHandler which handles an XML dialect of the given namespace and for
  102. * a specified output format defined by its MIME type.
  103. * @param mime the MIME type of the output format
  104. * @param ns the XML namespace associated with the XML to be rendered
  105. * @return the XMLHandler responsible for handling the XML or null if none is available
  106. */
  107. public XMLHandler getXMLHandler(String mime, String ns) {
  108. XMLHandler handler = null;
  109. Map mh = (Map)handlers.get(mime);
  110. if (mh != null) {
  111. handler = (XMLHandler)mh.get(ns);
  112. if (handler == null) {
  113. handler = (XMLHandler)mh.get(XMLHandler.HANDLE_ALL);
  114. }
  115. }
  116. if (handler == null) {
  117. mh = (Map)handlers.get(XMLHandler.HANDLE_ALL);
  118. if (mh != null) {
  119. handler = (XMLHandler)mh.get(ns);
  120. if (handler == null) {
  121. handler = (XMLHandler)mh.get(XMLHandler.HANDLE_ALL);
  122. }
  123. }
  124. }
  125. return handler;
  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 =
  134. Service.providers(XMLHandler.class);
  135. if (providers != null) {
  136. while (providers.hasNext()) {
  137. String str = (String)providers.next();
  138. try {
  139. if (log.isDebugEnabled()) {
  140. log.debug("Dynamically adding XMLHandler: " + str);
  141. }
  142. addXMLHandler(str);
  143. } catch (IllegalArgumentException e) {
  144. log.error("Error while adding XMLHandler", e);
  145. }
  146. }
  147. }
  148. }
  149. }