KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > spi > ImageTagRegistry


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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  */

18 package org.apache.batik.ext.awt.image.spi;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.StreamCorruptedException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ListIterator JavaDoc;
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 /**
39  * This class handles the registered Image tag handlers. These are
40  * instances of RegisteryEntry in this package.
41  *
42  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
43  * @version $Id: ImageTagRegistry.java,v 1.17 2004/08/18 07:14:15 vhardy Exp $
44  */

45 public class ImageTagRegistry implements ErrorConstants {
46
47     List JavaDoc entries = new LinkedList JavaDoc();
48     List JavaDoc extensions = null;
49     List JavaDoc 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         // I just realized that this whole thing could
75
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         // System.out.println("Image came from cache" + purl);
89
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 JavaDoc is, ParsedURL purl,
103                           ICCColorSpaceExt colorSpace,
104                           boolean allowOpenStream,
105                           boolean returnBrokenLink) {
106         if ((is != null) && !is.markSupported())
107             // Doesn't support mark so wrap with
108
// BufferedInputStream that does.
109
is = new BufferedInputStream JavaDoc(is);
110
111         // I just realized that this whole thing could
112
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                 // System.out.println("Image came from cache" + purl);
124
if (colorSpace != null)
125                     ret = new ProfileRable(ret, colorSpace);
126                 return ret;
127             }
128         }
129         // System.out.println("Image didn't come from cache: " + purl);
130

131         boolean openFailed = false;
132         List JavaDoc mimeTypes = getRegisteredMimeTypes();
133
134         Iterator JavaDoc 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                     // Check if we got an image.
147
if (ret != null) break;
148                 }
149                 continue;
150             }
151
152             if (re instanceof StreamRegistryEntry) {
153                 StreamRegistryEntry sre = (StreamRegistryEntry)re;
154                 // Quick out last time the open didn't work for this
155
// URL so don't try again...
156
if (openFailed) continue;
157
158                 try {
159                     if (is == null) {
160                         // Haven't opened the stream yet let's try.
161
if ((purl == null) || !allowOpenStream)
162                             break; // No purl nothing we can do...
163
try {
164                             is = purl.openStream(mimeTypes.iterator());
165                         } catch(IOException JavaDoc ioe) {
166                             // Couldn't open the stream, go to next entry.
167
openFailed = true;
168                             continue;
169                         }
170
171                         if (!is.markSupported())
172                             // Doesn't support mark so wrap with
173
// BufferedInputStream that does.
174
is = new BufferedInputStream JavaDoc(is);
175                     }
176
177                     if (sre.isCompatibleStream(is)) {
178                         ret = sre.handleStream(is, purl, needRawData);
179                         if (ret != null) break;
180                     }
181                 } catch (StreamCorruptedException JavaDoc sce) {
182                     // Stream is messed up so setup to reopen it..
183
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                 // Technially it's possible that it's an unknown
197
// 'protocol that caused the open to fail but probably
198
// it's a bad URL...
199
return getBrokenLinkImage(this, ERR_URL_UNREACHABLE,
200                                           new Object JavaDoc[] { purl });
201
202             // We were able to get to the data we just couldn't
203
// make sense of it...
204
return getBrokenLinkImage(this, ERR_URL_UNINTERPRETABLE,
205                                       new Object JavaDoc[] { purl } );
206         }
207
208         if (ret.getProperty(BrokenLinkProvider.BROKEN_LINK_PROPERTY) != null) {
209             // Don't Return Broken link image unless requested
210
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 JavaDoc is) {
220         return readStream(is, null);
221     }
222
223     public Filter readStream(InputStream JavaDoc is, ICCColorSpaceExt colorSpace) {
224         if (!is.markSupported())
225             // Doesn't support mark so wrap with BufferedInputStream that does.
226
is = new BufferedInputStream JavaDoc(is);
227
228         boolean needRawData = (colorSpace != null);
229
230         Filter ret = null;
231
232         Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
279      * Returns a List that contains String of all the extensions that
280      * can be handleded by the various registered image format
281      * handlers.
282      */

283     public synchronized List JavaDoc getRegisteredExtensions() {
284         if (extensions != null)
285             return extensions;
286         
287         extensions = new LinkedList JavaDoc();
288         Iterator JavaDoc 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     /**
298      * Returns a List that contains String of all the mime types that
299      * can be handleded by the various registered image format
300      * handlers.
301      */

302     public synchronized List JavaDoc getRegisteredMimeTypes() {
303         if (mimeTypes != null)
304             return mimeTypes;
305
306         mimeTypes = new LinkedList JavaDoc();
307         Iterator JavaDoc 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 JavaDoc iter = Service.providers(RegistryEntry.class);
330         while (iter.hasNext()) {
331             RegistryEntry re = (RegistryEntry)iter.next();
332             // System.out.println("RE: " + re);
333
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 JavaDoc base, String JavaDoc code, Object JavaDoc [] 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