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.

ContentHandlerFactoryRegistry.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2006 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.util;
  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 ContentHandlerFactoryRegistry {
  29. /** the logger */
  30. private static Log log = LogFactory.getLog(ContentHandlerFactoryRegistry.class);
  31. /** Map from namespace URIs to ContentHandlerFactories */
  32. private Map factories = new java.util.HashMap();
  33. /**
  34. * Default constructor.
  35. */
  36. public ContentHandlerFactoryRegistry() {
  37. discover();
  38. }
  39. /**
  40. * Add an XML handler. The handler itself is inspected to find out what it supports.
  41. * @param classname the fully qualified class name
  42. */
  43. public void addContentHandlerFactory(String classname) {
  44. try {
  45. ContentHandlerFactory factory
  46. = (ContentHandlerFactory)Class.forName(classname).newInstance();
  47. addContentHandlerFactory(factory);
  48. } catch (ClassNotFoundException e) {
  49. throw new IllegalArgumentException("Could not find "
  50. + classname);
  51. } catch (InstantiationException e) {
  52. throw new IllegalArgumentException("Could not instantiate "
  53. + classname);
  54. } catch (IllegalAccessException e) {
  55. throw new IllegalArgumentException("Could not access "
  56. + classname);
  57. } catch (ClassCastException e) {
  58. throw new IllegalArgumentException(classname
  59. + " is not an "
  60. + ContentHandlerFactory.class.getName());
  61. }
  62. }
  63. /**
  64. * Add an ContentHandlerFactory. The instance is inspected to find out what it supports.
  65. * @param factory the ContentHandlerFactory instance
  66. */
  67. public void addContentHandlerFactory(ContentHandlerFactory factory) {
  68. String[] ns = factory.getSupportedNamespaces();
  69. for (int i = 0; i < ns.length; i++) {
  70. factories.put(ns[i], factory);
  71. }
  72. }
  73. /**
  74. * Retrieves a ContentHandlerFactory instance of a given namespace URI.
  75. * @param namespaceURI the namespace to be handled.
  76. * @return the ContentHandlerFactory or null, if no suitable instance is available.
  77. */
  78. public ContentHandlerFactory getFactory(String namespaceURI) {
  79. ContentHandlerFactory factory = (ContentHandlerFactory)factories.get(namespaceURI);
  80. return factory;
  81. }
  82. /**
  83. * Discovers ContentHandlerFactory implementations through the classpath and dynamically
  84. * registers them.
  85. */
  86. private void discover() {
  87. // add mappings from available services
  88. Iterator providers = Service.providers(ContentHandlerFactory.class);
  89. if (providers != null) {
  90. while (providers.hasNext()) {
  91. String str = (String)providers.next();
  92. try {
  93. if (log.isDebugEnabled()) {
  94. log.debug("Dynamically adding ContentHandlerFactory: " + str);
  95. }
  96. addContentHandlerFactory(str);
  97. } catch (IllegalArgumentException e) {
  98. log.error("Error while adding ContentHandlerFactory", e);
  99. }
  100. }
  101. }
  102. }
  103. }