| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 117 |
|
| 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 |
} |