001 package ezvcard.io.scribe;
002
003 import java.util.List;
004
005 import ezvcard.VCardDataType;
006 import ezvcard.VCardVersion;
007 import ezvcard.io.json.JCardValue;
008 import ezvcard.io.xml.XCardElement;
009 import ezvcard.parameter.VCardParameters;
010 import ezvcard.property.Birthplace;
011
012 /*
013 Copyright (c) 2013, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met:
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer.
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution.
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035 */
036
037 /**
038 * Marshals {@link Birthplace} properties.
039 * @author Michael Angstadt
040 */
041 public class BirthplaceScribe extends VCardPropertyScribe<Birthplace> {
042 public BirthplaceScribe() {
043 super(Birthplace.class, "BIRTHPLACE");
044 }
045
046 @Override
047 protected VCardDataType _defaultDataType(VCardVersion version) {
048 return VCardDataType.TEXT;
049 }
050
051 @Override
052 protected VCardDataType _dataType(Birthplace property, VCardVersion version) {
053 if (property.getText() != null) {
054 return VCardDataType.TEXT;
055 }
056 if (property.getUri() != null) {
057 return VCardDataType.URI;
058 }
059 return _defaultDataType(version);
060 }
061
062 @Override
063 protected String _writeText(Birthplace property, VCardVersion version) {
064 String value = property.getText();
065 if (value != null) {
066 return escape(value);
067 }
068
069 value = property.getUri();
070 if (value != null) {
071 return value;
072 }
073
074 return "";
075 }
076
077 @Override
078 protected Birthplace _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) {
079 Birthplace property = new Birthplace();
080 value = unescape(value);
081
082 if (dataType == VCardDataType.TEXT) {
083 property.setText(value);
084 return property;
085 }
086
087 if (dataType == VCardDataType.URI) {
088 property.setUri(value);
089 return property;
090 }
091
092 property.setText(value);
093 return property;
094 }
095
096 @Override
097 protected void _writeXml(Birthplace property, XCardElement parent) {
098 String text = property.getText();
099 if (text != null) {
100 parent.append(VCardDataType.TEXT, text);
101 return;
102 }
103
104 String uri = property.getUri();
105 if (uri != null) {
106 parent.append(VCardDataType.URI, uri);
107 return;
108 }
109
110 parent.append(VCardDataType.TEXT, "");
111 }
112
113 @Override
114 protected Birthplace _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) {
115 Birthplace property = new Birthplace();
116
117 String text = element.first(VCardDataType.TEXT);
118 if (text != null) {
119 property.setText(text);
120 return property;
121 }
122
123 String uri = element.first(VCardDataType.URI);
124 if (uri != null) {
125 property.setUri(uri);
126 return property;
127 }
128
129 throw missingXmlElements(VCardDataType.TEXT, VCardDataType.URI);
130 }
131
132 @Override
133 protected JCardValue _writeJson(Birthplace property) {
134 String text = property.getText();
135 if (text != null) {
136 return JCardValue.single(text);
137 }
138
139 String uri = property.getUri();
140 if (uri != null) {
141 return JCardValue.single(uri);
142 }
143
144 return JCardValue.single("");
145 }
146
147 @Override
148 protected Birthplace _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) {
149 Birthplace property = new Birthplace();
150 String valueStr = value.asSingle();
151
152 if (dataType == VCardDataType.TEXT) {
153 property.setText(valueStr);
154 return property;
155 }
156 if (dataType == VCardDataType.URI) {
157 property.setUri(valueStr);
158 return property;
159 }
160
161 property.setText(valueStr);
162 return property;
163 }
164 }