| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueBuilder; |
| 4 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueIterator; |
| 5 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueIterator; |
| 6 |
|
| 7 |
import ezvcard.VCardDataType; |
| 8 |
import ezvcard.VCardVersion; |
| 9 |
import ezvcard.io.CannotParseException; |
| 10 |
import ezvcard.io.ParseContext; |
| 11 |
import ezvcard.io.json.JCardValue; |
| 12 |
import ezvcard.io.text.WriteContext; |
| 13 |
import ezvcard.io.xml.XCardElement; |
| 14 |
import ezvcard.parameter.VCardParameters; |
| 15 |
import ezvcard.property.ClientPidMap; |
| 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 ClientPidMapScribe extends VCardPropertyScribe<ClientPidMap> { |
| 47 |
public ClientPidMapScribe() { |
| 48 |
super(ClientPidMap.class, "CLIENTPIDMAP"); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 53 |
return VCardDataType.TEXT; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected String _writeText(ClientPidMap property, WriteContext context) { |
| 58 |
SemiStructuredValueBuilder builder = new SemiStructuredValueBuilder(); |
| 59 |
builder.append(property.getPid()); |
| 60 |
builder.append(property.getUri()); |
| 61 |
return builder.build(true, context.isIncludeTrailingSemicolons()); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
protected ClientPidMap _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 66 |
SemiStructuredValueIterator it = new SemiStructuredValueIterator(value, 2); |
| 67 |
String pid = it.next(); |
| 68 |
String uri = it.next(); |
| 69 |
if (pid == null || uri == null) { |
| 70 |
throw new CannotParseException(3); |
| 71 |
} |
| 72 |
|
| 73 |
return parse(pid, uri); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
protected void _writeXml(ClientPidMap property, XCardElement parent) { |
| 78 |
Integer pid = property.getPid(); |
| 79 |
parent.append("sourceid", (pid == null) ? "" : pid.toString()); |
| 80 |
|
| 81 |
parent.append(VCardDataType.URI, property.getUri()); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
protected ClientPidMap _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 86 |
String sourceid = element.first("sourceid"); |
| 87 |
String uri = element.first(VCardDataType.URI); |
| 88 |
|
| 89 |
if (uri == null && sourceid == null) { |
| 90 |
throw missingXmlElements(VCardDataType.URI.getName().toLowerCase(), "sourceid"); |
| 91 |
} |
| 92 |
if (uri == null) { |
| 93 |
throw missingXmlElements(VCardDataType.URI); |
| 94 |
} |
| 95 |
if (sourceid == null) { |
| 96 |
throw missingXmlElements("sourceid"); |
| 97 |
} |
| 98 |
|
| 99 |
return parse(sourceid, uri); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
protected JCardValue _writeJson(ClientPidMap property) { |
| 104 |
return JCardValue.structured(property.getPid(), property.getUri()); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
protected ClientPidMap _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 109 |
StructuredValueIterator it = new StructuredValueIterator(value.asStructured()); |
| 110 |
String pid = it.nextValue(); |
| 111 |
String uri = it.nextValue(); |
| 112 |
return parse(pid, uri); |
| 113 |
} |
| 114 |
|
| 115 |
private ClientPidMap parse(String pid, String uri) { |
| 116 |
try { |
| 117 |
return new ClientPidMap(Integer.parseInt(pid), uri); |
| 118 |
} catch (NumberFormatException e) { |
| 119 |
throw new CannotParseException(4); |
| 120 |
} |
| 121 |
} |
| 122 |
} |