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.

ImageHandlerRegistry.java 6.9KB

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