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