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.Related;
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 Related} properties.
039 * @author Michael Angstadt
040 */
041 public class RelatedScribe extends VCardPropertyScribe<Related> {
042 public RelatedScribe() {
043 super(Related.class, "RELATED");
044 }
045
046 @Override
047 protected VCardDataType _defaultDataType(VCardVersion version) {
048 return VCardDataType.URI;
049 }
050
051 @Override
052 protected VCardDataType _dataType(Related property, VCardVersion version) {
053 if (property.getUri() != null) {
054 return VCardDataType.URI;
055 }
056 if (property.getText() != null) {
057 return VCardDataType.TEXT;
058 }
059 return VCardDataType.URI;
060 }
061
062 @Override
063 protected String _writeText(Related property, VCardVersion version) {
064 String uri = property.getUri();
065 if (uri != null) {
066 return uri;
067 }
068
069 String text = property.getText();
070 if (text != null) {
071 return escape(text);
072 }
073
074 return "";
075 }
076
077 @Override
078 protected Related _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) {
079 value = unescape(value);
080
081 Related property = new Related();
082 if (dataType == VCardDataType.TEXT) {
083 property.setText(value);
084 } else {
085 property.setUri(value);
086 }
087 return property;
088 }
089
090 @Override
091 protected void _writeXml(Related property, XCardElement parent) {
092 String uri = property.getUri();
093 if (uri != null) {
094 parent.append(VCardDataType.URI, uri);
095 return;
096 }
097
098 String text = property.getText();
099 if (text != null) {
100 parent.append(VCardDataType.TEXT, text);
101 return;
102 }
103
104 parent.append(VCardDataType.URI, "");
105 }
106
107 @Override
108 protected Related _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) {
109 String uri = element.first(VCardDataType.URI);
110 if (uri != null) {
111 Related property = new Related();
112 property.setUri(uri);
113 return property;
114 }
115
116 String text = element.first(VCardDataType.TEXT);
117 if (text != null) {
118 Related property = new Related();
119 property.setText(text);
120 return property;
121 }
122
123 throw missingXmlElements(VCardDataType.URI, VCardDataType.TEXT);
124 }
125
126 @Override
127 protected JCardValue _writeJson(Related property) {
128 String uri = property.getUri();
129 if (uri != null) {
130 return JCardValue.single(uri);
131 }
132
133 String text = property.getText();
134 if (text != null) {
135 return JCardValue.single(text);
136 }
137
138 return JCardValue.single("");
139 }
140
141 @Override
142 protected Related _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) {
143 String valueStr = value.asSingle();
144
145 Related property = new Related();
146 if (dataType == VCardDataType.TEXT) {
147 property.setText(valueStr);
148 } else {
149 property.setUri(valueStr);
150 }
151 return property;
152 }
153 }