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.util.Resource; 18 19 import java.text.SimpleDateFormat ; 20 import java.util.Locale ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.jsp.JspWriter ; 24 import javax.servlet.jsp.tagext.TagSupport ; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29 30 31 36 public class Date extends TagSupport { 37 38 41 private static final long serialVersionUID = 222L; 42 43 private static Logger log = LoggerFactory.getLogger(Date.class); 44 45 private String pattern = "yyyy.MM.dd - HH:mm:ss"; 47 private String nodeDataName; 48 49 private String language; 50 51 private transient Content contentNode; 52 53 private transient NodeData nodeData; 54 55 private boolean actpage; 56 57 84 public void setPattern(String pattern) { 85 this.pattern = pattern; 86 } 87 88 91 public void setAtomName(String name) { 92 this.setNodeDataName(name); 93 } 94 95 99 public void setNodeDataName(String nodeDataName) { 100 this.nodeDataName = nodeDataName; 101 } 102 103 107 public void setActpage(boolean actpage) { 108 this.actpage = actpage; 109 } 110 111 115 public void setLanguage(String language) { 116 this.language = language; 117 } 118 119 122 public int doStartTag() { 123 if (this.actpage) { 124 this.contentNode = Resource.getCurrentActivePage((HttpServletRequest ) pageContext.getRequest()); 125 } 126 else { 127 this.contentNode = Resource.getLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 128 if (this.contentNode == null) { 129 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest ) pageContext.getRequest()); 130 } 131 } 132 String printDate = getDateString(); 133 JspWriter out = pageContext.getOut(); 134 try { 135 out.print(printDate); 136 } 137 catch (Exception e) { 138 log.error(e.getMessage()); 139 } 140 return EVAL_PAGE; 141 } 142 143 public String getDateString() { 144 String date = null; 145 if (this.contentNode == null) { 146 return StringUtils.EMPTY; 147 } 148 149 this.nodeData = this.contentNode.getNodeData(this.nodeDataName); 150 151 if (!this.nodeData.isExist()) { 152 return StringUtils.EMPTY; 153 } 154 if (this.nodeData.getDate() == null) { 155 return StringUtils.EMPTY; 156 } 157 SimpleDateFormat formatter; 158 if (StringUtils.isEmpty(this.language)) { 159 formatter = new SimpleDateFormat (this.pattern); 160 } 161 else { 162 formatter = new SimpleDateFormat (this.pattern, new Locale (this.language)); 163 } 164 date = formatter.format(this.nodeData.getDate().getTime()); 165 166 return date; 167 } 168 } 169 | Popular Tags |