Search in sources :

Example 1 with Xml

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

the class XCardDocumentTest method add_xml_property_with_null_value.

@Test
public void add_xml_property_with_null_value() throws Throwable {
    VCard vcard = new VCard();
    Xml xmlProperty = new Xml((Document) null);
    vcard.addXml(xmlProperty);
    XCardDocument xcard = new XCardDocument();
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.write(vcard);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard />" + "</vcards>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    assertXMLEqual(expected, actual);
}
Also used : Xml(ezvcard.property.Xml) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 2 with Xml

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

the class XCardDocumentTest method add_xml_property.

@Test
public void add_xml_property() throws Throwable {
    VCard vcard = new VCard();
    Xml xmlProperty = new Xml("<foo xmlns=\"http://example.com\" a=\"b\">bar<car/></foo>");
    xmlProperty.addParameter("name", "value");
    vcard.addXml(xmlProperty);
    XCardDocument xcard = new XCardDocument();
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.write(vcard);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<foo xmlns=\"http://example.com\" a=\"b\">" + "<parameters xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<name><unknown>value</unknown></name>" + "</parameters>" + "bar<car/>" + "</foo>" + "</vcard>" + "</vcards>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    /*
		 * When using xalan as the JAXP parser, XMLUnit thinks the <name>
		 * element in the "actual" DOM has the wrong namespace. But when you
		 * inspect the DOM yourself, the <name> element *does* have the correct
		 * namespace!
		 * 
		 * As a workaround, let's compare the string versions of the two DOMs.
		 */
    assertEquals(XmlUtils.toString(expected), XmlUtils.toString(actual));
// assertXMLEqual(expected, actual);
}
Also used : Xml(ezvcard.property.Xml) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 3 with Xml

use of ezvcard.property.Xml 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 4 with Xml

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

the class XCardWriterTest method write_xml_property.

@Test
public void write_xml_property() throws Exception {
    VCard vcard = new VCard();
    Xml xml = new Xml("<foo xmlns=\"http://example.com\" a=\"b\">bar<car/></foo>");
    xml.setParameter("x-foo", "bar");
    vcard.addXml(xml);
    writer.write(vcard);
    writer.close();
    // @formatter:off
    String expected = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<foo xmlns=\"http://example.com\" a=\"b\">" + "<parameters xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<x-foo><unknown>bar</unknown></x-foo>" + "</parameters>" + "bar<car/>" + "</foo>" + "</vcard>" + "</vcards>";
    // @formatter:on
    assertOutput(expected);
}
Also used : Xml(ezvcard.property.Xml) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 5 with Xml

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

the class XCardWriterTest method write_xml_property_null_value.

@Test
public void write_xml_property_null_value() throws Exception {
    VCard vcard = new VCard();
    vcard.addXml(new Xml((Document) null));
    writer.write(vcard);
    writer.close();
    // @formatter:off
    String expected = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard />" + "</vcards>";
    // @formatter:on
    assertOutput(expected);
}
Also used : Xml(ezvcard.property.Xml) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Aggregations

Xml (ezvcard.property.Xml)7 VCard (ezvcard.VCard)5 Test (org.junit.Test)5 Document (org.w3c.dom.Document)4 XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)2 Element (org.w3c.dom.Element)2 AgeProperty (ezvcard.io.AgeProperty)1 AgeScribe (ezvcard.io.AgeProperty.AgeScribe)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 LuckyNumProperty (ezvcard.io.LuckyNumProperty)1 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)1 MyFormattedNameProperty (ezvcard.io.MyFormattedNameProperty)1 MyFormattedNameScribe (ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe)1 SalaryProperty (ezvcard.io.SalaryProperty)1 SalaryScribe (ezvcard.io.SalaryProperty.SalaryScribe)1 SkipMeException (ezvcard.io.SkipMeException)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 XCardElement (ezvcard.io.xml.XCardElement)1 VCardParameters (ezvcard.parameter.VCardParameters)1 RawProperty (ezvcard.property.RawProperty)1