diff --git a/lib/src/androidTest/kotlin/at/bitfire/vcard4android/AndroidContactTest.kt b/lib/src/androidTest/kotlin/at/bitfire/vcard4android/AndroidContactTest.kt index c90e305c..bdcd6f46 100644 --- a/lib/src/androidTest/kotlin/at/bitfire/vcard4android/AndroidContactTest.kt +++ b/lib/src/androidTest/kotlin/at/bitfire/vcard4android/AndroidContactTest.kt @@ -115,7 +115,7 @@ class AndroidContactTest { "TEL;CELL=;PREF=:+12345\r\n" + "EMAIL;PREF=invalid:test@example.com\r\n" + "END:VCARD\r\n" - val contacts = Contact.fromReader(StringReader(vCard), false, null) + val contacts = Contact.fromReader(StringReader(vCard), null) val dbContact = AndroidContact(addressBook, contacts.first(), null, null) dbContact.add() @@ -139,7 +139,7 @@ class AndroidContactTest { "FN:John Doe\n\n" + "BDAY:20010415T000000+0200\n\n" + "END:VCARD\n\n" - val contacts = Contact.fromReader(StringReader(vCard), false, null) + val contacts = Contact.fromReader(StringReader(vCard), null) assertEquals(1, contacts.size) contacts.first().birthDay.let { birthday -> diff --git a/lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt b/lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt index 35f4caab..49236dab 100644 --- a/lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt +++ b/lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt @@ -11,7 +11,6 @@ import at.bitfire.vcard4android.property.XAbDate import com.google.common.base.Ascii import com.google.common.base.MoreObjects import ezvcard.VCardVersion -import ezvcard.io.json.JCardReader import ezvcard.io.text.VCardReader import ezvcard.property.Address import ezvcard.property.Anniversary @@ -92,28 +91,21 @@ data class Contact( const val TO_STRING_MAX_VALUE_SIZE = 2000 /** - * Parses an InputStream that contains a vCard. + * Parses a Reader that contains a vCard. * * @param reader reader for the input stream containing the vCard (pay attention to the charset) * @param downloader will be used to download external resources like contact photos (may be null) - * @param jCard *true*: content is jCard; *false*: content is vCard * - * @return list of filled Event data objects (may have size 0) – doesn't return null + * @return list of filled Contact data objects (may have size 0) – doesn't return null * * @throws IOException on I/O errors when reading the stream * @throws ezvcard.io.CannotParseException when the vCard can't be parsed */ - suspend fun fromReader(reader: Reader, jCard: Boolean, downloader: Downloader?): List { + suspend fun fromReader(reader: Reader, downloader: Downloader?): List { // create new reader and add custom scribes - val vCards = - if (jCard) - JCardReader(reader) - .registerCustomScribes() - .readAll() - else - VCardReader(reader, VCardVersion.V3_0) // CardDAV requires vCard 3 or newer - .registerCustomScribes() - .readAll() + val vCards = VCardReader(reader, VCardVersion.V3_0) // CardDAV requires vCard 3 or newer + .registerCustomScribes() + .readAll() return vCards.map { vCard -> // convert every vCard to a Contact data object @@ -124,16 +116,10 @@ data class Contact( } - @Throws(IOException::class) - fun writeJCard(os: OutputStream, productId: String) { - val generator = ContactWriter(this, VCardVersion.V4_0, productId) - generator.writeCard(os, true) - } - @Throws(IOException::class) fun writeVCard(vCardVersion: VCardVersion, os: OutputStream, productId: String) { val generator = ContactWriter(this, vCardVersion, productId) - generator.writeCard(os, false) + generator.writeVCard(os) } diff --git a/lib/src/main/kotlin/at/bitfire/vcard4android/ContactWriter.kt b/lib/src/main/kotlin/at/bitfire/vcard4android/ContactWriter.kt index 826563f4..42e5c181 100644 --- a/lib/src/main/kotlin/at/bitfire/vcard4android/ContactWriter.kt +++ b/lib/src/main/kotlin/at/bitfire/vcard4android/ContactWriter.kt @@ -22,7 +22,6 @@ import at.bitfire.vcard4android.property.XPhoneticMiddleName import ezvcard.Ezvcard import ezvcard.VCard import ezvcard.VCardVersion -import ezvcard.io.json.JCardWriter import ezvcard.io.text.VCardWriter import ezvcard.parameter.ImageType import ezvcard.parameter.RelatedType @@ -349,35 +348,24 @@ class ContactWriter( * Validates and writes the vCard to an output stream. * * @param stream target output stream - * @param jCard *true*: write as jCard; *false*: write as vCard */ - fun writeCard(stream: OutputStream, jCard: Boolean) { + fun writeVCard(stream: OutputStream) { validate() - val writer = - if (jCard) - JCardWriter(stream).apply { - isAddProdId = false // we handle PRODID ourselves - registerCustomScribes() + val writer = VCardWriter(stream, version).apply { + isAddProdId = false // we handle PRODID ourselves + registerCustomScribes() - // allow properties that are not defined in this vCard version - isVersionStrict = false - } - else - VCardWriter(stream, version).apply { - isAddProdId = false // we handle PRODID ourselves - registerCustomScribes() - - /* include trailing semicolons for maximum compatibility - Don't include trailing semicolons for groups because Apple then shows "N:Group;;;;" as "Group;;;;". */ - isIncludeTrailingSemicolons = !contact.group + /* include trailing semicolons for maximum compatibility + Don't include trailing semicolons for groups because Apple then shows "N:Group;;;;" as "Group;;;;". */ + isIncludeTrailingSemicolons = !contact.group - // use caret encoding for parameter values (RFC 6868) - isCaretEncodingEnabled = true + // use caret encoding for parameter values (RFC 6868) + isCaretEncodingEnabled = true - // allow properties that are not defined in this vCard version - isVersionStrict = false - } + // allow properties that are not defined in this vCard version + isVersionStrict = false + } writer.write(vCard) writer.flush() diff --git a/lib/src/main/kotlin/at/bitfire/vcard4android/property/CustomScribes.kt b/lib/src/main/kotlin/at/bitfire/vcard4android/property/CustomScribes.kt index eb91ebb2..2fb47bd5 100644 --- a/lib/src/main/kotlin/at/bitfire/vcard4android/property/CustomScribes.kt +++ b/lib/src/main/kotlin/at/bitfire/vcard4android/property/CustomScribes.kt @@ -7,8 +7,6 @@ package at.bitfire.vcard4android.property import ezvcard.io.chain.ChainingTextWriter -import ezvcard.io.json.JCardReader -import ezvcard.io.json.JCardWriter import ezvcard.io.scribe.ScribeIndex import ezvcard.io.text.VCardReader import ezvcard.io.text.VCardWriter @@ -40,14 +38,6 @@ object CustomScribes { register(scribe) } - fun JCardReader.registerCustomScribes(): JCardReader { - scribeIndex.registerCustomScribes() - return this - } - - fun JCardWriter.registerCustomScribes() = - scribeIndex.registerCustomScribes() - fun VCardReader.registerCustomScribes(): VCardReader { scribeIndex.registerCustomScribes() return this diff --git a/lib/src/test/kotlin/at/bitfire/vcard4android/ContactTest.kt b/lib/src/test/kotlin/at/bitfire/vcard4android/ContactTest.kt index a0989df6..fcbe4a28 100644 --- a/lib/src/test/kotlin/at/bitfire/vcard4android/ContactTest.kt +++ b/lib/src/test/kotlin/at/bitfire/vcard4android/ContactTest.kt @@ -36,13 +36,13 @@ class ContactTest { private suspend fun parseContact(fname: String, charset: Charset = Charsets.UTF_8): Contact = javaClass.classLoader!!.getResourceAsStream(fname).use { stream -> - Contact.fromReader(InputStreamReader(stream, charset), false, null).first() + Contact.fromReader(InputStreamReader(stream, charset), null).first() } private suspend fun regenerate(c: Contact, vCardVersion: VCardVersion): Contact { val os = ByteArrayOutputStream() c.writeVCard(vCardVersion, os, testProductId) - return Contact.fromReader(InputStreamReader(ByteArrayInputStream(os.toByteArray()), Charsets.UTF_8), false,null).first() + return Contact.fromReader(InputStreamReader(ByteArrayInputStream(os.toByteArray()), Charsets.UTF_8), null).first() } diff --git a/lib/src/test/kotlin/at/bitfire/vcard4android/ContactWriterTest.kt b/lib/src/test/kotlin/at/bitfire/vcard4android/ContactWriterTest.kt index 301e1883..e72e47ca 100644 --- a/lib/src/test/kotlin/at/bitfire/vcard4android/ContactWriterTest.kt +++ b/lib/src/test/kotlin/at/bitfire/vcard4android/ContactWriterTest.kt @@ -576,29 +576,13 @@ class ContactWriterTest { } - @Test - fun testWriteJCard() { - val generator = ContactWriter(Contact(), VCardVersion.V4_0, testProductId) - generator.vCard.revision = Revision( - ZonedDateTime.of(2021, 7, 30, 1, 2, 3, 0, ZoneOffset.UTC) - ) - - val stream = ByteArrayOutputStream() - generator.writeCard(stream, true) - assertEquals( - "[\"vcard\",[[\"version\",{},\"text\",\"4.0\"],[\"prodid\",{},\"text\",\"$testProductId (ez-vcard/${Ezvcard.VERSION})\"],[\"fn\",{},\"text\",\"\"],[\"rev\",{},\"timestamp\",\"2021-07-30T01:02:03+00:00\"]]]", - stream.toString() - ) - } - - @Test fun testWriteVCard() { val generator = ContactWriter(Contact(), VCardVersion.V4_0, testProductId) generator.vCard.revision = Revision(ZonedDateTime.of(2021, 7, 30, 1, 2, 3, 0, ZoneOffset.UTC)) val stream = ByteArrayOutputStream() - generator.writeCard(stream, false) + generator.writeVCard(stream) assertEquals("BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "PRODID:$testProductId (ez-vcard/${Ezvcard.VERSION})\r\n" + @@ -618,7 +602,7 @@ class ContactWriterTest { }) } ContactWriter(contact, VCardVersion.V4_0, testProductId) - .writeCard(stream, false) + .writeVCard(stream) assertTrue(stream.toString().contains("ADR;LABEL=\"Li^^ne 1,1 - ^' -\":;;Line1;;;;Line2")) }