Search in sources :

Example 11 with XCardDocumentStreamWriter

use of ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter in project ez-vcard by mangstadt.

the class ChainingXmlWriter method createXCardDocument.

private XCardDocument createXCardDocument() {
    XCardDocument document = new XCardDocument();
    XCardDocumentStreamWriter writer = document.writer();
    writer.setAddProdId(prodId);
    writer.setVersionStrict(versionStrict);
    for (Map.Entry<String, VCardDataType> entry : parameterDataTypes.entrySet()) {
        String parameterName = entry.getKey();
        VCardDataType dataType = entry.getValue();
        writer.registerParameterDataType(parameterName, dataType);
    }
    if (index != null) {
        writer.setScribeIndex(index);
    }
    for (VCard vcard : vcards) {
        writer.write(vcard);
    }
    return document;
}
Also used : XCardDocument(ezvcard.io.xml.XCardDocument) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) HashMap(java.util.HashMap) Map(java.util.Map) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard)

Example 12 with XCardDocumentStreamWriter

use of ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter in project ez-vcard by mangstadt.

the class XCardDocumentTest method add_no_existing_vcards_element.

@Test
public void add_no_existing_vcards_element() throws Throwable {
    VCard vcard = new VCard();
    vcard.setFormattedName("John Doe");
    XCardDocument xcard = new XCardDocument("<root><a /></root>");
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.write(vcard);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<root>" + "<a />" + "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<fn><text>John Doe</text></fn>" + "</vcard>" + "</vcards>" + "</root>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    assertXMLEqual(expected, actual);
}
Also used : XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 13 with XCardDocumentStreamWriter

use of ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter in project ez-vcard by mangstadt.

the class XCardDocumentTest method add_parameters.

@Test
public void add_parameters() throws Throwable {
    VCard vcard = new VCard();
    Note note = new Note("This is a\nnote.");
    note.setParameter(VCardParameters.ALTID, "value");
    note.setParameter(VCardParameters.CALSCALE, "value");
    note.setParameter(VCardParameters.GEO, "geo:123.456,234.567");
    note.setParameter(VCardParameters.LABEL, "value");
    note.setParameter(VCardParameters.LANGUAGE, "en");
    note.setParameter(VCardParameters.MEDIATYPE, "text/plain");
    note.getPids().add(new Pid(1, 1));
    note.setParameter(VCardParameters.PREF, "1");
    note.setParameter(VCardParameters.SORT_AS, "value");
    note.setParameter(VCardParameters.TYPE, "home");
    note.setParameter(VCardParameters.TZ, "America/New_York");
    note.setParameter("X-CUSTOM", "xxx");
    note.setParameter("X-INT", "11");
    vcard.addNote(note);
    XCardDocument xcard = new XCardDocument();
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.registerParameterDataType("X-INT", VCardDataType.INTEGER);
    writer.registerParameterDataType("X-CUSTOM", VCardDataType.BOOLEAN);
    writer.registerParameterDataType("X-CUSTOM", null);
    writer.write(vcard);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<note>" + "<parameters>" + "<altid><text>value</text></altid>" + "<calscale><text>value</text></calscale>" + "<geo><uri>geo:123.456,234.567</uri></geo>" + "<label><text>value</text></label>" + "<language><language-tag>en</language-tag></language>" + "<mediatype><text>text/plain</text></mediatype>" + "<pid><text>1.1</text></pid>" + "<pref><integer>1</integer></pref>" + "<sort-as><text>value</text></sort-as>" + "<type><text>home</text></type>" + "<tz><uri>America/New_York</uri></tz>" + "<x-custom><unknown>xxx</unknown></x-custom>" + "<x-int><integer>11</integer></x-int>" + "</parameters>" + "<text>This is a\nnote.</text>" + "</note>" + "</vcard>" + "</vcards>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    assertXMLEqual(expected, actual);
}
Also used : Note(ezvcard.property.Note) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) Pid(ezvcard.parameter.Pid) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 14 with XCardDocumentStreamWriter

use of ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter 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 15 with XCardDocumentStreamWriter

use of ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter in project ez-vcard by mangstadt.

the class XCardDocumentTest method add_multiple.

@Test
public void add_multiple() throws Throwable {
    VCard vcard1 = new VCard();
    vcard1.setFormattedName("John Doe");
    VCard vcard2 = new VCard();
    vcard2.addNote("Hello world!");
    XCardDocument xcard = new XCardDocument();
    XCardDocumentStreamWriter writer = xcard.writer();
    writer.setAddProdId(false);
    writer.write(vcard1);
    writer.write(vcard2);
    Document actual = xcard.getDocument();
    // @formatter:off
    String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<fn><text>John Doe</text></fn>" + "</vcard>" + "<vcard>" + "<note><text>Hello world!</text></note>" + "</vcard>" + "</vcards>";
    Document expected = XmlUtils.toDocument(xml);
    // @formatter:on
    assertXMLEqual(expected, actual);
}
Also used : XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) Document(org.w3c.dom.Document) VCard(ezvcard.VCard) Test(org.junit.Test)

Aggregations

XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)15 VCard (ezvcard.VCard)14 Test (org.junit.Test)13 Document (org.w3c.dom.Document)11 Note (ezvcard.property.Note)2 Xml (ezvcard.property.Xml)2 VCardDataType (ezvcard.VCardDataType)1 AgeProperty (ezvcard.io.AgeProperty)1 AgeScribe (ezvcard.io.AgeProperty.AgeScribe)1 LuckyNumProperty (ezvcard.io.LuckyNumProperty)1 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)1 SalaryProperty (ezvcard.io.SalaryProperty)1 SalaryScribe (ezvcard.io.SalaryProperty.SalaryScribe)1 SkipMeScribe (ezvcard.io.scribe.SkipMeScribe)1 XCardDocument (ezvcard.io.xml.XCardDocument)1 Pid (ezvcard.parameter.Pid)1 Photo (ezvcard.property.Photo)1 SkipMeProperty (ezvcard.property.SkipMeProperty)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1