/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/importcontacts/VCFImporter.java

  • Committer: edam
  • Date: 2010-10-17 18:43:13 UTC
  • Revision ID: edam@waxworlds.org-20101017184313-y2m6gsdzctjq5wtl
- changed case on charset and encoding warning strings (it looked bad)
- properly handle quoted-printable unencoding (including multi-line values)
- ignore blank lines in vCard
- ignore empty properties in vCard

Show diffs side-by-side

added added

removed removed

138
138
 
139
139
                }
140
140
                catch( FileNotFoundException e ) {
141
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
141
                        showError( getText( R.string.error_filenotfound ) +
 
142
                                file.getName() );
142
143
                }
143
144
                catch( IOException e ) {
144
145
                        showError( getText( R.string.error_ioerror ) + file.getName() );
162
163
                        importVCardFileContent( content.toString(), file.getName() );
163
164
                }
164
165
                catch( FileNotFoundException e ) {
165
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
166
                        showError( getText( R.string.error_filenotfound ) +
 
167
                                file.getName() );
166
168
                }
167
169
                catch( IOException e ) {
168
170
                        showError( getText( R.string.error_ioerror ) + file.getName() );
172
174
        private void importVCardFileContent( String content, String fileName )
173
175
                        throws AbortImportException
174
176
        {
175
 
                // unfold RFC2425 section 5.8.1 folded lines, except that we must also
176
 
                // handle embedded Quoted-Printable encodings that have a trailing '='.
177
 
                // So we remove these first before doing RFC2425 unfolding.
178
 
                content = content.replaceAll( "=\n[ \\t]", "" )
179
 
                                .replaceAll( "\n[ \\t]", "" );
180
 
 
181
177
                // get lines and parse them
182
178
                String[] lines = content.split( "\n" );
183
179
                VCard vCard = null;
252
248
 
253
249
                private String _version = null;
254
250
                private Vector< String > _lines = null;
255
 
                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 = "";
 
255
 
 
256
                protected class UnencodeResult
 
257
                {
 
258
                        private boolean _another_line_required;
 
259
                        private byte[] _bytes;
 
260
                        private int _num_bytes;
 
261
 
 
262
                        public UnencodeResult( boolean another_line_required, byte[] bytes,
 
263
                                int num_bytes )
 
264
                        {
 
265
                                _another_line_required = another_line_required;
 
266
                                _bytes = bytes;
 
267
                                _num_bytes = num_bytes;
 
268
                        }
 
269
 
 
270
                        public boolean isAnotherLineRequired()
 
271
                        {
 
272
                                return _another_line_required;
 
273
                        }
 
274
 
 
275
                        public byte[] getBytes()
 
276
                        {
 
277
                                return _bytes;
 
278
                        }
 
279
 
 
280
                        public int getNumBytes()
 
281
                        {
 
282
                                return _num_bytes;
 
283
                        }
 
284
                }
256
285
 
257
286
                @SuppressWarnings("serial")
258
287
                protected class ParseException extends Exception
276
305
                                throws ParseException, SkipContactException,
277
306
                                AbortImportException
278
307
                {
279
 
                        // get property halves
280
 
                        String[] props = line.split( ":" );
 
308
                        // ignore empty lines
 
309
                        if( line.trim() == "" ) return;
 
310
 
 
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
 
314
                        // versions first)
 
315
                        String[] props = line.split(  ":", 2 );
281
316
                        for( int i = 0; i < props.length; i++ )
282
317
                                props[ i ] = props[ i ].trim();
283
 
                        if( props.length < 2 ||
284
 
                                        props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
285
 
                                throw new ParseException( R.string.error_vcf_malformed );
286
318
 
 
319
                        // if we haven't yet got a version, we won't be paring anything!
287
320
                        if( _version == null )
288
321
                        {
289
 
                                if( props[ 0 ].equals( "VERSION" ) )
 
322
                                // is this a version?
 
323
                                if( props.length == 2 && props[ 0 ].equals( "VERSION" ) )
290
324
                                {
291
 
                                        // get version
 
325
                                        // yes, check/store it
292
326
                                        if( !props[ 1 ].equals( "2.1" ) &&
293
327
                                                        !props[ 1 ].equals( "3.0" ) )
294
328
                                                throw new ParseException( R.string.error_vcf_version );
302
336
                                }
303
337
                                else
304
338
                                {
305
 
                                        // stash this line till we have a version
 
339
                                        // no, so stash this line till we have a version
306
340
                                        if( _lines == null )
307
341
                                                _lines = new Vector< String >();
308
342
                                        _lines.add( line );
310
344
                        }
311
345
                        else
312
346
                        {
 
347
                                if( _parser_in_multiline )
 
348
                                {
 
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();
 
354
                                }
 
355
                                else
 
356
                                {
 
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 );
 
361
 
 
362
                                        // ignore empty properties
 
363
                                        if( props[ 1 ].length() < 1 )
 
364
                                                return;
 
365
 
 
366
                                        // reset the saved multi-line state
 
367
                                        _parser_current_name_and_params = props[ 0 ];
 
368
                                        _parser_buffered_value_so_far = "";
 
369
                                }
 
370
 
313
371
                                // get parameter parts
314
372
                                String[] params = props[ 0 ].split( ";" );
315
373
                                for( int i = 0; i < params.length; i++ )
316
374
                                        params[ i ] = params[ i ].trim();
317
375
 
 
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" ) )
 
380
                                {
 
381
                                        throw new ParseException( R.string.error_vcf_charset );
 
382
                                }
 
383
                                if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
 
384
                                        !encoding.equals( "QUOTED-PRINTABLE" ) &&
 
385
                                        !encoding.equals( "8BIT" ) )
 
386
                                        //&& !encoding.equals( "BASE64" ) )
 
387
                                {
 
388
                                        throw new ParseException( R.string.error_vcf_encoding );
 
389
                                }
 
390
 
 
391
                                // do unencoding (or default to a fake unencoding result with
 
392
                                // the raw string)
 
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 );
 
398
                                else
 
399
                                        result = new UnencodeResult( false, props[ 1 ].getBytes(),
 
400
                                                props[ 1 ].getBytes().length );
 
401
 
 
402
                                // process charset
 
403
                                try {
 
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 );
 
409
                                }
 
410
 
 
411
                                // handle multi-line requests
 
412
                                _parser_in_multiline = result.isAnotherLineRequired();
 
413
                                if( _parser_in_multiline ) {
 
414
                                        _parser_buffered_value_so_far += props[ 1 ];
 
415
                                        return;
 
416
                                }
 
417
 
 
418
                                // add on buffered multi-line content
 
419
                                String value = _parser_buffered_value_so_far + props[ 1 ];
 
420
 
318
421
                                // parse some properties
319
422
                                if( params[ 0 ].equals( "N" ) )
320
 
                                        parseN( params, props[ 1 ] );
 
423
                                        parseN( params, value );
321
424
                                else if( params[ 0 ].equals( "FN" ) )
322
 
                                        parseFN( params, props[ 1 ] );
 
425
                                        parseFN( params, value );
323
426
                                else if( params[ 0 ].equals( "ORG" ) )
324
 
                                        parseORG( params, props[ 1 ] );
 
427
                                        parseORG( params, value );
325
428
                                else if( params[ 0 ].equals( "TEL" ) )
326
 
                                        parseTEL( params, props[ 1 ] );
 
429
                                        parseTEL( params, value );
327
430
                                else if( params[ 0 ].equals( "EMAIL" ) )
328
 
                                        parseEMAIL( params, props[ 1 ] );
 
431
                                        parseEMAIL( params, value );
329
432
                        }
330
433
                }
