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.

FopImageFactory.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.image;
  8. // Java
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.File;
  12. import java.net.URL;
  13. import java.net.MalformedURLException;
  14. import java.lang.reflect.Constructor;
  15. import java.util.Hashtable;
  16. // FOP
  17. import org.apache.fop.image.analyser.ImageReaderFactory;
  18. import org.apache.fop.image.analyser.ImageReader;
  19. import org.apache.fop.configuration.Configuration;
  20. /*
  21. handle context: base dir, logger, caching
  22. */
  23. /**
  24. * create FopImage objects (with a configuration file - not yet implemented).
  25. * @author Eric SCHAEFFER
  26. */
  27. public class FopImageFactory {
  28. private static Hashtable m_urlMap = new Hashtable();
  29. /**
  30. * create an FopImage objects.
  31. * @param href image URL as a String
  32. * @return a new FopImage object
  33. * @exception java.net.MalformedURLException bad URL
  34. * @exception FopImageException an error occured during construction
  35. */
  36. public static FopImage Make(String href)
  37. throws MalformedURLException, FopImageException {
  38. /*
  39. * According to section 5.11 a <uri-specification> is:
  40. * "url(" + URI + ")"
  41. * according to 7.28.7 a <uri-specification> is:
  42. * URI
  43. * So handle both.
  44. */
  45. // Get the absolute URL
  46. URL absoluteURL = null;
  47. InputStream imgIS = null;
  48. href = href.trim();
  49. if(href.startsWith("url(") && (href.indexOf(")") != -1)) {
  50. href = href.substring(4, href.indexOf(")")).trim();
  51. if(href.startsWith("'") && href.endsWith("'")) {
  52. href = href.substring(1, href.length() - 1);
  53. } else if(href.startsWith("\"") && href.endsWith("\"")) {
  54. href = href.substring(1, href.length() - 1);
  55. }
  56. }
  57. try {
  58. // try url as complete first, this can cause
  59. // a problem with relative uri's if there is an
  60. // image relative to where fop is run and relative
  61. // to the base dir of the document
  62. try {
  63. absoluteURL = new URL(href);
  64. } catch (MalformedURLException mue) {
  65. // if the href contains only a path then file is assumed
  66. absoluteURL = new URL("file:" + href);
  67. }
  68. imgIS = absoluteURL.openStream();
  69. } catch (MalformedURLException e_context) {
  70. throw new FopImageException("Error with image URL: "
  71. + e_context.getMessage());
  72. } catch (Exception e) {
  73. // maybe relative
  74. URL context_url = null;
  75. String base = Configuration.getStringValue("baseDir");
  76. if(base == null) {
  77. throw new FopImageException("Error with image URL: "
  78. + e.getMessage()
  79. + " and no base directory is specified");
  80. }
  81. try {
  82. absoluteURL = new URL(Configuration.getStringValue("baseDir")
  83. + absoluteURL.getFile());
  84. } catch (MalformedURLException e_context) {
  85. // pb context url
  86. throw new FopImageException("Invalid Image URL - error on relative URL : "
  87. + e_context.getMessage());
  88. }
  89. }
  90. // check if already created
  91. FopImage imageObject = (FopImage)m_urlMap.get(absoluteURL.toString());
  92. if (imageObject != null)
  93. return imageObject;
  94. // If not, check image type
  95. ImageReader imgReader = null;
  96. try {
  97. if (imgIS == null) {
  98. imgIS = absoluteURL.openStream();
  99. }
  100. imgReader = ImageReaderFactory.Make(absoluteURL.toExternalForm(),
  101. imgIS);
  102. } catch (Exception e) {
  103. throw new FopImageException("Error while recovering Image Informations ("
  104. + absoluteURL.toString() + ") : "
  105. + e.getMessage());
  106. }
  107. finally {
  108. if (imgIS != null) {
  109. try {
  110. imgIS.close();
  111. } catch (IOException e) {}
  112. }
  113. }
  114. if (imgReader == null)
  115. throw new FopImageException("No ImageReader for this type of image ("
  116. + absoluteURL.toString() + ")");
  117. // Associate mime-type to FopImage class
  118. String imgMimeType = imgReader.getMimeType();
  119. String imgClassName = null;
  120. if ("image/gif".equals(imgMimeType)) {
  121. imgClassName = "org.apache.fop.image.GifImage";
  122. // imgClassName = "org.apache.fop.image.JAIImage";
  123. } else if ("image/jpeg".equals(imgMimeType)) {
  124. imgClassName = "org.apache.fop.image.JpegImage";
  125. // imgClassName = "org.apache.fop.image.JAIImage";
  126. } else if ("image/bmp".equals(imgMimeType)) {
  127. imgClassName = "org.apache.fop.image.BmpImage";
  128. // imgClassName = "org.apache.fop.image.JAIImage";
  129. } else if ("image/eps".equals(imgMimeType)) {
  130. imgClassName = "org.apache.fop.image.EPSImage";
  131. } else if ("image/png".equals(imgMimeType)) {
  132. imgClassName = "org.apache.fop.image.JimiImage";
  133. // imgClassName = "org.apache.fop.image.JAIImage";
  134. } else if ("image/tga".equals(imgMimeType)) {
  135. imgClassName = "org.apache.fop.image.JimiImage";
  136. // imgClassName = "org.apache.fop.image.JAIImage";
  137. } else if ("image/tiff".equals(imgMimeType)) {
  138. imgClassName = "org.apache.fop.image.JimiImage";
  139. // imgClassName = "org.apache.fop.image.JAIImage";
  140. } else if ("image/svg+xml".equals(imgMimeType)) {
  141. imgClassName = "org.apache.fop.image.SVGImage";
  142. }
  143. if (imgClassName == null)
  144. throw new FopImageException("Unsupported image type ("
  145. + absoluteURL.toString() + ") : "
  146. + imgMimeType);
  147. // load the right image class
  148. // return new <FopImage implementing class>
  149. Object imageInstance = null;
  150. Class imageClass = null;
  151. try {
  152. imageClass = Class.forName(imgClassName);
  153. Class[] imageConstructorParameters = new Class[2];
  154. imageConstructorParameters[0] = Class.forName("java.net.URL");
  155. imageConstructorParameters[1] =
  156. Class.forName("org.apache.fop.image.analyser.ImageReader");
  157. Constructor imageConstructor =
  158. imageClass.getDeclaredConstructor(imageConstructorParameters);
  159. Object[] initArgs = new Object[2];
  160. initArgs[0] = absoluteURL;
  161. initArgs[1] = imgReader;
  162. imageInstance = imageConstructor.newInstance(initArgs);
  163. } catch (java.lang.reflect.InvocationTargetException ex) {
  164. Throwable t = ex.getTargetException();
  165. String msg;
  166. if (t != null) {
  167. msg = t.getMessage();
  168. } else {
  169. msg = ex.getMessage();
  170. }
  171. throw new FopImageException("Error creating FopImage object ("
  172. + absoluteURL.toString() + ") : "
  173. + msg);
  174. } catch (Exception ex) {
  175. throw new FopImageException("Error creating FopImage object ("
  176. + "Error creating FopImage object ("
  177. + absoluteURL.toString() + ") : "
  178. + ex.getMessage());
  179. }
  180. if (!(imageInstance instanceof org.apache.fop.image.FopImage)) {
  181. throw new FopImageException("Error creating FopImage object ("
  182. + absoluteURL.toString() + ") : "
  183. + "class " + imageClass.getName()
  184. + " doesn't implement org.apache.fop.image.FopImage interface");
  185. }
  186. m_urlMap.put(absoluteURL.toString(), imageInstance);
  187. return (FopImage)imageInstance;
  188. }
  189. }