Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import java.util.List;
4
5 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
6
7 import ezvcard.VCard;
8 import ezvcard.VCardDataType;
9 import ezvcard.VCardVersion;
10 import ezvcard.io.ParseContext;
11 import ezvcard.io.html.HCardElement;
12 import ezvcard.io.json.JCardValue;
13 import ezvcard.io.text.WriteContext;
14 import ezvcard.io.xml.XCardElement;
15 import ezvcard.parameter.VCardParameters;
16 import ezvcard.property.Telephone;
17 import ezvcard.util.TelUri;
18
19 /*
20 Copyright (c) 2012-2023, Michael Angstadt
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions are met:
25
26 1. Redistributions of source code must retain the above copyright notice, this
27 list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright notice,
29 this list of conditions and the following disclaimer in the documentation
30 and/or other materials provided with the distribution.
31
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
36 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /**
45 * Marshals {@link Telephone} properties.
46 * @author Michael Angstadt
47 */
48 public class TelephoneScribe extends VCardPropertyScribe<Telephone> {
49 public TelephoneScribe() {
50 super(Telephone.class, "TEL");
51 }
52
53 @Override
54 protected VCardDataType _defaultDataType(VCardVersion version) {
55 return VCardDataType.TEXT;
56 }
57
58 @Override
59 protected VCardDataType _dataType(Telephone property, VCardVersion version) {
60 if (version == VCardVersion.V4_0) {
61 if (property.getText() != null) {
62 return VCardDataType.TEXT;
63 }
64 if (property.getUri() != null) {
65 return VCardDataType.URI;
66 }
67 }
68
69 return VCardDataType.TEXT;
70 }
71
72 @Override
73 protected void _prepareParameters(Telephone property, VCardParameters copy, VCardVersion version, VCard vcard) {
74 handlePrefParam(property, copy, version, vcard);
75 }
76
77 @Override
78 protected String _writeText(Telephone property, WriteContext context) {
79 String text = property.getText();
80 if (text != null) {
81 return escape(text, context);
82 }
83
84 TelUri uri = property.getUri();
85 if (uri != null) {
86 if (context.getVersion() == VCardVersion.V4_0) {
87 return uri.toString();
88 }
89
90 String ext = uri.getExtension();
91 String value = (ext == null) ? uri.getNumber() : uri.getNumber() + " x" + ext;
92 return escape(value, context);
93 }
94
95 return "";
96 }
97
98 @Override
99 protected Telephone _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
100 value = VObjectPropertyValues.unescape(value);
101 return parse(value, dataType, context);
102 }
103
104 @Override
105 protected void _writeXml(Telephone property, XCardElement parent) {
106 String text = property.getText();
107 if (text != null) {
108 parent.append(VCardDataType.TEXT, text);
109 return;
110 }
111
112 TelUri uri = property.getUri();
113 if (uri != null) {
114 parent.append(VCardDataType.URI, uri.toString());
115 return;
116 }
117
118 parent.append(VCardDataType.TEXT, "");
119 }
120
121 @Override
122 protected Telephone _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
123 String text = element.first(VCardDataType.TEXT);
124 if (text != null) {
125 return new Telephone(text);
126 }
127
128 String uri = element.first(VCardDataType.URI);
129 if (uri != null) {
130 try {
131 return new Telephone(TelUri.parse(uri));
132 } catch (IllegalArgumentException e) {
133 context.addWarning(18);
134 return new Telephone(uri);
135 }
136 }
137
138 throw missingXmlElements(VCardDataType.TEXT, VCardDataType.URI);
139 }
140
141 @Override
142 protected Telephone _parseHtml(HCardElement element, ParseContext context) {
143 Telephone property;
144 String href = element.attr("href");
145 try {
146 property = new Telephone(TelUri.parse(href));
147 } catch (IllegalArgumentException e) {
148 //not a tel URI
149 property = new Telephone(element.value());
150 }
151
152 List<String> types = element.types();
153 property.getParameters().putAll(VCardParameters.TYPE, types);
154
155 return property;
156 }
157
158 @Override
159 protected JCardValue _writeJson(Telephone property) {
160 String text = property.getText();
161 if (text != null) {
162 return JCardValue.single(text);
163 }
164
165 TelUri uri = property.getUri();
166 if (uri != null) {
167 return JCardValue.single(uri.toString());
168 }
169
170 return JCardValue.single("");
171 }
172
173 @Override
174 protected Telephone _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
175 String valueStr = value.asSingle();
176 return parse(valueStr, dataType, context);
177 }
178
179 private Telephone parse(String value, VCardDataType dataType, ParseContext context) {
180 try {
181 return new Telephone(TelUri.parse(value));
182 } catch (IllegalArgumentException e) {
183 if (dataType == VCardDataType.URI) {
184 context.addWarning(18);
185 }
186 }
187
188 return new Telephone(value);
189 }
190 }