68
70
String location = prefs.getString( "location", "" );
69
71
File dir = new File( location );
72
if( !dir.exists() || !dir.isDirectory() )
71
73
showError( R.string.error_locationnotfound );
73
// directory, or file?
74
if( dir.isDirectory() )
77
class VCardFilter implements FilenameFilter {
78
public boolean accept( File dir, String name ) {
79
return name.toLowerCase().endsWith( ".vcf" );
82
files = dir.listFiles( new VCardFilter() );
87
files = new File[ 1 ];
76
class VCardFilter implements FilenameFilter {
77
public boolean accept( File dir, String name ) {
78
return name.toLowerCase().endsWith( ".vcf" );
81
files = dir.listFiles( new VCardFilter() );
91
83
catch( SecurityException e ) {
92
84
showError( R.string.error_locationpermissions );
174
164
private void importVCardFileContent( String content, String fileName )
175
165
throws AbortImportException
167
// unfold RFC2425 section 5.8.1 folded lines, except that we must also
168
// handle embedded Quoted-Printable encodings that have a trailing '='.
169
// So we remove these first before doing RFC2425 unfolding.
170
content = content.replaceAll( "=\n[ \\t]", "" )
171
.replaceAll( "\n[ \\t]", "" );
177
173
// get lines and parse them
178
174
String[] lines = content.split( "\n" );
179
175
VCard vCard = null;
249
245
private String _version = null;
250
246
private Vector< String > _lines = null;
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")
247
private int _nameLevel = NAMELEVEL_NONE;
287
249
protected class ParseException extends Exception
289
@SuppressWarnings("unused")
290
251
public ParseException( String error )
301
@SuppressWarnings("serial")
302
262
protected class SkipContactException extends Exception { }
304
264
public void parseLine( String line )
305
265
throws ParseException, SkipContactException,
306
266
AbortImportException
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 );
268
// get property halves
269
String[] props = line.split( ":" );
316
270
for( int i = 0; i < props.length; i++ )
317
271
props[ i ] = props[ i ].trim();
272
if( props.length < 2 ||
273
props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
274
throw new ParseException( R.string.error_vcf_malformed );
319
// if we haven't yet got a version, we won't be paring anything!
320
276
if( _version == null )
322
// is this a version?
323
if( props.length == 2 && props[ 0 ].equals( "VERSION" ) )
278
if( props[ 0 ].equals( "VERSION" ) )
325
// yes, check/store it
326
281
if( !props[ 1 ].equals( "2.1" ) &&
327
282
!props[ 1 ].equals( "3.0" ) )
328
283
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 = "";
371
302
// get parameter parts
372
303
String[] params = props[ 0 ].split( ";" );
373
304
for( int i = 0; i < params.length; i++ )
374
305
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 ];
421
307
// parse some properties
422
308
if( params[ 0 ].equals( "N" ) )
423
parseN( params, value );
309
parseN( params, props[ 1 ] );
424
310
else if( params[ 0 ].equals( "FN" ) )
425
parseFN( params, value );
311
parseFN( params, props[ 1 ] );
426
312
else if( params[ 0 ].equals( "ORG" ) )
427
parseORG( params, value );
313
parseORG( params, props[ 1 ] );
428
314
else if( params[ 0 ].equals( "TEL" ) )
429
parseTEL( params, value );
315
parseTEL( params, props[ 1 ] );
430
316
else if( params[ 0 ].equals( "EMAIL" ) )
431
parseEMAIL( params, value );
317
parseEMAIL( params, props[ 1 ] );
465
351
throws ParseException, SkipContactException
467
353
// already got a better name?
468
if( _name_level >= NAMELEVEL_FN ) return;
354
if( _nameLevel >= NAMELEVEL_FN ) return;
472
_name_level = NAMELEVEL_FN;
357
setName( undoCharsetAndEncoding( params, value ) );
358
_nameLevel = NAMELEVEL_FN;
475
361
private void parseORG( String[] params, String value )
476
362
throws ParseException, SkipContactException
478
364
// already got a better name?
479
if( _name_level >= NAMELEVEL_ORG ) return;
365
if( _nameLevel >= NAMELEVEL_ORG ) return;
482
368
String[] orgparts = value.split( ";" );
546
431
throw new ParseException( R.string.error_vcf_malformed );
548
433
// missing name properties?
549
if( _name_level == NAMELEVEL_NONE )
434
if( _nameLevel == NAMELEVEL_NONE )
550
435
throw new ParseException( R.string.error_vcf_noname );
552
437
// check if we should import this one? If we've already got an 'N'-
553
438
// type name, this will already have been done by parseN() so we
554
439
// mustn't do this here (or it could prompt twice!)
555
if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
440
if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
556
441
throw new SkipContactException();
444
private String undoCharsetAndEncoding( String[] params, String value )
445
throws ParseException
447
// check encoding/charset
448
String charset, encoding;
449
if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
450
!charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
451
throw new ParseException( R.string.error_vcf_charset );
452
if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
453
!encoding.equals( "QUOTED-PRINTABLE" ) )
454
throw new ParseException( R.string.error_vcf_encoding );
457
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
458
return unencodeQuotedPrintable( value, charset );
559
464
private String checkParam( String[] params, String name )
561
466
Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
594
private UnencodeResult unencodeQuotedPrintable( String str, String charset )
499
private String unencodeQuotedPrintable( String str, String charset )
596
boolean another = false;
598
501
// default encoding scheme
599
502
if( charset == null ) charset = "UTF-8";
601
504
// unencode quoted-pritable encoding, as per RFC1521 section 5.1
602
505
byte[] bytes = new byte[ str.length() ];
604
for( int i = 0; i < str.length(); i++ )
606
// get next char and process...
507
for( int i = 0; i < str.length(); i++, j++ ) {
607
508
char ch = str.charAt( i );
608
if( ch == '=' && i < str.length() - 2 )
610
// we found a =XX format byte, add it
509
if( ch == '=' && i < str.length() - 2 ) {
611
510
bytes[ j ] = (byte)(
612
511
Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
613
512
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...
625
516
bytes[ j ] = (byte)ch;
629
return new UnencodeResult( another, bytes, j );
519
return new String( bytes, 0, j, charset );
520
} catch( UnsupportedEncodingException e ) { }