1 51 package org.apache.fop.image; 52 53 import org.apache.fop.image.analyser.ImageReaderFactory; 55 import org.apache.fop.image.analyser.ImageReader; 56 import org.apache.fop.configuration.Configuration; 57 import org.apache.fop.messaging.MessageHandler; 58 59 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.net.URL ; 63 import java.net.MalformedURLException ; 64 import java.lang.reflect.Constructor ; 65 import java.util.Map ; 66 67 71 public class FopImageFactory { 72 protected FopImageFactory() {} 74 75 private static Map m_urlMap = new java.util.HashMap (); 76 77 82 private static String m_genericImageClassName = null; 83 84 91 public static synchronized FopImage Make(String href) 92 throws MalformedURLException , FopImageException { 93 94 101 URL absoluteURL = null; 103 InputStream imgIS = null; 104 href = href.trim(); 105 if(href.startsWith("url(") && (href.indexOf(")") != -1)) { 106 href = href.substring(4, href.indexOf(")")).trim(); 107 if(href.startsWith("'") && href.endsWith("'")) { 108 href = href.substring(1, href.length() - 1); 109 } else if(href.startsWith("\"") && href.endsWith("\"")) { 110 href = href.substring(1, href.length() - 1); 111 } 112 } 113 114 FopImage imageObject = (FopImage)m_urlMap.get(href); 116 if (imageObject != null) 117 return imageObject; 118 119 try { 120 try { 125 absoluteURL = new URL (href); 126 } catch (MalformedURLException mue) { 127 absoluteURL = new URL ("file:" + href); 129 } 130 imgIS = absoluteURL.openStream(); 131 } catch (MalformedURLException e_context) { 132 throw new FopImageException("Error with image URL: " 133 + e_context.getMessage()); 134 } catch (Exception e) { 135 URL baseURL = Configuration.getBaseURL(); 137 138 if (baseURL == null) { 139 throw new FopImageException("Error with image URL: " 140 + e.getMessage() 141 + " and no base URL is specified"); 142 } 143 144 try { 145 163 164 String scheme = baseURL.getProtocol() + ":"; 165 if (href.startsWith(scheme)) { 166 href = href.substring(scheme.length()); 167 } 168 absoluteURL = new URL (baseURL, href); 169 } catch (MalformedURLException e_context) { 170 throw new FopImageException("Invalid Image URL - error on relative URL : " 171 + e_context.getMessage()); 172 } 173 } 174 175 ImageReader imgReader = null; 177 try { 178 if (imgIS == null) { 179 imgIS = absoluteURL.openStream(); 180 } 181 imgReader = ImageReaderFactory.Make(absoluteURL.toExternalForm(), 182 imgIS); 183 } catch (Exception e) { 184 throw new FopImageException("Error while recovering Image Informations (" 185 + absoluteURL.toString() + ") : " 186 + e.getMessage()); 187 } 188 finally { 189 if (imgIS != null) { 190 try { 191 imgIS.close(); 192 } catch (IOException e) {} 193 } 194 } 195 if (imgReader == null) 196 throw new FopImageException("No ImageReader for this type of image (" 197 + absoluteURL.toString() + ")"); 198 String imgMimeType = imgReader.getMimeType(); 200 String imgClassName = null; 201 if ("image/gif".equals(imgMimeType)) { 202 imgClassName = "org.apache.fop.image.GifImage"; 203 } else if ("image/jpeg".equals(imgMimeType)) { 204 imgClassName = "org.apache.fop.image.JpegImage"; 205 } else if ("image/bmp".equals(imgMimeType)) { 206 imgClassName = "org.apache.fop.image.BmpImage"; 207 } else if ("image/png".equals(imgMimeType)) { 208 imgClassName = getGenericImageClassName(); 209 } else if ("image/tga".equals(imgMimeType)) { 210 imgClassName = getGenericImageClassName(); 211 } else if ("image/eps".equals(imgMimeType)) { 212 imgClassName = "org.apache.fop.image.EPSImage"; 213 } else if ("image/tiff".equals(imgMimeType)) { 214 try { 215 imgClassName = "org.apache.fop.image.TiffImage"; 216 Class.forName(imgClassName); 217 } catch (Throwable t) { 218 imgClassName = getGenericImageClassName(); 219 } 220 } else if ("image/svg+xml".equals(imgMimeType)) { 221 imgClassName = "org.apache.fop.image.SVGImage"; 222 } 223 if (imgClassName == null) 224 throw new FopImageException("Unsupported image type (" 225 + absoluteURL.toString() + ") : " 226 + imgMimeType); 227 228 Object imageInstance = null; 231 Class imageClass = null; 232 try { 233 imageClass = Class.forName(imgClassName); 234 Class [] imageConstructorParameters = new Class [2]; 235 imageConstructorParameters[0] = Class.forName("java.net.URL"); 236 imageConstructorParameters[1] = 237 Class.forName("org.apache.fop.image.analyser.ImageReader"); 238 Constructor imageConstructor = 239 imageClass.getDeclaredConstructor(imageConstructorParameters); 240 Object [] initArgs = new Object [2]; 241 initArgs[0] = absoluteURL; 242 initArgs[1] = imgReader; 243 imageInstance = imageConstructor.newInstance(initArgs); 244 } catch (java.lang.reflect.InvocationTargetException ex) { 245 Throwable t = ex.getTargetException(); 246 String msg; 247 if (t != null) { 248 msg = t.getMessage(); 249 } else { 250 msg = ex.getMessage(); 251 } 252 throw new FopImageException("Error creating FopImage object (" 253 + absoluteURL.toString() + ") : " 254 + msg); 255 } catch (Exception ex) { 256 throw new FopImageException("Error creating FopImage object (" 257 + "Error creating FopImage object (" 258 + absoluteURL.toString() + ") : " 259 + ex.getMessage()); 260 } 261 if (!(imageInstance instanceof org.apache.fop.image.FopImage)) { 262 throw new FopImageException("Error creating FopImage object (" 263 + absoluteURL.toString() + ") : " 264 + "class " + imageClass.getName() 265 + " doesn't implement org.apache.fop.image.FopImage interface"); 266 } 267 m_urlMap.put(href, imageInstance); 268 return (FopImage)imageInstance; 269 } 270 271 272 277 private static String getGenericImageClassName() { 278 279 if (m_genericImageClassName == null) { 280 try { 281 Class.forName("org.apache.fop.image.JAIImage"); 283 m_genericImageClassName = "org.apache.fop.image.JAIImage"; 284 } catch (Throwable t) { 285 MessageHandler.logln("JAI support was not installed (read: not " 286 + "present at build time). Trying to use Jimi instead"); 287 288 m_genericImageClassName = "org.apache.fop.image.JimiImage"; 289 } 290 } 291 return m_genericImageClassName; 292 } 293 294 295 298 public static synchronized void resetCache() { 299 m_urlMap.clear(); 300 } 301 } 302 303 | Popular Tags |