Search in sources :

Example 6 with VCardProperty

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

the class VCardWriterTest method date_time_properties_should_not_have_a_VALUE_parameter.

@Test
public void date_time_properties_should_not_have_a_VALUE_parameter() throws Throwable {
    class DateTestScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {

        private final VCardDataType dataType;

        public DateTestScribe(Class<T> clazz, String name, VCardDataType dataType) {
            super(clazz, name);
            this.dataType = dataType;
        }

        @Override
        protected VCardDataType _defaultDataType(VCardVersion version) {
            return VCardDataType.DATE_AND_OR_TIME;
        }

        @Override
        protected VCardDataType _dataType(T property, VCardVersion version) {
            return dataType;
        }

        @Override
        protected String _writeText(T property, WriteContext context) {
            return "value";
        }

        @Override
        protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
            return null;
        }
    }
    class DateProperty extends VCardProperty {
        // empty
    }
    class DateTimeProperty extends VCardProperty {
        // empty
    }
    class TimeProperty extends VCardProperty {
        // empty
    }
    class DateAndOrTimeProperty extends VCardProperty {
        // empty
    }
    VCard vcard = new VCard();
    vcard.addProperty(new DateProperty());
    vcard.addProperty(new DateTimeProperty());
    vcard.addProperty(new TimeProperty());
    vcard.addProperty(new DateAndOrTimeProperty());
    StringWriter sw = new StringWriter();
    VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
    vcw.registerScribe(new DateTestScribe<DateProperty>(DateProperty.class, "DATE", VCardDataType.DATE));
    vcw.registerScribe(new DateTestScribe<DateTimeProperty>(DateTimeProperty.class, "DATETIME", VCardDataType.DATE_TIME));
    vcw.registerScribe(new DateTestScribe<TimeProperty>(TimeProperty.class, "TIME", VCardDataType.TIME));
    vcw.registerScribe(new DateTestScribe<DateAndOrTimeProperty>(DateAndOrTimeProperty.class, "DATEANDORTIME", VCardDataType.DATE_AND_OR_TIME));
    vcw.setAddProdId(false);
    vcw.write(vcard);
    String actual = sw.toString();
    // @formatter:off
    String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "DATE:value\r\n" + "DATETIME:value\r\n" + "TIME:value\r\n" + "DATEANDORTIME:value\r\n" + "END:VCARD\r\n";
    // @formatter:on
    assertEquals(actual, expected);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardVersion(ezvcard.VCardVersion) VCardParameters(ezvcard.parameter.VCardParameters) StringWriter(java.io.StringWriter) ParseContext(ezvcard.io.ParseContext) VCardProperty(ezvcard.property.VCardProperty) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 7 with VCardProperty

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

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

the class VCard method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("version=").append(version);
    for (VCardProperty property : properties.values()) {
        sb.append(StringUtils.NEWLINE).append(property);
    }
    return sb.toString();
}
Also used : VCardProperty(ezvcard.property.VCardProperty)

Example 9 with VCardProperty

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

the class VCard method hashCode.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((version == null) ? 0 : version.hashCode());
    int propertiesHash = 1;
    for (VCardProperty property : properties.values()) {
        propertiesHash += property.hashCode();
    }
    result = prime * result + propertiesHash;
    return result;
}
Also used : VCardProperty(ezvcard.property.VCardProperty)

Example 10 with VCardProperty

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

VCardProperty (ezvcard.property.VCardProperty)13 VCardVersion (ezvcard.VCardVersion)4 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 VCard (ezvcard.VCard)3 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)3 SkipMeException (ezvcard.io.SkipMeException)3 VCardParameters (ezvcard.parameter.VCardParameters)3 RawProperty (ezvcard.property.RawProperty)3 VCardDataType (ezvcard.VCardDataType)2 Label (ezvcard.property.Label)2 ProductId (ezvcard.property.ProductId)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 CannotParseException (ezvcard.io.CannotParseException)1 ParseContext (ezvcard.io.ParseContext)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1