331
434
 
334
437
                                AbortImportException
335
438
                {
336
439
                        // already got a better name?
337
 
                        if( _nameLevel >= NAMELEVEL_N ) return;
 
440
                        if( _name_level >= NAMELEVEL_N ) return;
338
441
 
339
442
                        // get name parts
340
443
                        String[] nameparts = value.split( ";" );
349
452
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
350
453
 
351
454
                        // set name
352
 
                        setName( undoCharsetAndEncoding( params, value ) );
353
 
                        _nameLevel = NAMELEVEL_N;
 
455
                        setName( value );
 
456
                        _name_level = NAMELEVEL_N;
354
457
 
355
458
                        // check now to see if we need to import this contact (to avoid
356
459
                        // parsing the rest of the vCard unnecessarily)
362
465
                                throws ParseException, SkipContactException
363
466
                {
364
467
                        // already got a better name?
365
 
                        if( _nameLevel >= NAMELEVEL_FN ) return;
 
468
                        if( _name_level >= NAMELEVEL_FN ) return;
366
469
 
367
470
                        // set name
368
 
                        setName( undoCharsetAndEncoding( params, value ) );
369
 
                        _nameLevel = NAMELEVEL_FN;
 
471
                        setName( value );
 
472
                        _name_level = NAMELEVEL_FN;
370
473
                }
371
474
 
372
475
                private void parseORG( String[] params, String value )
373
476
                                throws ParseException, SkipContactException
374
477
                {
375
478
                        // already got a better name?
376
 
                        if( _nameLevel >= NAMELEVEL_ORG ) return;
 
479
                        if( _name_level >= NAMELEVEL_ORG ) return;
377
480
 
378
481
                        // get org parts
379
482
                        String[] orgparts = value.split( ";" );
387
490
                                value = orgparts[ 0 ];
388
491
 
389
492
                        // set name
390
 
                        setName( undoCharsetAndEncoding( params, value ) );
391
 
                        _nameLevel = NAMELEVEL_ORG;
 
493
                        setName( value );
 
494
                        _name_level = NAMELEVEL_ORG;
392
495
                }
393
496
 
394
497
                private void parseTEL( String[] params, String value )
419
522
                }
