* @param fileName
* the vcard file
* @return the created Contact
*/
public Contact readFromVCard(String fileName) {
Contact contact = ContactsFactory.eINSTANCE.createContact();
contact.setSourceFile(fileName);
BufferedReader bufferedReader = null;
String charSet = "Cp1252";
/*
* first try to guess the char set (currently not working under some
* JVMs
*/
/*
* try { bufferedReader = new BufferedReader(new InputStreamReader( new
* FileInputStream(fileName))); String line; while ((line =
* bufferedReader.readLine()) != null) { int index =
* line.indexOf("CHARSET="); if (index != -1) { int endIndex = index +
* 8; while (line.charAt(endIndex) != ':' && line.charAt(endIndex) !=
* ';') { endIndex += 1; } charSet = line.substring(index + 8,
* endIndex); break; } } } catch (FileNotFoundException e) { // TODO
* Auto-generated catch block e.printStackTrace();
*
* } catch (IOException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } finally { try { if (bufferedReader != null) {
* bufferedReader.close(); } } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } }
*/
// Then parse the vCard
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileName), charSet);
bufferedReader = new BufferedReader(inputStreamReader);
String line;
String value;
while ((line = bufferedReader.readLine()) != null) {
value = getVCardValue(line, "N");
if (value != null) {
String[] result = value.split(";");
if (result.length > 0) {
contact.setLastName(result[0]);
}
if (result.length > 1) {
contact.setFirstName(result[1]);
}
if (result.length > 2) {
contact.setMiddleName(result[2]);
}
if (result.length > 3) {
contact.setTitle(result[3]);
}
continue;
}
value = getVCardValue(line, "TEL;WORK");
if (value != null) {
contact.setPhone(value);
continue;
}
value = getVCardValue(line, "TEL;CELL");
if (value != null) {
contact.setMobile(value);
continue;
}
value = getVCardValue(line, "ADR;WORK");
if (value != null) {
String[] result = value.split(";");
if (result.length > 2) {
contact.setStreet(result[2]);
}
if (result.length > 3) {
contact.setCity(result[3]);
}
if (result.length > 4) {
contact.setState(result[4]);
}
if (result.length > 5) {
contact.setZip(result[5]);
}
if (result.length > 6) {
contact.setCountry(result[6]);
}
continue;
}
value = getVCardValue(line, "EMAIL;PREF;INTERNET");
if (value != null) {
contact.setEmail(value);
continue;
}
value = getVCardValue(line, "URL;WORK");
if (value != null) {
contact.setWebPage(value);
continue;
}
value = getVCardValue(line, "ORG");
if (value != null) {
contact.setCompany(value);
continue;
}
value = getVCardValue(line, "TITLE");
if (value != null) {
contact.setJobTitle(value);
continue;
}
value = getVCardValue(line, "NOTE");
if (value != null) {
contact.setNote(value);
continue;
}
value = getVCardValue(line, "PHOTO;TYPE=JPEG;ENCODING=BASE64");
if (value != null) {
line = bufferedReader.readLine();
StringBuilder builder = new StringBuilder();
while (line != null && line.length() > 0 && line.charAt(0) == ' ') {
builder.append(line.trim());
line = bufferedReader.readLine();
}
String jpegString = builder.toString();
contact.setJpegString(jpegString);
continue;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block