View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: ImgTag.java,v $
31   * Revision 1.5  2005/04/28 18:23:35  colinmacleod
32   * Added server/contextPath rewriting.
33   *
34   * Revision 1.4  2005/04/11 15:12:28  colinmacleod
35   * Added maskProperties reset on doEndTag.
36   *
37   * Revision 1.3  2005/04/09 18:04:20  colinmacleod
38   * Changed copyright text to GPL v2 explicitly.
39   *
40   * Revision 1.2  2005/01/10 19:06:29  colinmacleod
41   * Ordered class members alphabetically.
42   *
43   * Revision 1.1  2005/01/06 23:10:06  colinmacleod
44   * Moved up a version number.
45   * Changed copyright notices to 2005.
46   * Updated the documentation:
47   *   - started working on multiproject:site docu.
48   *   - changed the logo.
49   * Added checkstyle and fixed LOADS of style issues.
50   * Added separate thirdparty subproject.
51   * Added struts (in web), util and webgui (in webtheme) from ivata op.
52   *
53   * Revision 1.6  2004/11/03 16:12:09  colinmacleod
54   * Changed fieldPath to resourceFieldPath.
55   *
56   * Revision 1.5  2004/07/13 19:48:10  colinmacleod
57   * Moved project to POJOs from EJBs.
58   * Applied PicoContainer to services layer (replacing session EJBs).
59   * Applied Hibernate to persistence layer (replacing entity EJBs).
60   *
61   * Revision 1.4  2004/03/21 21:16:35  colinmacleod
62   * Shortened name to ivata op.
63   *
64   * Revision 1.3  2004/02/10 19:57:25  colinmacleod
65   * Changed email address.
66   *
67   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
68   * Added full names to author tags
69   *
70   * Revision 1.1.1.1  2004/01/27 20:59:39  colinmacleod
71   * Moved ivata op to SourceForge.
72   *
73   * Revision 1.2  2003/10/16 15:43:03  jano
74   * Fixes problems with building and some problems with splitting to subprojects
75   *
76   * Revision 1.1.1.1  2003/10/13 20:49:24  colin
77   * Restructured portal into subprojects
78   *
79   * Revision 1.4  2003/06/12 05:53:18  peter
80   * the imageFile is closed at the end now
81   *
82   * Revision 1.3  2003/06/11 11:52:08  peter
83   * resetting of sizes in doEndTag added
84   *
85   * Revision 1.2  2003/06/10 10:45:40  peter
86   * removed awt and swing dependencies, a different - third party method used
87   *
88   * Revision 1.1  2003/06/10 06:17:20  peter
89   * added to cvs
90   * -----------------------------------------------------------------------------
91   */
92  package com.ivata.mask.web.tag.html;
93  import java.io.FileNotFoundException;
94  import java.io.IOException;
95  import java.io.RandomAccessFile;
96  
97  import javax.servlet.http.HttpServletRequest;
98  import javax.servlet.jsp.JspException;
99  
100 import net.freshmeat.imageinfo.ImageInfo;
101 
102 import org.apache.log4j.Logger;
103 
104 import com.ivata.mask.util.StringHandling;
105 import com.ivata.mask.web.RewriteHandling;
106 /***
107  * <p>
108  * The tag inherits everything from its <code>struts</code> equivalent, but it
109  * tries to find out the missing size information from the image headers to make
110  * client rendering faster.
111  * </p>
112  *
113  * @since ivata masks 0.4 (2003-06-08)
114  * @author Peter Illes
115  * @version $Revision: 1.5 $
116  */
117 public class ImgTag extends org.apache.struts.taglib.html.ImgTag {
118     /***
119      * Refer to {@link Logger}.
120      */
121     private Logger logger = Logger.getLogger(ImgTag.class);
122     /***
123      * <p>
124      * Stores and maintains <strong>ivata op </strong> specific mask properties.
125      * </p>
126      */
127     private MaskProperties maskProperties = new MaskProperties();
128     /***
129      * <p>
130      * Overridden to null the width and height values to prevent their reuse.
131      * </p>
132      *
133      * @return value returned by superclass method.
134      * @throws JspException Refer to
135      * {@link org.apache.struts.taglib.html.ImgTag#doEndTag}.
136      */
137     public int doEndTag() throws JspException {
138         int returnValue = super.doEndTag();
139         setHeight(null);
140         setWidth(null);
141         maskProperties.reset(this);
142         return returnValue;
143     }
144     /***
145      * <p>
146      * Overridden to set the value of height and width , when not specified in
147      * input parameters.
148      * </p>
149      *
150      * @return value returned by superclass method.
151      * @throws JspException wraps any exception looking for or accesing the
152      * image file
153      */
154     public int doStartTag() throws JspException {
155         String width = getWidth();
156         String height = getHeight();
157         // we want to have the sizes specified, and we want to have both
158         if (StringHandling.isNullOrEmpty(width)
159                 || StringHandling.isNullOrEmpty(height)) {
160             // we work with page only for now
161             // TODO: extend this to src , srcKey etc...
162             if (!StringHandling.isNullOrEmpty(page)) {
163                 String imgLocation = pageContext.getServletContext()
164                         .getRealPath(page);
165                 try {
166                     RandomAccessFile imageFile = new RandomAccessFile(
167                             imgLocation, "r");
168                     ImageInfo imageInfo = new ImageInfo();
169                     imageInfo.setInput(imageFile);
170                     if (imageInfo.check()) {
171                         if (StringHandling.isNullOrEmpty(width)) {
172                             setWidth(String.valueOf(imageInfo.getWidth()));
173                         }
174                         if (StringHandling.isNullOrEmpty(height)) {
175                             setHeight(String.valueOf(imageInfo.getHeight()));
176                         }
177                     }
178                     imageInfo = null;
179                     imageFile.close();
180                     // when the image is not there, it might be an error in
181                     // HTML, or the image is not local (we tried to test this
182                     // - page test)
183                 } catch (FileNotFoundException e) {
184                     String message =
185                         "File not found exception looking for image '"
186                         + imgLocation
187                         + "'";
188                     logger.error(message, e);
189                 } catch (IOException e) {
190                     String message = "IOException opening image '"
191                         + imgLocation
192                         + "'";
193                     logger.error(message, e);
194                     throw new JspException(message, e);
195                 }
196             }
197         }
198         maskProperties.doStartTag(this, pageContext);
199         // Evaluate the body of this tag
200         return super.doStartTag();
201     }
202     /***
203      * Get the <code>src</code> attribute URL.
204      *
205      * @exception JspException if the URL cannot be calculated.
206      * @return src URL or <code>null</code> if none is applicable.
207      */
208     protected String src() throws JspException {
209         return RewriteHandling.rewriteURL(super.src(),
210                 ((HttpServletRequest)
211                         pageContext.getRequest()).getContextPath());
212     }
213 }
214