Search in sources :

Example 1 with LuckyNumScribe

use of ezvcard.io.LuckyNumProperty.LuckyNumScribe in project ez-vcard by mangstadt.

the class XCardDocumentTest method add_extendedTypes.

@Test
public void add_extendedTypes() throws Throwable {
    VCard vcard = new VCard();
    // contains marshal methods and QName
    LuckyNumProperty num = new LuckyNumProperty(24);
    vcard.addProperty(num);
    // contains marshal methods, but does not have a QName
    SalaryProperty salary = new SalaryProperty(1000000);
    vcard.addProperty(salary);
    // does not contain marshal methods nor QName
    AgeProperty age = new AgeProperty(22);
    vcard.addProperty(age);
    XCardDocument xcard = new XCardDocument();
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.registerScribe(new LuckyNumScribe());
    writer.registerScribe(new SalaryScribe());
    writer.registerScribe(new AgeScribe());
    writer.write(vcard);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" + "<x-salary>1000000</x-salary>" + "<x-age><unknown>22</unknown></x-age>" + "</vcard>" + "</vcards>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    assertXMLEqual(XmlUtils.toString(actual), expected, actual);
}
Also used : AgeScribe(ezvcard.io.AgeProperty.AgeScribe) AgeProperty(ezvcard.io.AgeProperty) SalaryProperty(ezvcard.io.SalaryProperty) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) SalaryScribe(ezvcard.io.SalaryProperty.SalaryScribe) LuckyNumProperty(ezvcard.io.LuckyNumProperty) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 2 with LuckyNumScribe

use of ezvcard.io.LuckyNumProperty.LuckyNumScribe in project ez-vcard by mangstadt.

the class HCardParserTest method registerExtendedProperty.

@Test
public void registerExtendedProperty() throws Exception {
    // @formatter:off
    String html = "<html>" + "<body>" + "<div class=\"vcard\">" + "<span class=\"x-lucky-num\">24</span>" + "<span class=\"x-lucky-num\">22</span>" + "<span class=\"x-gender\">male</span>" + "</div>" + "</body>" + "</html>";
    // @formatter:on
    HCardParser parser = new HCardParser(html);
    parser.registerScribe(new LuckyNumScribe());
    VCard vcard = parser.readNext();
    assertVersion(V3_0, vcard);
    assertPropertyCount(3, vcard);
    // read a type that has a type class
    List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class);
    assertEquals(2, luckyNumTypes.size());
    assertEquals(24, luckyNumTypes.get(0).luckyNum);
    assertEquals(22, luckyNumTypes.get(1).luckyNum);
    // read a type without a type class
    assertEquals("male", vcard.getExtendedProperty("X-GENDER").getValue());
    assertParseWarnings(parser);
    assertNoMoreVCards(parser);
}
Also used : LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) LuckyNumProperty(ezvcard.io.LuckyNumProperty) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 3 with LuckyNumScribe

use of ezvcard.io.LuckyNumProperty.LuckyNumScribe in project ez-vcard by mangstadt.

the class VCardReaderTest method extended_properties.

@Test
public void extended_properties() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "X-LUCKY-NUM:24\r\n" + "X-GENDER:ma\\,le\r\n" + "X-LUCKY-NUM:22\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        reader.registerScribe(new LuckyNumScribe());
        VCard vcard = reader.readNext();
        assertVersion(version, vcard);
        assertPropertyCount(3, vcard);
        // read a type that has a type class
        List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class);
        assertEquals(2, luckyNumTypes.size());
        assertEquals(24, luckyNumTypes.get(0).luckyNum);
        assertEquals(22, luckyNumTypes.get(1).luckyNum);
        assertTrue(vcard.getExtendedProperties("X-LUCKY-NUM").isEmpty());
        // read a type without a type class
        List<RawProperty> genderTypes = vcard.getExtendedProperties("X-GENDER");
        assertEquals(1, genderTypes.size());
        // raw type values are not unescaped
        assertEquals("ma\\,le", genderTypes.get(0).getValue());
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) VCardVersion(ezvcard.VCardVersion) LuckyNumProperty(ezvcard.io.LuckyNumProperty) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 4 with LuckyNumScribe

use of ezvcard.io.LuckyNumProperty.LuckyNumScribe 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 5 with LuckyNumScribe

use of ezvcard.io.LuckyNumProperty.LuckyNumScribe in project ez-vcard by mangstadt.

the class XCardWriterTest method write_extended_properties.

@Test
public void write_extended_properties() throws Exception {
    writer.registerScribe(new LuckyNumScribe());
    writer.registerScribe(new SalaryScribe());
    writer.registerScribe(new AgeScribe());
    VCard vcard = new VCard();
    // contains marshal methods and QName
    LuckyNumProperty num = new LuckyNumProperty(24);
    vcard.addProperty(num);
    // contains marshal methods, but does not have a QName
    SalaryProperty salary = new SalaryProperty(1000000);
    vcard.addProperty(salary);
    // does not contain marshal methods nor QName
    AgeProperty age = new AgeProperty(22);
    vcard.addProperty(age);
    writer.write(vcard);
    writer.close();
    // @formatter:off
    String expected = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" + "<x-salary>1000000</x-salary>" + "<x-age><unknown>22</unknown></x-age>" + "</vcard>" + "</vcards>";
    // @formatter:on
    assertOutput(expected);
}
Also used : AgeScribe(ezvcard.io.AgeProperty.AgeScribe) AgeProperty(ezvcard.io.AgeProperty) SalaryProperty(ezvcard.io.SalaryProperty) LuckyNumScribe(ezvcard.io.LuckyNumProperty.LuckyNumScribe) SalaryScribe(ezvcard.io.SalaryProperty.SalaryScribe) LuckyNumProperty(ezvcard.io.LuckyNumProperty) VCard(ezvcard.VCard) Test(org.junit.Test)

Aggregations

LuckyNumProperty (ezvcard.io.LuckyNumProperty)6 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)6 Test (org.junit.Test)6 VCard (ezvcard.VCard)5 AgeProperty (ezvcard.io.AgeProperty)3 AgeScribe (ezvcard.io.AgeProperty.AgeScribe)3 SalaryProperty (ezvcard.io.SalaryProperty)3 SalaryScribe (ezvcard.io.SalaryProperty.SalaryScribe)3 RawProperty (ezvcard.property.RawProperty)2 VCardVersion (ezvcard.VCardVersion)1 MyFormattedNameProperty (ezvcard.io.MyFormattedNameProperty)1 MyFormattedNameScribe (ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe)1 XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)1 Xml (ezvcard.property.Xml)1 XCardBuilder (ezvcard.util.XCardBuilder)1 Document (org.w3c.dom.Document)1