2   Copyright (c) 2008, Adobe Systems Incorporated
 
   5   Redistribution and use in source and binary forms, with or without 
 
   6   modification, are permitted provided that the following conditions are
 
   9   * Redistributions of source code must retain the above copyright notice, 
 
  10     this list of conditions and the following disclaimer.
 
  12   * Redistributions in binary form must reproduce the above copyright
 
  13     notice, this list of conditions and the following disclaimer in the 
 
  14     documentation and/or other materials provided with the distribution.
 
  16   * Neither the name of Adobe Systems Incorporated nor the names of its 
 
  17     contributors may be used to endorse or promote products derived from 
 
  18     this software without specific prior written permission.
 
  20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
  21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
  22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
  23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
  24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
  25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
  26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
  27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
  28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
  29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
  30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  32 package com.adobe.fileformats.vcard
 
  34         import mx.utils.Base64Decoder;
 
  35         import mx.utils.StringUtil;
 
  37         public class VCardParser
 
  39                 public static function parse(vcardStr:String):Array
 
  41                         var vcards:Array = new Array();
 
  42                         var lines:Array = vcardStr.split(/\r\n/);
 
  48                         for (var i:uint = 0; i < lines.length; ++i)
 
  51                                 if (line == "BEGIN:VCARD")
 
  55                                 else if (line == "END:VCARD")
 
  62                                 else if(line.search(/^ORG/i) != -1)
 
  64                                         var org:String = line.substring(4, line.length);
 
  65                                         var orgArray:Array = org.split(";");
 
  66                                         for each (var orgToken:String in orgArray)
 
  68                                                 if (StringUtil.trim(orgToken).length > 0)
 
  70                                                         vcard.orgs.push(orgToken);
 
  74                                 else if(line.search(/^TITLE/i) != -1)
 
  76                                         var title:String = line.substring(6, line.length);
 
  79                                 else if (line.search(/^FN:/i) != -1)
 
  81                                         var fullName:String = line.substring(3, line.length);
 
  82                                         vcard.fullName = fullName;
 
  84                                 else if (line.search(/^TEL/i) != -1)
 
  87                                         typeTmp = new String();
 
  88                                         var phone:Phone = new Phone();
 
  90                                         var phoneTokens:Array = line.split(";");
 
  91                                         for each (var phoneToken:String in phoneTokens)
 
  93                                                 if (phoneToken.search(/^TYPE=/i) != -1)
 
  95                                                         if (phoneToken.indexOf(":") != -1)
 
  97                                                                 typeTmp = phoneToken.substring(5, phoneToken.indexOf(":"));
 
  98                                                                 number = phoneToken.substring(phoneToken.indexOf(":")+1, phoneToken.length);
 
 102                                                                 typeTmp = phoneToken.substring(5, phoneToken.length);
 
 105                                                         typeTmp = typeTmp.toLocaleLowerCase();
 
 107                                                         if (typeTmp == "pref")
 
 111                                                         if (type.length != 0)
 
 118                                         if (type.length > 0 && number != null)
 
 121                                                 phone.number = number;
 
 123                                         vcard.phones.push(phone);
 
 125                                 else if (line.search(/^EMAIL/i) != -1)
 
 128                                         typeTmp = new String();
 
 129                                         var email:Email = new Email();
 
 130                                         var emailAddress:String;
 
 131                                         var emailTokens:Array = line.split(";");
 
 132                                         for each (var emailToken:String in emailTokens)
 
 134                                                 if (emailToken.search(/^TYPE=/i) != -1)
 
 136                                                         if (emailToken.indexOf(":") != -1)
 
 138                                                                 typeTmp = emailToken.substring(5, emailToken.indexOf(":"));
 
 139                                                                 emailAddress = emailToken.substring(emailToken.indexOf(":")+1, emailToken.length);
 
 143                                                                 typeTmp = emailToken.substring(5, emailToken.length);
 
 146                                                         typeTmp = typeTmp.toLocaleLowerCase();
 
 148                                                         if (typeTmp == "pref" || typeTmp == "internet")
 
 152                                                         if (type.length != 0)
 
 159                                         if (type.length > 0 && emailAddress != null)
 
 162                                                 email.address = emailAddress;
 
 164                                         vcard.emails.push(email);
 
 166                                 else if (line.indexOf("ADR;") != -1)
 
 168                                         var addressTokens:Array = line.split(";");
 
 169                                         var address:Address = new Address();
 
 170                                         for (var j:uint = 0; j < addressTokens.length; ++j)
 
 172                                                 var addressToken:String = addressTokens[j];
 
 173                                                 if (addressToken.search(/^home:+$/i) != -1) // For Outlook, which uses non-standard vCards.
 
 175                                                         address.type = "home";
 
 177                                                 else if (addressToken.search(/^work:+$/i) != -1) // For Outlook, which uses non-standard vCards.
 
 179                                                         address.type = "work";
 
 181                                                 if (addressToken.search(/^type=/i) != -1)  // The "type" parameter is the standard way (which Address Book uses)
 
 183                                                         // First, remove the optional ":" character.
 
 184                                                         addressToken = addressToken.replace(/:/,"");
 
 185                                                         var addressType:String = addressToken.substring(5, addressToken.length).toLowerCase();
 
 186                                                         if (addressType == "pref") // Not interested in which one is preferred.
 
 190                                                         else if (addressType.indexOf("home") != -1) // home
 
 192                                                                 addressType = "home";
 
 194                                                         else if (addressType.indexOf("work") != -1) // work
 
 196                                                                 addressType = "work";
 
 198                                                         else if (addressType.indexOf(",") != -1) // if the comma technique is used, just use the first one
 
 200                                                                 var typeTokens:Array = addressType.split(",");
 
 201                                                                 for each (var typeToken:String in typeTokens)
 
 203                                                                         if (typeToken != "pref")
 
 205                                                                                 addressType = typeToken;
 
 210                                                         address.type = addressType;
 
 212                                                 else if (addressToken.search(/^\d/) != -1 && address.street == null)
 
 214                                                         address.street = addressToken.replace(/\\n/, "");
 
 215                                                         address.city = addressTokens[j+1];
 
 216                                                         address.state = addressTokens[j+2];
 
 217                                                         address.postalCode = addressTokens[j+3];
 
 220                                         if (address.type != null && address.street != null)
 
 222                                                 vcard.addresses.push(address);
 
 226                                 else if (line.search(/^PHOTO;BASE64/i) != -1)
 
 228                                         var imageStr:String = new String();
 
 229                                         for (var k:uint = i+1; k < lines.length; ++k)
 
 231                                                 imageStr += lines[k];
 
 232                                                 //if (lines[k].search(/.+\=$/) != -1) // Very slow in Mac due to RegEx bug
 
 233                                                 if (lines[k].indexOf('=') != -1)
 
 235                                                         var decoder:Base64Decoder = new Base64Decoder();
 
 236                                                         decoder.decode(imageStr);
 
 237                                                         vcard.image = decoder.flush();