| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues; |
| 4 |
|
| 5 |
import ezvcard.VCard; |
| 6 |
import ezvcard.VCardDataType; |
| 7 |
import ezvcard.VCardVersion; |
| 8 |
import ezvcard.io.ParseContext; |
| 9 |
import ezvcard.io.json.JCardValue; |
| 10 |
import ezvcard.io.text.WriteContext; |
| 11 |
import ezvcard.io.xml.XCardElement; |
| 12 |
import ezvcard.parameter.KeyType; |
| 13 |
import ezvcard.parameter.MediaTypeParameter; |
| 14 |
import ezvcard.parameter.VCardParameters; |
| 15 |
import ezvcard.property.Key; |
| 16 |
|
| 17 |
|
| 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 |
public class KeyScribe extends BinaryPropertyScribe<Key, KeyType> { |
| 47 |
public KeyScribe() { |
| 48 |
super(Key.class, "KEY"); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected VCardDataType _dataType(Key property, VCardVersion version) { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if (version == VCardVersion.V4_0 && property.getText() != null) { |
| 57 |
return VCardDataType.TEXT; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if (property.getUrl() != null) { |
| 65 |
return VCardDataType.URI; |
| 66 |
} |
| 67 |
|
| 68 |
return super._dataType(property, version); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
protected void _prepareParameters(Key property, VCardParameters copy, VCardVersion version, VCard vcard) { |
| 73 |
if (property.getText() != null) { |
| 74 |
MediaTypeParameter contentType = property.getContentType(); |
| 75 |
if (contentType == null) { |
| 76 |
contentType = new MediaTypeParameter(null, null, null); |
| 77 |
} |
| 78 |
|
| 79 |
copy.setEncoding(null); |
| 80 |
|
| 81 |
switch (version) { |
| 82 |
case V2_1: |
| 83 |
copy.setType(contentType.getValue()); |
| 84 |
copy.setMediaType(null); |
| 85 |
break; |
| 86 |
case V3_0: |
| 87 |
copy.setType(contentType.getValue()); |
| 88 |
copy.setMediaType(null); |
| 89 |
break; |
| 90 |
case V4_0: |
| 91 |
copy.setMediaType(contentType.getMediaType()); |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
super._prepareParameters(property, copy, version, vcard); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
protected String _writeText(Key property, WriteContext context) { |
| 103 |
String text = property.getText(); |
| 104 |
if (text != null) { |
| 105 |
return text; |
| 106 |
} |
| 107 |
|
| 108 |
return super._writeText(property, context); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
protected Key _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 113 |
value = VObjectPropertyValues.unescape(value); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if (dataType == VCardDataType.TEXT) { |
| 119 |
KeyType contentType = parseContentTypeFromValueAndParameters(value, parameters, context.getVersion()); |
| 120 |
Key property = new Key(); |
| 121 |
property.setText(value, contentType); |
| 122 |
return property; |
| 123 |
} |
| 124 |
|
| 125 |
return parse(value, dataType, parameters, context.getVersion()); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
protected void _writeXml(Key property, XCardElement parent) { |
| 130 |
String text = property.getText(); |
| 131 |
if (text != null) { |
| 132 |
parent.append(VCardDataType.TEXT, text); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
super._writeXml(property, parent); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
protected Key _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 141 |
String text = element.first(VCardDataType.TEXT); |
| 142 |
if (text != null) { |
| 143 |
KeyType contentType = parseContentTypeFromValueAndParameters(text, parameters, element.version()); |
| 144 |
Key property = new Key(); |
| 145 |
property.setText(text, contentType); |
| 146 |
return property; |
| 147 |
} |
| 148 |
|
| 149 |
String value = element.first(VCardDataType.URI); |
| 150 |
if (value != null) { |
| 151 |
return parse(value, VCardDataType.URI, parameters, element.version()); |
| 152 |
} |
| 153 |
|
| 154 |
throw missingXmlElements(VCardDataType.URI, VCardDataType.TEXT); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
protected JCardValue _writeJson(Key property) { |
| 159 |
String text = property.getText(); |
| 160 |
if (text != null) { |
| 161 |
return JCardValue.single(text); |
| 162 |
} |
| 163 |
|
| 164 |
return super._writeJson(property); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
protected Key _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 169 |
if (dataType == VCardDataType.TEXT) { |
| 170 |
String valueStr = value.asSingle(); |
| 171 |
KeyType contentType = parseContentTypeFromValueAndParameters(valueStr, parameters, VCardVersion.V4_0); |
| 172 |
Key property = new Key(); |
| 173 |
property.setText(valueStr, contentType); |
| 174 |
return property; |
| 175 |
} |
| 176 |
|
| 177 |
return super._parseJson(value, dataType, parameters, context); |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
protected KeyType _mediaTypeFromTypeParameter(String type) { |
| 182 |
return KeyType.get(type, null, null); |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
protected KeyType _mediaTypeFromMediaTypeParameter(String mediaType) { |
| 187 |
return KeyType.get(null, mediaType, null); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
protected KeyType _mediaTypeFromFileExtension(String extension) { |
| 192 |
return KeyType.find(null, null, extension); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
protected Key _newInstance(String uri, KeyType contentType) { |
| 197 |
return new Key(uri, contentType); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
protected Key _newInstance(byte[] data, KeyType contentType) { |
| 202 |
return new Key(data, contentType); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
protected Key cannotUnmarshalValue(String value, VCardVersion version, KeyType contentType) { |
| 207 |
switch (version) { |
| 208 |
case V2_1: |
| 209 |
case V3_0: |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
Key key = new Key(); |
| 215 |
key.setText(value, contentType); |
| 216 |
return key; |
| 217 |
case V4_0: |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
return new Key(value, contentType); |
| 223 |
} |
| 224 |
|
| 225 |
return null; |
| 226 |
} |
| 227 |
} |