Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1937784
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2011-12-18 14:17:35

昨天帮忙把android手机上的联系人信息汇总一下,发现ANDROID保存格式人VCF格式.于是写程序把它解析一下.
只能解析特定文件,就是UTF-8中文编码的.
网上有开源的VCF解析包,但我下载用了一下,运行有错,又不好排错,于是就自己写了.实际文件格式不复杂,类似于键值对,以BEGING:VCARD开始,以END:VCARD结束,但值的内容还有键值,我只是按字符串查找方式解析的.
以下是代码,谁能用到我将不胜荣幸.
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.ArrayList;
  5. import java.util.List;

  6. import com.sun.org.apache.bcel.internal.generic.L2D;

  7. public class VcfFileParser
  8. {
  9.     private static String BEGIN = "BEGIN:VCARD";
  10.     private static String END = "END:VCARD";

  11.     /**
  12.      * "VERSION", "N", "FN", "TEL", "PHOTO"
  13.      */
  14.     private static String[] keys = new String[] { "VERSION", "N", "FN", "TEL", "PHOTO" };

  15.     public String fileName;
  16.     public List<String> list = new ArrayList<String>();

  17.     public List<Vcard> getVcards()
  18.     {
  19.         return vcards;
  20.     }

  21.     public List<Vcard> vcards = new ArrayList<Vcard>();

  22.     public VcfFileParser(String fileName)
  23.     {
  24.         this.fileName = fileName;
  25.     }

  26.     public void parse() throws Exception
  27.     {
  28.         File f = new File(fileName);
  29.         BufferedReader reader = new BufferedReader(new FileReader(f));
  30.         String str = null;
  31.         StringBuilder buf = new StringBuilder();
  32.         int i = 0;
  33.         while ((str = reader.readLine()) != null)
  34.         {
  35.             i++;
  36.             if ("".equals(str))
  37.             {
  38.                 continue;
  39.             }
  40.             else if (BEGIN.equals(str))
  41.             {
  42.                 buf.delete(0, buf.length());
  43.             }
  44.             else if (END.equals(str))
  45.             {
  46.                 this.list.add(buf.toString());
  47.             }
  48.             else
  49.             {
  50.                 buf.append(str + "\n");
  51.             }
  52.         }
  53.         System.out.println("parse " + i + " lines");

  54.         for (String s : list)
  55.             vcards.add(parseVcard(s));
  56.     }

  57.     public Vcard parseVcard(String s)
  58.     {
  59.         String[] ss = s.split("\n");
  60.         Vcard v = new Vcard();
  61.         for (String tmp : ss)
  62.         {
  63.             if (tmp.startsWith(keys[0]))
  64.             {
  65.                 v.version = tmp.substring(tmp.indexOf(":") + 1);
  66.             }
  67.             else if (tmp.startsWith(keys[1]))
  68.             {
  69.                 if (tmp.indexOf("CHARSET") >= 0)
  70.                     v.name = utf8_to_string(tmp.substring(tmp.lastIndexOf(":") + 2, tmp.length() - 3));
  71.                 else
  72.                 {
  73.                     v.name = tmp.substring(tmp.indexOf(";") + 1, tmp.length() - 3);
  74.                 }
  75.             }
  76.             else if (tmp.startsWith(keys[2]))
  77.             {
  78.                 if (tmp.indexOf("CHARSET") >= 0)
  79.                     v.fname = utf8_to_string(tmp.substring(tmp.lastIndexOf(":") + 1));
  80.                 else
  81.                 {
  82.                     v.fname = tmp.substring(tmp.indexOf(":") + 1);
  83.                 }
  84.             }
  85.             else if (tmp.startsWith(keys[3]))
  86.             {
  87.                 v.tels.add(tmp.substring(tmp.lastIndexOf(":") + 1));
  88.             }
  89.             else if (tmp.startsWith(keys[4]))
  90.             {
  91.                 // v.photo = tmp;

  92.             }
  93.         }
  94.         return v;
  95.     }

  96.     public void output()
  97.     {
  98.         int i = 1;
  99.         for (Vcard s : vcards)
  100.             System.out.println(i++ + " " + s.toString());
  101.     }

  102.     class Vcard implements Comparable
  103.     {
  104.         String version;
  105.         String name;
  106.         String fname;
  107.         List<String> tels = new ArrayList<String>();
  108.         String photo;

  109.         public String toString()
  110.         {
  111.             return "version=" + version + ", name=" + name + ", fname=" + fname + ", tel=" + tels();
  112.             // ", photo=" + photo;

  113.         }

  114.         private String tels()
  115.         {
  116.             if (tels.size() == 0)
  117.                 return "";
  118.             String str = "";
  119.             for (String t : tels)
  120.                 str = str + t + ",";
  121.             return str.substring(0, str.length() - 1);
  122.         }
  123.         
  124.         public boolean equals(Object o)
  125.         {
  126.             if (o instanceof Vcard)
  127.             {
  128.                 Vcard tmp = (Vcard) o;
  129.                 return this.name.equals(tmp.name) && this.fname.equals(tmp.fname) && this.tels.equals(tmp.tels);
  130.             }
  131.             return false;
  132.         }

  133.         @Override
  134.         public int compareTo(Object o)
  135.         {
  136.             return this.name.compareToIgnoreCase(((Vcard) o).name);
  137.         }
  138.     }

  139.     public static String utf8_to_string(String str)
  140.     {
  141.         str = str.replace(";", "");
  142.         if (str.startsWith("="))
  143.             str = str.substring(1);
  144.         try
  145.         {
  146.             String[] ss = str.split("=");
  147.             byte[] bs = new byte[ss.length];
  148.             // {0xE4,0xBD,0x95,0xE4,0xBC,0x9F,0xE9,0x9D,0x99};

  149.             for (int i = 0; i < bs.length; i++)
  150.                 bs[i] = (byte) Integer.parseInt(ss[i], 16);
  151.             return new String(bs, "utf-8");
  152.         }
  153.         catch (Exception e)
  154.         {
  155.             System.err.println("Error: " + str);
  156.             return str;
  157.         }
  158.     }

  159.     public static void main(String[] args)
  160.     {
  161.         List<Vcard> l = new ArrayList<Vcard>();
  162.         for (int i = 1; i <= 8; i++)
  163.         {
  164.             String filename = "c:/0000" + i + ".vcf";
  165.             System.out.println(filename);
  166.             VcfFileParser parser = new VcfFileParser(filename);
  167.             try
  168.             {
  169.                 parser.parse();
  170.                 // parser.output();

  171.             }
  172.             catch (Exception e)
  173.             {
  174.                 e.printStackTrace();
  175.             }
  176.             l.addAll(parser.getVcards());
  177.         }
  178.         List<Vcard> l2 = new ArrayList<Vcard>();
  179.         for (Vcard s : l)
  180.         {
  181.             if (l2.contains(s))
  182.             {
  183.                 System.out.println("s has equals " + s.toString());
  184.             }
  185.             else
  186.             {
  187.                 l2.add(s);
  188.             }
  189.         }
  190.         
  191.         int i = 1;

  192.         System.out.println("Both: ");
  193.         for (Vcard s : l2)
  194.         {
  195.             System.out.println(i++ + " " + s.toString());
  196.         }

  197.         System.out.println("Short Num: ");
  198.         i = 1;
  199.         for (Vcard s : l2)
  200.         {
  201.             for (String t : s.tels)
  202.             {
  203.                 if (t.length() < 4)
  204.                     System.out.println(i++ + " " + s.toString());
  205.             }
  206.             if (!s.name.equals(s.fname))
  207.                 System.out.println(i++ + " " + s.toString());
  208.         }
  209.     }
  210. }
 
 
阅读(6998) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~