1 package be
.nikiroo
.jvcard
.parsers
;
3 import java
.util
.Iterator
;
4 import java
.util
.LinkedList
;
7 import be
.nikiroo
.jvcard
.Card
;
8 import be
.nikiroo
.jvcard
.Contact
;
9 import be
.nikiroo
.jvcard
.Data
;
10 import be
.nikiroo
.jvcard
.TypeInfo
;
12 public class Vcard21Parser
{
13 public static List
<Contact
> parse(Iterable
<String
> textData
) {
14 Iterator
<String
> lines
= textData
.iterator();
15 List
<Contact
> contacts
= new LinkedList
<Contact
>();
16 List
<Data
> datas
= null;
18 String nextRawLine
= null;
19 if (lines
.hasNext()) {
20 nextRawLine
= lines
.next();
21 while (lines
.hasNext() && isContinuation(nextRawLine
)) {
22 // BAD INPUT FILE. IGNORE.
24 .println("VCARD Parser warning: CONTINUATION line seen before any data line");
25 nextRawLine
= lines
.next();
29 while (nextRawLine
!= null) {
30 StringBuilder rawLine
= new StringBuilder(nextRawLine
.trim());
32 nextRawLine
= lines
.next();
36 while (isContinuation(nextRawLine
)) {
37 rawLine
.append(nextRawLine
.trim());
39 nextRawLine
= lines
.next();
44 String line
= rawLine
.toString();
45 if (line
.equals("BEGIN:VCARD")) {
46 datas
= new LinkedList
<Data
>();
47 } else if (line
.equals("END:VCARD")) {
49 // BAD INPUT FILE. IGNORE.
51 .println("VCARD Parser warning: END:VCARD seen before any VCARD:BEGIN");
53 contacts
.add(new Contact(datas
));
57 // BAD INPUT FILE. IGNORE.
59 .println("VCARD Parser warning: data seen before any VCARD:BEGIN");
61 List
<TypeInfo
> types
= new LinkedList
<TypeInfo
>();
66 if (line
.contains(":")) {
67 String rest
= line
.split(":")[0];
68 value
= line
.substring(rest
.length() + 1);
70 if (rest
.contains(";")) {
71 String tab
[] = rest
.split(";");
74 for (int i
= 1; i
< tab
.length
; i
++) {
75 if (tab
[i
].contains("=")) {
76 String tname
= tab
[i
].split("=")[0];
77 String tvalue
= tab
[i
].substring(tname
79 types
.add(new TypeInfo(tname
, tvalue
));
81 types
.add(new TypeInfo(tab
[i
], ""));
91 if (name
.contains(".")) {
92 group
= name
.split("\\.")[0];
93 name
= name
.substring(group
.length() + 1);
96 datas
.add(new Data(types
, name
, value
, group
));
105 public static String
toString(Contact contact
, int startingBKey
) {
106 StringBuilder builder
= new StringBuilder();
108 builder
.append("BEGIN:VCARD");
109 builder
.append("\r\n");
110 builder
.append("VERSION:2.1");
111 builder
.append("\r\n");
112 for (Data data
: contact
.getContent()) {
113 if (data
.getGroup() != null && !data
.getGroup().trim().equals("")) {
114 builder
.append(data
.getGroup().trim());
117 builder
.append(data
.getName());
118 for (TypeInfo type
: data
.getTypes()) {
120 builder
.append(type
.getName());
121 if (type
.getValue() != null
122 && !type
.getValue().trim().equals("")) {
124 builder
.append(type
.getValue());
130 builder
.append(data
.getValue());
131 builder
.append("\r\n");
133 builder
.append("END:VCARD");
134 builder
.append("\r\n");
136 return builder
.toString();
139 public static String
toString(Card card
) {
140 StringBuilder builder
= new StringBuilder();
142 for (Contact contact
: card
.getContactsList()) {
143 builder
.append(toString(contact
, -1));
146 builder
.append("\r\n");
148 return builder
.toString();
152 * Check if the given line is a continuation line or not.
157 * @return TRUE if the line is a continuation line
159 private static boolean isContinuation(String line
) {
160 if (line
!= null && line
.length() > 0)
161 return (line
.charAt(0) == ' ' || line
.charAt(0) == '\t');