| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import java.util.List; |
| 4 |
|
| 5 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues; |
| 6 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueIterator; |
| 7 |
|
| 8 |
import ezvcard.VCardDataType; |
| 9 |
import ezvcard.VCardVersion; |
| 10 |
import ezvcard.io.ParseContext; |
| 11 |
import ezvcard.io.html.HCardElement; |
| 12 |
import ezvcard.io.json.JCardValue; |
| 13 |
import ezvcard.io.text.WriteContext; |
| 14 |
import ezvcard.io.xml.XCardElement; |
| 15 |
import ezvcard.parameter.VCardParameters; |
| 16 |
import ezvcard.property.Organization; |
| 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 |
|
| 47 |
public class OrganizationScribe extends VCardPropertyScribe<Organization> { |
| 48 |
public OrganizationScribe() { |
| 49 |
super(Organization.class, "ORG"); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 54 |
return VCardDataType.TEXT; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected String _writeText(Organization property, WriteContext context) { |
| 59 |
boolean escapeCommas = (context.getVersion() != VCardVersion.V2_1); |
| 60 |
return VObjectPropertyValues.writeSemiStructured(property.getValues(), escapeCommas, context.isIncludeTrailingSemicolons()); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected Organization _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 65 |
Organization property = new Organization(); |
| 66 |
|
| 67 |
List<String> values = VObjectPropertyValues.parseSemiStructured(value); |
| 68 |
property.getValues().addAll(values); |
| 69 |
|
| 70 |
return property; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
protected void _writeXml(Organization property, XCardElement parent) { |
| 75 |
parent.append(VCardDataType.TEXT.getName().toLowerCase(), property.getValues()); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
protected Organization _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 80 |
List<String> values = element.all(VCardDataType.TEXT); |
| 81 |
if (!values.isEmpty()) { |
| 82 |
Organization property = new Organization(); |
| 83 |
property.getValues().addAll(values); |
| 84 |
return property; |
| 85 |
} |
| 86 |
|
| 87 |
throw missingXmlElements(VCardDataType.TEXT); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
protected Organization _parseHtml(HCardElement element, ParseContext context) { |
| 92 |
Organization property = new Organization(); |
| 93 |
|
| 94 |
String orgName = element.firstValue("organization-name"); |
| 95 |
if (orgName != null) { |
| 96 |
property.getValues().add(orgName); |
| 97 |
} |
| 98 |
|
| 99 |
String orgUnit = element.firstValue("organization-unit"); |
| 100 |
if (orgUnit != null) { |
| 101 |
property.getValues().add(orgUnit); |
| 102 |
} |
| 103 |
|
| 104 |
if (property.getValues().isEmpty()) { |
| 105 |
String value = element.value(); |
| 106 |
if (value.length() > 0) { |
| 107 |
property.getValues().add(value); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return property; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
protected JCardValue _writeJson(Organization property) { |
| 116 |
List<String> values = property.getValues(); |
| 117 |
if (values.isEmpty()) { |
| 118 |
return JCardValue.single(""); |
| 119 |
} |
| 120 |
|
| 121 |
if (values.size() == 1) { |
| 122 |
return JCardValue.single(values.get(0)); |
| 123 |
} |
| 124 |
|
| 125 |
return JCardValue.structured(values.toArray(new Object[0])); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
protected Organization _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 130 |
Organization property = new Organization(); |
| 131 |
|
| 132 |
StructuredValueIterator it = new StructuredValueIterator(value.asStructured()); |
| 133 |
while (it.hasNext()) { |
| 134 |
String next = it.nextValue(); |
| 135 |
if (next != null) { |
| 136 |
property.getValues().add(next); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return property; |
| 141 |
} |
| 142 |
} |