Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 ->
Expand Down
28 changes: 7 additions & 21 deletions lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Contact> {
suspend fun fromReader(reader: Reader, downloader: Downloader?): List<Contact> {
// 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
Expand All @@ -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)
}


Expand Down
36 changes: 12 additions & 24 deletions lib/src/main/kotlin/at/bitfire/vcard4android/ContactWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/src/test/kotlin/at/bitfire/vcard4android/ContactTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}


Expand Down
20 changes: 2 additions & 18 deletions lib/src/test/kotlin/at/bitfire/vcard4android/ContactWriterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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"))
}

Expand Down
Loading