Chương trình VCF Parser phân tích cú pháp VCF | VCF Parser By EZ-VCard-0.9.6.

Hôm nay nhận được một yêu cầu lấy thông tin từ một tập tin có đuôi là VCF, thật sự lúc đầu cũng chả biết nó là gì, sau khi khi lên mạng search thì cũng hiểu nó đại khái là gì, thấy cũng hay nên viết lại chút kinh nghiệp của mình về cách phân tích cấu trúc (parser) tập tin VCF này.


VCF là một từ viết tắt cho Virtual Liên hệ với tập tin. File VCF, cũng thường được gọi là vCards, là định dạng tập tin tiêu chuẩn được sử dụng để lưu trữ thông tin liên lạc cho các cá nhân và các doanh nghiệp trong một định dạng tập tin kỹ thuật số. Một tập tin VCF thường bao gồm các tên liên lạc, địa chỉ, địa chỉ email, số điện thoại và các thông tin liên lạc của người tạo ra các tập tin.

Các định dạng tập tin VCF cũng cho phép sự hỗ trợ của hình ảnh và nội dung phương tiện truyền thông khác. file VCF thường được sử dụng để chuyển thông tin liên lạc giữa các sổ địa chỉ và cũng có thể được đính kèm vào email. File VCF có thể được sử dụng trên hệ điều hành Windows và Mac và thường được sử dụng trên iPhone để tải các liên hệ trực tiếp vào thiết bị di động của người dùng.

Để có thể sử dụng ví dụ bên dưới bạn cần download thêm thư viện EZ-VCard-0.9.6 - https://code.google.com/p/ez-vcard/wiki/Downloads add vào project.

VCFParser Example
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vcfparser;

import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.parameter.TelephoneType;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author bnson
*/
public class VCFParser extends javax.swing.JFrame {

/**
* Creates new form VCFParser
*/
public VCFParser() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(317, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(266, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);

pack();
}//


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
File file = new File("D:\\Test\\032\\ebarbara.vcf");
VCard vcard = Ezvcard.parse(file).first();

System.out.println("Full Name: " + vcard.getFormattedName().getValue());
System.out.println("First Name: " + vcard.getStructuredName().getGiven());
System.out.println("Middle Name: ");
System.out.println("Last Name: " + vcard.getStructuredName().getFamily());
System.out.println("Gender: " + vcard.getGender());
System.out.println("Title: " + vcard.getTitles().get(0).getValue());
System.out.println("PREFIX: " + vcard.getStructuredName().getPrefixes());
System.out.println("SUFFIX: " + vcard.getStructuredName().getSuffixes());
System.out.println("-----------------------------------------------");
System.out.println("Address: " + vcard.getAddresses().get(0).toString());
System.out.println("Country: " + vcard.getAddresses().get(0).getCountry());
System.out.println("ExtendedAddress: " + vcard.getAddresses().get(0).getExtendedAddress());
System.out.println("Group: " + vcard.getAddresses().get(0).getGroup());
System.out.println("Label: " + vcard.getAddresses().get(0).getLabel());
System.out.println("Language: " + vcard.getAddresses().get(0).getLanguage());
System.out.println("Locality: " + vcard.getAddresses().get(0).getLocality());
System.out.println("PoBox: " + vcard.getAddresses().get(0).getPoBox());
System.out.println("PostalCode: " + vcard.getAddresses().get(0).getPostalCode());
System.out.println("Region: " + vcard.getAddresses().get(0).getRegion());
System.out.println("Street Address: " + vcard.getAddresses().get(0).getStreetAddress());
System.out.println("Timezone: " + vcard.getAddresses().get(0).getTimezone());
System.out.println("-----------------------------------------------");
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getTypes().toString());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getType());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getMediaType());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getLabel());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getCharset());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getLanguage());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameters().getLevel());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getAltId());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getGroup());
System.out.println("TEL: " + vcard.getTelephoneNumbers().get(0).getParameter(null));


System.out.println("FAX: " + vcard.getTelephoneNumbers().get(1).getText());
System.out.println("Email: " + vcard.getEmails().get(0).getValue());
System.out.println("URL: " + vcard.getUrls().get(0).getValue());
} catch (IOException ex) {
Logger.getLogger(VCFParser.class.getName()).log(Level.SEVERE, null, ex);
}


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(VCFParser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VCFParser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VCFParser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VCFParser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//


/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new VCFParser().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}






No comments:

Post a Comment