1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.taglibs.util;
35
36 import info.magnolia.cms.beans.runtime.FileProperties;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.i18n.I18nContentSupportFactory;
40 import info.magnolia.cms.taglibs.BaseContentTag;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.commons.lang.exception.NestableRuntimeException;
43
44 import javax.jcr.PropertyType;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.jsp.JspException;
47 import javax.servlet.jsp.JspWriter;
48 import java.io.IOException;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.Map;
52
53
54
55
56
57
58
59
60
61
62
63 public class ImgTag extends BaseContentTag {
64
65
66
67
68 private static final long serialVersionUID = 222L;
69
70 private Map<String, String> htmlAttributes = new HashMap<String, String>();
71
72 private String altNodeDataName;
73
74
75
76
77
78 public void setNodeDataName(String nodeDataName) {
79 super.setNodeDataName(nodeDataName);
80 }
81
82
83
84
85
86
87 public void setAltNodeDataName(String altNodeDataName) {
88 this.altNodeDataName = altNodeDataName;
89 }
90
91
92
93
94
95 public void setHeight(String value) {
96 this.htmlAttributes.put("height", value);
97 }
98
99
100
101
102
103 public void setWidth(String value) {
104 this.htmlAttributes.put("width", value);
105 }
106
107
108
109
110
111 public void setClass(String value) {
112 this.htmlAttributes.put("class", value);
113 }
114
115
116
117
118
119 public void setStyle(String value) {
120 this.htmlAttributes.put("style", value);
121 }
122
123
124
125
126
127 public void setId(String value) {
128 this.htmlAttributes.put("id", value);
129 }
130
131
132
133
134 public int doEndTag() throws JspException {
135 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
136
137 Content contentNode = getFirstMatchingNode();
138 if (contentNode == null) {
139 return EVAL_PAGE;
140 }
141
142 NodeData imageNodeData = contentNode.getNodeData(this.getNodeDataName());
143
144 if (!imageNodeData.isExist() || imageNodeData.getType() != PropertyType.BINARY) {
145 return EVAL_PAGE;
146 }
147
148 NodeData nodeData = I18nContentSupportFactory.getI18nSupport().getNodeData(contentNode, this.getNodeDataName());
149 FileProperties props = new FileProperties(contentNode, nodeData.getName());
150 String imgSrc = props.getProperty(FileProperties.PATH);
151
152 String altNodeDataNameDef = this.altNodeDataName;
153 if (StringUtils.isEmpty(altNodeDataNameDef)) {
154 altNodeDataNameDef = getNodeDataName() + "Alt";
155 }
156
157 String alt = contentNode.getNodeData(altNodeDataNameDef).getString();
158
159 if (StringUtils.isEmpty(alt)) {
160 alt = props.getProperty(FileProperties.NAME_WITHOUT_EXTENSION);
161 }
162
163 JspWriter out = pageContext.getOut();
164
165
166 Map<String, String> attributes = new HashMap<String, String>(htmlAttributes);
167 attributes.put("title", alt);
168
169 if (StringUtils.isBlank(attributes.get("width"))
170 || StringUtils.isBlank(attributes.get("height"))) {
171 String width = props.getProperty(FileProperties.PROPERTY_WIDTH);
172 if (StringUtils.isNotEmpty(width)) {
173 attributes.put("width", width);
174 }
175
176 String height = props.getProperty(FileProperties.PROPERTY_HEIGHT);
177 if (StringUtils.isNotEmpty(height)) {
178 attributes.put("height", height);
179 }
180 }
181
182 try {
183 if (StringUtils.lowerCase(imgSrc).endsWith(".swf")) {
184
185
186 out.write("<object type=\"application/x-shockwave-flash\" data=\"");
187 out.write(request.getContextPath());
188 out.write(imgSrc);
189 out.write("\" ");
190 writeAttributes(out, attributes);
191 out.write(">");
192
193 out.write("<param name=\"movie\" value=\"");
194 out.write(request.getContextPath());
195 out.write(imgSrc);
196 out.write("\"/>");
197 out.write("<param name=\"wmode\" value=\"transparent\"/>");
198 out.write("</object>");
199
200 }
201 else {
202
203 attributes.put("alt", alt);
204
205 out.write("<img src=\"");
206 out.write(request.getContextPath());
207 out.write(imgSrc);
208 out.write("\" ");
209
210 writeAttributes(out, attributes);
211 out.write("/>");
212 }
213 }
214 catch (IOException e) {
215
216 throw new NestableRuntimeException(e);
217 }
218
219 return super.doEndTag();
220 }
221
222
223
224
225
226 private void writeAttributes(JspWriter out, Map<String, String> attributes) throws IOException {
227 for (Iterator<String> iter = attributes.keySet().iterator(); iter.hasNext();) {
228 String name = iter.next();
229 String value = attributes.get(name);
230 out.write(name);
231 out.write("=\"");
232 out.write(value);
233 out.write("\" ");
234 }
235 }
236
237
238
239
240 public void release() {
241 super.release();
242 altNodeDataName = null;
243 htmlAttributes.clear();
244 }
245
246 }