KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > url > StreamJDKRegistryEntry


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.url;
17
18 import java.awt.Component JavaDoc;
19 import java.awt.Graphics2D JavaDoc;
20 import java.awt.Image JavaDoc;
21 import java.awt.Label JavaDoc;
22 import java.awt.MediaTracker JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.image.RenderedImage JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32 import org.apache.batik.ext.awt.image.renderable.Filter;
33 import org.apache.batik.ext.awt.image.renderable.RedRable;
34 import org.apache.batik.ext.awt.image.spi.AbstractRegistryEntry;
35 import org.apache.batik.ext.awt.image.spi.MagicNumberRegistryEntry;
36 import org.apache.batik.ext.awt.image.spi.URLRegistryEntry;
37 import org.apache.batik.util.ParsedURL;
38
39 /**
40  * This Image tag registy entry is setup to wrap the core JDK Image stream tools.
41  *
42  * @version CVS $Id: StreamJDKRegistryEntry.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public class StreamJDKRegistryEntry extends AbstractRegistryEntry
45     implements URLRegistryEntry {
46
47     /**
48      * The priority of this entry.
49      * This entry should in most cases be the last entry.
50      * but if one wishes one could set a priority higher and be called
51      * afterwords
52      */

53     public final static float PRIORITY =
54         1000*MagicNumberRegistryEntry.PRIORITY;
55
56     public StreamJDKRegistryEntry() {
57         super ("Stream-JDK", PRIORITY, new String JavaDoc[0], new String JavaDoc [] {"image/gif"});
58     }
59
60     /**
61      * Check if the Stream references an image that can be handled by
62      * this format handler. The input stream passed in should be
63      * assumed to support mark and reset.
64      *
65      * If this method throws a StreamCorruptedException then the
66      * InputStream will be closed and a new one opened (if possible).
67      *
68      * This method should only throw a StreamCorruptedException if it
69      * is unable to restore the state of the InputStream
70      * (i.e. mark/reset fails basically).
71      */

72     public boolean isCompatibleURL(ParsedURL purl) {
73         String JavaDoc contentType = purl.getContentType();
74         if (contentType == null) {
75             return false;
76         }
77
78         Iterator JavaDoc iter = this.getMimeTypes().iterator();
79         while (iter.hasNext()) {
80             if (contentType.equals(iter.next())) {
81                 return true;
82             }
83         }
84         return false;
85     }
86
87     /**
88      * Decode the URL into a RenderableImage
89      *
90      * @param purl The URLto decode
91      * @param needRawData If true the image returned should not have
92      * any default color correction the file may
93      * specify applied.
94      */

95     public Filter handleURL(ParsedURL purl, boolean needRawData) {
96         
97         // Read all bytes from the ParsedURL (too bad, there's no Toolkit.createImage(InputStream))
98
InputStream JavaDoc is = null;
99         byte[] buffer = new byte[1024];
100         int len;
101         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
102         try {
103             is = purl.openStream();
104             while((len = is.read(buffer)) != -1) {
105                 bos.write(buffer, 0, len);
106             }
107         } catch(IOException JavaDoc ioe) {
108             return null;
109         } finally {
110             try {
111                 if (is != null) is.close();
112             } catch (Exception JavaDoc e) {}
113         }
114         
115         buffer = bos.toByteArray();
116
117         Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
118         final Image JavaDoc img = tk.createImage(buffer);
119         if (img == null) {
120             return null;
121         }
122
123         RenderedImage JavaDoc ri = loadImage(img);
124         if (ri == null) {
125             return null;
126         }
127         return new RedRable(GraphicsUtil.wrap(ri));
128     }
129
130     // Stuff for Image Loading.
131
static Component JavaDoc mediaComponent = new Label JavaDoc();
132     static MediaTracker JavaDoc mediaTracker = new MediaTracker JavaDoc(mediaComponent);
133     static int id = 0;
134
135     public RenderedImage JavaDoc loadImage(Image JavaDoc img) {
136         // In some cases the image will be a
137
// BufferedImage (subclass of RenderedImage).
138
if (img instanceof RenderedImage JavaDoc) {
139             return (RenderedImage JavaDoc)img;
140         }
141
142         // Setup the mediaTracker.
143
int myID;
144         synchronized (mediaTracker) {
145             myID = id++;
146         }
147
148         // Add our image to the media tracker and wait....
149
mediaTracker.addImage(img, myID);
150         while (true) {
151             try {
152                 mediaTracker.waitForID(myID);
153             } catch(InterruptedException JavaDoc ie) {
154                 // Something woke us up but the image
155
// isn't done yet, so try again.
156
continue;
157             }
158             // All done!
159
break;
160         }
161
162         // Clean up our registraction
163
mediaTracker.removeImage(img, myID);
164
165         if ((img.getWidth(null) == -1)||
166             (img.getHeight(null) == -1)) {
167             return null;
168         }
169
170         // Build the image to .
171
BufferedImage JavaDoc bi = new BufferedImage JavaDoc(img.getWidth(null),
172                                img.getHeight(null),
173                                BufferedImage.TYPE_INT_ARGB);
174         Graphics2D JavaDoc g2d = bi.createGraphics();
175
176         g2d.drawImage(img, 0, 0, null);
177         g2d.dispose();
178         return bi;
179     }
180 }
181
Popular Tags