use of ezvcard.property.Address in project ez-vcard by mangstadt.
the class AddressScribeTest method prepareParameters_label.
@Test
public void prepareParameters_label() {
Address property = new Address();
property.setLabel("label");
// 2.1 and 3.0 should remove it
sensei.assertPrepareParams(property).versions(V2_1, V3_0).run();
// 4.0 should keep it
sensei.assertPrepareParams(property).versions(V4_0).expected("LABEL", "label").run();
}
use of ezvcard.property.Address in project ez-vcard by mangstadt.
the class StreamReader method assignLabels.
/**
* Matches up a list of {@link Label} properties with their corresponding
* {@link Address} properties. If no match can be found, then the LABEL
* property itself is assigned to the vCard.
* @param vcard the vCard that the properties belong to
* @param labels the LABEL properties
*/
protected void assignLabels(VCard vcard, List<Label> labels) {
List<Address> adrs = vcard.getAddresses();
for (Label label : labels) {
boolean orphaned = true;
Set<AddressType> labelTypes = new HashSet<AddressType>(label.getTypes());
for (Address adr : adrs) {
if (adr.getLabel() != null) {
// a label has already been assigned to it
continue;
}
Set<AddressType> adrTypes = new HashSet<AddressType>(adr.getTypes());
if (adrTypes.equals(labelTypes)) {
adr.setLabel(label.getValue());
orphaned = false;
break;
}
}
if (orphaned) {
vcard.addOrphanedLabel(label);
}
}
}
use of ezvcard.property.Address in project ez-vcard by mangstadt.
the class StreamWriter method prepare.
/**
* Determines which properties need to be written.
* @param vcard the vCard to write
* @return the properties to write
* @throws IllegalArgumentException if a scribe hasn't been registered for a
* custom property class (see: {@link #registerScribe(VCardPropertyScribe)
* registerScribe})
*/
private List<VCardProperty> prepare(VCard vcard) {
VCardVersion targetVersion = getTargetVersion();
List<VCardProperty> propertiesToAdd = new ArrayList<VCardProperty>();
Set<Class<? extends VCardProperty>> unregistered = new HashSet<Class<? extends VCardProperty>>();
VCardProperty prodIdProperty = null;
for (VCardProperty property : vcard) {
if (versionStrict && !property.isSupportedBy(targetVersion)) {
// do not add the property to the vCard if it is not supported by the target version
continue;
}
// do not add PRODID to the property list yet
if (property instanceof ProductId) {
prodIdProperty = property;
continue;
}
// check for scribe
if (!index.hasPropertyScribe(property)) {
unregistered.add(property.getClass());
continue;
}
propertiesToAdd.add(property);
// add LABEL properties for each ADR property if the target version is 2.1 or 3.0
if ((targetVersion == VCardVersion.V2_1 || targetVersion == VCardVersion.V3_0) && property instanceof Address) {
Address adr = (Address) property;
String labelStr = adr.getLabel();
if (labelStr == null) {
continue;
}
Label label = new Label(labelStr);
label.getTypes().addAll(adr.getTypes());
propertiesToAdd.add(label);
}
}
if (!unregistered.isEmpty()) {
List<String> classes = new ArrayList<String>(unregistered.size());
for (Class<? extends VCardProperty> clazz : unregistered) {
classes.add(clazz.getName());
}
throw Messages.INSTANCE.getIllegalArgumentException(14, classes);
}
// create a PRODID property, saying the vCard was generated by this library
if (addProdId) {
if (targetVersion == VCardVersion.V2_1) {
prodIdProperty = new RawProperty("X-PRODID", "ez-vcard " + Ezvcard.VERSION);
} else {
prodIdProperty = new ProductId("ez-vcard " + Ezvcard.VERSION);
}
}
// add PRODID to the beginning of the vCard
if (prodIdProperty != null) {
propertiesToAdd.add(0, prodIdProperty);
}
return propertiesToAdd;
}
Aggregations