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