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

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