KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagImage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagImage.java,v $
3  * Date : $Date: 2006/09/22 15:17:06 $
4  * Version: $Revision: 1.4 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.loader.CmsImageScaler;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42 import org.opencms.util.CmsStringUtil;
43
44 import java.util.Arrays JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import javax.servlet.ServletRequest JavaDoc;
51 import javax.servlet.jsp.JspException JavaDoc;
52 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
53
54 import org.apache.commons.logging.Log;
55
56 /**
57  * Creates HTML code for &lt;img src&gt; tags that use the OpenCms image scaling capabilities.<p>
58  *
59  * @author Alexander Kandzior
60  *
61  * @version $Revision: 1.4 $
62  *
63  * @since 6.2.0
64  */

65 public class CmsJspTagImage extends BodyTagSupport JavaDoc implements I_CmsJspTagParamParent {
66
67     // optional HTML attribute constants
68
private static final String JavaDoc ATTR_ALIGN = "align";
69     private static final String JavaDoc ATTR_ALT = "alt";
70     private static final String JavaDoc ATTR_BORDER = "border";
71     private static final String JavaDoc ATTR_HSPACE = "hspace";
72     private static final String JavaDoc ATTR_ID = "id";
73     private static final String JavaDoc ATTR_LONGDESC = "longdesc";
74     private static final String JavaDoc ATTR_NAME = "name";
75     private static final String JavaDoc ATTR_STYLE = "style";
76     private static final String JavaDoc ATTR_TITLE = "title";
77     private static final String JavaDoc ATTR_USEMAP = "usemap";
78     private static final String JavaDoc ATTR_VSPACE = "vspace";
79
80     /** The log object for this class. */
81     private static final Log LOG = CmsLog.getLog(CmsJspTagImage.class);
82
83     // image scaler required attribute constants
84
private static final String JavaDoc SCALE_ATTR_COLOR = "scalecolor";
85     private static final String JavaDoc SCALE_ATTR_FILTER = "scalefilter";
86     private static final String JavaDoc SCALE_ATTR_HEIGHT = "height";
87     private static final String JavaDoc SCALE_ATTR_PARTIALTAG = "partialtag";
88     private static final String JavaDoc SCALE_ATTR_POSITION = "scaleposition";
89     private static final String JavaDoc SCALE_ATTR_QUALITY = "scalequality";
90     private static final String JavaDoc SCALE_ATTR_RENDERMODE = "scalerendermode";
91     private static final String JavaDoc SCALE_ATTR_SRC = "src";
92     private static final String JavaDoc SCALE_ATTR_TYPE = "scaletype";
93     private static final String JavaDoc SCALE_ATTR_WIDTH = "width";
94
95     // lists for fast lookup
96
private static final String JavaDoc[] SCALER_ATTRS = {
97         SCALE_ATTR_COLOR,
98         SCALE_ATTR_FILTER,
99         SCALE_ATTR_HEIGHT,
100         SCALE_ATTR_PARTIALTAG,
101         SCALE_ATTR_POSITION,
102         SCALE_ATTR_QUALITY,
103         SCALE_ATTR_RENDERMODE,
104         SCALE_ATTR_SRC,
105         SCALE_ATTR_TYPE,
106         SCALE_ATTR_WIDTH};
107     private static final List JavaDoc SCALER_ATTRS_LIST = Arrays.asList(SCALER_ATTRS);
108
109     /** Serial version UID required for safe serialization. */
110     private static final long serialVersionUID = 6513320107441256414L;
111
112     /** Map with additionally set image attributes not needed by the image scaler. */
113     private Map JavaDoc m_attributes;
114
115     /** Controls if the created HTML image tag is a full or partial tag. */
116     private boolean m_partialTag;
117
118     /** The given image scaler parameters. */
119     private CmsImageScaler m_scaler;
120
121     /** The image source. */
122     private String JavaDoc m_src;
123
124     /**
125      * Creates a new image scaling tag instance.<p>
126      */

127     public CmsJspTagImage() {
128
129         // initialize the image scaler parameter container
130
m_scaler = new CmsImageScaler();
131     }
132
133     /**
134      * Internal action method to create the tag content.<p>
135      *
136      * @param src the image source
137      * @param scaler the image scaleing parameters
138      * @param attributes the additional image HTML attributes
139      * @param partialTag if <code>true</code>, the opening <code>&lt;img</code> and closing <code> /&gt;</code> is omitted
140      * @param req the current request
141      *
142      * @return the created &lt;img src&gt; tag content
143      *
144      * @throws CmsException in case something goes wrong
145      */

146     public static String JavaDoc imageTagAction(
147         String JavaDoc src,
148         CmsImageScaler scaler,
149         Map JavaDoc attributes,
150         boolean partialTag,
151         ServletRequest JavaDoc req) throws CmsException {
152
153         CmsFlexController controller = CmsFlexController.getController(req);
154         CmsObject cms = controller.getCmsObject();
155         CmsResource imageRes = cms.readResource(src);
156
157         // calculate target scale dimensions (if required)
158
if ((scaler.getHeight() <= 0) || (scaler.getWidth() <= 0)) {
159             // read the image properties for the selected resource
160
CmsImageScaler original = new CmsImageScaler(cms, imageRes);
161             if (original.isValid()) {
162                 scaler = original.getReScaler(scaler);
163             }
164         }
165
166         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
167         if (!partialTag) {
168             // open tag if not a partial tag
169
result.append("<img");
170         }
171
172         // append the image source
173
result.append(" SRC=\"");
174         String JavaDoc imageLink = cms.getSitePath(imageRes);
175         if (scaler.isValid()) {
176             // now append the scaler parameters
177
imageLink += scaler.toRequestParam();
178         }
179         result.append(OpenCms.getLinkManager().substituteLink(cms, imageLink));
180         result.append("\"");
181
182         if (scaler.isValid()) {
183             // append image width and height
184
result.append(" width=\"");
185             result.append(scaler.getWidth());
186             result.append("\"");
187             result.append(" height=\"");
188             result.append(scaler.getHeight());
189             result.append("\"");
190         }
191
192         if (attributes != null) {
193             // append the HTML attributes
194
Iterator JavaDoc i = attributes.keySet().iterator();
195             while (i.hasNext()) {
196                 String JavaDoc attr = (String JavaDoc)i.next();
197                 String JavaDoc value = (String JavaDoc)attributes.get(attr);
198                 result.append(" ");
199                 result.append(attr);
200                 result.append("=\"");
201                 result.append(CmsEncoder.escapeXml(value));
202                 result.append("\"");
203             }
204         }
205
206         if (!partialTag) {
207             // close tag if not a partial tag
208
result.append(" />");
209         }
210         return result.toString();
211     }
212
213     /**
214      * @see org.opencms.jsp.I_CmsJspTagParamParent#addParameter(java.lang.String, java.lang.String)
215      */

216     public void addParameter(String JavaDoc name, String JavaDoc value) {
217
218         String JavaDoc key = name.trim().toLowerCase();
219         switch (SCALER_ATTRS_LIST.indexOf(key)) {
220             case 0: // scaleColor
221
setScaleColor(value);
222                 break;
223             case 1: // scaleFilter
224
setScaleFilter(value);
225                 break;
226             case 2: // height
227
setHeight(value);
228                 break;
229             case 3: // partialTag
230
setPartialTag(value);
231                 break;
232             case 4: // scalePosition
233
setScalePosition(value);
234                 break;
235             case 5: // scaleQuality
236
setScaleQuality(value);
237                 break;
238             case 6: // scaleRendermode
239
setScaleRendermode(value);
240                 break;
241             case 7: // src
242
setSrc(value);
243                 break;
244             case 8: // scaleType
245
setScaleType(value);
246                 break;
247             case 9: // width
248
setWidth(value);
249                 break;
250             default: // no a value used by the image scaler, treat as HTML attribute
251
setAttribute(key, value);
252         }
253     }
254
255     /**
256      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
257      */

258     public int doEndTag() throws JspException JavaDoc {
259
260         ServletRequest JavaDoc req = pageContext.getRequest();
261
262         // this will always be true if the page is called through OpenCms
263
if (CmsFlexController.isCmsRequest(req)) {
264
265             try {
266                 // create the HTML image tag
267
String JavaDoc imageTag = null;
268                 try {
269                     imageTag = imageTagAction(m_src, m_scaler, m_attributes, m_partialTag, req);
270                 } catch (CmsException e) {
271                     // any issue accessing the VFS - just return an empty string
272
// otherwise template layout will get mixed up with nasty exception messages
273
if (LOG.isWarnEnabled()) {
274                         LOG.warn(Messages.get().getBundle().key(Messages.ERR_IMAGE_TAG_VFS_ACCESS_1, m_src), e);
275                     }
276                 }
277                 // make sure that no null String is returned
278
pageContext.getOut().print(imageTag == null ? "" : imageTag);
279
280             } catch (Exception JavaDoc ex) {
281                 if (LOG.isErrorEnabled()) {
282                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "image"), ex);
283                 }
284                 throw new javax.servlet.jsp.JspException JavaDoc(ex);
285             }
286         }
287         // need to release manually, JSP container may not call release as required (happens with Tomcat)
288
release();
289         return EVAL_PAGE;
290     }
291
292     /**
293      * Returns <code>{@link #EVAL_BODY_BUFFERED}</code>.<p>
294      *
295      * @return <code>{@link #EVAL_BODY_BUFFERED}</code>
296      *
297      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
298      */

