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.

AbstractImageHandlerRegistry.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.Comparator;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.image.loader.Image;
  27. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  28. import org.apache.xmlgraphics.util.Service;
  29. /**
  30. * This class holds references to various image handlers used by the renderers. It also
  31. * supports automatic discovery of additional handlers available through
  32. * the class path.
  33. */
  34. public abstract class AbstractImageHandlerRegistry {
  35. /** the logger */
  36. private static Log log = LogFactory.getLog(AbstractImageHandlerRegistry.class);
  37. private static final Comparator HANDLER_COMPARATOR = new Comparator() {
  38. public int compare(Object o1, Object o2) {
  39. ImageHandlerBase h1 = (ImageHandlerBase)o1;
  40. ImageHandlerBase h2 = (ImageHandlerBase)o2;
  41. return h1.getPriority() - h2.getPriority();
  42. }
  43. };
  44. /** Map containing image handlers for various MIME types */
  45. private final Map/*<Class, ImageHandler>*/ handlers
  46. = new java.util.HashMap/*<Class, ImageHandler>*/();
  47. /** List containing the same handlers as above but ordered by priority */
  48. private final List/*<ImageHandler>*/ handlerList
  49. = new java.util.LinkedList/*<ImageHandler>*/();
  50. /** Sorted Set of registered handlers */
  51. private ImageFlavor[] supportedFlavors = new ImageFlavor[0];
  52. private int handlerRegistrations;
  53. private int lastSync;
  54. /**
  55. * Default constructor.
  56. */
  57. public AbstractImageHandlerRegistry() {
  58. discoverHandlers();
  59. }
  60. /**
  61. * Add an ImageHandler. The handler itself is inspected to find out what it supports.
  62. * @param classname the fully qualified class name
  63. */
  64. public void addHandler(String classname) {
  65. try {
  66. ImageHandlerBase handlerInstance
  67. = (ImageHandlerBase)Class.forName(classname).newInstance();
  68. addHandler(handlerInstance);
  69. } catch (ClassNotFoundException e) {
  70. throw new IllegalArgumentException("Could not find "
  71. + classname);
  72. } catch (InstantiationException e) {
  73. throw new IllegalArgumentException("Could not instantiate "
  74. + classname);
  75. } catch (IllegalAccessException e) {
  76. throw new IllegalArgumentException("Could not access "
  77. + classname);
  78. } catch (ClassCastException e) {
  79. throw new IllegalArgumentException(classname
  80. + " is not an "
  81. + getHandlerClass().getName());
  82. }
  83. }
  84. /**
  85. * Add an image handler. The handler itself is inspected to find out what it supports.
  86. * @param handler the ImageHandler instance
  87. */
  88. public synchronized void addHandler(ImageHandlerBase handler) {
  89. this.handlers.put(handler.getSupportedImageClass(), handler);
  90. //Sorted insert
  91. ListIterator iter = this.handlerList.listIterator();
  92. while (iter.hasNext()) {
  93. ImageHandlerBase h = (ImageHandlerBase)iter.next();
  94. if (getHandlerComparator().compare(handler, h) < 0) {
  95. iter.previous();
  96. break;
  97. }
  98. }
  99. iter.add(handler);
  100. this.handlerRegistrations++;
  101. }
  102. /**
  103. * Returns an ImageHandler which handles an specific image type given the MIME type
  104. * of the image.
  105. * @param img the Image to be handled
  106. * @return the ImageHandler responsible for handling the image or null if none is available
  107. */
  108. public ImageHandlerBase getHandler(Image img) {
  109. return getHandler(img.getClass());
  110. }
  111. /**
  112. * Returns an ImageHandler which handles an specific image type given the MIME type
  113. * of the image.
  114. * @param imageClass the Image subclass for which to get a handler
  115. * @return the ImageHandler responsible for handling the image or null if none is available
  116. */
  117. public synchronized ImageHandlerBase getHandler(Class imageClass) {
  118. ImageHandlerBase handler = null;
  119. Class cl = imageClass;
  120. while (cl != null) {
  121. handler = (ImageHandlerBase)handlers.get(cl);
  122. if (handler != null) {
  123. break;
  124. }
  125. cl = cl.getSuperclass();
  126. }
  127. return handler;
  128. }
  129. /**
  130. * Returns the ordered array of supported image flavors.
  131. * @return the array of image flavors
  132. */
  133. public synchronized ImageFlavor[] getSupportedFlavors() {
  134. if (this.lastSync != this.handlerRegistrations) {
  135. //Extract all ImageFlavors into a single array
  136. List flavors = new java.util.ArrayList();
  137. Iterator iter = this.handlerList.iterator();
  138. while (iter.hasNext()) {
  139. ImageFlavor[] f = ((ImageHandlerBase)iter.next()).getSupportedImageFlavors();
  140. for (int i = 0; i < f.length; i++) {
  141. flavors.add(f[i]);
  142. }
  143. }
  144. this.supportedFlavors = (ImageFlavor[])flavors.toArray(new ImageFlavor[flavors.size()]);
  145. this.lastSync = this.handlerRegistrations;
  146. }
  147. return this.supportedFlavors;
  148. }
  149. /**
  150. * Discovers ImageHandler implementations through the classpath and dynamically
  151. * registers them.
  152. */
  153. private void discoverHandlers() {
  154. // add mappings from available services
  155. Class imageHandlerClass = getHandlerClass();
  156. Iterator providers = Service.providers(imageHandlerClass);
  157. if (providers != null) {
  158. while (providers.hasNext()) {
  159. ImageHandlerBase handler = (ImageHandlerBase)providers.next();
  160. try {
  161. if (log.isDebugEnabled()) {
  162. log.debug("Dynamically adding ImageHandler: "
  163. + handler.getClass().getName());
  164. }
  165. addHandler(handler);
  166. } catch (IllegalArgumentException e) {
  167. log.error("Error while adding ImageHandler", e);
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * Returns the ImageHandler comparator
  174. *
  175. * @return the ImageHandler comparator
  176. */
  177. public Comparator getHandlerComparator() {
  178. return HANDLER_COMPARATOR;
  179. }
  180. /**
  181. * Returns the ImageHandler implementing class
  182. *
  183. * @return the ImageHandler implementing class
  184. */
  185. public abstract Class getHandlerClass();
  186. }