View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.taglibs.Resource;
39  
40  import java.text.SimpleDateFormat;
41  import java.util.Locale;
42  
43  import javax.servlet.jsp.JspWriter;
44  import javax.servlet.jsp.tagext.TagSupport;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * @jsp.tag name="date" body-content="empty"
53   * @deprecated see cms:out.
54   *
55   * @author Marcel Salathe
56   * @version $Revision $ ($Author $)
57   */
58  public class Date extends TagSupport {
59  
60      /**
61       * Stable serialVersionUID.
62       */
63      private static final long serialVersionUID = 222L;
64  
65      private static Logger log = LoggerFactory.getLogger(Date.class);
66  
67      private String pattern = "yyyy.MM.dd - HH:mm:ss"; //$NON-NLS-1$
68  
69      private String nodeDataName;
70  
71      private String language;
72  
73      private transient Content contentNode;
74  
75      private transient NodeData nodeData;
76  
77      private boolean actpage;
78  
79      /**
80       * Date pattern. Defaults to "yyyy.MM.dd - HH:mm:ss".
81       * See the java.text.SimpleDateFormat javadoc for details.
82       *
83       * @deprecated
84       * @jsp.attribute required="false" rtexprvalue="true"
85       */
86      public void setPattern(String pattern) {
87          this.pattern = pattern;
88      }
89  
90      /**
91       * @deprecated
92       * @jsp.attribute required="false" rtexprvalue="true"
93       */
94      public void setAtomName(String name) {
95          this.setNodeDataName(name);
96      }
97  
98      /**
99       * Where the date comes from.
100      * @deprecated
101      * @jsp.attribute required="false" rtexprvalue="true"
102      */
103     public void setNodeDataName(String nodeDataName) {
104         this.nodeDataName = nodeDataName;
105     }
106 
107     /**
108      * If "true", atom is taken from the currently active page.
109      * @deprecated
110      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
111      */
112     public void setActpage(boolean actpage) {
113         this.actpage = actpage;
114     }
115 
116     /**
117      * Locale string (see java.util.Locale)
118      * @deprecated
119      * @jsp.attribute required="false" rtexprvalue="true"
120      */
121     public void setLanguage(String language) {
122         this.language = language;
123     }
124 
125     /**
126      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
127      */
128     public int doStartTag() {
129         if (this.actpage) {
130             this.contentNode = Resource.getCurrentActivePage();
131         }
132         else {
133             this.contentNode = Resource.getLocalContentNode();
134             if (this.contentNode == null) {
135                 this.contentNode = Resource.getGlobalContentNode();
136             }
137         }
138         String printDate = getDateString();
139         JspWriter out = pageContext.getOut();
140         try {
141             out.print(printDate);
142         }
143         catch (Exception e) {
144             log.error(e.getMessage());
145         }
146         return EVAL_PAGE;
147     }
148 
149     public String getDateString() {
150         String date = null;
151         if (this.contentNode == null) {
152             return StringUtils.EMPTY;
153         }
154 
155         this.nodeData = this.contentNode.getNodeData(this.nodeDataName);
156 
157         if (!this.nodeData.isExist()) {
158             return StringUtils.EMPTY;
159         }
160         if (this.nodeData.getDate() == null) {
161             return StringUtils.EMPTY;
162         }
163         SimpleDateFormat formatter;
164         if (StringUtils.isEmpty(this.language)) {
165             formatter = new SimpleDateFormat(this.pattern);
166         }
167         else {
168             formatter = new SimpleDateFormat(this.pattern, new Locale(this.language));
169         }
170         date = formatter.format(this.nodeData.getDate().getTime());
171 
172         return date;
173     }
174 }