KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > Date


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * @author Marcel Salathe
33  * @deprecated
34  * @version $Revision $ ($Author $)
35  */

36 public class Date extends TagSupport JavaDoc {
37
38     /**
39      * Stable serialVersionUID.
40      */

41     private static final long serialVersionUID = 222L;
42
43     private static Logger log = LoggerFactory.getLogger(Date.class);
44
45     private String JavaDoc pattern = "yyyy.MM.dd - HH:mm:ss"; //$NON-NLS-1$
46

47     private String JavaDoc nodeDataName;
48
49     private String JavaDoc language;
50
51     private transient Content contentNode;
52
53     private transient NodeData nodeData;
54
55     private boolean actpage;
56
57     /**
58      * Date pattern. see http://java.sun.com/j2se/1.4.1/docs/api/java/text/SimpleDateFormat.html
59      *
60      * <pre>
61      * G Era designator Text AD
62      * y Year Year 1996; 96
63      * M Month in year Month July; Jul; 07
64      * w Week in year Number 27
65      * W Week in month Number 2
66      * D Day in year Number 189
67      * d Day in month Number 10
68      * F Day of week in month Number 2
69      * E Day in week Text Tuesday; Tue
70      * a Am/pm marker Text PM
71      * H Hour in day (0-23) Number 0
72      * k Hour in day (1-24) Number 24
73      * K Hour in am/pm (0-11) Number 0
74      * h Hour in am/pm (1-12) Number 12
75      * m Minute in hour Number 30
76      * s Second in minute Number 55
77      * S Millisecond Number 978
78      * z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
79      * Z Time zone RFC 822 time zone -0800
80      * </pre>
81      *
82      * @deprecated
83      */

84     public void setPattern(String JavaDoc pattern) {
85         this.pattern = pattern;
86     }
87
88     /**
89      * @deprecated
90      */

91     public void setAtomName(String JavaDoc name) {
92         this.setNodeDataName(name);
93     }
94
95     /**
96      * @deprecated
97      * @param nodeDataName
98      */

99     public void setNodeDataName(String JavaDoc nodeDataName) {
100         this.nodeDataName = nodeDataName;
101     }
102
103     /**
104      * @deprecated
105      * @param actpage
106      */

107     public void setActpage(boolean actpage) {
108         this.actpage = actpage;
109     }
110
111     /**
112      * @deprecated
113      * @param language
114      */

115     public void setLanguage(String JavaDoc language) {
116         this.language = language;
117     }
118
119     /**
120      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
121      */

122     public int doStartTag() {
123         if (this.actpage) {
124             this.contentNode = Resource.getCurrentActivePage((HttpServletRequest JavaDoc) pageContext.getRequest());
125         }
126         else {
127             this.contentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
128             if (this.contentNode == null) {
129                 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
130             }
131         }
132         String JavaDoc printDate = getDateString();
133         JspWriter JavaDoc out = pageContext.getOut();
134         try {
135             out.print(printDate);
136         }
137         catch (Exception JavaDoc e) {
138             log.error(e.getMessage());
139         }
140         return EVAL_PAGE;
141     }
142
143     public String JavaDoc getDateString() {
144         String JavaDoc 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 JavaDoc formatter;
158         if (StringUtils.isEmpty(this.language)) {
159             formatter = new SimpleDateFormat JavaDoc(this.pattern);
160         }
161         else {
162             formatter = new SimpleDateFormat JavaDoc(this.pattern, new Locale JavaDoc(this.language));
163         }
164         date = formatter.format(this.nodeData.getDate().getTime());
165
166         return date;
167     }
168 }
169
Popular Tags