Search in sources :

Example 1 with ProductId

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

the class StreamWriterTest method productId.

@Test
public void productId() throws IOException {
    assertNull(vcard.getProductId());
    // default value
    {
        vcard.setProductId((String) null);
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                assertEquals(1, writer.count(ProductId.class));
            }
        }
        // should be ignored
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                ProductId prodid = writer.first(ProductId.class);
                assertNotEquals("value", prodid.getValue());
            }
        }
    }
    writer.setAddProdId(false);
    {
        vcard.setProductId((String) null);
        {
            for (VCardVersion version : VCardVersion.values()) {
                writer.write(vcard, version);
                assertEquals(0, writer.count());
            }
        }
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(0, writer.count());
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                ProductId prodId = writer.first(ProductId.class);
                assertEquals("value", prodId.getValue());
            }
        }
    }
    writer.setAddProdId(true);
    {
        vcard.setProductId((String) null);
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                assertEquals(1, writer.count(ProductId.class));
            }
        }
        // should be ignored
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                ProductId prodid = writer.first(ProductId.class);
                assertNotEquals("value", prodid.getValue());
            }
        }
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) Test(org.junit.Test)

Example 2 with ProductId

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

the class VCard method setProductId.

/**
 * <p>
 * Sets the product ID, which identifies the software that created the
 * vCard.
 * </p>
 * <p>
 * <b>Property name:</b> {@code PRODID}<br>
 * <b>Supported versions:</b> {@code 3.0, 4.0}
 * </p>
 * @param productId the product ID (e.g. "ez-vcard 1.0") or null to remove
 * @return the property object that was created
 * @see <a href="http://tools.ietf.org/html/rfc6350#page-44">RFC 6350
 * p.44</a>
 * @see <a href="http://tools.ietf.org/html/rfc2426#page-21">RFC 2426
 * p.21</a>
 */
public ProductId setProductId(String productId) {
    ProductId type = (productId == null) ? null : new ProductId(productId);
    setProductId(type);
    return type;
}
Also used : ProductId(ezvcard.property.ProductId)

Example 3 with ProductId

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

the class StreamWriterTest method productId_order.

/**
 * Asserts that the PRODID property is always put at the front of the vCard.
 */
@Test
public void productId_order() throws IOException {
    vcard.setFormattedName("Name");
    {
        writer.write(vcard, V2_1);
        Iterator<VCardProperty> it = writer.propertiesList.iterator();
        VCardProperty property = it.next();
        assertTrue(property instanceof RawProperty);
        property = it.next();
        assertTrue(property instanceof FormattedName);
        assertFalse(it.hasNext());
    }
    for (VCardVersion version : each(V3_0, V4_0)) {
        writer.write(vcard, version);
        Iterator<VCardProperty> it = writer.propertiesList.iterator();
        VCardProperty property = it.next();
        assertTrue(property instanceof ProductId);
        property = it.next();
        assertTrue(property instanceof FormattedName);
        assertFalse(it.hasNext());
    }
    vcard.setProductId("value");
    writer.setAddProdId(false);
    for (VCardVersion version : each(V3_0, V4_0)) {
        writer.write(vcard, version);
        Iterator<VCardProperty> it = writer.propertiesList.iterator();
        VCardProperty property = it.next();
        assertTrue(property instanceof ProductId);
        property = it.next();
        assertTrue(property instanceof FormattedName);
        assertFalse(it.hasNext());
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) FormattedName(ezvcard.property.FormattedName) Iterator(java.util.Iterator) VCardProperty(ezvcard.property.VCardProperty) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) Test(org.junit.Test)

Example 4 with ProductId

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

ProductId (ezvcard.property.ProductId)4 VCardVersion (ezvcard.VCardVersion)3 RawProperty (ezvcard.property.RawProperty)3 VCardProperty (ezvcard.property.VCardProperty)2 Test (org.junit.Test)2 Address (ezvcard.property.Address)1 FormattedName (ezvcard.property.FormattedName)1 Label (ezvcard.property.Label)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1