Debian Codegraph @

src/main/java/ezvcard/io/scribe/RawPropertyScribe.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.VCardDataType;
8 import ezvcard.VCardVersion;
9 import ezvcard.io.ParseContext;
10 import ezvcard.io.html.HCardElement;
11 import ezvcard.io.json.JCardValue;
12 import ezvcard.io.json.JsonValue;
13 import ezvcard.io.text.WriteContext;
14 import ezvcard.io.xml.XCardElement;
15 import ezvcard.io.xml.XCardElement.XCardValue;
16 import ezvcard.parameter.VCardParameters;
17 import ezvcard.property.RawProperty;
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 RawProperty} properties.
46 * @author Michael Angstadt
47 */
48 /*
49 * Note concerning escaping and unescaping special characters:
50 *
51 * Values are not escaped and unescaped for the following reason: If the
52 * extended property's value is a list or structured list, then the escaping
53 * must be preserved or else escaped special characters will be lost.
54 *
55 * This is an inconvenience, considering the fact that most extended properties
56 * contain simple text values. But it is necessary in order to prevent data
57 * loss.
58 */
59 public class RawPropertyScribe extends VCardPropertyScribe<RawProperty> {
60 public RawPropertyScribe(String propertyName) {
61 super(RawProperty.class, propertyName);
62 }
63
64 @Override
65 protected VCardDataType _defaultDataType(VCardVersion version) {
66 return null;
67 }
68
69 @Override
70 protected VCardDataType _dataType(RawProperty property, VCardVersion version) {
71 return property.getDataType();
72 }
73
74 @Override
75 protected String _writeText(RawProperty property, WriteContext context) {
76 String value = property.getValue();
77 return (value == null) ? "" : value;
78 }
79
80 @Override
81 protected RawProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
82 RawProperty property = new RawProperty(propertyName, value);
83 property.setDataType(dataType);
84 return property;
85 }
86
87 @Override
88 protected RawProperty _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
89 XCardValue firstValue = element.firstValue();
90 VCardDataType dataType = firstValue.getDataType();
91 String value = firstValue.getValue();
92
93 RawProperty property = new RawProperty(propertyName, value);
94 property.setDataType(dataType);
95 return property;
96 }
97
98 @Override
99 protected RawProperty _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
100 String valueStr = jcardValueToString(value);
101
102 RawProperty property = new RawProperty(propertyName, valueStr);
103 property.setDataType(dataType);
104 return property;
105 }
106
107 @Override
108 protected RawProperty _parseHtml(HCardElement element, ParseContext context) {
109 String value = element.value();
110
111 return new RawProperty(propertyName, value);
112 }
113
114 private static String jcardValueToString(JCardValue value) {
115 /*
116 * VCardPropertyScribe.jcardValueToString() cannot be used because it
117 * escapes single values.
118 */
119 List<JsonValue> values = value.getValues();
120 if (values.size() > 1) {
121 List<String> multi = value.asMulti();
122 if (!multi.isEmpty()) {
123 return VObjectPropertyValues.writeList(multi);
124 }
125 }
126
127 if (!values.isEmpty() && values.get(0).getArray() != null) {
128 List<List<String>> structured = value.asStructured();
129 if (!structured.isEmpty()) {
130 return VObjectPropertyValues.writeStructured(structured, true);
131 }
132 }
133
134 return value.asSingle();
135 }
136 }