299     public int doStartTag() {
300
301         return EVAL_BODY_BUFFERED;
302     }
303
304     /**
305      * Returns the value of the HTML "align" attribute.<p>
306      *
307      * @return the value of the HTML "align" attribute
308      */

309     public String JavaDoc getAlign() {
310
311         return getAttribute(ATTR_ALIGN);
312     }
313
314     /**
315      * Returns the value of the HTML "alt" attribute.<p>
316      *
317      * @return the value of the HTML "alt" attribute
318      */

319     public String JavaDoc getAlt() {
320
321         return getAttribute(ATTR_ALT);
322     }
323
324     /**
325      * Returns the value of the HTML "border" attribute.<p>
326      *
327      * @return the value of the HTML "border" attribute
328      */

329     public String JavaDoc getBorder() {
330
331         return getAttribute(ATTR_BORDER);
332     }
333
334     /**
335      * Returns the scaling height for the image.<p>
336      *
337      * @return the scaling height for the image
338      */

339     public String JavaDoc getHeight() {
340
341         return String.valueOf(m_scaler.getHeight());
342     }
343
344     /**
345      * Returns the value of the HTML "hspace" attribute.<p>
346      *
347      * @return the value of the HTML "hspace" attribute
348      */

349     public String JavaDoc getHspace() {
350
351         return getAttribute(ATTR_HSPACE);
352     }
353
354     /**
355      * Returns the value of the HTML "id" attribute.<p>
356      *
357      * @return the value of the HTML "id" attribute
358      */

