BrightSide Workbench Full Report + Source Code
VCardParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.vcard.db;
19 
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Vector;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.vcard.VCard;
29 import org.turro.vcard.properties.Version;
30 
35 public class VCardParser {
36 
37  private BufferedReader in;
38  private boolean endReached = false;
39 
40  public VCardParser(BufferedReader in) {
41  this.in = in;
42  try {
43  in.readLine();
44  } catch (IOException ex) {
45  Logger.getLogger(VCardParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
46  }
47  }
48 
49  public List<VCard> getVCards() {
50  List<VCard> l = new ArrayList<VCard>();
51  while(!endReached) {
52  processLines(l, readNextVCard());
53  }
54  return l;
55  }
56 
57  private void processLines(List<VCard> l, List<String> lines) {
58  Version version = Version.getInstance(lines);
59  VCard vc = new VCard(version);
60  vc.readLines(lines);
61  l.add(vc);
62  }
63 
64  private List<String> readNextVCard() {
65  Vector<String> v = new Vector<String>();
66  try {
67  boolean nextAppend = false;
68  v.add("BEGIN:VCARD");
69  while(!endReached) {
70  String s = in.readLine();
71  endReached = s == null;
72  if(!endReached) {
73  if(s.equalsIgnoreCase("BEGIN:VCARD")) {
74  break;
75  } else {
76  if(!v.isEmpty() && (nextAppend || s.startsWith(" "))) {
77  String tmp = v.lastElement();
78  v.add(v.size() - 1, tmp + " " + s.trim());
79  nextAppend = false;
80  } else {
81  if(s.endsWith("\\n")) {
82  nextAppend = true;
83  s = s.substring(0, s.length() - 1);
84  }
85  v.add(s);
86  }
87  }
88  }
89  }
90  } catch (IOException ex) {
91  Logger.getLogger(VCardParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
92  }
93  return v;
94  }
95 }
void readLines(List< String > lines)
Definition: VCard.java:83
VCardParser(BufferedReader in)
static Version getInstance(List< String > lines)
Definition: Version.java:31