Debian Codegraph @

src/test/java/ezvcard/property/FavoriteColors.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
7
8 import ezvcard.VCard;
9 import ezvcard.VCardDataType;
10 import ezvcard.VCardVersion;
11 import ezvcard.ValidationWarning;
12 import ezvcard.io.CannotParseException;
13 import ezvcard.io.ParseContext;
14 import ezvcard.io.html.HCardElement;
15 import ezvcard.io.json.JCardValue;
16 import ezvcard.io.scribe.VCardPropertyScribe;
17 import ezvcard.io.text.WriteContext;
18 import ezvcard.io.xml.XCardElement;
19 import ezvcard.parameter.VCardParameters;
20
21 /*
22 Copyright (c) 2012-2023, Michael Angstadt
23 All rights reserved.
24
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions are met:
27
28 1. Redistributions of source code must retain the above copyright notice, this
29 list of conditions and the following disclaimer.
30 2. Redistributions in binary form must reproduce the above copyright notice,
31 this list of conditions and the following disclaimer in the documentation
32 and/or other materials provided with the distribution.
33
34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
38 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46 /**
47 * @author Michael Angstadt
48 */
49 public class FavoriteColors extends VCardProperty {
50 private List<String> colors = new ArrayList<>();
51
52 public List<String> getColors() {
53 return colors;
54 }
55
56 public void addColor(String color) {
57 colors.add(color);
58 }
59
60 public String getLang() {
61 return parameters.getLanguage();
62 }
63
64 public void setLang(String lang) {
65 parameters.setLanguage(lang);
66 }
67
68 //optional
69 //validates the property's data
70 //invoked when "VCard.validate()" is called
71 @Override
72 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
73 if (colors.isEmpty()) {
74 warnings.add(new ValidationWarning("No colors are defined."));
75 }
76
77 if (colors.contains("periwinkle") && version == VCardVersion.V4_0) {
78 warnings.add(new ValidationWarning("Periwinkle is deprecated in vCard 4.0."));
79 }
80 }
81
82 public static class FavoriteColorsScribe extends VCardPropertyScribe<FavoriteColors> {
83 public FavoriteColorsScribe() {
84 super(FavoriteColors.class, "X-FAV-COLORS");
85 }
86
87 //required
88 //defines the property's default data type
89 @Override
90 protected VCardDataType _defaultDataType(VCardVersion version) {
91 return null;
92 }
93
94 //optional
95 //determines the data type based on the property value
96 @Override
97 protected VCardDataType _dataType(FavoriteColors property, VCardVersion version) {
98 return VCardDataType.TEXT;
99 }
100
101 //optional
102 //tweaks the property's parameters before the property is written
103 @Override
104 protected void _prepareParameters(FavoriteColors property, VCardParameters copy, VCardVersion version, VCard vcard) {
105 if (copy.getLanguage() == null) {
106 copy.setLanguage("en");
107 }
108 }
109
110 //required
111 //writes the property's value to a plain-text vCard
112 @Override
113 protected String _writeText(FavoriteColors property, WriteContext context) {
114 return VObjectPropertyValues.writeList(property.getColors());
115 }
116
117 //required
118 //parses the property's value from a plain-text vCard
119 @Override
120 protected FavoriteColors _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
121 FavoriteColors prop = new FavoriteColors();
122 for (String color : VObjectPropertyValues.parseList(value)) {
123 prop.addColor(color);
124 }
125 return prop;
126 }
127
128 //optional
129 //writes the property to an XML document (xCard)
130 @Override
131 protected void _writeXml(FavoriteColors property, XCardElement element) {
132 for (String color : property.getColors()) {
133 element.append(VCardDataType.TEXT, color);
134 }
135 }
136
137 //optional
138 //parses the property from an XML document (xCard)
139 @Override
140 protected FavoriteColors _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
141 List<String> colors = element.all(VCardDataType.TEXT);
142 if (colors.isEmpty()) {
143 throw new CannotParseException("No <text> elements found.");
144 }
145
146 FavoriteColors property = new FavoriteColors();
147 for (String color : colors) {
148 property.addColor(color);
149 }
150 return property;
151 }
152
153 //optional
154 //parses the property value from an HTML page (hCard)
155 @Override
156 protected FavoriteColors _parseHtml(HCardElement element, ParseContext context) {
157 FavoriteColors property = new FavoriteColors();
158
159 String lang = element.attr("lang");
160 property.setLanguage((lang.isEmpty()) ? null : lang);
161
162 property.getColors().addAll(element.allValues("color")); //gets the hCard values of all descendant elements that have a CSS class named "color"
163
164 return property;
165 }
166
167 //optional
168 //writes the property to a JSON stream (jCard)
169 @Override
170 protected JCardValue _writeJson(FavoriteColors property) {
171 return JCardValue.multi(property.getColors());
172 }
173
174 //optional
175 //parses the property value from a JSON stream (jCard)
176 @Override
177 protected FavoriteColors _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
178 FavoriteColors property = new FavoriteColors();
179 for (String color : value.asMulti()) {
180 property.addColor(color);
181 }
182 return property;
183 }
184 }
185 }