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