420
523
 
421
524
                public void parseEMAIL( String[] params, String value )
 
525
                                throws ParseException
422
526
                {
423
527
                        if( value.length() == 0 ) return;
424
528
 
442
546
                                throw new ParseException( R.string.error_vcf_malformed );
443
547
 
444
548
                        //  missing name properties?
445
 
                        if( _nameLevel == NAMELEVEL_NONE )
 
549
                        if( _name_level == NAMELEVEL_NONE )
446
550
                                throw new ParseException( R.string.error_vcf_noname );
447
551
 
448
552
                        // check if we should import this one? If we've already got an 'N'-
449
553
                        // type name, this will already have been done by parseN() so we
450
554
                        // mustn't do this here (or it could prompt twice!)
451
 
                        if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
 
555
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
452
556
                                throw new SkipContactException();
453
557
                }
454
558
 
455
 
                private String undoCharsetAndEncoding( String[] params, String value )
456
 
                                throws ParseException
457
 
                {
458
 
                        // check encoding/charset
459
 
                        String charset, encoding;
460
 
                        if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
461
 
                                        !charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
462
 
                                throw new ParseException( R.string.error_vcf_charset );
463
 
                        if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
464
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) &&
465
 
                                        !encoding.equals( "8BIT" ) ) //&& !encoding.equals( "BASE64" ) )
466
 
                                throw new ParseException( R.string.error_vcf_encoding );
467
 
 
468
 
                        // do decoding?
469
 
                        if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
470
 
                                return unencodeQuotedPrintable( value, charset );
471
 
//                      if( encoding != null && encoding.equals( "BASE64" ) )
472
 
//                              return unencodeBase64( value, charset );
473
 
 
474
 
                        // nothing to do!
475
 
                        return value;
476
 
                }
477
 
 
478
559
                private String checkParam( String[] params, String name )
479
560
                {
480
561
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
510
591
                        return types;
511
592
                }
512
593
 
513
 
                private String unencodeQuotedPrintable( String str, String charset )
 
594
                private UnencodeResult unencodeQuotedPrintable( String str, String charset )
514
595
                {
 
596
                        boolean another = false;
 
597
 
515
598
                        // default encoding scheme
516
599
                        if( charset == null ) charset = "UTF-8";
517
600
 
518
601
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
519
602
                        byte[] bytes = new byte[ str.length() ];
520
603
                        int j = 0;
521
 
                        for( int i = 0; i < str.length(); i++, j++ ) {
 
604
                        for( int i = 0; i < str.length(); i++ )
 
605
                        {
 
606
                                // get next char and process...
522
607
                                char ch = str.charAt( i );
523
 
                                if( ch == '=' && i < str.length() - 2 ) {
 
608
                                if( ch == '=' && i < str.length() - 2 )
 
609
                                {
 
610
                                        // we found a =XX format byte, add it
524
611
                                        bytes[ j ] = (byte)(
525
612
                                                        Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
526
613
                                                        Character.digit( str.charAt( i + 2 ), 16 ) );
527
614
                                        i += 2;
528
615
                                }
 
616
                                else if( ch == '=' && i == str.length() - 1 )
 
617
                                {
 
618
                                        // we found a '=' at the end of a line signifying a multi-
 
619
                                        // line string, so we don't add it.
 
620
                                        another = true;
 
621
                                        continue;
 
622
                                }
529
623
                                else
 
624
                                        // just a normal char...
530
625
                                        bytes[ j ] = (byte)ch;
 
626
                                j++;
531
627
                        }
532
 
                        try {
533
 
                                return new String( bytes, 0, j, charset );
534
 
                        } catch( UnsupportedEncodingException e ) { }
535
 
                        return null;
 
628
 
 
629
                        return new UnencodeResult( another, bytes, j );
536
630
                }
537
631
        }
538
632
}