Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueBuilder;
4 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueIterator;
5 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueIterator;
6
7 import ezvcard.VCardDataType;
8 import ezvcard.VCardVersion;
9 import ezvcard.io.CannotParseException;
10 import ezvcard.io.ParseContext;
11 import ezvcard.io.json.JCardValue;
12 import ezvcard.io.text.WriteContext;
13 import ezvcard.io.xml.XCardElement;
14 import ezvcard.parameter.VCardParameters;
15 import ezvcard.property.ClientPidMap;
16
17 /*
18 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /**
43 * Marshals {@link ClientPidMap} properties.
44 * @author Michael Angstadt
45 */
46 public class ClientPidMapScribe extends VCardPropertyScribe<ClientPidMap> {
47 public ClientPidMapScribe() {
48 super(ClientPidMap.class, "CLIENTPIDMAP");
49 }
50
51 @Override
52 protected VCardDataType _defaultDataType(VCardVersion version) {
53 return VCardDataType.TEXT;
54 }
55
56 @Override
57 protected String _writeText(ClientPidMap property, WriteContext context) {
58 SemiStructuredValueBuilder builder = new SemiStructuredValueBuilder();
59 builder.append(property.getPid());
60 builder.append(property.getUri());
61 return builder.build(true, context.isIncludeTrailingSemicolons());
62 }
63
64 @Override
65 protected ClientPidMap _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
66 SemiStructuredValueIterator it = new SemiStructuredValueIterator(value, 2);
67 String pid = it.next();
68 String uri = it.next();
69 if (pid == null || uri == null) {
70 throw new CannotParseException(3);
71 }
72
73 return parse(pid, uri);
74 }
75
76 @Override
77 protected void _writeXml(ClientPidMap property, XCardElement parent) {
78 Integer pid = property.getPid();
79 parent.append("sourceid", (pid == null) ? "" : pid.toString());
80
81 parent.append(VCardDataType.URI, property.getUri());
82 }
83
84 @Override
85 protected ClientPidMap _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
86 String sourceid = element.first("sourceid");
87 String uri = element.first(VCardDataType.URI);
88
89 if (uri == null && sourceid == null) {
90 throw missingXmlElements(VCardDataType.URI.getName().toLowerCase(), "sourceid");
91 }
92 if (uri == null) {
93 throw missingXmlElements(VCardDataType.URI);
94 }
95 if (sourceid == null) {
96 throw missingXmlElements("sourceid");
97 }
98
99 return parse(sourceid, uri);
100 }
101
102 @Override
103 protected JCardValue _writeJson(ClientPidMap property) {
104 return JCardValue.structured(property.getPid(), property.getUri());
105 }
106
107 @Override
108 protected ClientPidMap _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
109 StructuredValueIterator it = new StructuredValueIterator(value.asStructured());
110 String pid = it.nextValue();
111 String uri = it.nextValue();
112 return parse(pid, uri);
113 }
114
115 private ClientPidMap parse(String pid, String uri) {
116 try {
117 return new ClientPidMap(Integer.parseInt(pid), uri);
118 } catch (NumberFormatException e) {
119 throw new CannotParseException(4);
120 }
121 }
122 }