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

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