Search in sources :

Example 11 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters in project ez-vcard by mangstadt.

the class VCardWriter method writeNestedVCard.

@SuppressWarnings("rawtypes")
private void writeNestedVCard(VCard nestedVCard, VCardProperty property, VCardPropertyScribe scribe, VCardParameters parameters, String value) throws IOException {
    if (targetVersion == VCardVersion.V2_1) {
        // write a nested vCard (2.1 style)
        writer.writeProperty(property.getGroup(), scribe.getPropertyName(), new VObjectParameters(parameters.getMap()), value);
        prodIdStack.add(addProdId);
        addProdId = false;
        write(nestedVCard);
        addProdId = prodIdStack.remove(prodIdStack.size() - 1);
    } else {
        // write an embedded vCard (3.0 style)
        StringWriter sw = new StringWriter();
        VCardWriter agentWriter = new VCardWriter(sw, targetVersion);
        agentWriter.getVObjectWriter().getFoldedLineWriter().setLineLength(null);
        agentWriter.setAddProdId(false);
        agentWriter.setCaretEncodingEnabled(isCaretEncodingEnabled());
        agentWriter.setIncludeTrailingSemicolons(this.includeTrailingSemicolons);
        agentWriter.setScribeIndex(index);
        agentWriter.setTargetApplication(targetApplication);
        agentWriter.setVersionStrict(versionStrict);
        try {
            agentWriter.write(nestedVCard);
        } catch (IOException e) {
        // should never be thrown because we're writing to a string
        } finally {
            IOUtils.closeQuietly(agentWriter);
        }
        String vcardStr = sw.toString();
        vcardStr = VObjectPropertyValues.escape(vcardStr);
        writer.writeProperty(property.getGroup(), scribe.getPropertyName(), new VObjectParameters(parameters.getMap()), vcardStr);
    }
}
Also used : StringWriter(java.io.StringWriter) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) IOException(java.io.IOException)

Example 12 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters in project vinnie by mangstadt.

the class VObjectWriterTest method property_name_starts_with_whitespace.

@Test
public void property_name_starts_with_whitespace() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        for (char c : " \t".toCharArray()) {
            try {
                writer.writeProperty(null, c + "", new VObjectParameters(), "");
                fail("IllegalArgumentException expected when property name starts with character " + ch(c) + " and style is " + style.name());
            } catch (IllegalArgumentException e) {
            // expected
            }
            writer.writeProperty(null, "PROP" + c, new VObjectParameters(), "");
        }
        String actual = sw.toString();
        // @formatter:off
        String expected = "PROP :\r\n" + "PROP	:\r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 13 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters in project vinnie by mangstadt.

the class VObjectWriterTest method parameters_invalid_characters_in_name.

@Test
public void parameters_invalid_characters_in_name() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.setCaretEncodingEnabled(caretEncoding);
            for (char c : ";:=\n\r".toCharArray()) {
                VObjectParameters parameters = new VObjectParameters();
                parameters.put(c + "", "");
                try {
                    writer.writeProperty(null, "PROP", parameters, "");
                    fail("IllegalArgumentException expected when parameter name contains character " + ch(c) + " and style is " + style.name());
                } catch (IllegalArgumentException e) {
                // expected
                }
                String actual = sw.toString();
                // @formatter:off
                String expected = "";
                // @formatter:on
                assertEquals(expected, actual);
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 14 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters in project vinnie by mangstadt.

the class VObjectWriterTest method property_name_invalid_characters.

@Test
public void property_name_invalid_characters() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        for (char c : ".;:\n\r".toCharArray()) {
            try {
                writer.writeProperty(null, c + "", new VObjectParameters(), "");
                fail("IllegalArgumentException expected when property name contains character " + ch(c) + " and style is " + style.name());
            } catch (IllegalArgumentException e) {
            // expected
            }
            String actual = sw.toString();
            // @formatter:off
            String expected = "";
            // @formatter:on
            assertEquals(expected, actual);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 15 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters in project vinnie by mangstadt.

the class VObjectWriterTest method group_invalid_characters.

@Test
public void group_invalid_characters() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        for (char c : ".;:\n\r".toCharArray()) {
            try {
                writer.writeProperty(c + "", "PROP", new VObjectParameters(), "");
                fail("IllegalArgumentException expected when group name contains character " + ch(c) + " and style is " + style.name());
            } catch (IllegalArgumentException e) {
            // expected
            }
        }
        String actual = sw.toString();
        // @formatter:off
        String expected = "";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Aggregations

VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)18 StringWriter (java.io.StringWriter)16 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)2 VCard (ezvcard.VCard)1 VCardVersion (ezvcard.VCardVersion)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 SkipMeException (ezvcard.io.SkipMeException)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 VCardParameters (ezvcard.parameter.VCardParameters)1 VCardProperty (ezvcard.property.VCardProperty)1 IOException (java.io.IOException)1