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);
}
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());
}
}
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();
}
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;
}
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;
}
Aggregations