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

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