use of ezvcard.property.RawProperty 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.RawProperty in project ez-vcard by mangstadt.
the class XCardReaderTest method read_non_standard_properties.
@Test
public void read_non_standard_properties() throws Exception {
// @formatter:off
String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + // expected: XML property
"<foo xmlns=\"http://example.com\">bar</foo>" + // expected: LuckyNumProperty
"<a:lucky-num xmlns:a=\"http://luckynum.com\"><a:num>21</a:num></a:lucky-num>" + // expected: SalaryProperty
"<x-salary><integer>1000000</integer></x-salary>" + // expected: AgeProperty (should be unmarshalled using the default parseXml implementation)
"<x-age><integer>24</integer></x-age>" + // expected: RawProperty
"<x-gender><text>m</text></x-gender>" + // expected: MyFormattedNameProperty
"<fn><name>John Doe</name></fn>" + "</vcard>" + "</vcards>";
// @formatter:on
XCardReader reader = new XCardReader(xml);
reader.registerScribe(new LuckyNumScribe());
reader.registerScribe(new SalaryScribe());
reader.registerScribe(new AgeScribe());
reader.registerScribe(new MyFormattedNameScribe());
{
VCard vcard = reader.readNext();
assertVersion(V4_0, vcard);
assertPropertyCount(6, vcard);
{
Iterator<Xml> xmlIt = vcard.getXmls().iterator();
Xml xmlType = xmlIt.next();
assertXMLEqual(XmlUtils.toDocument("<foo xmlns=\"http://example.com\">bar</foo>"), xmlType.getValue());
assertFalse(xmlIt.hasNext());
}
LuckyNumProperty luckyNum = vcard.getProperty(LuckyNumProperty.class);
assertEquals(21, luckyNum.luckyNum);
SalaryProperty salary = vcard.getProperty(SalaryProperty.class);
assertEquals(1000000, salary.salary);
AgeProperty age = vcard.getProperty(AgeProperty.class);
assertEquals(24, age.age);
RawProperty gender = vcard.getExtendedProperty("X-GENDER");
assertEquals(VCardDataType.TEXT, gender.getDataType());
assertEquals("m", gender.getValue());
MyFormattedNameProperty fn = vcard.getProperty(MyFormattedNameProperty.class);
assertEquals("JOHN DOE", fn.value);
assertParseWarnings(reader);
}
assertNoMoreVCards(reader);
}
use of ezvcard.property.RawProperty in project ez-vcard by mangstadt.
the class VCard method addExtendedProperty.
/**
* Adds an extended property.
* @param name the property name (e.g. "X-ALT-DESC")
* @param value the property value
* @param dataType the property value's data type
* @return the property object that was created
*/
public RawProperty addExtendedProperty(String name, String value, VCardDataType dataType) {
RawProperty raw = new RawProperty(name, value, dataType);
addProperty(raw);
return raw;
}
use of ezvcard.property.RawProperty in project ez-vcard by mangstadt.
the class VCard method addExtendedProperty.
/**
* Adds an extended property.
* @param name the property name (e.g. "X-ALT-DESC")
* @param value the property value
* @return the property object that was created
*/
public RawProperty addExtendedProperty(String name, String value) {
RawProperty raw = new RawProperty(name, value);
addProperty(raw);
return raw;
}
use of ezvcard.property.RawProperty 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