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: 41137 $ ($Author: gjoseph $)
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      public void setNodeDataName(String nodeDataName) {
79          super.setNodeDataName(nodeDataName);
80      }
81  
82      /**
83       * Name of the node data holding the alt text for the image.
84       * If not set the default is nodeDataName + "Alt". The same value is added to the "title" attribute, if not null.
85       * @jsp.attribute required="false" rtexprvalue="true"
86       */
87      public void setAltNodeDataName(String altNodeDataName) {
88          this.altNodeDataName = altNodeDataName;
89      }
90  
91      /**
92       * html pass through attribute.
93       * @jsp.attribute required="false" rtexprvalue="true"
94       */
95      public void setHeight(String value) {
96          this.htmlAttributes.put("height", value);
97      }
98  
99      /**
100      * html pass through attribute.
101      * @jsp.attribute required="false" rtexprvalue="true"
102      */
103     public void setWidth(String value) {
104         this.htmlAttributes.put("width", value);
105     }
106 
107     /**
108      * html pass through attribute.
109      * @jsp.attribute required="false" rtexprvalue="true"
110      */
111     public void setClass(String value) {
112         this.htmlAttributes.put("class", value);
113     }
114 
115     /**
116      * html pass through attribute.
117      * @jsp.attribute required="false" rtexprvalue="true"
118      */
119     public void setStyle(String value) {
120         this.htmlAttributes.put("style", value);
121     }
122 
123     /**
124      * html pass through attribute.
125      * @jsp.attribute required="false" rtexprvalue="true"
126      */
127     public void setId(String value) {
128         this.htmlAttributes.put("id", value);
129     }
130 
131     /**
132      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
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         // don't modify the original map, remember tag pooling
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                 // handle flash movies
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             // should never happen
216             throw new NestableRuntimeException(e);
217         }
218 
219         return super.doEndTag();
220     }
221 
222     /**
223      * @param out
224      * @throws IOException
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      * @see javax.servlet.jsp.tagext.TagSupport#release()
239      */
240     public void release() {
241         super.release();
242         altNodeDataName = null;
243         htmlAttributes.clear();
244     }
245 
246 }