Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static ezvcard.util.StringUtils.join;
4
5 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueBuilder;
6 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueIterator;
7 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueBuilder;
8 import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueIterator;
9
10 import ezvcard.VCardDataType;
11 import ezvcard.VCardVersion;
12 import ezvcard.io.ParseContext;
13 import ezvcard.io.html.HCardElement;
14 import ezvcard.io.json.JCardValue;
15 import ezvcard.io.text.WriteContext;
16 import ezvcard.io.xml.XCardElement;
17 import ezvcard.parameter.VCardParameters;
18 import ezvcard.property.StructuredName;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 /**
46 * Marshals {@link StructuredName} properties.
47 * @author Michael Angstadt
48 */
49 public class StructuredNameScribe extends VCardPropertyScribe<StructuredName> {
50 public StructuredNameScribe() {
51 super(StructuredName.class, "N");
52 }
53
54 @Override
55 protected VCardDataType _defaultDataType(VCardVersion version) {
56 return VCardDataType.TEXT;
57 }
58
59 @Override
60 protected String _writeText(StructuredName property, WriteContext context) {
61 /*
62 * StructuredValueBuilder cannot be used with 2.1 because it escapes
63 * comma characters. For example, if someone's last name is "Foo,bar",
64 * the comma character must NOT be escaped when written to a 2.1 vCard.
65 *
66 * The reason commas are not escaped in 2.1 is because 2.1 does not
67 * allow multi-valued components like 3.0 and 4.0 do (for example,
68 * multiple suffixes).
69 *
70 * If a StructuredName object has multi-valued components, and it is
71 * being written to a 2.1 vCard, then ez-vcard will comma-delimit them
72 * to prevent data loss. But this is not part of the 2.1 syntax.
73 */
74 if (context.getVersion() == VCardVersion.V2_1) {
75 SemiStructuredValueBuilder builder = new SemiStructuredValueBuilder();
76 builder.append(property.getFamily());
77 builder.append(property.getGiven());
78 builder.append(join(property.getAdditionalNames(), ","));
79 builder.append(join(property.getPrefixes(), ","));
80 builder.append(join(property.getSuffixes(), ","));
81 return builder.build(false, context.isIncludeTrailingSemicolons());
82 } else {
83 StructuredValueBuilder builder = new StructuredValueBuilder();
84 builder.append(property.getFamily());
85 builder.append(property.getGiven());
86 builder.append(property.getAdditionalNames());
87 builder.append(property.getPrefixes());
88 builder.append(property.getSuffixes());
89 return builder.build(context.isIncludeTrailingSemicolons());
90 }
91 }
92
93 @Override
94 protected StructuredName _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
95 StructuredName property = new StructuredName();
96
97 if (context.getVersion() == VCardVersion.V2_1) {
98 /*
99 * 2.1 does not recognize multi-valued components.
100 */
101 SemiStructuredValueIterator it = new SemiStructuredValueIterator(value);
102 property.setFamily(it.next());
103 property.setGiven(it.next());
104
105 String next = it.next();
106 if (next != null) {
107 property.getAdditionalNames().add(next);
108 }
109
110 next = it.next();
111 if (next != null) {
112 property.getPrefixes().add(next);
113 }
114
115 next = it.next();
116 if (next != null) {
117 property.getSuffixes().add(next);
118 }
119 } else {
120 StructuredValueIterator it = new StructuredValueIterator(value);
121 property.setFamily(it.nextValue());
122 property.setGiven(it.nextValue());
123 property.getAdditionalNames().addAll(it.nextComponent());
124 property.getPrefixes().addAll(it.nextComponent());
125 property.getSuffixes().addAll(it.nextComponent());
126 }
127
128 return property;
129 }
130
131 @Override
132 protected void _writeXml(StructuredName property, XCardElement parent) {
133 parent.append("surname", property.getFamily()); //the XML element still needs to be printed if value == null
134 parent.append("given", property.getGiven());
135 parent.append("additional", property.getAdditionalNames());
136 parent.append("prefix", property.getPrefixes());
137 parent.append("suffix", property.getSuffixes());
138 }
139
140 @Override
141 protected StructuredName _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
142 StructuredName property = new StructuredName();
143
144 property.setFamily(s(element.first("surname")));
145 property.setGiven(s(element.first("given")));
146 property.getAdditionalNames().addAll(element.all("additional"));
147 property.getPrefixes().addAll(element.all("prefix"));
148 property.getSuffixes().addAll(element.all("suffix"));
149
150 return property;
151 }
152
153 private static String s(String value) {
154 return (value == null || value.isEmpty()) ? null : value;
155 }
156
157 @Override
158 protected StructuredName _parseHtml(HCardElement element, ParseContext context) {
159 StructuredName property = new StructuredName();
160
161 property.setFamily(s(element.firstValue("family-name")));
162 property.setGiven(s(element.firstValue("given-name")));
163 property.getAdditionalNames().addAll(element.allValues("additional-name"));
164 property.getPrefixes().addAll(element.allValues("honorific-prefix"));
165 property.getSuffixes().addAll(element.allValues("honorific-suffix"));
166
167 return property;
168 }
169
170 @Override
171 protected JCardValue _writeJson(StructuredName property) {
172 return JCardValue.structured(property.getFamily(), property.getGiven(), property.getAdditionalNames(), property.getPrefixes(), property.getSuffixes());
173 }
174
175 @Override
176 protected StructuredName _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
177 StructuredName property = new StructuredName();
178 StructuredValueIterator it = new StructuredValueIterator(value.asStructured());
179
180 property.setFamily(it.nextValue());
181 property.setGiven(it.nextValue());
182 property.getAdditionalNames().addAll(it.nextComponent());
183 property.getPrefixes().addAll(it.nextComponent());
184 property.getSuffixes().addAll(it.nextComponent());
185
186 return property;
187 }
188 }