1 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 ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.commons.lang.exception.NestableRuntimeException; 31 32 33 37 public class ImgTag extends BaseContentTag { 38 39 42 private static final long serialVersionUID = 222L; 43 44 private Map htmlAttributes = new HashMap (); 45 46 private String altNodeDataName; 47 48 52 public void setNodeDataName(String nodeDataName) { 53 this.nodeDataName = nodeDataName; 54 } 55 56 60 public void setAltNodeDataName(String altNodeDataName) { 61 this.altNodeDataName = altNodeDataName; 62 } 63 64 68 public void setHeight(String value) { 69 this.htmlAttributes.put("height", value); 70 } 71 72 76 public void setWidth(String value) { 77 this.htmlAttributes.put("width", value); 78 } 79 80 84 public void setClass(String value) { 85 this.htmlAttributes.put("class", value); 86 } 87 88 92 public void setStyle(String value) { 93 this.htmlAttributes.put("style", value); 94 } 95 96 100 public void setId(String value) { 101 this.htmlAttributes.put("id", value); 102 } 103 104 107 public int doEndTag() throws JspException { 108 HttpServletRequest request = (HttpServletRequest ) 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 imgSrc = props.getProperty(FileProperties.PATH); 123 124 String altNodeDataNameDef = this.altNodeDataName; 125 if (StringUtils.isEmpty(altNodeDataNameDef)) { 126 altNodeDataNameDef = nodeDataName + "Alt"; 127 } 128 129 String alt = contentNode.getNodeData(altNodeDataNameDef).getString(); 130 131 if (StringUtils.isEmpty(alt)) { 132 alt = props.getProperty(FileProperties.NAME_WITHOUT_EXTENSION); 133 } 134 135 JspWriter out = pageContext.getOut(); 136 137 Map attributes = new HashMap (htmlAttributes); 139 attributes.put("title", alt); 140 141 if (!attributes.containsKey("width") && !attributes.containsKey("height")) { 142 String width = props.getProperty(FileProperties.PROPERTY_WIDTH); 143 if (StringUtils.isNotEmpty(width)) { 144 attributes.put("width", width); 145 } 146 147 String 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 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 e) { 185 throw new NestableRuntimeException(e); 187 } 188 189 return super.doEndTag(); 190 } 191 192 196 private void writeAttributes(JspWriter out, Map attributes) throws IOException { 197 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 198 String name = (String ) iter.next(); 199 String value = (String ) attributes.get(name); 200 out.write(name); 201 out.write("=\""); 202 out.write(value); 203 out.write("\" "); 204 } 205 } 206 207 210 public void release() { 211 super.release(); 212 altNodeDataName = null; 213 htmlAttributes.clear(); 214 } 215 216 } 217 | Popular Tags |