001 package ezvcard.types;
002
003 import java.util.Date;
004 import java.util.List;
005
006 import ezvcard.VCardVersion;
007 import ezvcard.io.CompatibilityMode;
008 import ezvcard.io.SkipMeException;
009 import ezvcard.util.HCardElement;
010 import ezvcard.util.ISOFormat;
011 import ezvcard.util.VCardDateFormatter;
012 import ezvcard.util.VCardStringUtils;
013 import ezvcard.util.XCardElement;
014
015 /*
016 Copyright (c) 2012, Michael Angstadt
017 All rights reserved.
018
019 Redistribution and use in source and binary forms, with or without
020 modification, are permitted provided that the following conditions are met:
021
022 1. Redistributions of source code must retain the above copyright notice, this
023 list of conditions and the following disclaimer.
024 2. Redistributions in binary form must reproduce the above copyright notice,
025 this list of conditions and the following disclaimer in the documentation
026 and/or other materials provided with the distribution.
027
028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038
039 The views and conclusions contained in the software and documentation are those
040 of the authors and should not be interpreted as representing official policies,
041 either expressed or implied, of the FreeBSD Project.
042 */
043
044 /**
045 * Represents a type whose value is timestamp (i.e. a date, time, and timezone)
046 * @author Michael Angstadt
047 */
048 public class TimestampType extends VCardType {
049 protected Date timestamp;
050
051 /**
052 * @param typeName the name of the type
053 */
054 public TimestampType(String typeName) {
055 this(typeName, null);
056 }
057
058 /**
059 * @param typeName the name of the type
060 * @param timestamp the timestamp
061 */
062 public TimestampType(String typeName, Date timestamp) {
063 super(typeName);
064 this.timestamp = timestamp;
065 }
066
067 /**
068 * Gets the timestamp.
069 * @return the timestamp
070 */
071 public Date getTimestamp() {
072 return timestamp;
073 }
074
075 /**
076 * Sets the timestamp.
077 * @param timestamp the timestamp
078 */
079 public void setTimestamp(Date timestamp) {
080 this.timestamp = timestamp;
081 }
082
083 @Override
084 protected void doMarshalText(StringBuilder sb, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
085 String value = writeValue();
086 sb.append(VCardStringUtils.escape(value));
087 }
088
089 @Override
090 protected void doUnmarshalText(String value, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
091 try {
092 timestamp = VCardDateFormatter.parse(value);
093 } catch (IllegalArgumentException e) {
094 warnings.add("Date string \"" + value + "\" for type \"" + typeName + "\" could not be parsed.");
095 }
096 }
097
098 @Override
099 protected void doMarshalXml(XCardElement parent, List<String> warnings, CompatibilityMode compatibilityMode) {
100 String value = writeValue();
101 parent.timestamp(value);
102 }
103
104 @Override
105 protected void doUnmarshalXml(XCardElement element, List<String> warnings, CompatibilityMode compatibilityMode) {
106 String value = element.timestamp();
107 if (value != null) {
108 doUnmarshalText(value, element.version(), warnings, compatibilityMode);
109 }
110 }
111
112 private String writeValue() {
113 if (timestamp == null) {
114 throw new SkipMeException("Property has no timestamp value associated with it.");
115 }
116 return VCardDateFormatter.format(timestamp, ISOFormat.UTC_TIME_BASIC); //"UTC_TIME_BASIC" works with all vCard versions
117 }
118
119 @Override
120 protected void doUnmarshalHtml(HCardElement element, List<String> warnings) {
121 String value = null;
122 if ("time".equals(element.tagName())) {
123 String datetime = element.attr("datetime");
124 if (datetime.length() > 0) {
125 value = datetime;
126 }
127 }
128 if (value == null) {
129 value = element.value();
130 }
131 doUnmarshalText(value, VCardVersion.V3_0, warnings, CompatibilityMode.RFC);
132 }
133 }