X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fparsers%2FVcard21Parser.java;fp=src%2Fbe%2Fnikiroo%2Fjvcard%2Fparsers%2FVcard21Parser.java;h=cc7421672d62ec3b74d3f74ad92ece2f65478a1a;hb=bcb54330afff6a443ab43ee3d38cc7f863c701b7;hp=6cb4635b27fe1aba3394618d1b493170dfd637b0;hpb=0b0b2b0ff1f5e21f7b0feb955b4b54855fb3d508;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/parsers/Vcard21Parser.java b/src/be/nikiroo/jvcard/parsers/Vcard21Parser.java index 6cb4635..cc74216 100644 --- a/src/be/nikiroo/jvcard/parsers/Vcard21Parser.java +++ b/src/be/nikiroo/jvcard/parsers/Vcard21Parser.java @@ -1,5 +1,6 @@ package be.nikiroo.jvcard.parsers; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -9,12 +10,38 @@ import be.nikiroo.jvcard.Data; import be.nikiroo.jvcard.TypeInfo; public class Vcard21Parser { - public static List parse(List lines) { + public static List parse(Iterable textData) { + Iterator lines = textData.iterator(); List contacts = new LinkedList(); List datas = null; - for (String l : lines) { - String line = l.trim(); + String nextRawLine = null; + if (lines.hasNext()) { + nextRawLine = lines.next(); + while (lines.hasNext() && isContinuation(nextRawLine)) { + // BAD INPUT FILE. IGNORE. + System.err + .println("VCARD Parser warning: CONTINUATION line seen before any data line"); + nextRawLine = lines.next(); + } + } + + while (nextRawLine != null) { + StringBuilder rawLine = new StringBuilder(nextRawLine.trim()); + if (lines.hasNext()) + nextRawLine = lines.next(); + else + nextRawLine = null; + + while (isContinuation(nextRawLine)) { + rawLine.append(nextRawLine.trim()); + if (lines.hasNext()) + nextRawLine = lines.next(); + else + nextRawLine = null; + } + + String line = rawLine.toString(); if (line.equals("BEGIN:VCARD")) { datas = new LinkedList(); } else if (line.equals("END:VCARD")) { @@ -98,8 +125,8 @@ public class Vcard21Parser { } } builder.append(':'); - - //TODO: bkey! + + // TODO: bkey! builder.append(data.getValue()); builder.append("\r\n"); } @@ -112,10 +139,26 @@ public class Vcard21Parser { public static String toString(Card card) { StringBuilder builder = new StringBuilder(); - for (Contact contact : card.getContacts()) { + for (Contact contact : card.getContactsList()) { builder.append(toString(contact, -1)); } + + builder.append("\r\n"); return builder.toString(); } + + /** + * Check if the given line is a continuation line or not. + * + * @param line + * the line to check + * + * @return TRUE if the line is a continuation line + */ + private static boolean isContinuation(String line) { + if (line != null && line.length() > 0) + return (line.charAt(0) == ' ' || line.charAt(0) == '\t'); + return false; + } }