359     public String JavaDoc getId() {
360
361         return getAttribute(ATTR_ID);
362     }
363
364     /**
365      * Returns the value of the HTML "longdesc" attribute.<p>
366      *
367      * @return the value of the HTML "longdesc" attribute
368      */

369     public String JavaDoc getLongdesc() {
370
371         return getAttribute(ATTR_LONGDESC);
372     }
373
374     /**
375      * Returns the value of the HTML "name" attribute.<p>
376      *
377      * @return the value of the HTML "name" attribute
378      */

379     public String JavaDoc getName() {
380
381         return getAttribute(ATTR_NAME);
382     }
383
384     /**
385      * Returns the background color used by the image scaler.<p>
386      *
387      * @return the background color used by the image scaler
388      */

389     public String JavaDoc getScaleColor() {
390
391         return m_scaler.getColorString();
392     }
393
394     /**
395      * Returns the filter list used by the image scaler.<p>
396      *
397      * @return the filter list used by the image scaler
398      */

399     public String JavaDoc getScaleFilter() {
400
401         return m_scaler.getFiltersString();
402     }
403
404     /**
405      * Returns the position used by the image scaler.<p>
406      *
407      * @return the position used by the image scaler
408      */

409     public String JavaDoc getScalePosition() {
410
411         return String.valueOf(m_scaler.getPosition());
412     }
413
414     /**
415      * Returns the quality used by the image scaler.<p>
416      *
417      * @return the quality used by the image scaler
418      */

419     public String JavaDoc getScaleQuality() {
420
421         return String.valueOf(m_scaler.getQuality());
422     }
423
424     /**
425      * Returns the render mode used by the image scaler.<p>
426      *
427      * @return the render mode used by the image scaler
428      */

429     public String JavaDoc getScaleRendermode() {
430
431         return String.valueOf(m_scaler.getRenderMode());
432     }
433
434     /**
435      * Returns the scaling type for the image.<p>
436      *
437      * @return the scaling type for the image
438      */

439     public String JavaDoc getScaleType() {
440
441         return String.valueOf(m_scaler.getType());
442     }
443
444     /**
445      * Returns the source of the image to scale,
446      * which will have the OpenCms webapp / servlet prefix added.<p>
447      *
448      * @return the source of the image to scale
449      */

