View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Writes an html img tag for the image at "nodeDataName". Also supports writing object tags for flash movies.
56   * (detected by the "swf" extension)
57   *
58   * @jsp.tag name="img" body-content="empty"
59   *
60   * @author Fabrizio Giustina
61   * @version $Revision: 45983 $ ($Author: pbaerfuss $)
62   */
63  public class ImgTag extends BaseContentTag {
64  
65      /**
66       * Stable serialVersionUID.
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       * the name of the nodeData containing the image path.
76       * @jsp.attribute required="true" rtexprvalue="true"
77       */
78      @Override
79      public void setNodeDataName(String nodeDataName) {
80          super.setNodeDataName(nodeDataName);
81      }
82  
83      /**
84       * Name of the node data holding the alt text for the image.
85       * If not set the default is nodeDataName + "Alt". The same value is added to the "title" attribute, if not null.
86       * @jsp.attribute required="false" rtexprvalue="true"
87       */
88      public void setAltNodeDataName(String altNodeDataName) {
89          this.altNodeDataName = altNodeDataName;
90      }
91  
92      /**
93       * html pass through attribute.
94       * @jsp.attribute required="false" rtexprvalue="true"
95       */
96      public void setHeight(String value) {
97          this.htmlAttributes.put("height", value);
98      }
99  
100     /**
101      * html pass through attribute.
102      * @jsp.attribute required="false" rtexprvalue="true"
103      */
104     public void setWidth(String value) {
105         this.htmlAttributes.put("width", value);
106     }
107 
108     /**
109      * html pass through attribute.
110      * @jsp.attribute required="false" rtexprvalue="true"
111      */
112     public void setClass(String value) {
113         this.htmlAttributes.put("class", value);
114     }
115 
116     /**
117      * html pass through attribute.
118      * @jsp.attribute required="false" rtexprvalue="true"
119      */
120     public void setStyle(String value) {
121         this.htmlAttributes.put("style", value);
122     }
123 
124     /**
125      * html pass through attribute.
126      * @jsp.attribute required="false" rtexprvalue="true"
127      */
128     @Override
129     public void setId(String value) {
130         this.htmlAttributes.put("id", value);
131     }
132 
133     /**
134      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
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         // don't modify the original map, remember tag pooling
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                 // handle flash movies
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             // should never happen
219             throw new NestableRuntimeException(e);
220         }
221 
222         return super.doEndTag();
223     }
224 
225     /**
226      * @param out
227      * @throws IOException
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      * @see javax.servlet.jsp.tagext.TagSupport#release()
242      */
243     @Override
244     public void release() {
245         super.release();
246         altNodeDataName = null;
247         htmlAttributes.clear();
248     }
249 
250 }