1 16 package org.apache.cocoon.components.url; 17 18 import java.awt.Component ; 19 import java.awt.Graphics2D ; 20 import java.awt.Image ; 21 import java.awt.Label ; 22 import java.awt.MediaTracker ; 23 import java.awt.Toolkit ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.RenderedImage ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.Iterator ; 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 44 public class StreamJDKRegistryEntry extends AbstractRegistryEntry 45 implements URLRegistryEntry { 46 47 53 public final static float PRIORITY = 54 1000*MagicNumberRegistryEntry.PRIORITY; 55 56 public StreamJDKRegistryEntry() { 57 super ("Stream-JDK", PRIORITY, new String [0], new String [] {"image/gif"}); 58 } 59 60 72 public boolean isCompatibleURL(ParsedURL purl) { 73 String contentType = purl.getContentType(); 74 if (contentType == null) { 75 return false; 76 } 77 78 Iterator 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 95 public Filter handleURL(ParsedURL purl, boolean needRawData) { 96 97 InputStream is = null; 99 byte[] buffer = new byte[1024]; 100 int len; 101 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 102 try { 103 is = purl.openStream(); 104 while((len = is.read(buffer)) != -1) { 105 bos.write(buffer, 0, len); 106 } 107 } catch(IOException ioe) { 108 return null; 109 } finally { 110 try { 111 if (is != null) is.close(); 112 } catch (Exception e) {} 113 } 114 115 buffer = bos.toByteArray(); 116 117 Toolkit tk = Toolkit.getDefaultToolkit(); 118 final Image img = tk.createImage(buffer); 119 if (img == null) { 120 return null; 121 } 122 123 RenderedImage ri = loadImage(img); 124 if (ri == null) { 125 return null; 126 } 127 return new RedRable(GraphicsUtil.wrap(ri)); 128 } 129 130 static Component mediaComponent = new Label (); 132 static MediaTracker mediaTracker = new MediaTracker (mediaComponent); 133 static int id = 0; 134 135 public RenderedImage loadImage(Image img) { 136 if (img instanceof RenderedImage ) { 139 return (RenderedImage )img; 140 } 141 142 int myID; 144 synchronized (mediaTracker) { 145 myID = id++; 146 } 147 148 mediaTracker.addImage(img, myID); 150 while (true) { 151 try { 152 mediaTracker.waitForID(myID); 153 } catch(InterruptedException ie) { 154 continue; 157 } 158 break; 160 } 161 162 mediaTracker.removeImage(img, myID); 164 165 if ((img.getWidth(null) == -1)|| 166 (img.getHeight(null) == -1)) { 167 return null; 168 } 169 170 BufferedImage bi = new BufferedImage (img.getWidth(null), 172 img.getHeight(null), 173 BufferedImage.TYPE_INT_ARGB); 174 Graphics2D g2d = bi.createGraphics(); 175 176 g2d.drawImage(img, 0, 0, null); 177 g2d.dispose(); 178 return bi; 179 } 180 } 181 | Popular Tags |