KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > html > Image


1 // ========================================================================
2
// $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
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
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.html;
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.mortbay.log.LogFactory;
23 import org.mortbay.util.IO;
24 import org.mortbay.util.LogSupport;
25
26 /* ---------------------------------------------------------------- */
27 /** HTML Image Tag.
28  * @see org.mortbay.html.Block
29  * @version $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
30  * @author Greg Wilkins
31 */

32 public class Image extends Tag
33 {
34     private static Log log = LogFactory.getLog(Image.class);
35
36     /* ------------------------------------------------------------ */
37     public Image(String JavaDoc src)
38     {
39         super("img");
40         attribute("src",src);
41     }
42     
43     /* ------------------------------------------------------------ */
44     /** Construct from GIF file.
45      */

46     public Image(String JavaDoc dirname, String JavaDoc src)
47     {
48         super("img");
49         attribute("src",src);
50         setSizeFromGif(dirname,src);
51     }
52     
53     /* ------------------------------------------------------------ */
54     /** Construct from GIF file.
55      */

56     public Image(File JavaDoc gif)
57     {
58         super("img");
59         attribute("src",gif.getName());
60         setSizeFromGif(gif);
61     }
62
63     /* ------------------------------------------------------------ */
64     public Image(String JavaDoc src,int width, int height, int border)
65     {
66         this(src);
67         width(width);
68         height(height);
69         border(border);
70     }
71     
72     /* ------------------------------------------------------------ */
73     public Image border(int b)
74     {
75         attribute("border",b);
76         return this;
77     }
78     
79     /* ------------------------------------------------------------ */
80     public Image alt(String JavaDoc alt)
81     {
82         attribute("alt",alt);
83         return this;
84     }
85     
86     /* ------------------------------------------------------------ */
87     /** Set the image size from the header of a GIF file.
88      * @param dirname The directory name, expected to be in OS format
89      * @param pathname The image path name relative to the directory.
90      * Expected to be in WWW format (i.e. with slashes)
91      * and will be converted to OS format.
92      */

93     public Image setSizeFromGif(String JavaDoc dirname,
94                                 String JavaDoc pathname)
95     {
96         String JavaDoc filename =dirname + pathname.replace('/',File.separatorChar);
97         return setSizeFromGif(filename);
98     }
99     
100     /* ------------------------------------------------------------ */
101     /** Set the image size from the header of a GIF file.
102      */

103     public Image setSizeFromGif(String JavaDoc filename)
104     {
105         return setSizeFromGif(new File JavaDoc(filename));
106     }
107     
108     /* ------------------------------------------------------------ */
109     /** Set the image size from the header of a GIF file.
110      */

111     public Image setSizeFromGif(File JavaDoc gif)
112     {
113         if (gif.canRead())
114         {
115             FileInputStream JavaDoc in = null;
116             try{
117                 byte [] buf = new byte[10];
118                 in = new FileInputStream JavaDoc(gif);
119                 if (in.read(buf,0,10)==10)
120                 {
121                     if(log.isDebugEnabled())log.debug("Image "+gif.getName()+
122                                " is " +
123                                ((0x00ff&buf[7])*256+(0x00ff&buf[6])) +
124                                " x " +
125                                (((0x00ff&buf[9])*256+(0x00ff&buf[8]))));
126                     width((0x00ff&buf[7])*256+(0x00ff&buf[6]));
127                     height(((0x00ff&buf[9])*256+(0x00ff&buf[8])));
128                 }
129             }
130             catch (IOException JavaDoc e){
131                 LogSupport.ignore(log,e);
132             }
133             finally {
134                 IO.close(in);
135             }
136         }
137         
138         return this;
139     }
140     
141 }
142
143
144
145
Popular Tags