View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.webdav.xml.dav.response.properties;
14  
15  import org.abstracthorizon.danube.http.Status;
16  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
17  import org.abstracthorizon.danube.webdav.xml.common.XMLUtils;
18  import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
19  
20  import java.io.PrintWriter;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  /**
25   * This class models WebDAV's creationdate response tag.
26   *
27   * @author Daniel Sendula
28   */
29  public class CreationDate extends ResponseProperty {
30  
31      /** Date format or "yyy-MM-dd'T'HH:mm:ssZ"" */
32      public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ssZ");
33  
34      /** Modified timestamp */
35      protected long modified = -1;
36  
37      /** Cached formatted date */
38      protected String cachedDate;
39  
40      /** Tag name */
41      public static final String TAG_NAME = "creationdate";
42  
43      /** Date string */
44      protected String dateString;
45  
46      /**
47       * Constructor
48       * @param status status
49       */
50      public CreationDate(Status status) {
51          super(status);
52      }
53  
54      /**
55       * Constructor
56       * @param modified timestamp
57       */
58      public CreationDate(long modified) {
59          this.modified = modified;
60      }
61  
62      /**
63       * Returns timestamp
64       * @return timestamp
65       */
66      public long getLastModified() {
67          return modified;
68      }
69  
70      /**
71       * Returns formatted timestamp
72       * @return formatted timestamp
73       */
74      public String asString() {
75          if (cachedDate == null) {
76              if (modified != -1) {
77                  cachedDate = DATE_FORMAT.format(new Date(modified));
78              }
79          }
80          return cachedDate;
81      }
82  
83      @Override
84      public String toString() {
85          return "CreationDate[" + asString() + "]";
86      }
87  
88      /**
89       * Renders the tag
90       * @param writer writer
91       * @param provider namespace provider
92       */
93      public void render(PrintWriter writer, NamespacesProvider provider) {
94          if ((dateString == null) || (dateString.length() == 0)) {
95              writer.println(XMLUtils.createEmptyTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
96          } else {
97              writer.println(XMLUtils.createTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME, asString()));
98          }
99      }
100 }