Search in sources :

Example 26 with Address

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();
}
Also used : Address(ezvcard.property.Address) Test(org.junit.Test)

Example 27 with Address

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);
        }
    }
}
Also used : Address(ezvcard.property.Address) Label(ezvcard.property.Label) AddressType(ezvcard.parameter.AddressType) HashSet(java.util.HashSet)

Example 28 with Address

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;
}
Also used : Address(ezvcard.property.Address) ArrayList(java.util.ArrayList) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) RawProperty(ezvcard.property.RawProperty) VCardProperty(ezvcard.property.VCardProperty) HashSet(java.util.HashSet)

Aggregations

Address (ezvcard.property.Address)28 Test (org.junit.Test)14 VCard (ezvcard.VCard)12 StructuredName (ezvcard.property.StructuredName)9 Telephone (ezvcard.property.Telephone)8 Timezone (ezvcard.property.Timezone)6 TelUri (ezvcard.util.TelUri)6 UtcOffset (ezvcard.util.UtcOffset)6 Birthday (ezvcard.property.Birthday)5 Anniversary (ezvcard.property.Anniversary)4 Geo (ezvcard.property.Geo)4 Key (ezvcard.property.Key)4 Label (ezvcard.property.Label)4 VCardVersion (ezvcard.VCardVersion)3 Email (ezvcard.property.Email)3 Photo (ezvcard.property.Photo)3 Sound (ezvcard.property.Sound)3 File (java.io.File)3 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)2 Person (com.google.api.services.people.v1.model.Person)2