Debian Codegraph @

src/main/java/ezvcard/io/scribe/RelatedScribe.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
4
5 import ezvcard.VCardDataType;
6 import ezvcard.VCardVersion;
7 import ezvcard.io.ParseContext;
8 import ezvcard.io.json.JCardValue;
9 import ezvcard.io.text.WriteContext;
10 import ezvcard.io.xml.XCardElement;
11 import ezvcard.parameter.VCardParameters;
12 import ezvcard.property.Related;
13
14 /*
15 Copyright (c) 2012-2023, Michael Angstadt
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21 1. Redistributions of source code must retain the above copyright notice, this
22 list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /**
40 * Marshals {@link Related} properties.
41 * @author Michael Angstadt
42 */
43 public class RelatedScribe extends VCardPropertyScribe<Related> {
44 public RelatedScribe() {
45 super(Related.class, "RELATED");
46 }
47
48 @Override
49 protected VCardDataType _defaultDataType(VCardVersion version) {
50 return VCardDataType.URI;
51 }
52
53 @Override
54 protected VCardDataType _dataType(Related property, VCardVersion version) {
55 if (property.getUri() != null) {
56 return VCardDataType.URI;
57 }
58 if (property.getText() != null) {
59 return VCardDataType.TEXT;
60 }
61 return VCardDataType.URI;
62 }
63
64 @Override
65 protected String _writeText(Related property, WriteContext context) {
66 String uri = property.getUri();
67 if (uri != null) {
68 return uri;
69 }
70
71 String text = property.getText();
72 if (text != null) {
73 return VObjectPropertyValues.escape(text);
74 }
75
76 return "";
77 }
78
79 @Override
80 protected Related _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
81 value = VObjectPropertyValues.unescape(value);
82
83 Related property = new Related();
84 if (dataType == VCardDataType.TEXT) {
85 property.setText(value);
86 } else {
87 property.setUri(value);
88 }
89 return property;
90 }
91
92 @Override
93 protected void _writeXml(Related property, XCardElement parent) {
94 String uri = property.getUri();
95 if (uri != null) {
96 parent.append(VCardDataType.URI, uri);
97 return;
98 }
99
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 }