1 13 package info.magnolia.cms.taglibs; 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.util.LinkUtil; 19 import info.magnolia.cms.util.Resource; 20 21 import java.io.IOException ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 25 import javax.jcr.PropertyType; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.commons.lang.exception.NestableRuntimeException; 32 import org.apache.commons.lang.time.DateFormatUtils; 33 34 35 40 public class Out extends BaseContentTag { 41 42 45 private static final long serialVersionUID = 222L; 46 47 private static final String DEFAULT_LINEBREAK = "<br />"; 49 private static final String DEFAULT_DATEPATTERN = "yyyy-MM-dd"; 51 private String fileProperty = StringUtils.EMPTY; 52 53 private String datePattern = DEFAULT_DATEPATTERN; 55 private String dateLanguage; 56 57 private String lineBreak = DEFAULT_LINEBREAK; 58 59 63 private String var; 64 65 69 private int scope = PageContext.PAGE_SCOPE; 70 71 75 public void setVar(String var) { 76 this.var = var; 77 } 78 79 83 public void setScope(String scope) { 84 if ("request".equalsIgnoreCase(scope)) { this.scope = PageContext.REQUEST_SCOPE; 86 } 87 else if ("session".equalsIgnoreCase(scope)) { this.scope = PageContext.SESSION_SCOPE; 89 } 90 else if ("application".equalsIgnoreCase(scope)) { this.scope = PageContext.APPLICATION_SCOPE; 92 } 93 else { 94 this.scope = PageContext.PAGE_SCOPE; 96 } 97 } 98 99 128 public void setFileProperty(String property) { 129 this.fileProperty = property; 130 } 131 132 162 public void setDatePattern(String pattern) { 163 this.datePattern = pattern; 164 } 165 166 171 public void setDateLanguage(String language) { 172 this.dateLanguage = language; 173 } 174 175 179 public void setLineBreak(String lineBreak) { 180 this.lineBreak = lineBreak; 181 } 182 183 protected String getFilePropertyValue(Content contentNode) { 184 FileProperties props = new FileProperties(contentNode, this.nodeDataName); 185 String value = props.getProperty(this.fileProperty); 186 return value; 187 } 188 189 192 public int doEndTag() { 193 195 Content contentNode = getFirtMatchingNode(); 196 if (contentNode == null) { 197 return EVAL_PAGE; 198 } 199 200 NodeData nodeData = contentNode.getNodeData(this.nodeDataName); 201 202 if (!nodeData.isExist()) { 203 return EVAL_PAGE; 204 } 205 206 String value = null; 207 int type = nodeData.getType(); 208 209 switch (type) { 210 case PropertyType.DATE: 211 212 Date date = nodeData.getDate().getTime(); 213 if (date != null) { 214 if (this.dateLanguage == null) { 215 value = DateFormatUtils.format(date, this.datePattern); 216 } 217 else { 218 value = DateFormatUtils.format(date, this.datePattern, new Locale (this.dateLanguage)); 219 } 220 } 221 break; 222 223 case PropertyType.BINARY: 224 value = this.getFilePropertyValue(contentNode); 225 break; 226 227 default: 228 value = StringUtils.isEmpty(this.lineBreak) ? nodeData.getString() : nodeData.getString(this.lineBreak); 229 value = LinkUtil.convertUUIDsToRelativeLinks(value, Resource 231 .getActivePage((HttpServletRequest ) pageContext.getRequest())); break; 233 } 234 235 if (var != null) { 236 pageContext.setAttribute(var, value, scope); 238 } 239 else if (value != null) { 240 JspWriter out = pageContext.getOut(); 241 try { 242 out.print(value); 243 } 244 catch (IOException e) { 245 throw new NestableRuntimeException(e); 247 } 248 } 249 250 return EVAL_PAGE; 251 } 252 253 256 public void release() { 257 super.release(); 258 259 this.fileProperty = StringUtils.EMPTY; 260 this.datePattern = DEFAULT_DATEPATTERN; 261 this.dateLanguage = null; 262 this.lineBreak = DEFAULT_LINEBREAK; 263 this.var = null; 264 this.scope = PageContext.PAGE_SCOPE; 265 } 266 267 } | Popular Tags |