PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/export/VCFParser.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 235 lines | 121 code | 26 blank | 88 comment | 9 complexity | fd64cf11bbf7bcb587a3f7d103b1f3fd MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package mpv5.utils.export;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Vector;
  13. import mpv5.db.common.Context;
  14. import mpv5.db.common.DatabaseObject;
  15. import mpv5.db.common.NodataFoundException;
  16. import mpv5.db.objects.Address;
  17. import mpv5.db.objects.Contact;
  18. import mpv5.logging.Log;
  19. import mpv5.ui.dialogs.Notificator;
  20. import mpv5.utils.files.FileReaderWriter;
  21. import net.sf.vcard4j.java.AddressBook;
  22. import net.sf.vcard4j.java.VCard;
  23. import net.sf.vcard4j.java.type.*;
  24. import net.sf.vcard4j.parser.DomParser;
  25. import net.sf.vcard4j.parser.VCardParseException;
  26. import org.apache.xerces.dom.DocumentImpl;
  27. import org.w3c.dom.Document;
  28. /**
  29. *
  30. * @author anti
  31. */
  32. public class VCFParser {
  33. /**
  34. * Parse a file into {@Link VCard} objects
  35. * @param f
  36. * @return
  37. * @throws VCardParseException
  38. * @throws IOException
  39. */
  40. public static synchronized List<VCard> parse(File f) throws VCardParseException, IOException {
  41. DomParser parser = new DomParser();
  42. Document document = new DocumentImpl();
  43. parser.parse(new FileInputStream(f), document);
  44. List<VCard> cards = new Vector<VCard>();
  45. AddressBook addressBook = new AddressBook(document);
  46. for (Iterator vcards = addressBook.getVCards(); vcards.hasNext();) {
  47. VCard vcard = (VCard) vcards.next();
  48. cards.add(vcard);
  49. }
  50. return cards;
  51. }
  52. /**
  53. * Create a Contactlist from VCards
  54. * @param list
  55. * @return
  56. */
  57. public static List<Contact> toContacts(List<VCard> list) {
  58. List<Contact> contacts = new Vector<Contact>(list.size());
  59. for (VCard card : list) {
  60. Contact c = new Contact();
  61. try {
  62. // private String prename = "";
  63. // public String cname = "";
  64. c.setCname(((N) card.getTypes("N").next()).getFamily());
  65. c.setPrename(((N) card.getTypes("N").next()).getGiven());
  66. // private String cnumber = "";
  67. // private String taxnumber = "";
  68. // private String title = "";
  69. c.setTitle(((TITLE) card.getTypes("TITLE").next()).get());
  70. // private String street = "";
  71. c.setStreet(((ADR) card.getTypes("ADR").next()).getStreet());
  72. // private String zip = "";
  73. c.setZip(((ADR) card.getTypes("ADR").next()).getPcode());
  74. // private String city = "";
  75. c.setCity(((ADR) card.getTypes("ADR").next()).getLocality());
  76. // private String mainphone = "";
  77. // private String workphone = "";
  78. // private String fax = "";
  79. // private String mobilephone = "";
  80. for (Iterator tels = card.getTypes("TEL"); tels.hasNext();) {
  81. TEL tel = (TEL) tels.next();
  82. if (((TEL.Parameters) tel.getParameters()).containsTYPE(TEL.Parameters.TYPE_CELL)) {
  83. c.setMobilephone(tel.get());
  84. }
  85. if (((TEL.Parameters) tel.getParameters()).containsTYPE(TEL.Parameters.TYPE_WORK)) {
  86. c.setWorkphone(tel.get());
  87. }
  88. if (((TEL.Parameters) tel.getParameters()).containsTYPE(TEL.Parameters.TYPE_FAX)) {
  89. c.setFax(tel.get());
  90. }
  91. if (((TEL.Parameters) tel.getParameters()).containsTYPE(TEL.Parameters.TYPE_HOME)) {
  92. c.setMainphone(tel.get());
  93. }
  94. }
  95. // private String mailaddress = "";
  96. c.setMailaddress(((EMAIL) card.getTypes("EMAIL").next()).get());
  97. // private String website = "";
  98. c.setWebsite(((URL) card.getTypes("URL").next()).get());
  99. // private String notes = "";
  100. c.setNotes(((NOTE) card.getTypes("NOTE").next()).get());
  101. // private String company = "";
  102. c.setCompany(((ORG) card.getTypes("ORG").next()).getOrgname());
  103. // private String department = "";
  104. c.setDepartment(((ORG) card.getTypes("ORG").next()).getOrgunit());
  105. // private boolean ismale = true;
  106. // private boolean isenabled = true;
  107. // private boolean iscompany = false;
  108. // private boolean iscustomer = false;
  109. // private boolean ismanufacturer = false;
  110. // private boolean issupplier = false;
  111. // private String country = "";
  112. c.setCountry(((ADR) card.getTypes("ADR").next()).getCountry());
  113. } catch (Exception e) {
  114. Log.Debug(VCFParser.class, e.getLocalizedMessage());
  115. Notificator.raiseNotification(e.getLocalizedMessage());
  116. }
  117. contacts.add(c);
  118. }
  119. //BEGIN:VCARD
  120. //VERSION:3.0
  121. //N:Gump;Forrest
  122. //FN:Forrest Gump
  123. //ORG:Bubba Gump Shrimp Co.
  124. //TITLE:Shrimp Man
  125. //TEL;TYPE=WORK,VOICE:(111) 555-1212
  126. //TEL;TYPE=HOME,VOICE:(404) 555-1212
  127. //ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
  128. //LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
  129. //ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
  130. //LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
  131. //EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
  132. //REV:20080424T195243Z
  133. //END:VCARD
  134. //Name Description Semantic
  135. //N Name a structured representation of the name of the person, place or thing associated with the vCard object.
  136. //FN Formatted Name the formatted name string associated with the vCard object
  137. //PHOTO Photograph an image or photograph of the individual associated with the vCard
  138. //BDAY Birthday date of birth of the individual associated with the vCard
  139. //ADR Delivery Address a structured representation of the physical delivery address for the vCard object
  140. //LABEL Label Address addressing label for physical delivery to the person/object associated with the vCard
  141. //TEL Telephone the canonical number string for a telephone number for telephony communication with the vCard object
  142. //EMAIL Email the address for electronic mail communication with the vCard object
  143. //MAILER Email Program (Optional) Type of email program used
  144. //TZ Time Zone information related to the standard time zone of the vCard object
  145. //GEO Global Positioning The property specifies a latitude and longitude
  146. //TITLE Title specifies the job title, functional position or function of the individual associated with the vCard object within an organization (V. P. Research and Development)
  147. //ROLE Role or occupation the role, occupation, or business category of the vCard object within an organization (eg. Executive)
  148. //LOGO Logo an image or graphic of the logo of the organization that is associated with the individual to which the vCard belongs
  149. //AGENT Agent information about another person who will act on behalf of the vCard object. Typically this would be an area administrator, assistant, or secretary for the individual
  150. //ORG Organization Name or Organizational unit the name and optionally the unit(s) of the organization associated with the vCard object. This property is based on the X.520 Organization Name attribute and the X.520 Organization Unit attribute
  151. //NOTE Note specifies supplemental information or a comment that is associated with the vCard
  152. //REV Last Revision combination of the calendar date and time of day of the last update to the vCard object
  153. //SOUND Sound By default, if this property is not grouped with other properties it specifies the pronunciation of the Formatted Name property of the vCard object.
  154. //URL URL An URL is a representation of an Internet location that can be used to obtain real-time information about the vCard object
  155. //UID Unique Identifier specifies a value that represents a persistent, globally unique identifier associated with the object
  156. //VERSION Version Version of the vCard Specification
  157. //KEY Public Key the public encryption key associated with the vCard object
  158. return contacts;
  159. }
  160. /**
  161. *
  162. * @param cs
  163. * @param saveTo
  164. */
  165. public static synchronized void toVCard(ArrayList<DatabaseObject> cs, File saveTo) {
  166. FileReaderWriter rw = new FileReaderWriter(saveTo);
  167. for (int i = 0; i < cs.size(); i++) {
  168. Contact c = (Contact) cs.get(i);
  169. String text =
  170. "BEGIN:VCARD\n"
  171. + "VERSION:3.0\n"
  172. + "N:" + c.__getCname() + ";" + c.__getPrename()
  173. + "\n"
  174. + "FN:" + c.__getPrename() + " " + c.__getCname()
  175. + "\n"
  176. + "ORG:" + c.__getCompany() + ";" + c.__getDepartment()
  177. + "\n"
  178. + "TITLE:" + c.__getTitle()
  179. + "\n"
  180. + "TEL;TYPE=WORK:" + c.__getWorkphone()
  181. + "\n"
  182. + "TEL;TYPE=HOME:" + c.__getMainphone()
  183. + "\n"
  184. + "TEL;TYPE=FAX:" + c.__getFax()
  185. + "\n"
  186. + "TEL;TYPE=CELL:" + c.__getMobilephone()
  187. + "\n"
  188. + "ADR;TYPE=WORK:;;" + c.__getStreet() + ";" + c.__getCity() + ";" + ";" + c.__getZip() + ";" + c.__getCountry()
  189. + "\n";
  190. try {
  191. List data = DatabaseObject.getReferencedObjects(c, Context.getAddress());
  192. for (int ix = 0; ix < data.size(); ix++) {
  193. Address a = (Address) data.get(i);
  194. text += "ADR;TYPE=WORK:;;" + a.__getStreet() + ";" + a.__getCity() + ";" + ";" + a.__getZip() + ";" + a.__getCountry()
  195. + "\n";
  196. }
  197. } catch (NodataFoundException ex) {
  198. }
  199. text += "EMAIL;TYPE=PREF,INTERNET:" + c.__getMailaddress() +
  200. "\n"
  201. + "URL:" + c.__getWebsite() +
  202. "\n"
  203. + "NOTE:" + c.__getNotes() + " (" + c.__getTaxnumber() + ")" +
  204. "\n"
  205. + "END:VCARD";
  206. rw.write(text);
  207. }
  208. }
  209. }