001: // ========================================================================
002: // $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.IOException;
021:
022: import org.mortbay.log.Log;
023: import org.mortbay.util.IO;
024:
025: /* ---------------------------------------------------------------- */
026: /** HTML Image Tag.
027: * @see org.mortbay.html.Block
028: * @version $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
029: * @author Greg Wilkins
030: */
031: public class Image extends Tag {
032:
033: /* ------------------------------------------------------------ */
034: public Image(String src) {
035: super ("img");
036: attribute("src", src);
037: }
038:
039: /* ------------------------------------------------------------ */
040: /** Construct from GIF file.
041: */
042: public Image(String dirname, String src) {
043: super ("img");
044: attribute("src", src);
045: setSizeFromGif(dirname, src);
046: }
047:
048: /* ------------------------------------------------------------ */
049: /** Construct from GIF file.
050: */
051: public Image(File gif) {
052: super ("img");
053: attribute("src", gif.getName());
054: setSizeFromGif(gif);
055: }
056:
057: /* ------------------------------------------------------------ */
058: public Image(String src, int width, int height, int border) {
059: this (src);
060: width(width);
061: height(height);
062: border(border);
063: }
064:
065: /* ------------------------------------------------------------ */
066: public Image border(int b) {
067: attribute("border", b);
068: return this ;
069: }
070:
071: /* ------------------------------------------------------------ */
072: public Image alt(String alt) {
073: attribute("alt", alt);
074: return this ;
075: }
076:
077: /* ------------------------------------------------------------ */
078: /** Set the image size from the header of a GIF file.
079: * @param dirname The directory name, expected to be in OS format
080: * @param pathname The image path name relative to the directory.
081: * Expected to be in WWW format (i.e. with slashes)
082: * and will be converted to OS format.
083: */
084: public Image setSizeFromGif(String dirname, String pathname) {
085: String filename = dirname
086: + pathname.replace('/', File.separatorChar);
087: return setSizeFromGif(filename);
088: }
089:
090: /* ------------------------------------------------------------ */
091: /** Set the image size from the header of a GIF file.
092: */
093: public Image setSizeFromGif(String filename) {
094: return setSizeFromGif(new File(filename));
095: }
096:
097: /* ------------------------------------------------------------ */
098: /** Set the image size from the header of a GIF file.
099: */
100: public Image setSizeFromGif(File gif) {
101: if (gif.canRead()) {
102: FileInputStream in = null;
103: try {
104: byte[] buf = new byte[10];
105: in = new FileInputStream(gif);
106: if (in.read(buf, 0, 10) == 10) {
107: if (Log.isDebugEnabled())
108: Log
109: .debug("Image "
110: + gif.getName()
111: + " is "
112: + ((0x00ff & buf[7]) * 256 + (0x00ff & buf[6]))
113: + " x "
114: + (((0x00ff & buf[9]) * 256 + (0x00ff & buf[8]))));
115: width((0x00ff & buf[7]) * 256 + (0x00ff & buf[6]));
116: height(((0x00ff & buf[9]) * 256 + (0x00ff & buf[8])));
117: }
118: } catch (IOException e) {
119: Log.ignore(e);
120: } finally {
121: IO.close(in);
122: }
123: }
124:
125: return this;
126: }
127:
128: }
|