| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import static ezvcard.util.StringUtils.join; |
| 4 |
|
| 5 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueBuilder; |
| 6 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.SemiStructuredValueIterator; |
| 7 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueBuilder; |
| 8 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues.StructuredValueIterator; |
| 9 |
|
| 10 |
import ezvcard.VCardDataType; |
| 11 |
import ezvcard.VCardVersion; |
| 12 |
import ezvcard.io.ParseContext; |
| 13 |
import ezvcard.io.html.HCardElement; |
| 14 |
import ezvcard.io.json.JCardValue; |
| 15 |
import ezvcard.io.text.WriteContext; |
| 16 |
import ezvcard.io.xml.XCardElement; |
| 17 |
import ezvcard.parameter.VCardParameters; |
| 18 |
import ezvcard.property.StructuredName; |
| 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 |
public class StructuredNameScribe extends VCardPropertyScribe<StructuredName> { |
| 50 |
public StructuredNameScribe() { |
| 51 |
super(StructuredName.class, "N"); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 56 |
return VCardDataType.TEXT; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected String _writeText(StructuredName property, WriteContext context) { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if (context.getVersion() == VCardVersion.V2_1) { |
| 75 |
SemiStructuredValueBuilder builder = new SemiStructuredValueBuilder(); |
| 76 |
builder.append(property.getFamily()); |
| 77 |
builder.append(property.getGiven()); |
| 78 |
builder.append(join(property.getAdditionalNames(), ",")); |
| 79 |
builder.append(join(property.getPrefixes(), ",")); |
| 80 |
builder.append(join(property.getSuffixes(), ",")); |
| 81 |
return builder.build(false, context.isIncludeTrailingSemicolons()); |
| 82 |
} else { |
| 83 |
StructuredValueBuilder builder = new StructuredValueBuilder(); |
| 84 |
builder.append(property.getFamily()); |
| 85 |
builder.append(property.getGiven()); |
| 86 |
builder.append(property.getAdditionalNames()); |
| 87 |
builder.append(property.getPrefixes()); |
| 88 |
builder.append(property.getSuffixes()); |
| 89 |
return builder.build(context.isIncludeTrailingSemicolons()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
protected StructuredName _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 95 |
StructuredName property = new StructuredName(); |
| 96 |
|
| 97 |
if (context.getVersion() == VCardVersion.V2_1) { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
SemiStructuredValueIterator it = new SemiStructuredValueIterator(value); |
| 102 |
property.setFamily(it.next()); |
| 103 |
property.setGiven(it.next()); |
| 104 |
|
| 105 |
String next = it.next(); |
| 106 |
if (next != null) { |
| 107 |
property.getAdditionalNames().add(next); |
| 108 |
} |
| 109 |
|
| 110 |
next = it.next(); |
| 111 |
if (next != null) { |
| 112 |
property.getPrefixes().add(next); |
| 113 |
} |
| 114 |
|
| 115 |
next = it.next(); |
| 116 |
if (next != null) { |
| 117 |
property.getSuffixes().add(next); |
| 118 |
} |
| 119 |
} else { |
| 120 |
StructuredValueIterator it = new StructuredValueIterator(value); |
| 121 |
property.setFamily(it.nextValue()); |
| 122 |
property.setGiven(it.nextValue()); |
| 123 |
property.getAdditionalNames().addAll(it.nextComponent()); |
| 124 |
property.getPrefixes().addAll(it.nextComponent()); |
| 125 |
property.getSuffixes().addAll(it.nextComponent()); |
| 126 |
} |
| 127 |
|
| 128 |
return property; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
protected void _writeXml(StructuredName property, XCardElement parent) { |
| 133 |
parent.append("surname", property.getFamily()); |
| 134 |
parent.append("given", property.getGiven()); |
| 135 |
parent.append("additional", property.getAdditionalNames()); |
| 136 |
parent.append("prefix", property.getPrefixes()); |
| 137 |
parent.append("suffix", property.getSuffixes()); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
protected StructuredName _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 142 |
StructuredName property = new StructuredName(); |
| 143 |
|
| 144 |
property.setFamily(s(element.first("surname"))); |
| 145 |
property.setGiven(s(element.first("given"))); |
| 146 |
property.getAdditionalNames().addAll(element.all("additional")); |
| 147 |
property.getPrefixes().addAll(element.all("prefix")); |
| 148 |
property.getSuffixes().addAll(element.all("suffix")); |
| 149 |
|
| 150 |
return property; |
| 151 |
} |
| 152 |
|
| 153 |
private static String s(String value) { |
| 154 |
return (value == null || value.isEmpty()) ? null : value; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
protected StructuredName _parseHtml(HCardElement element, ParseContext context) { |
| 159 |
StructuredName property = new StructuredName(); |
| 160 |
|
| 161 |
property.setFamily(s(element.firstValue("family-name"))); |
| 162 |
property.setGiven(s(element.firstValue("given-name"))); |
| 163 |
property.getAdditionalNames().addAll(element.allValues("additional-name")); |
| 164 |
property.getPrefixes().addAll(element.allValues("honorific-prefix")); |
| 165 |
property.getSuffixes().addAll(element.allValues("honorific-suffix")); |
| 166 |
|
| 167 |
return property; |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
protected JCardValue _writeJson(StructuredName property) { |
| 172 |
return JCardValue.structured(property.getFamily(), property.getGiven(), property.getAdditionalNames(), property.getPrefixes(), property.getSuffixes()); |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
protected StructuredName _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 177 |
StructuredName property = new StructuredName(); |
| 178 |
StructuredValueIterator it = new StructuredValueIterator(value.asStructured()); |
| 179 |
|
| 180 |
property.setFamily(it.nextValue()); |
| 181 |
property.setGiven(it.nextValue()); |
| 182 |
property.getAdditionalNames().addAll(it.nextComponent()); |
| 183 |
property.getPrefixes().addAll(it.nextComponent()); |
| 184 |
property.getSuffixes().addAll(it.nextComponent()); |
| 185 |
|
| 186 |
return property; |
| 187 |
} |
| 188 |
} |