Search in sources :

Example 16 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class StreamWriterTest method productId.

@Test
public void productId() throws IOException {
    assertNull(vcard.getProductId());
    // default value
    {
        vcard.setProductId((String) null);
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                assertEquals(1, writer.count(ProductId.class));
            }
        }
        // should be ignored
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                ProductId prodid = writer.first(ProductId.class);
                assertNotEquals("value", prodid.getValue());
            }
        }
    }
    writer.setAddProdId(false);
    {
        vcard.setProductId((String) null);
        {
            for (VCardVersion version : VCardVersion.values()) {
                writer.write(vcard, version);
                assertEquals(0, writer.count());
            }
        }
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(0, writer.count());
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                ProductId prodId = writer.first(ProductId.class);
                assertEquals("value", prodId.getValue());
            }
        }
    }
    writer.setAddProdId(true);
    {
        vcard.setProductId((String) null);
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                assertEquals(1, writer.count(ProductId.class));
            }
        }
        // should be ignored
        vcard.setProductId("value");
        {
            writer.write(vcard, V2_1);
            assertEquals(1, writer.count());
            assertEquals(1, writer.count(RawProperty.class));
            for (VCardVersion version : each(V3_0, V4_0)) {
                writer.write(vcard, version);
                assertEquals(1, writer.count());
                ProductId prodid = writer.first(ProductId.class);
                assertNotEquals("value", prodid.getValue());
            }
        }
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) Test(org.junit.Test)

Example 17 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardParameters method validate.

/**
 * <p>
 * Checks the parameters for data consistency problems or deviations from
 * the specification.
 * </p>
 * <p>
 * These problems will not prevent the vCard from being written to a data
 * stream*, but may prevent it from being parsed correctly by the consuming
 * application.
 * </p>
 * <p>
 * *With a few exceptions: One thing this method does is check for illegal
 * characters. There are certain characters that will break the vCard syntax
 * if written (such as a newline character in a parameter name). If one of
 * these characters is present, it WILL prevent the vCard from being
 * written.
 * </p>
 * @param version the vCard version to validate against
 * @return a list of warnings or an empty list if no problems were found
 */
public List<ValidationWarning> validate(VCardVersion version) {
    List<ValidationWarning> warnings = new ArrayList<ValidationWarning>(0);
    /*
		 * Check for invalid characters in names and values.
		 */
    SyntaxStyle syntax = version.getSyntaxStyle();
    for (Map.Entry<String, List<String>> entry : this) {
        String name = entry.getKey();
        /*
			 * Don't check LABEL parameter for 2.1 and 3.0 because this
			 * parameter is converted to a property in those versions.
			 */
        if (version != VCardVersion.V4_0 && LABEL.equalsIgnoreCase(name)) {
            continue;
        }
        // check the parameter name
        if (!VObjectValidator.validateParameterName(name, syntax, true)) {
            if (syntax == SyntaxStyle.OLD) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterName(syntax, true).flip();
                warnings.add(new ValidationWarning(30, name, notAllowed.toString(true)));
            } else {
                warnings.add(new ValidationWarning(26, name));
            }
        }
        // check the parameter value(s)
        List<String> values = entry.getValue();
        for (String value : values) {
            /*
				 * Newlines are allowed in LABEL parameters, but are not allowed
				 * by vobject, so remove them from the value before validating.
				 */
            if (LABEL.equalsIgnoreCase(name)) {
                value = value.replaceAll("\r\n|\r|\n", "");
            }
            if (!VObjectValidator.validateParameterValue(value, syntax, false, true)) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterValue(syntax, false, true).flip();
                int code = (syntax == SyntaxStyle.OLD) ? 31 : 25;
                warnings.add(new ValidationWarning(code, name, value, notAllowed.toString(true)));
            }
        }
    }
    /*
		 * Check for invalid or unsupported values (e.g. "ENCODING=foo").
		 */
    {
        final int nonStandardValueCode = 3;
        final int unsupportedValueCode = 4;
        String value = first(CALSCALE);
        if (value != null && Calscale.find(value) == null) {
            warnings.add(new ValidationWarning(nonStandardValueCode, CALSCALE, value, Calscale.all()));
        }
        value = first(ENCODING);
        if (value != null) {
            Encoding encoding = Encoding.find(value);
            if (encoding == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, ENCODING, value, Encoding.all()));
            } else if (!encoding.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, ENCODING, value));
            }
        }
        value = first(VALUE);
        if (value != null) {
            VCardDataType dataType = VCardDataType.find(value);
            if (dataType == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, VALUE, value, VCardDataType.all()));
            } else if (!dataType.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, VALUE, value));
            }
        }
    }
    /*
		 * Check for parameters with malformed values.
		 */
    {
        final int malformedValueCode = 5;
        try {
            getGeo();
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, GEO, first(GEO)));
        }
        try {
            Integer index = getIndex();
            if (index != null && index <= 0) {
                warnings.add(new ValidationWarning(28, index));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, INDEX, first(INDEX)));
        }
        List<String> pids = get(PID);
        for (String pid : pids) {
            if (!isPidValid(pid)) {
                warnings.add(new ValidationWarning(27, pid));
            }
        }
        try {
            Integer pref = getPref();
            if (pref != null && (pref < 1 || pref > 100)) {
                warnings.add(new ValidationWarning(29, pref));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, PREF, first(PREF)));
        }
    }
    /*
		 * Check that each parameter is supported by the given vCard version.
		 */
    {
        final int paramNotSupportedCode = 6;
        for (Map.Entry<String, Set<VCardVersion>> entry : supportedVersions.entrySet()) {
            String name = entry.getKey();
            String value = first(name);
            if (value == null) {
                continue;
            }
            Set<VCardVersion> versions = entry.getValue();
            if (!versions.contains(version)) {
                warnings.add(new ValidationWarning(paramNotSupportedCode, name));
            }
        }
    }
    /*
		 * Check that the CHARSET parameter has a character set that is
		 * supported by this JVM.
		 */
    {
        final int invalidCharsetCode = 22;
        String charsetStr = getCharset();
        if (charsetStr != null) {
            try {
                Charset.forName(charsetStr);
            } catch (IllegalCharsetNameException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            } catch (UnsupportedCharsetException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            }
        }
    }
    return warnings;
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) ArrayList(java.util.ArrayList) VCardVersion(ezvcard.VCardVersion) ValidationWarning(ezvcard.ValidationWarning) SortString(ezvcard.property.SortString) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) ClientPidMap(ezvcard.property.ClientPidMap) Map(java.util.Map) VCardDataType(ezvcard.VCardDataType)

