KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > ImgTag


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.NodeData;
17 import info.magnolia.cms.gui.misc.FileProperties;
18 import info.magnolia.cms.taglibs.BaseContentTag;
19
20 import java.io.IOException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.commons.lang.exception.NestableRuntimeException;
31
32
33 /**
34  * @author Fabrizio Giustina
35  * @version $Revision: 6341 $ ($Author: philipp $)
36  */

37 public class ImgTag extends BaseContentTag {
38
39     /**
40      * Stable serialVersionUID.
41      */

42     private static final long serialVersionUID = 222L;
43
44     private Map JavaDoc htmlAttributes = new HashMap JavaDoc();
45
46     private String JavaDoc altNodeDataName;
47
48     /**
49      * Setter for <code>nodeDataName</code>.
50      * @param nodeDataName The nodeDataName to set.
51      */

52     public void setNodeDataName(String JavaDoc nodeDataName) {
53         this.nodeDataName = nodeDataName;
54     }
55
56     /**
57      * Setter for <code>altNodeDataName</code>.
58      * @param altNodeDataName The altNodeDataName to set.
59      */

60     public void setAltNodeDataName(String JavaDoc altNodeDataName) {
61         this.altNodeDataName = altNodeDataName;
62     }
63
64     /**
65      * Setter for <code>height</code>.
66      * @param height html attribute
67      */

68     public void setHeight(String JavaDoc value) {
69         this.htmlAttributes.put("height", value);
70     }
71
72     /**
73      * Setter for <code>width</code>.
74      * @param width html attribute
75      */

76     public void setWidth(String JavaDoc value) {
77         this.htmlAttributes.put("width", value);
78     }
79
80     /**
81      * Setter for <code>class</code>.
82      * @param class html attribute
83      */

84     public void setClass(String JavaDoc value) {
85         this.htmlAttributes.put("class", value);
86     }
87
88     /**
89      * Setter for <code>style</code>.
90      * @param style html attribute
91      */

92     public void setStyle(String JavaDoc value) {
93         this.htmlAttributes.put("style", value);
94     }
95
96     /**
97      * Setter for <code>id</code>.
98      * @param id html attribute
99      */

100     public void setId(String JavaDoc value) {
101         this.htmlAttributes.put("id", value);
102     }
103
104     /**
105      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
106      */

107     public int doEndTag() throws JspException JavaDoc {
108         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
109
110         Content contentNode = getFirtMatchingNode();
111         if (contentNode == null) {
112             return EVAL_PAGE;
113         }
114
115         NodeData imageNodeData = contentNode.getNodeData(this.nodeDataName);
116
117         if (!imageNodeData.isExist()) {
118             return EVAL_PAGE;
119         }
120
121         FileProperties props = new FileProperties(contentNode, this.nodeDataName);
122         String JavaDoc imgSrc = props.getProperty(FileProperties.PATH);
123
124         String JavaDoc altNodeDataNameDef = this.altNodeDataName;
125         if (StringUtils.isEmpty(altNodeDataNameDef)) {
126             altNodeDataNameDef = nodeDataName + "Alt";
127         }
128
129         String JavaDoc alt = contentNode.getNodeData(altNodeDataNameDef).getString();
130
131         if (StringUtils.isEmpty(alt)) {
132             alt = props.getProperty(FileProperties.NAME_WITHOUT_EXTENSION);
133         }
134
135         JspWriter JavaDoc out = pageContext.getOut();
136
137         // don't modify the original map, remember tag pooling
138
Map JavaDoc attributes = new HashMap JavaDoc(htmlAttributes);
139         attributes.put("title", alt);
140
141         if (!attributes.containsKey("width") && !attributes.containsKey("height")) {
142             String JavaDoc width = props.getProperty(FileProperties.PROPERTY_WIDTH);
143             if (StringUtils.isNotEmpty(width)) {
144                 attributes.put("width", width);
145             }
146
147             String JavaDoc height = props.getProperty(FileProperties.PROPERTY_HEIGHT);
148             if (StringUtils.isNotEmpty(height)) {
149                 attributes.put("height", height);
150             }
151         }
152
153         try {
154             if (StringUtils.lowerCase(imgSrc).endsWith(".swf")) {
155                 // handle flash movies
156

157                 out.write("<object type=\"application/x-shockwave-flash\" data=\"");
158                 out.write(request.getContextPath());
159                 out.write(imgSrc);
160                 out.write("\" ");
161                 writeAttributes(out, attributes);
162                 out.write(">");
163
164                 out.write("<param name=\"movie\" value=\"");
165                 out.write(request.getContextPath());
166                 out.write(imgSrc);
167                 out.write("\"/>");
168                 out.write("</object>");
169
170             }
171             else {
172
173                 attributes.put("alt", alt);
174
175                 out.write("<img SRC=\"");
176                 out.write(request.getContextPath());
177                 out.write(imgSrc);
178                 out.write("\" ");
179
180                 writeAttributes(out, attributes);
181                 out.write("/>");
182             }
183         }
184         catch (IOException JavaDoc e) {
185             // should never happen
186
throw new NestableRuntimeException(e);
187         }
188
189         return super.doEndTag();
190     }
191
192     /**
193      * @param out
194      * @throws IOException
195      */

196     private void writeAttributes(JspWriter JavaDoc out, Map JavaDoc attributes) throws IOException JavaDoc {
197         for (Iterator JavaDoc iter = attributes.keySet().iterator(); iter.hasNext();) {
198             String JavaDoc name = (String JavaDoc) iter.next();
199             String JavaDoc value = (String JavaDoc) attributes.get(name);
200             out.write(name);
201             out.write("=\"");
202             out.write(value);
203             out.write("\" ");
204         }
205     }
206
207     /**
208      * @see javax.servlet.jsp.tagext.TagSupport#release()
209      */

210     public void release() {
211         super.release();
212         altNodeDataName = null;
213         htmlAttributes.clear();
214     }
215
216 }
217
Popular Tags