Search in sources :

Example 6 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class HCardParserTest method assign_labels_to_addresses.

@Test
public void assign_labels_to_addresses() throws Exception {
    // @formatter:off
    VCardAsserter asserter = readHtml("<html>" + "<body>" + "<div class=\"vcard\">" + "<div class=\"adr\">" + "<span class=\"type\">Home</span>:<br>" + "<span class=\"street-address\">123 Main St.</span><br>" + "<span class=\"locality\">Austin</span>, <span class=\"region\">TX</span> <span class=\"postal-code\">12345</span>" + "</div>" + "<div class=\"label\"><abbr class=\"type\" title=\"home\"></abbr>123 Main St.\nAustin, TX 12345</div>" + "<abbr class=\"label\" title=\"456 Wall St., New York, NY 67890\"><abbr class=\"type\" title=\"work\"></abbr></abbr>" + "</div>" + "</body>" + "</html>");
    asserter.next(V3_0);
    asserter.address().streetAddress("123 Main St.").locality("Austin").region("TX").postalCode("12345").label("123 Main St. Austin, TX 12345").types(AddressType.HOME).noMore();
    asserter.simpleProperty(Label.class).value("456 Wall St., New York, NY 67890").param("TYPE", "work").noMore();
    asserter.done();
// @formatter:on
}
Also used : Label(ezvcard.property.Label) VCardAsserter(ezvcard.property.asserter.VCardAsserter) Test(org.junit.Test)

Example 7 with Label

use of ezvcard.property.Label 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 8 with Label

use of ezvcard.property.Label 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

Label (ezvcard.property.Label)8 VCardVersion (ezvcard.VCardVersion)4 Address (ezvcard.property.Address)4 Test (org.junit.Test)4 VCard (ezvcard.VCard)2 RawProperty (ezvcard.property.RawProperty)2 VCardProperty (ezvcard.property.VCardProperty)2 VCardAsserter (ezvcard.property.asserter.VCardAsserter)2 HashSet (java.util.HashSet)2 CannotParseException (ezvcard.io.CannotParseException)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 SkipMeException (ezvcard.io.SkipMeException)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 AddressType (ezvcard.parameter.AddressType)1 Categories (ezvcard.property.Categories)1 Impp (ezvcard.property.Impp)1 Nickname (ezvcard.property.Nickname)1 ProductId (ezvcard.property.ProductId)1 ArrayList (java.util.ArrayList)1