VCardParser.as 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.fileformats.vcard
  28. {
  29. import mx.utils.Base64Decoder;
  30. import mx.utils.StringUtil;
  31. public class VCardParser
  32. {
  33. public static function parse(vcardStr:String):Array
  34. {
  35. var vcards:Array = new Array();
  36. var lines:Array = vcardStr.split(/\r\n/);
  37. var vcard:VCard;
  38. var type:String;
  39. var typeTmp:String;
  40. var line:String;
  41. for (var i:uint = 0; i < lines.length; ++i)
  42. {
  43. line = lines[i];
  44. if (line == "BEGIN:VCARD")
  45. {
  46. vcard = new VCard();
  47. }
  48. else if (line == "END:VCARD")
  49. {
  50. if (vcard != null)
  51. {
  52. vcards.push(vcard);
  53. }
  54. }
  55. else if(line.search(/^ORG/i) != -1)
  56. {
  57. var org:String = line.substring(4, line.length);
  58. var orgArray:Array = org.split(";");
  59. for each (var orgToken:String in orgArray)
  60. {
  61. if (StringUtil.trim(orgToken).length > 0)
  62. {
  63. vcard.orgs.push(orgToken);
  64. }
  65. }
  66. }
  67. else if(line.search(/^TITLE/i) != -1)
  68. {
  69. var title:String = line.substring(6, line.length);
  70. vcard.title = title;
  71. }
  72. else if (line.search(/^FN:/i) != -1)
  73. {
  74. var fullName:String = line.substring(3, line.length);
  75. vcard.fullName = fullName;
  76. }
  77. else if (line.search(/^TEL/i) != -1)
  78. {
  79. type = new String();
  80. typeTmp = new String();
  81. var phone:Phone = new Phone();
  82. var number:String;
  83. var phoneTokens:Array = line.split(";");
  84. for each (var phoneToken:String in phoneTokens)
  85. {
  86. if (phoneToken.search(/^TYPE=/i) != -1)
  87. {
  88. if (phoneToken.indexOf(":") != -1)
  89. {
  90. typeTmp = phoneToken.substring(5, phoneToken.indexOf(":"));
  91. number = phoneToken.substring(phoneToken.indexOf(":")+1, phoneToken.length);
  92. }
  93. else
  94. {
  95. typeTmp = phoneToken.substring(5, phoneToken.length);
  96. }
  97. typeTmp = typeTmp.toLocaleLowerCase();
  98. if (typeTmp == "pref")
  99. {
  100. continue;
  101. }
  102. if (type.length != 0)
  103. {
  104. type += (" ");
  105. }
  106. type += typeTmp;
  107. }
  108. }
  109. if (type.length > 0 && number != null)
  110. {
  111. phone.type = type;
  112. phone.number = number;
  113. }
  114. vcard.phones.push(phone);
  115. }
  116. else if (line.search(/^EMAIL/i) != -1)
  117. {
  118. type = new String();
  119. typeTmp = new String();
  120. var email:Email = new Email();
  121. var emailAddress:String;
  122. var emailTokens:Array = line.split(";");
  123. for each (var emailToken:String in emailTokens)
  124. {
  125. if (emailToken.search(/^TYPE=/i) != -1)
  126. {
  127. if (emailToken.indexOf(":") != -1)
  128. {
  129. typeTmp = emailToken.substring(5, emailToken.indexOf(":"));
  130. emailAddress = emailToken.substring(emailToken.indexOf(":")+1, emailToken.length);
  131. }
  132. else
  133. {
  134. typeTmp = emailToken.substring(5, emailToken.length);
  135. }
  136. typeTmp = typeTmp.toLocaleLowerCase();
  137. if (typeTmp == "pref" || typeTmp == "internet")
  138. {
  139. continue;
  140. }
  141. if (type.length != 0)
  142. {
  143. type += (" ");
  144. }
  145. type += typeTmp;
  146. }
  147. }
  148. if (type.length > 0 && emailAddress != null)
  149. {
  150. email.type = type;
  151. email.address = emailAddress;
  152. }
  153. vcard.emails.push(email);
  154. }
  155. else if (line.indexOf("ADR;") != -1)
  156. {
  157. var addressTokens:Array = line.split(";");
  158. var address:Address = new Address();
  159. for (var j:uint = 0; j < addressTokens.length; ++j)
  160. {
  161. var addressToken:String = addressTokens[j];
  162. if (addressToken.search(/^home:+$/i) != -1) // For Outlook, which uses non-standard vCards.
  163. {
  164. address.type = "home";
  165. }
  166. else if (addressToken.search(/^work:+$/i) != -1) // For Outlook, which uses non-standard vCards.
  167. {
  168. address.type = "work";
  169. }
  170. if (addressToken.search(/^type=/i) != -1) // The "type" parameter is the standard way (which Address Book uses)
  171. {
  172. // First, remove the optional ":" character.
  173. addressToken = addressToken.replace(/:/,"");
  174. var addressType:String = addressToken.substring(5, addressToken.length).toLowerCase();
  175. if (addressType == "pref") // Not interested in which one is preferred.
  176. {
  177. continue;
  178. }
  179. else if (addressType.indexOf("home") != -1) // home
  180. {
  181. addressType = "home";
  182. }
  183. else if (addressType.indexOf("work") != -1) // work
  184. {
  185. addressType = "work";
  186. }
  187. else if (addressType.indexOf(",") != -1) // if the comma technique is used, just use the first one
  188. {
  189. var typeTokens:Array = addressType.split(",");
  190. for each (var typeToken:String in typeTokens)
  191. {
  192. if (typeToken != "pref")
  193. {
  194. addressType = typeToken;
  195. break;
  196. }
  197. }
  198. }
  199. address.type = addressType;
  200. }
  201. else if (addressToken.search(/^\d/) != -1 && address.street == null)
  202. {
  203. address.street = addressToken.replace(/\\n/, "");
  204. address.city = addressTokens[j+1];
  205. address.state = addressTokens[j+2];
  206. address.postalCode = addressTokens[j+3];
  207. }
  208. }
  209. if (address.type != null && address.street != null)
  210. {
  211. vcard.addresses.push(address);
  212. }
  213. }
  214. else if (line.search(/^PHOTO;BASE64/i) != -1)
  215. {
  216. var imageStr:String = new String();
  217. for (var k:uint = i+1; k < lines.length; ++k)
  218. {
  219. imageStr += lines[k];
  220. //if (lines[k].search(/.+\=$/) != -1) // Very slow in Mac due to RegEx bug
  221. if (lines[k].indexOf('=') != -1)
  222. {
  223. var decoder:Base64Decoder = new Base64Decoder();
  224. decoder.decode(imageStr);
  225. vcard.image = decoder.flush();
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. return vcards;
  232. }
  233. }
  234. }