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 @Override
79 public void setNodeDataName(String nodeDataName) {
80 super.setNodeDataName(nodeDataName);
81 }
82
83
84
85
86
87
88 public void setAltNodeDataName(String altNodeDataName) {
89 this.altNodeDataName = altNodeDataName;
90 }
91
92
93
94
95
96 public void setHeight(String value) {
97 this.htmlAttributes.put("height", value);
98 }
99
100
101
102
103
104 public void setWidth(String value) {
105 this.htmlAttributes.put("width", value);
106 }
107
108
109
110
111
112 public void setClass(String value) {
113 this.htmlAttributes.put("class", value);
114 }
115
116
117
118
119
120 public void setStyle(String value) {
121 this.htmlAttributes.put("style", value);
122 }
123
124
125
126
127
128 @Override
129 public void setId(String value) {
130 this.htmlAttributes.put("id", value);
131 }
132
133
134
135
136 @Override
137 public int doEndTag() throws JspException {
138 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
139
140 Content contentNode = getFirstMatchingNode();
141 if (contentNode == null) {
142 return EVAL_PAGE;
143 }
144
145 NodeData imageNodeData = contentNode.getNodeData(this.getNodeDataName());
146
147 if (!imageNodeData.isExist() || imageNodeData.getType() != PropertyType.BINARY) {
148 return EVAL_PAGE;
149 }
150
151 NodeData nodeData = I18nContentSupportFactory.getI18nSupport().getNodeData(contentNode, this.getNodeDataName());
152 FileProperties props = new FileProperties(contentNode, nodeData.getName());
153 String imgSrc = props.getProperty(FileProperties.PATH);
154
155 String altNodeDataNameDef = this.altNodeDataName;
156 if (StringUtils.isEmpty(altNodeDataNameDef)) {
157 altNodeDataNameDef = getNodeDataName() + "Alt";
158 }
159
160 String alt = contentNode.getNodeData(altNodeDataNameDef).getString();
161
162 if (StringUtils.isEmpty(alt)) {
163 alt = props.getProperty(FileProperties.NAME_WITHOUT_EXTENSION);
164 }
165
166 JspWriter out = pageContext.getOut();
167
168
169 Map<String, String> attributes = new HashMap<String, String>(htmlAttributes);
170 attributes.put("title", alt);
171
172 if (StringUtils.isBlank(attributes.get("width"))
173 || StringUtils.isBlank(attributes.get("height"))) {
174 String width = props.getProperty(FileProperties.PROPERTY_WIDTH);
175 if (StringUtils.isNotEmpty(width)) {
176 attributes.put("width", width);
177 }
178
179 String height = props.getProperty(FileProperties.PROPERTY_HEIGHT);
180 if (StringUtils.isNotEmpty(height)) {
181 attributes.put("height", height);
182 }
183 }
184
185 try {
186 if (StringUtils.lowerCase(imgSrc).endsWith(".swf")) {
187
188
189 out.write("<object type=\"application/x-shockwave-flash\" data=\"");
190 out.write(request.getContextPath());
191 out.write(imgSrc);
192 out.write("\" ");
193 writeAttributes(out, attributes);
194 out.write(">");
195
196 out.write("<param name=\"movie\" value=\"");
197 out.write(request.getContextPath());
198 out.write(imgSrc);
199 out.write("\"/>");
200 out.write("<param name=\"wmode\" value=\"transparent\"/>");
201 out.write("</object>");
202
203 }
204 else {
205
206 attributes.put("alt", alt);
207
208 out.write("<img src=\"");
209 out.write(request.getContextPath());
210 out.write(imgSrc);
211 out.write("\" ");
212
213 writeAttributes(out, attributes);
214 out.write("/>");
215 }
216 }
217 catch (IOException e) {
218
219 throw new NestableRuntimeException(e);
220 }
221
222 return super.doEndTag();
223 }
224
225
226
227
228
229 private void writeAttributes(JspWriter out, Map<String, String> attributes) throws IOException {
230 for (Iterator<String> iter = attributes.keySet().iterator(); iter.hasNext();) {
231 String name = iter.next();
232 String value = attributes.get(name);
233 out.write(name);
234 out.write("=\"");
235 out.write(value);
236 out.write("\" ");
237 }
238 }
239
240
241
242
243 @Override
244 public void release() {
245 super.release();
246 altNodeDataName = null;
247 htmlAttributes.clear();
248 }
249
250 }