450     public String JavaDoc getSrc() {
451
452         return m_src;
453     }
454
455     /**
456      * Returns the value of the HTML "style" attribute.<p>
457      *
458      * @return the value of the HTML "style" attribute
459      */

460     public String JavaDoc getStyle() {
461
462         return getAttribute(ATTR_STYLE);
463     }
464
465     /**
466      * Returns the value of the HTML "title" attribute.<p>
467      *
468      * @return the value of the HTML "title" attribute
469      */

470     public String JavaDoc getTitle() {
471
472         return getAttribute(ATTR_TITLE);
473     }
474
475     /**
476      * Returns the value of the HTML "usemap" attribute.<p>
477      *
478      * @return the value of the HTML "usemap" attribute
479      */

480     public String JavaDoc getUsemap() {
481
482         return getAttribute(ATTR_USEMAP);
483     }
484
485     /**
486      * Returns the value of the HTML "vspace" attribute.<p>
487      *
488      * @return the value of the HTML "vspace" attribute
489      */

490     public String JavaDoc getVspace() {
491
492         return getAttribute(ATTR_VSPACE);
493     }
494
495     /**
496      * Returns the scaling width for the image.<p>
497      *
498      * @return the scaling width for the image
499      */

500     public String JavaDoc getWidth() {
501
502         return String.valueOf(m_scaler.getWidth());
503     }
504
505     /**
506      * Returns <code>"true"</code> if the HTML tag should only be created as partial tag.<p>
507      *
508      * @return <code>"true"</code> if the HTML tag should only be created as partial tag
509      */

510     public String JavaDoc isPartialTag() {
511
512         return String.valueOf(m_partialTag);
513     }
514
515     /**
516      * @see javax.servlet.jsp.tagext.Tag#release()
517      */

518     public void release() {
519
520         m_attributes = null;
521         m_scaler = new CmsImageScaler();
522         m_partialTag = false;
523         m_src = null;
524         super.release();
525     }
526
527     /**
528      * Sets the value of the HTML "align" attribute.<p>
529      *
530      * @param value the value of the HTML "align" attribute to set
531      */

532     public void setAlign(String JavaDoc value) {
533
534         setAttribute(ATTR_ALIGN, value);
535     }
536
537     /**
538      * Sets the value of the HTML "alt" attribute.<p>
539      *
540      * @param value the value of the HTML "alt" attribute to set
541      */

542     public void setAlt(String JavaDoc value) {
543
544         setAttribute(ATTR_ALT, value);
545
546     }
547
548     /**
549      * Sets the value of the HTML "border" attribute.<p>
550      *
551      * @param value the value of the HTML "border" attribute to set
552      */

553     public void setBorder(String JavaDoc value) {
554
555         setAttribute(ATTR_BORDER, value);
556
557     }
558
559     /**
560      * Sets the scaling height for the image.<p>
561      *
562      * If no valid integer is given, then "0" is used as value.<p>
563      *
564      * @param value the scaling height for the image to set
565      */

566     public void setHeight(String JavaDoc value) {
567
568         m_scaler.setHeight(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_HEIGHT));
569     }
570
571     /**
572      * Sets the value of the HTML "hspace" attribute.<p>
573      *
574      * @param value the value of the HTML "hspace" attribute to set
575      */

576     public void setHspace(String JavaDoc value) {
577
578         setAttribute(ATTR_HSPACE, value);
579
580     }
581
582     /**
583      * Sets the value of the HTML "id" attribute.<p>
584      *
585      * @param value the value of the HTML "id" attribute to set
586      */

587     public void setId(String JavaDoc value) {
588
589         setAttribute(ATTR_ID, value);
590
591     }
592
593     /**
594      * Sets the value of the HTML "longdesc" attribute.<p>
595      *
596      * @param value the value of the HTML "longdesc" attribute to set
597      */

598     public void setLongdesc(String JavaDoc value) {
599
600         setAttribute(ATTR_LONGDESC, value);
601
602     }
603
604     /**
605      * Sets the value of the HTML "name" attribute.<p>
606      *
607      * @param value the value of the HTML "name" attribute to set
608      */

609     public void setName(String JavaDoc value) {
610
611         setAttribute(ATTR_NAME, value);
612
613     }
614
615     /**
616      * Controls if the created HTML image tag is a full or partial tag.<p>
617      *
618      * @param partialTag the value to set
619      */

