1
package org.waxworlds.importcontacts;
4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/import-contacts
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package org.waxworlds.edam.importcontacts;
3
26
import java.io.BufferedReader;
4
27
import java.io.File;
47
String location = prefs.getString( "location", "" );
48
File dir = new File( location );
49
if( !dir.exists() || !dir.isDirectory() )
68
String path = "/sdcard" + prefs.getString( "location", "/" );
69
File file = new File( path );
50
71
showError( R.string.error_locationnotfound );
53
class VCardFilter implements FilenameFilter {
54
public boolean accept( File dir, String name ) {
55
return name.toLowerCase().endsWith( ".vcf" );
58
files = dir.listFiles( new VCardFilter() );
73
// directory, or file?
74
if( file.isDirectory() )
77
class VCardFilter implements FilenameFilter {
78
public boolean accept( File dir, String name ) {
79
return name.toLowerCase().endsWith( ".vcf" );
82
files = file.listFiles( new VCardFilter() );
87
files = new File[ 1 ];
60
91
catch( SecurityException e ) {
61
92
showError( R.string.error_locationpermissions );
141
174
private void importVCardFileContent( String content, String fileName )
142
175
throws AbortImportException
144
// unfold RFC2425 section 5.8.1 folded lines, except that we must also
145
// handle embedded Quoted-Printable encodings that have a trailing '='.
146
// So we remove these first before doing RFC2425 unfolding.
147
content = content.replaceAll( "=\n[ \\t]", "" )
148
.replaceAll( "\n[ \\t]", "" );
150
177
// get lines and parse them
151
178
String[] lines = content.split( "\n" );
152
179
VCard vCard = null;
222
249
private String _version = null;
223
250
private Vector< String > _lines = null;
224
private int _nameLevel = NAMELEVEL_NONE;
251
private int _name_level = NAMELEVEL_NONE;
252
private boolean _parser_in_multiline = false;
253
private String _parser_current_name_and_params = null;
254
private String _parser_buffered_value_so_far = "";
256
protected class UnencodeResult
258
private boolean _another_line_required;
259
private byte[] _bytes;
260
private int _num_bytes;
262
public UnencodeResult( boolean another_line_required, byte[] bytes,
265
_another_line_required = another_line_required;
267
_num_bytes = num_bytes;
270
public boolean isAnotherLineRequired()
272
return _another_line_required;
275
public byte[] getBytes()
280
public int getNumBytes()
286
@SuppressWarnings("serial")
226
287
protected class ParseException extends Exception
289
@SuppressWarnings("unused")
228
290
public ParseException( String error )
301
@SuppressWarnings("serial")
239
302
protected class SkipContactException extends Exception { }
241
304
public void parseLine( String line )
242
305
throws ParseException, SkipContactException,
243
306
AbortImportException
245
// get property halves
246
String[] props = line.split( ":" );
308
// ignore empty lines
309
if( line.trim() == "" ) return;
311
// split line into name and value parts (this may turn out to be
312
// unwanted if the line is a subsequent line in a multi-line
313
// value, but we have to do this now to check for and handle VCF
315
String[] props = line.split( ":", 2 );
247
316
for( int i = 0; i < props.length; i++ )
248
317
props[ i ] = props[ i ].trim();
249
if( props.length < 2 ||
250
props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
251
throw new ParseException( R.string.error_vcf_malformed );
319
// if we haven't yet got a version, we won't be paring anything!
253
320
if( _version == null )
255
if( props[ 0 ].equals( "VERSION" ) )
322
// is this a version?
323
if( props.length == 2 && props[ 0 ].equals( "VERSION" ) )
325
// yes, check/store it
258
326
if( !props[ 1 ].equals( "2.1" ) &&
259
327
!props[ 1 ].equals( "3.0" ) )
260
328
throw new ParseException( R.string.error_vcf_version );
347
if( _parser_in_multiline )
349
// if we're currently in a multi-line value, use the stored
350
// property name and parameters
351
props = new String[ 2 ];
352
props[ 0 ] = _parser_current_name_and_params;
353
props[ 1 ] = line.trim();
357
// for normal lines, check the property name/value bits
358
if( props.length < 2 || props[ 0 ].length() == 0 )
359
throw new ParseException(
360
R.string.error_vcf_malformed );
362
// ignore empty properties
363
if( props[ 1 ].length() < 1 )
366
// reset the saved multi-line state
367
_parser_current_name_and_params = props[ 0 ];
368
_parser_buffered_value_so_far = "";
279
371
// get parameter parts
280
372
String[] params = props[ 0 ].split( ";" );
281
373
for( int i = 0; i < params.length; i++ )
282
374
params[ i ] = params[ i ].trim();
376
// parse charset and encoding parameters
377
String charset, encoding;
378
if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
379
!charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
381
throw new ParseException( R.string.error_vcf_charset );
383
if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
384
!encoding.equals( "QUOTED-PRINTABLE" ) &&
385
!encoding.equals( "8BIT" ) )
386
//&& !encoding.equals( "BASE64" ) )
388
throw new ParseException( R.string.error_vcf_encoding );
391
// do unencoding (or default to a fake unencoding result with
393
UnencodeResult result;
394
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
395
result = unencodeQuotedPrintable( props[ 1 ], charset );
396
// else if( encoding != null && encoding.equals( "BASE64" ) )
397
// result = unencodeBase64( props[ 1 ], charset );
399
result = new UnencodeResult( false, props[ 1 ].getBytes(),
400
props[ 1 ].getBytes().length );
404
props[ 1 ] = new String( result.getBytes(), 0,
405
result.getNumBytes(),
406
charset == null? "UTF-8" : charset );
407
} catch( UnsupportedEncodingException e ) {
408
throw new ParseException( R.string.error_vcf_charset );
411
// handle multi-line requests
412
_parser_in_multiline = result.isAnotherLineRequired();
413
if( _parser_in_multiline ) {
414
_parser_buffered_value_so_far += props[ 1 ];
418
// add on buffered multi-line content
419
String value = _parser_buffered_value_so_far + props[ 1 ];
284
421
// parse some properties
285
422
if( params[ 0 ].equals( "N" ) )
286
parseN( params, props[ 1 ] );
423
parseN( params, value );
287
424
else if( params[ 0 ].equals( "FN" ) )
288
parseFN( params, props[ 1 ] );
425
parseFN( params, value );
289
426
else if( params[ 0 ].equals( "ORG" ) )
290
parseORG( params, props[ 1 ] );
427
parseORG( params, value );
291
428
else if( params[ 0 ].equals( "TEL" ) )
292
parseTEL( params, props[ 1 ] );
429
parseTEL( params, value );
293
430
else if( params[ 0 ].equals( "EMAIL" ) )
294
parseEMAIL( params, props[ 1 ] );
431
parseEMAIL( params, value );
328
465
throws ParseException, SkipContactException
330
467
// already got a better name?
331
if( _nameLevel >= NAMELEVEL_FN ) return;
468
if( _name_level >= NAMELEVEL_FN ) return;
334
setName( undoCharsetAndEncoding( params, value ) );
335
_nameLevel = NAMELEVEL_FN;
472
_name_level = NAMELEVEL_FN;
338
475
private void parseORG( String[] params, String value )
339
476
throws ParseException, SkipContactException
341
478
// already got a better name?
342
if( _nameLevel >= NAMELEVEL_ORG ) return;
479
if( _name_level >= NAMELEVEL_ORG ) return;
345
482
String[] orgparts = value.split( ";" );
408
546
throw new ParseException( R.string.error_vcf_malformed );
410
548
// missing name properties?
411
if( _nameLevel == NAMELEVEL_NONE )
549
if( _name_level == NAMELEVEL_NONE )
412
550
throw new ParseException( R.string.error_vcf_noname );
414
552
// check if we should import this one? If we've already got an 'N'-
415
553
// type name, this will already have been done by parseN() so we
416
554
// mustn't do this here (or it could prompt twice!)
417
if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
555
if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
418
556
throw new SkipContactException();
421
private String undoCharsetAndEncoding( String[] params, String value )
422
throws ParseException
424
// check encoding/charset
425
String charset, encoding;
426
if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
427
!charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
428
throw new ParseException( R.string.error_vcf_charset );
429
if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
430
!encoding.equals( "QUOTED-PRINTABLE" ) )
431
throw new ParseException( R.string.error_vcf_encoding );
434
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
435
return unencodeQuotedPrintable( value, charset );
441
559
private String checkParam( String[] params, String name )
443
561
Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
476
private String unencodeQuotedPrintable( String str, String charset )
594
private UnencodeResult unencodeQuotedPrintable( String str, String charset )
596
boolean another = false;
478
598
// default encoding scheme
479
599
if( charset == null ) charset = "UTF-8";
481
601
// unencode quoted-pritable encoding, as per RFC1521 section 5.1
482
602
byte[] bytes = new byte[ str.length() ];
484
for( int i = 0; i < str.length(); i++, j++ ) {
604
for( int i = 0; i < str.length(); i++ )
606
// get next char and process...
485
607
char ch = str.charAt( i );
486
if( ch == '=' && i < str.length() - 2 ) {
608
if( ch == '=' && i < str.length() - 2 )
610
// we found a =XX format byte, add it
487
611
bytes[ j ] = (byte)(
488
612
Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
489
613
Character.digit( str.charAt( i + 2 ), 16 ) );
616
else if( ch == '=' && i == str.length() - 1 )
618
// we found a '=' at the end of a line signifying a multi-
619
// line string, so we don't add it.
624
// just a normal char...
493
625
bytes[ j ] = (byte)ch;
496
return new String( bytes, 0, j, charset );
497
} catch( UnsupportedEncodingException e ) { }
629
return new UnencodeResult( another, bytes, j );