Debian Codegraph @

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

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