Improve UI, take "dirty" check into account, move launcher to Main.java
[jvcard.git] / src / be / nikiroo / jvcard / parsers / Vcard21Parser.java
... / ...
CommitLineData
1package be.nikiroo.jvcard.parsers;
2
3import java.util.LinkedList;
4import java.util.List;
5
6import be.nikiroo.jvcard.Card;
7import be.nikiroo.jvcard.Contact;
8import be.nikiroo.jvcard.Data;
9import be.nikiroo.jvcard.TypeInfo;
10
11public class Vcard21Parser {
12 public static List<Contact> parse(List<String> lines) {
13 List<Contact> contacts = new LinkedList<Contact>();
14 List<Data> datas = null;
15
16 for (String l : lines) {
17 String line = l.trim();
18 if (line.equals("BEGIN:VCARD")) {
19 datas = new LinkedList<Data>();
20 } else if (line.equals("END:VCARD")) {
21 if (datas == null) {
22 // BAD INPUT FILE. IGNORE.
23 System.err
24 .println("VCARD Parser warning: END:VCARD seen before any VCARD:BEGIN");
25 } else {
26 contacts.add(new Contact(datas));
27 }
28 } else {
29 if (datas == null) {
30 // BAD INPUT FILE. IGNORE.
31 System.err
32 .println("VCARD Parser warning: data seen before any VCARD:BEGIN");
33 } else {
34 List<TypeInfo> types = new LinkedList<TypeInfo>();
35 String name = "";
36 String value = "";
37 String group = "";
38
39 if (line.contains(":")) {
40 String rest = line.split(":")[0];
41 value = line.substring(rest.length() + 1);
42
43 if (rest.contains(";")) {
44 String tab[] = rest.split(";");
45 name = tab[0];
46
47 for (int i = 1; i < tab.length; i++) {
48 if (tab[i].contains("=")) {
49 String tname = tab[i].split("=")[0];
50 String tvalue = tab[i].substring(tname
51 .length() + 1);
52 types.add(new TypeInfo(tname, tvalue));
53 } else {
54 types.add(new TypeInfo(tab[i], ""));
55 }
56 }
57 } else {
58 name = rest;
59 }
60 } else {
61 name = line;
62 }
63
64 if (name.contains(".")) {
65 group = name.split("\\.")[0];
66 name = name.substring(group.length() + 1);
67 }
68
69 datas.add(new Data(types, name, value, group));
70 }
71 }
72 }
73
74 return contacts;
75 }
76
77 // -1 = no bkeys
78 public static String toString(Contact contact, int startingBKey) {
79 StringBuilder builder = new StringBuilder();
80
81 builder.append("BEGIN:VCARD");
82 builder.append("\r\n");
83 builder.append("VERSION:2.1");
84 builder.append("\r\n");
85 for (Data data : contact.getContent()) {
86 if (data.getGroup() != null && !data.getGroup().trim().equals("")) {
87 builder.append(data.getGroup().trim());
88 builder.append('.');
89 }
90 builder.append(data.getName());
91 for (TypeInfo type : data.getTypes()) {
92 builder.append(';');
93 builder.append(type.getName());
94 if (type.getValue() != null
95 && !type.getValue().trim().equals("")) {
96 builder.append('=');
97 builder.append(type.getValue());
98 }
99 }
100 builder.append(':');
101
102 //TODO: bkey!
103 builder.append(data.getValue());
104 builder.append("\r\n");
105 }
106 builder.append("END:VCARD");
107 builder.append("\r\n");
108
109 return builder.toString();
110 }
111
112 public static String toString(Card card) {
113 StringBuilder builder = new StringBuilder();
114
115 for (Contact contact : card.getContacts()) {
116 builder.append(toString(contact, -1));
117 }
118
119 return builder.toString();
120 }
121}