Skip to content

Commit

Permalink
Folded lines begin with *only one* whitespace character (issue 30).
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Oct 13, 2015
1 parent 4d28e83 commit 10c3111
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ezvcard/io/scribe/BinaryPropertyScribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ protected T cannotUnmarshalValue(String value, VCardVersion version, List<String
if (value.startsWith("http")) {
return _newInstance(value, contentType);
}

//remove the folding whitespace left over from improperly-folded lines
value = removeWhitespace(value);

return _newInstance(Base64.decodeBase64(value), contentType);
case V4_0:
return _newInstance(value, contentType);
Expand Down Expand Up @@ -294,6 +298,9 @@ private T parse(String value, VCardDataType dataType, VCardParameters parameters
//parse as binary
Encoding encodingSubType = parameters.getEncoding();
if (encodingSubType == Encoding.BASE64 || encodingSubType == Encoding.B) {
//remove the folding whitespace left over from improperly-folded lines
value = removeWhitespace(value);

return _newInstance(Base64.decodeBase64(value), contentType);
}

Expand All @@ -313,6 +320,10 @@ private T parse(String value, VCardDataType dataType, VCardParameters parameters
return cannotUnmarshalValue(value, version, warnings, contentType);
}

private String removeWhitespace(String base64) {
return base64.replaceAll("[ \\t]", "");
}

private String write(T property, VCardVersion version) {
String url = property.getUrl();
if (url != null) {
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/ezvcard/io/text/FoldedLineReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ezvcard.io.text;

import static ezvcard.util.StringUtils.ltrim;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -180,7 +178,9 @@ public String readLine() throws IOException {

if (foldedQuotedPrintableLine) {
//remove any folding whitespace
line = ltrim(line);
if (isFoldedLine(line)) {
line = line.substring(1);
}

boolean endsInEquals = line.endsWith("=");
if (endsInEquals) {
Expand All @@ -199,9 +199,8 @@ public String readLine() throws IOException {
break;
}

if (line.length() > 0 && Character.isWhitespace(line.charAt(0))) {
//the line is folded
line = ltrim(line);
if (isFoldedLine(line)) {
line = line.substring(1);
unfoldedLine.append(line);
continue;
}
Expand All @@ -213,6 +212,15 @@ public String readLine() throws IOException {
return unfoldedLine.toString();
}

private static boolean isFoldedLine(String line) {
if (line.length() == 0) {
return false;
}

char first = line.charAt(0);
return first == ' ' || first == '\t';
}

/**
* Removes the last character from a string.
* @param string the string
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ezvcard/io/text/VCardRawReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public VCardRawLine readLine() throws IOException {
this.version = version;
}

value = value.trim();
return new VCardRawLine(group, propertyName, parameters, value);
}

Expand Down
18 changes: 11 additions & 7 deletions src/test/java/ezvcard/io/text/FoldedLineReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ public void readLine() throws Exception {
//a quoted-printable line whose additional lines *are* folded where the lines end in "="
"LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0A=\n" +
" New York, New York 12345=0D=0A=\n" +
" USA\n" +
" USA\n" +

//a quoted-printable line whose additional lines *are* folded, where the lines don't end in "="
"LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0A\n" +
" New York, New York 12345=0D=0A\n" +
" USA\n" +
" USA\n" +

//this line is folded
"NOTE:folded \n line\n" +
"NOTE:folded \n" +
" line\n" +

//this line is folded with multiple whitespace characters
"NOTE:one \n two \n three \n \t four";
"NOTE:one \n" +
" two \n" +
" three \n" +
"\t \tfour";
//@formatter:on

FoldedLineReader reader = new FoldedLineReader(vcardStr);
Expand All @@ -116,11 +120,11 @@ public void readLine() throws Exception {
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0AUSA", reader.readLine());
assertEquals("LABEL;HOME:Some text QUOTED-PRINTABLE more text=", reader.readLine());
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0AUSA=0D=0A4th line", reader.readLine());
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0AUSA", reader.readLine());
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0AUSA", reader.readLine());
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0A USA", reader.readLine());
assertEquals("LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Silicon Alley 5,=0D=0ANew York, New York 12345=0D=0A USA", reader.readLine());

assertEquals("NOTE:folded line", reader.readLine());
assertEquals("NOTE:one two three four", reader.readLine());
assertEquals("NOTE:one two three \tfour", reader.readLine());
assertNull(reader.readLine());
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/ezvcard/io/text/SampleVCardsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public void evolutionVCard() throws Throwable {
.locality("Albaney")
.region("New York")
.postalCode("12345")
.country("UnitedStates of America") //the space between "United" and "States" is lost because it was included with the folding character and ignored (see .vcf file)
.country("United States of America")
.types(AddressType.HOME)
.noMore();

Expand Down Expand Up @@ -512,7 +512,7 @@ public void gmailVCard() throws Throwable {

//ADR
assertAddress(vcard)
.extendedAddress("Crescent moon drive" + NEWLINE + "555-asd" + NEWLINE + "Nice Area, Albaney, New York12345" + NEWLINE + "United States of America")
.extendedAddress("Crescent moon drive" + NEWLINE + "555-asd" + NEWLINE + "Nice Area, Albaney, New York 12345" + NEWLINE + "United States of America")
.types(AddressType.HOME)
.noMore();

Expand Down Expand Up @@ -1065,7 +1065,7 @@ public void lotusNotesVCard() throws Throwable {

//LABEL
assertSimpleProperty(vcard.getOrphanedLabels())
.value("John Doe" + NEWLINE + "New York, NewYork," + NEWLINE + "South Crecent Drive," + NEWLINE + "Building 5, floor 3," + NEWLINE + "USA")
.value("John Doe" + NEWLINE + "New York, NewYork," + NEWLINE + "South Crecent Dr ive," + NEWLINE + "Building 5, floor 3," + NEWLINE + "USA")
.param("TYPE", "HOME")
.param("TYPE", "PARCEL")
.param("TYPE", "PREF")
Expand Down Expand Up @@ -1114,7 +1114,7 @@ public void lotusNotesVCard() throws Throwable {
.noMore();

assertRawProperty("X-LONG-STRING", vcard)
.value("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")
.value("12345678901234567890123456789012345678901234567890123456789012 34567890123456789012345678901234567890")
.noMore();
//@formatter:on
}
Expand Down
12 changes: 0 additions & 12 deletions src/test/java/ezvcard/io/text/VCardRawReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,6 @@ public void empty_value() throws Throwable {
assertNull(reader.readLine());
}

@Test
public void trim_value() throws Throwable {
String vcard = "NOTE: Hello world!\t";
VCardRawReader reader = create(vcard);

VCardRawLine expected = line("NOTE").value("Hello world!").build();
VCardRawLine actual = reader.readLine();
assertEquals(expected, actual);

assertNull(reader.readLine());
}

@Test(expected = VCardParseException.class)
public void invalid_line() throws Throwable {
String vcard = "This is not a valid vCard line.";
Expand Down

0 comments on commit 10c3111

Please sign in to comment.