1 18 package org.apache.batik.ext.awt.image.spi; 19 20 import java.io.BufferedInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.StreamCorruptedException ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.ListIterator ; 29 30 import org.apache.batik.ext.awt.color.ICCColorSpaceExt; 31 import org.apache.batik.ext.awt.image.URLImageCache; 32 import org.apache.batik.ext.awt.image.renderable.Filter; 33 import org.apache.batik.ext.awt.image.renderable.ProfileRable; 34 import org.apache.batik.util.ParsedURL; 35 import org.apache.batik.util.Service; 36 37 38 45 public class ImageTagRegistry implements ErrorConstants { 46 47 List entries = new LinkedList (); 48 List extensions = null; 49 List mimeTypes = null; 50 51 URLImageCache rawCache; 52 URLImageCache imgCache; 53 54 public ImageTagRegistry() { 55 this(null, null); 56 } 57 58 public ImageTagRegistry(URLImageCache rawCache, URLImageCache imgCache) { 59 if (rawCache == null) 60 rawCache = new URLImageCache(); 61 if (imgCache == null) 62 imgCache = new URLImageCache(); 63 64 this.rawCache= rawCache; 65 this.imgCache= imgCache; 66 } 67 68 public void flushCache() { 69 rawCache.flush(); 70 imgCache.flush(); 71 } 72 73 public Filter checkCache(ParsedURL purl, ICCColorSpaceExt colorSpace) { 74 boolean needRawData = (colorSpace != null); 76 77 Filter ret = null; 78 URLImageCache cache; 79 if (needRawData) cache = rawCache; 80 else cache = imgCache; 81 82 ret = cache.request(purl); 83 if (ret == null) { 84 cache.clear(purl); 85 return null; 86 } 87 88 if (colorSpace != null) 90 ret = new ProfileRable(ret, colorSpace); 91 return ret; 92 } 93 94 public Filter readURL(ParsedURL purl) { 95 return readURL(null, purl, null, true, true); 96 } 97 98 public Filter readURL(ParsedURL purl, ICCColorSpaceExt colorSpace) { 99 return readURL(null, purl, colorSpace, true, true); 100 } 101 102 public Filter readURL(InputStream is, ParsedURL purl, 103 ICCColorSpaceExt colorSpace, 104 boolean allowOpenStream, 105 boolean returnBrokenLink) { 106 if ((is != null) && !is.markSupported()) 107 is = new BufferedInputStream (is); 110 111 boolean needRawData = (colorSpace != null); 113 114 Filter ret = null; 115 URLImageCache cache = null; 116 117 if (purl != null) { 118 if (needRawData) cache = rawCache; 119 else cache = imgCache; 120 121 ret = cache.request(purl); 122 if (ret != null) { 123 if (colorSpace != null) 125 ret = new ProfileRable(ret, colorSpace); 126 return ret; 127 } 128 } 129 131 boolean openFailed = false; 132 List mimeTypes = getRegisteredMimeTypes(); 133 134 Iterator i; 135 i = entries.iterator(); 136 while (i.hasNext()) { 137 RegistryEntry re = (RegistryEntry)i.next(); 138 139 if (re instanceof URLRegistryEntry) { 140 if ((purl == null) || !allowOpenStream) continue; 141 142 URLRegistryEntry ure = (URLRegistryEntry)re; 143 if (ure.isCompatibleURL(purl)) { 144 ret = ure.handleURL(purl, needRawData); 145 146 if (ret != null) break; 148 } 149 continue; 150 } 151 152 if (re instanceof StreamRegistryEntry) { 153 StreamRegistryEntry sre = (StreamRegistryEntry)re; 154 if (openFailed) continue; 157 158 try { 159 if (is == null) { 160 if ((purl == null) || !allowOpenStream) 162 break; try { 164 is = purl.openStream(mimeTypes.iterator()); 165 } catch(IOException ioe) { 166 openFailed = true; 168 continue; 169 } 170 171 if (!is.markSupported()) 172 is = new BufferedInputStream (is); 175 } 176 177 if (sre.isCompatibleStream(is)) { 178 ret = sre.handleStream(is, purl, needRawData); 179 if (ret != null) break; 180 } 181 } catch (StreamCorruptedException sce) { 182 is = null; 184 } 185 continue; 186 } 187 } 188 189 if (cache != null) 190 cache.put(purl, ret); 191 192 if (ret == null) { 193 if (!returnBrokenLink) 194 return null; 195 if (openFailed) 196 return getBrokenLinkImage(this, ERR_URL_UNREACHABLE, 200 new Object [] { purl }); 201 202 return getBrokenLinkImage(this, ERR_URL_UNINTERPRETABLE, 205 new Object [] { purl } ); 206 } 207 208 if (ret.getProperty(BrokenLinkProvider.BROKEN_LINK_PROPERTY) != null) { 209 return (returnBrokenLink)?ret:null; 211 } 212 213 if (colorSpace != null) 214 ret = new ProfileRable(ret, colorSpace); 215 216 return ret; 217 } 218 219 public Filter readStream(InputStream is) { 220 return readStream(is, null); 221 } 222 223 public Filter readStream(InputStream is, ICCColorSpaceExt colorSpace) { 224 if (!is.markSupported()) 225 is = new BufferedInputStream (is); 227 228 boolean needRawData = (colorSpace != null); 229 230 Filter ret = null; 231 232 Iterator i = entries.iterator(); 233 while (i.hasNext()) { 234 RegistryEntry re = (RegistryEntry)i.next(); 235 if (! (re instanceof StreamRegistryEntry)) 236 continue; 237 StreamRegistryEntry sre = (StreamRegistryEntry)re; 238 239 try { 240 if (sre.isCompatibleStream(is)) { 241 ret = sre.handleStream(is, null, needRawData); 242 243 if (ret != null) break; 244 } 245 } catch (StreamCorruptedException sce) { 246 break; 247 } 248 } 249 250 if (ret == null) 251 return getBrokenLinkImage(this, ERR_STREAM_UNREADABLE, null); 252 253 if ((colorSpace != null) && 254 (ret.getProperty(BrokenLinkProvider.BROKEN_LINK_PROPERTY) == null)) 255 ret = new ProfileRable(ret, colorSpace); 256 257 return ret; 258 } 259 260 public synchronized void register(RegistryEntry newRE) { 261 float priority = newRE.getPriority(); 262 263 ListIterator li; 264 li = entries.listIterator(); 265 while (li.hasNext()) { 266 RegistryEntry re = (RegistryEntry)li.next(); 267 if (re.getPriority() > priority) { 268 li.previous(); 269 li.add(newRE); 270 return; 271 } 272 } 273 li.add(newRE); 274 extensions = null; 275 mimeTypes = null; 276 } 277 278 283 public synchronized List getRegisteredExtensions() { 284 if (extensions != null) 285 return extensions; 286 287 extensions = new LinkedList (); 288 Iterator iter = entries.iterator(); 289 while(iter.hasNext()) { 290 RegistryEntry re = (RegistryEntry)iter.next(); 291 extensions.addAll(re.getStandardExtensions()); 292 } 293 extensions = Collections.unmodifiableList(extensions); 294 return extensions; 295 } 296 297 302 public synchronized List getRegisteredMimeTypes() { 303 if (mimeTypes != null) 304 return mimeTypes; 305 306 mimeTypes = new LinkedList (); 307 Iterator iter = entries.iterator(); 308 while(iter.hasNext()) { 309 RegistryEntry re = (RegistryEntry)iter.next(); 310 mimeTypes.addAll(re.getMimeTypes()); 311 } 312 mimeTypes = Collections.unmodifiableList(mimeTypes); 313 return mimeTypes; 314 } 315 316 static ImageTagRegistry registry = null; 317 318 public synchronized static ImageTagRegistry getRegistry() { 319 if (registry != null) 320 return registry; 321 322 registry = new ImageTagRegistry(); 323 324 registry.register(new PNGRegistryEntry()); 325 registry.register(new TIFFRegistryEntry()); 326 registry.register(new JPEGRegistryEntry()); 327 registry.register(new JDKRegistryEntry()); 328 329 Iterator iter = Service.providers(RegistryEntry.class); 330 while (iter.hasNext()) { 331 RegistryEntry re = (RegistryEntry)iter.next(); 332 registry.register(re); 334 } 335 336 return registry; 337 } 338 339 static BrokenLinkProvider defaultProvider 340 = new DefaultBrokenLinkProvider(); 341 342 static BrokenLinkProvider brokenLinkProvider = null; 343 344 public synchronized static Filter 345 getBrokenLinkImage(Object base, String code, Object [] params) { 346 Filter ret = null; 347 if (brokenLinkProvider != null) 348 ret = brokenLinkProvider.getBrokenLinkImage(base, code, params); 349 350 if (ret == null) 351 ret = defaultProvider.getBrokenLinkImage(base, code, params); 352 353 return ret; 354 } 355 356 357 public synchronized static void 358 setBrokenLinkProvider(BrokenLinkProvider provider) { 359 brokenLinkProvider = provider; 360 } 361 } 362 363 | Popular Tags |