Search in sources :

Example 16 with RawProperty

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());
    }
}
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 17 with RawProperty

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);
}
Also used : SalaryProperty(ezvcard.io.SalaryProperty) LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) LuckyNumProperty(ezvcard.io.LuckyNumProperty) AgeScribe(ezvcard.io.AgeProperty.AgeScribe) MyFormattedNameScribe(ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe) AgeProperty(ezvcard.io.AgeProperty) RawProperty(ezvcard.property.RawProperty) MyFormattedNameProperty(ezvcard.io.MyFormattedNameProperty) Xml(ezvcard.property.Xml) SalaryScribe(ezvcard.io.SalaryProperty.SalaryScribe) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 18 with RawProperty

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;
}
Also used : RawProperty(ezvcard.property.RawProperty)

Example 19 with RawProperty

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;
}
Also used : RawProperty(ezvcard.property.RawProperty)

Example 20 with RawProperty

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;
}
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

RawProperty (ezvcard.property.RawProperty)22 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 VCard (ezvcard.VCard)3 VCardVersion (ezvcard.VCardVersion)3 FormattedName (ezvcard.property.FormattedName)3 Impp (ezvcard.property.Impp)3 VCardProperty (ezvcard.property.VCardProperty)3 LuckyNumProperty (ezvcard.io.LuckyNumProperty)2 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)2 Label (ezvcard.property.Label)2 ProductId (ezvcard.property.ProductId)2 StructuredName (ezvcard.property.StructuredName)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 VCardDataType (ezvcard.VCardDataType)1 AgeProperty (ezvcard.io.AgeProperty)1 AgeScribe (ezvcard.io.AgeProperty.AgeScribe)1 CannotParseException (ezvcard.io.CannotParseException)1