diff --git a/build.gradle b/build.gradle index 392e55c..e1f84aa 100644 --- a/build.gradle +++ b/build.gradle @@ -48,7 +48,8 @@ dependencies { // spock testImplementation platform("org.spockframework:spock-bom:$spockVersion"), "org.spockframework:spock-core", - "org.apache.logging.log4j:log4j-core:$log4jVersion" + "org.apache.logging.log4j:log4j-core:$log4jVersion", + "org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion" testRuntimeOnly("org.mnode.ical4j:ical4j:$ical4jVersion") { capabilities { diff --git a/gradle.properties b/gradle.properties index 25887f7..81a96f2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,10 +1,9 @@ -ical4jVersion=4.0.3 - -log4jVersion=2.22.1 -commonsIoVersion=2.15.1 -groovyVersion=3.0.20 +ical4jVersion=4.0.4 +log4jVersion=2.23.1 +commonsIoVersion=2.16.1 +groovyVersion=3.0.22 bndVersion=6.3.1 -junitVintageVersion=5.10.2 +junitVintageVersion=5.10.3 spockVersion=2.4-M1-groovy-3.0 jacoco_htmlReport=true diff --git a/src/main/java/net/fortuna/ical4j/vcard/VCardOutputter.java b/src/main/java/net/fortuna/ical4j/vcard/VCardOutputter.java index 3c6f7b1..59c4861 100644 --- a/src/main/java/net/fortuna/ical4j/vcard/VCardOutputter.java +++ b/src/main/java/net/fortuna/ical4j/vcard/VCardOutputter.java @@ -75,7 +75,7 @@ public VCardOutputter(boolean validating, int foldLength) { } /** - * Outputs an iCalender string to the specified output stream. + * Outputs a vCard string to the specified output stream. * @param card a vCard object to output as a string * @param out an output stream the output stream to write the vCard string to * @throws IOException thrown when unable to write to output stream @@ -86,7 +86,7 @@ public final void output(final VCard card, final OutputStream out) throws IOExce } /** - * Outputs an iCalender string to the specified writer. + * Outputs an vCard string to the specified writer. * @param card a vCard object to output as a string * @param out a writer to write the output string to * @throws IOException thrown when unable to write to writer @@ -102,4 +102,33 @@ public final void output(final VCard card, final Writer out) throws IOException, } } + /** + * Outputs a vCard string to the specified output stream. + * + * @param cards a vCard object to output as a string + * @param out an output stream the output stream to write the vCard string to + * @throws IOException thrown when unable to write to output stream + * @throws ValidationException where the specified vCard is not valid + */ + public final void output(final VCardList cards, final OutputStream out) throws IOException, ValidationException { + output(cards, new OutputStreamWriter(out, DEFAULT_CHARSET)); + } + + /** + * Outputs an vCard string to the specified writer. + * + * @param cards a vCard object to output as a string + * @param out a writer to write the output string to + * @throws IOException thrown when unable to write to writer + * @throws ValidationException where the specified vCard is not valid + */ + public final void output(final VCardList cards, final Writer out) throws IOException, ValidationException { + if (isValidating()) { + cards.getAll().forEach(VCard::validate); + } + + try (FoldingWriter writer = new FoldingWriter(out, foldLength)) { + writer.write(cards.toString()); + } + } } diff --git a/src/test/groovy/net/fortuna/ical4j/vcard/VCardOutputterSpec.groovy b/src/test/groovy/net/fortuna/ical4j/vcard/VCardOutputterSpec.groovy new file mode 100644 index 0000000..ace8cdd --- /dev/null +++ b/src/test/groovy/net/fortuna/ical4j/vcard/VCardOutputterSpec.groovy @@ -0,0 +1,41 @@ +package net.fortuna.ical4j.vcard + +import spock.lang.Specification + +class VCardOutputterSpec extends Specification { + + def 'test output of vcard list'() { + given: 'a list of cards' + def list = new VCardBuilder(getClass().getResourceAsStream('/samples/vcard-rfc2426.vcf')) + .buildAll() + + expect: 'string output matches expected' + StringWriter w = [] + new VCardOutputter(true).output(list, w) + w as String == '''BEGIN:VCARD\r +VERSION:3.0\r +FN:Frank Dawson\r +N:Dawson;Frank;;;\r +ORG:Lotus Development Corporation\r +ADR;TYPE=WORK,POSTAL,PARCEL:;;6544 Battleford Drive;Raleigh;NC;27613-3502\r + ;U.S.A.;\r +TEL;TYPE=VOICE,MSG,WORK:+1-919-676-9515\r +TEL;TYPE=FAX,WORK:+1-919-676-9564\r +EMAIL;TYPE=INTERNET,PREF:Frank_Dawson@Lotus.com\r +EMAIL;TYPE=INTERNET:fdawson@earthlink.net\r +URL:http://home.earthlink.net/~fdawson\r +END:VCARD\r +\r +BEGIN:VCARD\r +VERSION:3.0\r +FN:Tim Howes\r +N:Howes;Tim;;;\r +ORG:Netscape Communications Corp.\r +ADR;TYPE=WORK:;;501 E. Middlefield Rd.;Mountain View;CA; 94043;U.S.A.;\r +TEL;TYPE=VOICE,MSG,WORK:+1-415-937-3419\r +TEL;TYPE=FAX,WORK:+1-415-528-4164\r +EMAIL;TYPE=INTERNET:howes@netscape.com\r +END:VCARD\r +''' + } +} diff --git a/src/test/java/net/fortuna/ical4j/vcard/VCardOutputterTest.java b/src/test/java/net/fortuna/ical4j/vcard/VCardOutputterTest.java index 4a43f9c..53ea92d 100644 --- a/src/test/java/net/fortuna/ical4j/vcard/VCardOutputterTest.java +++ b/src/test/java/net/fortuna/ical4j/vcard/VCardOutputterTest.java @@ -42,7 +42,6 @@ import java.io.*; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import static junit.framework.Assert.assertEquals; @@ -74,24 +73,24 @@ public VCardOutputterTest(VCardOutputter outputter, VCard card, String expectedO @Test public void testOutput() throws IOException, ValidationException { - StringWriter out = new StringWriter(); + var out = new StringWriter(); outputter.output(card, out); assertEquals(expectedOutput, out.toString().replaceAll("\\r\\n ", "")); } @Parameters public static Collection parameters() throws IOException, ParserException { - VCardOutputter outputter = new VCardOutputter(false, 1000); + var outputter = new VCardOutputter(false, 1000); VCardBuilder builder = null; - List params = new ArrayList(); - File[] testFiles = new File("src/test/resources/samples/valid").listFiles( + var params = new ArrayList(); + var testFiles = new File("src/test/resources/samples/valid").listFiles( (FileFilter) VCardFileFilter.INSTANCE); // enable relaxed parsing for non-standard GEO support.. CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true); - for (int i = 0; i < testFiles.length; i++) { - builder = new VCardBuilder(new FileReader(testFiles[i])); - VCard card = builder.build(); - params.add(new Object[] {outputter, card, card.toString()}); + for (var testFile : testFiles) { + builder = new VCardBuilder(new FileReader(testFile)); + var card = builder.build(); + params.add(new Object[]{outputter, card, card.toString()}); } CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, false); return params;