Skip to content

Commit

Permalink
Create parse warning if VERSION property is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed May 27, 2024
1 parent d08a011 commit 3b8138b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/ezvcard/io/text/VCardReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ public void onComponentEnd(String name, Context context) {
VCardStack.Item item = stack.pop();
assignLabels(item.vcard, item.labels);

/*
* Is the version property completely absent?
*
* If a VERSION property with an invalid value is present, the
* property is parsed as a RawProperty and a different warning is
* generated by vinnie.
*/
if (!item.validVersionPropertyFound && item.vcard.getExtendedProperty("VERSION") == null) {
//@formatter:off
warnings.add(new ParseWarning.Builder()
.message(39, item.vcard.getVersion())
.build()
);
//@formatter:on
}

if (stack.isEmpty()) {
context.stop();
}
Expand Down Expand Up @@ -423,7 +439,10 @@ private void handleLabelParameter(VCardProperty property) {
public void onVersion(String value, Context vobjectContext) {
VCardVersion version = VCardVersion.valueOfByStr(value);
context.setVersion(version);
stack.peek().vcard.setVersion(version);

VCardStack.Item item = stack.peek();
item.vcard.setVersion(version);
item.validVersionPropertyFound = true;
}

public void onWarning(Warning warning, VObjectProperty property, Exception thrown, Context vobjectContext) {
Expand Down Expand Up @@ -572,6 +591,7 @@ public boolean isEmpty() {
private static class Item {
public final VCard vcard;
public final List<Label> labels;
public boolean validVersionPropertyFound = false;

public Item(VCard vcard, List<Label> labels) {
this.vcard = vcard;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/ezvcard/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ parse.37=Line {0}: {2}
parse.22=Property has requested that it be skipped: {0}
parse.25=Property value could not be parsed: {0}
#parse.26=Problem parsing property in nested vCard: {0} #removed in 0.10.2-SNAPSHOT
parse.39=VERSION property missing. Defaulting to version {0}.

#plain-text
parse.23=The property''s character encoding ("{0}") is not supported by this system. {1} will be used instead.
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/ezvcard/io/text/VCardReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,20 @@ public void value_parameter() throws Exception {
assertNoMoreVCards(reader);
}
}

@Test
public void missing_version() throws Exception {
//@formatter:off
VCardAsserter asserter = read(
"BEGIN:VCARD\r\n" +
"END:VCARD\r\n"
);

asserter.next(V2_1); //default to 2.1
asserter.warnings(39);
asserter.done();
//@formatter:on
}

@Test
public void invalid_version() throws Exception {
Expand Down

0 comments on commit 3b8138b

Please sign in to comment.