Search in sources :

Example 46 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle 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)

Example 47 with SyntaxStyle

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

the class VObjectWriterTest method group.

@Test
public void group() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.writeProperty("group", "PROP", new VObjectParameters(), "value");
        writer.writeProperty("", "PROP", new VObjectParameters(), "value");
        writer.writeProperty(null, "PROP", new VObjectParameters(), "value");
        String actual = sw.toString();
        // @formatter:off
        String expected = "group.PROP:value\r\n" + "PROP:value\r\n" + "PROP:value\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 48 with SyntaxStyle

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

the class VObjectReader method parse.

/**
 * <p>
 * Starts or continues to parse the data off the input stream.
 * </p>
 * <p>
 * This method blocks until one of the following events happen:
 * </p>
 * <ol>
 * <li>The end of the input stream has been reached or</li>
 * <li>One of the methods in the given {@link VObjectDataListener}
 * implementation has invoked {@link Context#stop()}.</li>
 * </ol>
 * @param listener callback interface for handling data as it is read off
 * the input stream
 * @throws IOException if there's a problem reading from the input stream
 */
public void parse(VObjectDataListener listener) throws IOException {
    context.stop = false;
    while (!eos && !context.stop) {
        context.lineNumber = lineNumber;
        buffer.clear();
        context.unfoldedLine.clear();
        VObjectProperty property = parseProperty(listener);
        if (context.unfoldedLine.size() == 0) {
            // input stream was empty
            return;
        }
        if (property == null) {
            listener.onWarning(Warning.MALFORMED_LINE, null, null, context);
            continue;
        }
        if ("BEGIN".equalsIgnoreCase(property.getName().trim())) {
            String componentName = property.getValue().trim().toUpperCase();
            if (componentName.length() == 0) {
                listener.onWarning(Warning.EMPTY_BEGIN, null, null, context);
                continue;
            }
            listener.onComponentBegin(componentName, context);
            stack.push(componentName);
            continue;
        }
        if ("END".equalsIgnoreCase(property.getName().trim())) {
            String componentName = property.getValue().trim().toUpperCase();
            if (componentName.length() == 0) {
                listener.onWarning(Warning.EMPTY_END, null, null, context);
                continue;
            }
            // find the component that this END property matches up with
            int popCount = stack.popCount(componentName);
            if (popCount == 0) {
                // END property does not match up with any BEGIN properties, so ignore
                listener.onWarning(Warning.UNMATCHED_END, null, null, context);
                continue;
            }
            while (popCount > 0) {
                String poppedName = stack.pop();
                listener.onComponentEnd(poppedName, context);
                popCount--;
            }
            continue;
        }
        if ("VERSION".equalsIgnoreCase(property.getName())) {
            String parentComponent = stack.peekName();
            if (syntaxRules.hasSyntaxRules(parentComponent)) {
                SyntaxStyle style = syntaxRules.getSyntaxStyle(parentComponent, property.getValue());
                if (style == null) {
                    listener.onWarning(Warning.UNKNOWN_VERSION, property, null, context);
                } else {
                    listener.onVersion(property.getValue(), context);
                    stack.updateSyntax(style);
                    continue;
                }
            }
        }
        listener.onProperty(property, context);
    }
}
Also used : VObjectProperty(com.github.mangstadt.vinnie.VObjectProperty) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle)

Example 49 with SyntaxStyle

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

the class VCardProperty method validate.

/**
 * Checks the property for data consistency problems or deviations from the
 * spec. These problems will not prevent the property from being written to
 * a data stream, but may prevent it from being parsed correctly by the
 * consuming application. These problems can largely be avoided by reading
 * the Javadocs of the property class, or by being familiar with the vCard
 * standard.
 * @param version the version to check the property against (use 4.0 for
 * xCard and jCard)
 * @param vcard the vCard this property belongs to
 * @see VCard#validate
 * @return a list of warnings or an empty list if no problems were found
 */
public final List<ValidationWarning> validate(VCardVersion version, VCard vcard) {
    List<ValidationWarning> warnings = new ArrayList<ValidationWarning>(0);
    // check the supported versions
    if (!isSupportedBy(version)) {
        warnings.add(new ValidationWarning(2, Arrays.toString(getSupportedVersions())));
    }
    // check parameters
    warnings.addAll(parameters.validate(version));
    // check group
    if (group != null) {
        SyntaxStyle syntax = version.getSyntaxStyle();
        AllowedCharacters allowed = VObjectValidator.allowedCharactersGroup(syntax, true);
        if (!allowed.check(group)) {
            if (syntax == SyntaxStyle.OLD) {
                AllowedCharacters notAllowed = allowed.flip();
                warnings.add(new ValidationWarning(32, group, notAllowed.toString(true)));
            } else {
                warnings.add(new ValidationWarning(23, group));
            }
        }
    }
    _validate(warnings, version, vcard);
    return warnings;
}
Also used : AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) ArrayList(java.util.ArrayList) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) ValidationWarning(ezvcard.ValidationWarning)

Example 50 with SyntaxStyle

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

the class RawProperty method _validate.

@Override
protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
    SyntaxStyle syntax = version.getSyntaxStyle();
    AllowedCharacters allowed = VObjectValidator.allowedCharactersParameterName(syntax, true);
    if (!allowed.check(propertyName)) {
        if (syntax == SyntaxStyle.OLD) {
            AllowedCharacters notAllowed = allowed.flip();
            warnings.add(new ValidationWarning(33, propertyName, notAllowed.toString(true)));
        } else {
            warnings.add(new ValidationWarning(24, propertyName));
        }
    }
}
Also used : AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) ValidationWarning(ezvcard.ValidationWarning)

Aggregations

SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)50 Test (org.junit.Test)45 InOrder (org.mockito.InOrder)24 StringWriter (java.io.StringWriter)21 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)15 VObjectProperty (com.github.mangstadt.vinnie.VObjectProperty)12 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)7 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)7 DecoderException (com.github.mangstadt.vinnie.codec.DecoderException)6 ArrayList (java.util.ArrayList)4 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)3 ValidationWarning (ezvcard.ValidationWarning)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 VCardDataType (ezvcard.VCardDataType)1 VCardVersion (ezvcard.VCardVersion)1 ClientPidMap (ezvcard.property.ClientPidMap)1 SortString (ezvcard.property.SortString)1 AbstractList (java.util.AbstractList)1 EnumSet (java.util.EnumSet)1