Example 18 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class ChainingTextWriter method go.

private void go(VCardWriter writer) throws IOException {
    writer.setAddProdId(prodId);
    writer.setCaretEncodingEnabled(caretEncoding);
    writer.setVersionStrict(versionStrict);
    writer.setIncludeTrailingSemicolons(includeTrailingSemicolons);
    if (!foldLines) {
        writer.getVObjectWriter().getFoldedLineWriter().setLineLength(null);
    }
    writer.setTargetApplication(targetApplication);
    if (index != null) {
        writer.setScribeIndex(index);
    }
    for (VCard vcard : vcards) {
        if (version == null) {
            // use the version that's assigned to each individual vCard
            VCardVersion vcardVersion = vcard.getVersion();
            if (vcardVersion == null) {
                vcardVersion = VCardVersion.V3_0;
            }
            writer.setTargetVersion(vcardVersion);
        }
        writer.write(vcard);
        writer.flush();
    }
}
Also used : VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard)

Example 19 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardPropertyScribeTest method prepareParameters.

@Test
public void prepareParameters() {
    VCardPropertyScribeImpl m = new VCardPropertyScribeImpl() {

        @Override
        protected void _prepareParameters(TestProperty property, VCardParameters copy, VCardVersion version, VCard vcard) {
            copy.put("PARAM", "value");
        }
    };
    TestProperty property = new TestProperty("value");
    VCardParameters copy = m.prepareParameters(property, V4_0, new VCard());
    assertNotSame(property.getParameters(), copy);
    assertEquals("value", copy.first("PARAM"));
}
Also used : VCardParameters(ezvcard.parameter.VCardParameters) VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 20 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardReaderTest method quoted_printable_encoding_invalid_value.

@Test
public void quoted_printable_encoding_invalid_value() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        // @formatter:off
        VCardAsserter asserter = read("BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE:=nnnn\r\n" + "END:VCARD\r\n");
        asserter.next(version);
        asserter.rawProperty("PROP").param("ENCODING", "QUOTED-PRINTABLE").value("=nnnn").noMore();
        asserter.warnings(27);
        asserter.done();
    // @formatter:on
    }
}
Also used : VCardVersion(ezvcard.VCardVersion) VCardAsserter(ezvcard.property.asserter.VCardAsserter) Test(org.junit.Test)

Aggregations

VCardVersion (ezvcard.VCardVersion)36 Test (org.junit.Test)30 VCard (ezvcard.VCard)14 VCardAsserter (ezvcard.property.asserter.VCardAsserter)10 VCardParameters (ezvcard.parameter.VCardParameters)5 Label (ezvcard.property.Label)4 RawProperty (ezvcard.property.RawProperty)4 VCardProperty (ezvcard.property.VCardProperty)4 Address (ezvcard.property.Address)3 ProductId (ezvcard.property.ProductId)3 VCardDataType (ezvcard.VCardDataType)2 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)2 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)2 ArrayList (java.util.ArrayList)2 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)1 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)1 ValidationWarning (ezvcard.ValidationWarning)1 LuckyNumProperty (ezvcard.io.LuckyNumProperty)1 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)1