KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > ResourceImageReference


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * A representation of an image that will be retrieved as a resource from
41  * the CLASSPATH.
42  */

43 public class ResourceImageReference
44 extends StreamImageReference {
45     
46     /**
47      * Size of buffer used for reading image data from CLASSPATH and writing
48      * it to <code>OutputStream</code>s.
49      */

50     private static final int BUFFER_SIZE = 4096;
51     
52     /**
53      * Mapping of extensions to content types.
54      */

55     private static final Map JavaDoc extensionToContentType;
56     static {
57         Map JavaDoc map = new HashMap JavaDoc();
58         map.put("gif", "image/gif");
59         map.put("png", "image/png");
60         map.put("jpeg", "image/jpeg");
61         map.put("jpg", "image/jpg");
62         map.put("bmp", "image/bmp");
63         extensionToContentType = Collections.unmodifiableMap(map);
64     }
65     
66     /**
67      * Automatically determines the content type based on the name of a
68      * resource.
69      *
70      * @param resourceName the resource name
71      * @return the discovered content type
72      * @throws IllegalArgumentException if no content type can be determined
73      */

74     private static final String JavaDoc getContentType(String JavaDoc resourceName) {
75         String JavaDoc contentType;
76     
77         // Determine content type.
78
int extensionDelimiterPosition = resourceName.lastIndexOf(".");
79         if (extensionDelimiterPosition == -1) {
80             throw new IllegalArgumentException JavaDoc("Invalid file extension (resource has no extension: " + resourceName + ")");
81         } else {
82             String JavaDoc extension = resourceName.substring(extensionDelimiterPosition + 1).toLowerCase();
83             contentType = (String JavaDoc) extensionToContentType.get(extension);
84             if (contentType == null) {
85                 throw new IllegalArgumentException JavaDoc("Invalid file extension (no matching content type: " + resourceName + ")");
86             }
87         }
88         
89         return contentType;
90     }
91
92     private Extent width, height;
93     private String JavaDoc contentType;
94     private String JavaDoc resource;
95     private String JavaDoc id;
96     
97     /**
98      * Creates a <code>ResourceImageReference</code>.
99      * The content type will be automatically determined.
100      *
101      * @param resource the resource name containing the binary image data
102      */

103     public ResourceImageReference(String JavaDoc resource) {
104         this(resource, null, null, null);
105     }
106     
107     /**
108      * Creates a <code>ResourceImageReference</code>.
109      *
110      * @param resource the resource name containing the binary image data
111      * (all resource names will be treated as absolute, it is
112      * unnecessary to prepend a leading slash to the resource name)
113      * @param contentType the content type of the image (or null to
114      * automatically determine the content type based on the resource
115      * extension)
116      */

117     public ResourceImageReference(String JavaDoc resource, String JavaDoc contentType) {
118         this(resource, contentType, null, null);
119     }
120
121     /**
122      * Creates a <code>ResourceImageReference</code>.
123      * The content type will be automatically determined.
124      *
125      * @param resource the resource name containing the binary image data
126      * (all resource names will be treated as absolute, it is
127      * unnecessary to prepend a leading slash to the resource name)
128      * @param width the width of the image
129      * @param height the height of the image
130      */

131     public ResourceImageReference(String JavaDoc resource, Extent width, Extent height) {
132         this(resource, null, width, height);
133     }
134
135     /**
136      * Creates a <code>ResourceImageReference</code>.
137      *
138      * @param resource the resource name containing the binary image data
139      * (all resource names will be treated as absolute, it is
140      * unnecessary to prepend a leading slash to the resource name)
141      * @param contentType the content type of the image (or null to
142      * automatically determine the content type based on the resource
143      * extension)
144      * @param width the width of the image
145      * @param height the height of the image
146      */

147     public ResourceImageReference(String JavaDoc resource, String JavaDoc contentType, Extent width, Extent height) {
148         super();
149         
150         // Drop leading slash as all resource paths are absolute.
151
if (resource.charAt(0) == '/') {
152             this.resource = resource.substring(1);
153         } else {
154             this.resource = resource;
155         }
156         
157         this.contentType = contentType == null ? getContentType(resource) : contentType;
158         this.width = width;
159         this.height = height;
160         id = ApplicationInstance.generateSystemId();
161     }
162     
163     /**
164      * @see java.lang.Object#equals(java.lang.Object)
165      */

166     public boolean equals(Object JavaDoc o) {
167         if (!(o instanceof ResourceImageReference)) {
168             return false;
169         }
170         ResourceImageReference that = (ResourceImageReference) o;
171         if (!(this.resource == that.resource || (this.resource != null && this.resource.equals(that.resource)))) {
172             return false;
173         }
174         if (!(this.contentType == that.contentType || (this.contentType != null && this.contentType.equals(that.contentType)))) {
175             return false;
176         }
177         if (!(this.width == that.width || (this.width != null && this.width.equals(that.width)))) {
178             return false;
179         }
180         if (!(this.height == that.height || (this.height != null && this.height.equals(that.height)))) {
181             return false;
182         }
183         return true;
184     }
185     
186     /**
187      * @see nextapp.echo2.app.StreamImageReference#getContentType()
188      */

189     public String JavaDoc getContentType() {
190         return contentType;
191     }
192
193     /**
194      * @see nextapp.echo2.app.ImageReference#getHeight()
195      */

196     public Extent getHeight() {
197         return height;
198     }
199
200     /**
201      * @see nextapp.echo2.app.RenderIdSupport#getRenderId()
202      */

203     public String JavaDoc getRenderId() {
204         return id;
205     }
206     
207     /**
208      * Returns the name of the resource.
209      *
210      * @return the name of the resource
211      */

212     public String JavaDoc getResource() {
213         return resource;
214     }
215     
216     /**
217      * @see nextapp.echo2.app.ImageReference#getWidth()
218      */

219     public Extent getWidth() {
220         return width;
221     }
222
223     /**
224      * @see java.lang.Object#hashCode()
225      */

226     public int hashCode() {
227         return resource == null ? 0 : resource.hashCode();
228     }
229
230     /**
231      * @see nextapp.echo2.app.StreamImageReference#render(java.io.OutputStream)
232      */

233     public void render(OutputStream JavaDoc out)
234     throws IOException JavaDoc {
235         InputStream JavaDoc in = null;
236         byte[] buffer = new byte[BUFFER_SIZE];
237         int bytesRead = 0;
238         
239         try {
240             in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
241             if (in == null) {
242                 throw new IllegalArgumentException JavaDoc("Specified resource does not exist: " + resource + ".");
243             }
244             do {
245                 bytesRead = in.read(buffer);
246                 if (bytesRead > 0) {
247                     out.write(buffer, 0, bytesRead);
248                 }
249             } while (bytesRead > 0);
250         } finally {
251             if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
252         }
253     }
254 }
255
Popular Tags