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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. ImageHandler h1 = (ImageHandler)o1;
  40. ImageHandler h2 = (ImageHandler)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. ImageHandler handlerInstance
  67. = (ImageHandler)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(ImageHandler handler) {
  89. Class[] imageClasses = handler.getSupportedImageClasses();
  90. for (int i = 0; i < imageClasses.length; i++) {
  91. this.handlers.put(imageClasses[i], handler);
  92. }
  93. //Sorted insert
  94. ListIterator iter = this.handlerList.listIterator();
  95. while (iter.hasNext()) {
  96. ImageHandler h = (ImageHandler)iter.next();
  97. if (getHandlerComparator().compare(handler, h) < 0) {
  98. iter.previous();
  99. break;
  100. }
  101. }
  102. iter.add(handler);
  103. this.handlerRegistrations++;
  104. }
  105. /**
  106. * Returns an ImageHandler which handles an specific image type given the MIME type
  107. * of the image.
  108. * @param img the Image to be handled
  109. * @return the ImageHandler responsible for handling the image or null if none is available
  110. */
  111. public ImageHandler getHandler(Image img) {
  112. return getHandler(img.getClass());
  113. }
  114. /**
  115. * Returns an ImageHandler which handles an specific image type given the MIME type
  116. * of the image.
  117. * @param imageClass the Image subclass for which to get a handler
  118. * @return the ImageHandler responsible for handling the image or null if none is available
  119. */
  120. public synchronized ImageHandler getHandler(Class imageClass) {
  121. ImageHandler handler = null;
  122. Class cl = imageClass;
  123. while (cl != null) {
  124. handler = (ImageHandler)handlers.get(cl);
  125. if (handler != null) {
  126. break;
  127. }
  128. cl = cl.getSuperclass();
  129. }
  130. return handler;
  131. }
  132. /**
  133. * Returns the ordered array of supported image flavors.
  134. * @return the array of image flavors
  135. */
  136. public synchronized ImageFlavor[] getSupportedFlavors() {
  137. if (this.lastSync != this.handlerRegistrations) {
  138. //Extract all ImageFlavors into a single array
  139. List flavors = new java.util.ArrayList();
  140. Iterator iter = this.handlerList.iterator();
  141. while (iter.hasNext()) {
  142. ImageFlavor[] f = ((ImageHandler)iter.next()).getSupportedImageFlavors();
  143. for (int i = 0; i < f.length; i++) {
  144. flavors.add(f[i]);
  145. }
  146. }
  147. this.supportedFlavors = (ImageFlavor[])flavors.toArray(new ImageFlavor[flavors.size()]);
  148. this.lastSync = this.handlerRegistrations;
  149. }
  150. return this.supportedFlavors;
  151. }
  152. /**
  153. * Discovers ImageHandler implementations through the classpath and dynamically
  154. * registers them.
  155. */
  156. private void discoverHandlers() {
  157. // add mappings from available services
  158. Class imageHandlerClass = getHandlerClass();
  159. Iterator providers = Service.providers(imageHandlerClass);
  160. if (providers != null) {
  161. while (providers.hasNext()) {
  162. ImageHandler handler = (ImageHandler)providers.next();
  163. try {
  164. if (log.isDebugEnabled()) {
  165. log.debug("Dynamically adding ImageHandler: "
  166. + handler.getClass().getName());
  167. }
  168. addHandler(handler);
  169. } catch (IllegalArgumentException e) {
  170. log.error("Error while adding ImageHandler", e);
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Returns the ImageHandler comparator
  177. *
  178. * @return the ImageHandler comparator
  179. */
  180. public Comparator getHandlerComparator() {
  181. return HANDLER_COMPARATOR;
  182. }
  183. /**
  184. * Returns the ImageHandler implementing class
  185. *
  186. * @return the ImageHandler implementing class
  187. */
  188. public abstract Class getHandlerClass();
  189. }