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);
}
}
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);
}
}
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);
}
}
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;
}
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));
}
}
}
Aggregations