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

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