620     public void setPartialTag(String JavaDoc partialTag) {
621
622         m_partialTag = Boolean.valueOf(partialTag).booleanValue();
623     }
624
625     /**
626      * Sets the background color used by the image scaler.<p>
627      *
628      * @param value the background color to set
629      */

630     public void setScaleColor(String JavaDoc value) {
631
632         m_scaler.setColor(value);
633     }
634
635     /**
636      * Sets the filter(s) used by the image scaler.<p>
637      *
638      * @param value the filter(s) to set
639      */

640     public void setScaleFilter(String JavaDoc value) {
641
642         m_scaler.setFilters(value);
643     }
644
645     /**
646      * Sets the position used by the image scaler.<p>
647      *
648      * @param value the position to set
649      */

650     public void setScalePosition(String JavaDoc value) {
651
652         m_scaler.setPosition(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_POSITION));
653     }
654
655     /**
656      * Sets the quality used by the image scaler.<p>
657      *
658      * @param value the quality to set
659      */

660     public void setScaleQuality(String JavaDoc value) {
661
662         m_scaler.setQuality(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_QUALITY));
663     }
664
665     /**
666      * Sets the render mode used by the image scaler.<p>
667      *
668      * @param value the render mode to set
669      */

670     public void setScaleRendermode(String JavaDoc value) {
671
672         m_scaler.setRenderMode(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_RENDERMODE));
673     }
674
675     /**
676      * Sets the scaling type for the image.<p>
677      *
678      * If no valid integer is given, then "0" is used as value.<p>
679      *
680      * @param value the scaling type for the image to set
681      */

682     public void setScaleType(String JavaDoc value) {
683
684         m_scaler.setType(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_TYPE));
685     }
686
687     /**
688      * Sets the source of the image.<p>
689      *
690      * The source must be an absolute path in the current users OpenCms site, without any
691      * webapp or servlet prefix.<p>
692      *
693      * @param value the image source to set
694      */

695     public void setSrc(String JavaDoc value) {
696
697         m_src = value;
698     }
699
700     /**
701      * Sets the value of the HTML "style" attribute.<p>
702      *
703      * @param value the value of the HTML "style" attribute to set
704      */

705     public void setStyle(String JavaDoc value) {
706
707         setAttribute(ATTR_STYLE, value);
708     }
709
710     /**
711      * Sets the value of the HTML "title" attribute.<p>
712      *
713      * @param value the value of the HTML "title" attribute to set
714      */

715     public void setTitle(String JavaDoc value) {
716
717         setAttribute(ATTR_TITLE, value);
718     }
719
720     /**
721      * Sets the value of the HTML "usemap" attribute.<p>
722      *
723      * @param value the value of the HTML "usemap" attribute to set
724      */

725     public void setUsemap(String JavaDoc value) {
726
727         setAttribute(ATTR_USEMAP, value);
728     }
729
730     /**
731      * Sets the value of the HTML "vspace" attribute.<p>
732      *
733      * @param value the value of the HTML "vspace" attribute to set
734      */

735     public void setVspace(String JavaDoc value) {
736
737         setAttribute(ATTR_VSPACE, value);
738
739     }
740
741     /**
742      * Sets the scaling width for the image.<p>
743      *
744      * If no valid integer is given, then "0" is used as value.<p>
745      *
746      * @param value the scaling width for the image to set
747      */

748     public void setWidth(String JavaDoc value) {
749
750         m_scaler.setWidth(CmsStringUtil.getIntValue(value, 0, SCALE_ATTR_WIDTH));
751     }
752
753     /**
754      * Returns the given keys attribute value from the attribute map.<p>
755      *
756      * @param key the attribute to read from the map
757      * @return the given keys attribute value from the attribute map
758      */

759     private String JavaDoc getAttribute(String JavaDoc key) {
760
761         if (m_attributes != null) {
762             return (String JavaDoc)m_attributes.get(key);
763         }
764         return null;
765     }
766
767     /**
768      * Sets the given key with the given value in the attribute map.<p>
769      *
770      * @param key the key to set
771      * @param value the value to set
772      */

773     private void setAttribute(String JavaDoc key, String JavaDoc value) {
774
775         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
776             if (m_attributes == null) {
777                 m_attributes = new HashMap JavaDoc();
778             }
779             m_attributes.put(key, value);
780         }
781     }
782 }
Popular Tags