/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-12-12 00:16:42 UTC
  • Revision ID: edam@waxworlds.org-20101212001642-dqxbxww4ik9x7zbp
- update TODO

Show diffs side-by-side

added added

removed removed

32
32
import java.io.IOException;
33
33
import java.io.UnsupportedEncodingException;
34
34
import java.nio.ByteBuffer;
35
 
import java.util.ArrayList;
36
35
import java.util.Arrays;
37
36
import java.util.HashSet;
38
 
import java.util.Iterator;
39
37
import java.util.List;
40
 
import java.util.NoSuchElementException;
41
38
import java.util.Set;
42
39
import java.util.Vector;
43
40
import java.util.regex.Matcher;
123
120
                {
124
121
                        // open file
125
122
                        BufferedReader reader = new BufferedReader(
126
 
                                new FileReader( file ) );
 
123
                                        new FileReader( file ) );
127
124
 
128
125
                        // read
129
126
                        String line;
132
129
                        {
133
130
                                if( !inVCard ) {
134
131
                                        // look for vcard beginning
135
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
132
                                        if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
136
133
                                                inVCard = true;
137
134
                                                _vCardCount++;
138
135
                                        }
139
136
                                }
140
 
                                else if( line.matches( "^END:VCARD" ) )
 
137
                                else if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
141
138
                                        inVCard = false;
142
139
                        }
143
140
 
181
178
        }
182
179
 
183
180
        private void importVCardFileContent( byte[] content, String fileName )
184
 
                throws AbortImportException
 
181
                        throws AbortImportException
185
182
        {
 
183
                ByteBuffer buffers[] = getLinesFromContent( content );
 
184
 
186
185
                // go through lines
187
186
                VCard vCard = null;
188
 
                ContentLineIterator cli = new ContentLineIterator( content );
189
 
                while( cli.hasNext() )
 
187
                for( int i = 0; i < buffers.length; i++ )
190
188
                {
191
 
                        ByteBuffer buffer = cli.next();
192
 
 
193
189
                        // get a US-ASCII version of the line for processing
194
190
                        String line;
195
191
                        try {
196
 
                                line = new String( buffer.array(), buffer.position(),
197
 
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
192
                                line = new String( buffers[ i ].array(), buffers[ i ].position(),
 
193
                                        buffers[ i ].limit() - buffers[ i ].position(), "US-ASCII" );
198
194
                        }
199
195
                        catch( UnsupportedEncodingException e ) {
200
196
                                // we know US-ASCII is supported, so appease the compiler...
203
199
 
204
200
                        if( vCard == null ) {
205
201
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN:VCARD" ) ) {
 
202
                                if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
207
203
                                        setProgress( ++_progress );
208
204
                                        vCard = new VCard();
209
205
                                }
210
206
                        }
211
207
                        else {
212
208
                                // look for vcard content or ending
213
 
                                if( line.matches( "^END:VCARD" ) )
 
209
                                if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
214
210
                                {
215
211
                                        // store vcard and do away with it
216
212
                                        try {
220
216
                                        catch( VCard.ParseException e ) {
221
217
                                                skipContact();
222
218
                                                if( !showContinue(
223
 
                                                        getText( R.string.error_vcf_parse ).toString()
224
 
                                                        + fileName + "\n" + e.getMessage() ) )
225
 
                                                {
 
219
                                                                getText( R.string.error_vcf_parse ).toString()
 
220
                                                                + fileName + "\n" + e.getMessage() ) )
226
221
                                                        finish( ACTION_ABORT );
227
 
                                                }
228
222
                                        }
229
223
                                        catch( VCard.SkipContactException e ) {
230
224
                                                skipContact();
236
230
                                {
237
231
                                        // try giving the line to the vcard
238
232
                                        try {
239
 
                                                vCard.parseLine( buffer, line,
240
 
                                                        cli.doesNextLineLookFolded() );
 
233
                                                vCard.parseLine( buffers[ i ] );
241
234
                                        }
242
235
                                        catch( VCard.ParseException e ) {
243
236
                                                skipContact();
244
237
                                                if( !showContinue(
245
 
                                                        getText( R.string.error_vcf_parse ).toString()
246
 
                                                        + fileName + "\n" + e.getMessage() ) )
247
 
                                                {
 
238
                                                                getText( R.string.error_vcf_parse ).toString()
 
239
                                                                + fileName + "\n" + e.getMessage() ) )
248
240
                                                        finish( ACTION_ABORT );
249
 
                                                }
250
241
 
251
242
                                                // although we're continuing, we still need to abort
252
243
                                                // this vCard. Further lines will be ignored until we
264
255
                }
265
256
        }
266
257
 
267
 
        class ContentLineIterator implements Iterator< ByteBuffer >
 
258
        private ByteBuffer[] getLinesFromContent( byte[] content )
268
259
        {
269
 
                protected byte[] _content = null;
270
 
                protected int _pos = 0;
271
 
 
272
 
                public ContentLineIterator( byte[] content )
273
 
                {
274
 
                        _content = content;
275
 
                }
276
 
 
277
 
                @Override
278
 
                public boolean hasNext()
279
 
                {
280
 
                        return _pos < _content.length;
281
 
                }
282
 
 
283
 
                @Override
284
 
                public ByteBuffer next()
285
 
                {
286
 
                        int initial_pos = _pos;
287
 
 
288
 
                        // find newline
289
 
                        for( ; _pos < _content.length; _pos++ )
290
 
                                if( _content[ _pos ] == '\n' )
291
 
                                {
292
 
                                        // adjust for a \r preceding the \n
293
 
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
294
 
                                                _pos > initial_pos )? _pos - 1 : _pos;
295
 
                                        _pos++;
296
 
                                        return ByteBuffer.wrap( _content, initial_pos,
297
 
                                                to - initial_pos );
298
 
                                }
299
 
 
300
 
                        // we didn't find one, but were there bytes left?
301
 
                        if( _pos != initial_pos ) {
302
 
                                int to = _pos;
303
 
                                _pos++;
304
 
                                return ByteBuffer.wrap( _content, initial_pos,
305
 
                                        to - initial_pos );
 
260
                // count lines in data
 
261
                int num_lines = 1;
 
262
                for( int a = 0; a < content.length; a++ )
 
263
                        if( content[ a ] == '\n' )
 
264
                                num_lines++;
 
265
 
 
266
                // get lines, removing \r's and \n's as we go
 
267
                ByteBuffer lines[] = new ByteBuffer[ num_lines ];
 
268
                int last = 0;
 
269
                for( int a = 0, b = 0; a < content.length; a++ )
 
270
                        if( content[ a ] == '\n' ) {
 
271
                                int to = ( a > 0 && content[ a - 1 ] == '\r' &&
 
272
                                        a - 1 >= last )? a - 1 : a;
 
273
                                lines[ b++ ] = ByteBuffer.wrap( content, last, to - last );
 
274
                                last = a + 1;
306
275
                        }
307
 
 
308
 
                        // no bytes left
309
 
                        throw new NoSuchElementException();
310
 
                }
311
 
 
312
 
                @Override
313
 
                public void remove()
314
 
                {
315
 
                        throw new UnsupportedOperationException();
316
 
                }
317
 
 
318
 
                /**
319
 
                 * Does the next line, if there is one, look like it should be folded
320
 
                 * onto the end of this one?
321
 
                 * @return
322
 
                 */
323
 
                public boolean doesNextLineLookFolded()
324
 
                {
325
 
                        return _pos > 0 && _pos < _content.length &&
326
 
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
327
 
                }
 
276
                lines[ lines.length - 1 ] = ByteBuffer.wrap( content, last,
 
277
                        content.length - last );
 
278
 
 
279
                return lines;
328
280
        }
329
281
 
330
282
        private class VCard extends ContactData
334
286
                private final static int NAMELEVEL_FN = 2;
335
287
                private final static int NAMELEVEL_N = 3;
336
288
 
337
 
                private final static int MULTILINE_NONE = 0;
338
 
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
339
 
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
340
 
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
341
 
 
342
289
                private String _version = null;
343
290
                private Vector< ByteBuffer > _buffers = null;
344
291
                private int _name_level = NAMELEVEL_NONE;
345
 
                private int _parser_multiline_state = MULTILINE_NONE;
 
292
                private boolean _parser_in_multiline = false;
346
293
                private String _parser_current_name_and_params = null;
347
294
                private String _parser_buffered_value_so_far = "";
348
295
 
387
334
                @SuppressWarnings("serial")
388
335
                protected class SkipContactException extends Exception { }
389
336
 
390
 
                private String extractCollonPartFromLine( ByteBuffer buffer,
391
 
                        String line, boolean former )
392
 
                {
393
 
                        String ret = null;
394
 
 
395
 
                        // get a US-ASCII version of the line for processing, unless we were
396
 
                        // supplied with one
397
 
                        if( line == null ) {
398
 
                                try {
399
 
                                        line = new String( buffer.array(), buffer.position(),
400
 
                                                buffer.limit() - buffer.position(), "US-ASCII" );
401
 
                                }
402
 
                                catch( UnsupportedEncodingException e ) {
403
 
                                        // we know US-ASCII is supported, so appease the compiler...
404
 
                                        line = "";
405
 
                                }
406
 
                        }
407
 
 
408
 
                        // split line into name and value parts and check to make sure we
409
 
                        // only got 2 parts and that the first part is not zero in length
410
 
                        String[] parts = line.split( ":", 2 );
411
 
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
412
 
                                ret = parts[ former? 0 : 1 ];
413
 
 
414
 
                        return ret;
415
 
                }
416
 
 
417
 
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
418
 
                        String line )
419
 
                {
420
 
                        return extractCollonPartFromLine( buffer, line, true );
421
 
                }
422
 
 
423
 
                private String extractValueFromLine( ByteBuffer buffer, String line )
424
 
                {
425
 
                        return extractCollonPartFromLine( buffer, line, false );
426
 
                }
427
 
 
428
 
                public void parseLine( ByteBuffer buffer, String line,
429
 
                        boolean next_line_looks_folded )
430
 
                        throws ParseException, SkipContactException,
431
 
                        AbortImportException
432
 
                {
433
 
                        // do we have a version yet?
 
337
                public void parseLine( ByteBuffer buffer )
 
338
                                throws ParseException, SkipContactException,
 
339
                                AbortImportException
 
340
                {
 
341
                        // get a US-ASCII version of the line for processing
 
342
                        String line;
 
343
                        try {
 
344
                                line = new String( buffer.array(), buffer.position(),
 
345
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
346
                        }
 
347
                        catch( UnsupportedEncodingException e ) {
 
348
                                // we know US-ASCII is supported, so appease the compiler...
 
349
                                line = "";
 
350
                        }
 
351
 
 
352
                        // ignore empty lines
 
353
                        if( line.trim() == "" ) return;
 
354
 
 
355
                        // split line into name and value parts (this may turn out to be
 
356
                        // unwanted if the line is a subsequent line in a multi-line
 
357
                        // value, but we have to do this now to check for and handle VCF
 
358
                        // versions first). Also, the value part is only created tentatively
 
359
                        // because it may have an encoding/charset. Since we're treating it
 
360
                        // as UTF-8 (which is compatible with 7-bit US-ASCII) this is ok
 
361
                        // though so long as we later use the raw bytes. ALso we check for
 
362
                        // malformed property:name pairs.
 
363
                        String name_and_params, string_value;
 
364
                        {
 
365
                                String[] bits = line.split(  ":", 2 );
 
366
                                if( bits.length == 2 ) {
 
367
                                        name_and_params = bits[ 0 ].trim();
 
368
                                        string_value = bits[ 1 ].trim();
 
369
                                        if( name_and_params.length() == 0 )
 
370
                                                throw new ParseException( R.string.error_vcf_malformed );
 
371
                                }
 
372
                                else
 
373
                                {
 
374
                                        if( !_parser_in_multiline )
 
375
                                                throw new ParseException( R.string.error_vcf_malformed );
 
376
                                        name_and_params = null;
 
377
                                        string_value = null;
 
378
                                }
 
379
                        }
 
380
 
 
381
                        // if we haven't yet got a version, we won't be paring anything!
434
382
                        if( _version == null )
435
383
                        {
436
 
                                // tentatively get name and params from line
437
 
                                String name_and_params =
438
 
                                        extractNameAndParamsFromLine( buffer, line );
439
 
 
440
 
                                // is it a version line?
441
 
                                if( name_and_params != null &&
442
 
                                        name_and_params.equals( "VERSION" ) )
 
384
                                // is this a version?
 
385
                                if( name_and_params.equals( "VERSION" ) )
443
386
                                {
444
 
                                        // yes, get it!
445
 
                                        String value = extractValueFromLine( buffer, line );
446
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
387
                                        // yes, check/store it
 
388
                                        if( !string_value.equals( "2.1" ) &&
 
389
                                                        !string_value.equals( "3.0" ) )
447
390
                                                throw new ParseException( R.string.error_vcf_version );
448
 
                                        _version = value;
 
391
                                        _version = string_value;
449
392
 
450
 
                                        // parse any buffers we've been accumulating while we waited
451
 
                                        // for a version
 
393
                                        // parse any other buffers we've accumulated so far
452
394
                                        if( _buffers != null )
453
395
                                                for( int i = 0; i < _buffers.size(); i++ )
454
 
                                                        parseLine( _buffers.get( i ), null,
455
 
                                                                i + 1 < _buffers.size() &&
456
 
                                                                _buffers.get( i + 1 ).hasRemaining() &&
457
 
                                                                _buffers.get( i + 1 ).get(
458
 
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
 
396
                                                        parseLine( _buffers.get( i ) );
459
397
                                        _buffers = null;
460
398
                                }
461
399
                                else
462
400
                                {
463
 
                                        // no, so stash this line till we get a version
 
401
                                        // no, so stash this buffer till we have a version
464
402
                                        if( _buffers == null )
465
403
                                                _buffers = new Vector< ByteBuffer >();
466
404
                                        _buffers.add( buffer );
468
406
                        }
469
407
                        else
470
408
                        {
471
 
                                // name and params and the position in the buffer where the
472
 
                                // "value" part of the line start
473
 
                                String name_and_params;
474
 
                                int pos;
 
409
                                // value bytes, for processing
 
410
                                ByteBuffer value;
475
411
 
476
 
                                if( _parser_multiline_state != MULTILINE_NONE )
 
412
                                if( _parser_in_multiline )
477
413
                                {
478
414
                                        // if we're currently in a multi-line value, use the stored
479
415
                                        // property name and parameters
480
416
                                        name_and_params = _parser_current_name_and_params;
481
417
 
482
 
                                        // skip some initial line characters, depending on the type
483
 
                                        // of multi-line we're handling
484
 
                                        pos = buffer.position();
485
 
                                        switch( _parser_multiline_state )
 
418
                                        // find start of string (skip spaces/tabs)
 
419
                                        int pos = buffer.position();
 
420
                                        byte[] buffer_array = buffer.array();
 
421
                                        while( pos < buffer.limit() && (
 
422
                                                buffer_array[ pos ] == ' ' ||
 
423
                                                buffer_array[ pos ] == '\t' ) )
486
424
                                        {
487
 
                                        case MULTILINE_FOLDED:
488
425
                                                pos++;
489
 
                                                break;
490
 
                                        case MULTILINE_ENCODED:
491
 
                                                while( pos < buffer.limit() && (
492
 
                                                        buffer.get( pos ) == ' ' ||
493
 
                                                        buffer.get( pos ) == '\t' ) )
494
 
                                                {
495
 
                                                        pos++;
496
 
                                                }
497
 
                                                break;
498
 
                                        default:
499
 
                                                // do nothing
500
426
                                        }
501
427
 
502
 
                                        // take us out of multi-line so that we can re-detect that
503
 
                                        // this line is a multi-line or not
504
 
                                        _parser_multiline_state = MULTILINE_NONE;
 
428
                                        // get value from buffer
 
429
                                        value = ByteBuffer.wrap( buffer.array(), pos,
 
430
                                                buffer.limit() - pos );
505
431
                                }
506
432
                                else
507
433
                                {
508
 
                                        // get name and params from line, and since we're not
509
 
                                        // parsing a subsequent line in a multi-line, this should
510
 
                                        // not fail, or it's an error
511
 
                                        name_and_params =
512
 
                                                extractNameAndParamsFromLine( buffer, line );
513
 
                                        if( name_and_params == null )
514
 
                                                throw new ParseException(
515
 
                                                        R.string.error_vcf_malformed );
 
434
                                        // ignore empty values
 
435
                                        if( string_value.length() < 1 ) return;
516
436
 
517
437
                                        // calculate how many chars to skip from beginning of line
518
438
                                        // so we skip the property "name:" part
519
 
                                        pos = buffer.position() + name_and_params.length() + 1;
 
439
                                        int pos = buffer.position() + name_and_params.length() + 1;
 
440
 
 
441
                                        // get value from buffer
 
442
                                        value = ByteBuffer.wrap( buffer.array(), pos,
 
443
                                                buffer.limit() - pos );
520
444
 
521
445
                                        // reset the saved multi-line state
522
446
                                        _parser_current_name_and_params = name_and_params;
523
447
                                        _parser_buffered_value_so_far = "";
524
448
                                }
525
449
 
526
 
                                // get value from buffer, as raw bytes
527
 
                                ByteBuffer value;
528
 
                                value = ByteBuffer.wrap( buffer.array(), pos,
529
 
                                        buffer.limit() - pos );
530
 
 
531
450
                                // get parameter parts
532
 
                                String[] name_param_parts = name_and_params.split( ";", -1 );
533
 
                                for( int i = 0; i < name_param_parts.length; i++ )
534
 
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
 
451
                                String[] name_and_param_bits = name_and_params.split( ";" );
 
452
                                for( int i = 0; i < name_and_param_bits.length; i++ )
 
453
                                        name_and_param_bits[ i ] = name_and_param_bits[ i ].trim();
535
454
 
536
455
                                // parse encoding parameter
537
 
                                String encoding = checkParam( name_param_parts, "ENCODING" );
 
456
                                String encoding = checkParam( name_and_param_bits, "ENCODING" );
538
457
                                if( encoding != null ) encoding = encoding.toUpperCase();
539
458
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
540
459
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
544
463
                                }
545
464
 
546
465
                                // parse charset parameter
547
 
                                String charset = checkParam( name_param_parts, "CHARSET" );
 
466
                                String charset = checkParam( name_and_param_bits, "CHARSET" );
548
467
                                if( charset != null ) charset = charset.toUpperCase();
549
468
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
550
 
                                        !charset.equals( "ASCII" ) &&
551
 
                                        !charset.equals( "UTF-8" ) )
 
469
                                        !charset.equals( "ASCII" ) && !charset.equals( "UTF-8" ) )
552
470
                                {
553
471
                                        throw new ParseException( R.string.error_vcf_charset );
554
472
                                }
559
477
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
560
478
                                        unencoding_result = unencodeQuotedPrintable( value );
561
479
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
562
 
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
 
480
//                                      result = unencodeBase64( props[ 1 ], charset );
563
481
                                if( unencoding_result != null ) {
564
482
                                        value = unencoding_result.getBuffer();
565
 
                                        if( unencoding_result.isAnotherLineRequired() )
566
 
                                                _parser_multiline_state = MULTILINE_ENCODED;
 
483
                                        _parser_in_multiline =
 
484
                                                unencoding_result.isAnotherLineRequired();
567
485
                                }
568
486
 
569
487
                                // convert 8-bit ASCII charset to US-ASCII
570
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
488
                                if( charset == null || charset == "ASCII" ) {
571
489
                                        value = transcodeAsciiToUtf8( value );
572
490
                                        charset = "UTF-8";
573
491
                                }
574
492
 
575
493
                                // process charset
576
 
                                String string_value;
577
494
                                try {
578
 
                                        string_value = new String( value.array(), value.position(),
579
 
                                                value.limit() - value.position(), charset );
 
495
                                        string_value =
 
496
                                                new String( value.array(), value.position(),
 
497
                                                        value.limit() - value.position(), charset );
580
498
                                } catch( UnsupportedEncodingException e ) {
581
499
                                        throw new ParseException( R.string.error_vcf_charset );
582
500
                                }
583
501
 
584
 
                                // for some entries that have semicolon-separated value parts,
585
 
                                // check to see if the value ends in an escape character, which
586
 
                                // indicates that we have a multi-line value
587
 
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
588
 
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
589
 
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
590
 
                                        doesStringEndInAnEscapeChar( string_value ) )
591
 
                                {
592
 
                                        _parser_multiline_state = MULTILINE_ESCAPED;
593
 
                                        string_value = string_value.substring( 0,
594
 
                                                string_value.length() - 1 );
595
 
                                }
596
 
 
597
 
                                // now we know whether we're in an encoding multi-line,
598
 
                                // determine if we're in a v3 folded multi-line or not
599
 
                                if( _parser_multiline_state == MULTILINE_NONE &&
600
 
                                        _version.equals( "3.0" ) && next_line_looks_folded )
601
 
                                {
602
 
                                        _parser_multiline_state = MULTILINE_FOLDED;
603
 
                                }
604
 
 
605
 
                                // handle multi-lines by buffering them and parsing them when we
606
 
                                // are processing the last line in a multi-line sequence
607
 
                                if( _parser_multiline_state != MULTILINE_NONE ) {
 
502
                                // handle multi-line requests
 
503
                                if( _parser_in_multiline ) {
608
504
                                        _parser_buffered_value_so_far += string_value;
609
505
                                        return;
610
506
                                }
 
507
 
 
508
                                // add on buffered multi-line content
611
509
                                String complete_value =
612
 
                                        ( _parser_buffered_value_so_far + string_value ).trim();
613
 
 
614
 
                                // ignore empty values
615
 
                                if( complete_value.length() < 1 ) return;
 
510
                                        _parser_buffered_value_so_far + string_value;
616
511
 
617
512
                                // parse some properties
618
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
619
 
                                        parseN( name_param_parts, complete_value );
620
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
621
 
                                        parseFN( name_param_parts, complete_value );
622
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
623
 
                                        parseORG( name_param_parts, complete_value );
624
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
625
 
                                        parseTEL( name_param_parts, complete_value );
626
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
627
 
                                        parseEMAIL( name_param_parts, complete_value );
628
 
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
629
 
                                        parseADR( name_param_parts, complete_value );
630
 
                        }
631
 
                }
632
 
 
633
 
                private boolean doesStringEndInAnEscapeChar( String string )
634
 
                {
635
 
                        // count the number of backslashes at the end of the string
636
 
                        int count = 0;
637
 
                        for( int a = string.length() - 1; a >= 0; a-- )
638
 
                                if( string.charAt( a ) == '\\' )
639
 
                                        count++;
640
 
                                else
641
 
                                        break;
642
 
 
643
 
                        // if there are an even number of backslashes then the final one
644
 
                        // doesn't count
645
 
                        return ( count & 1 ) == 1;
646
 
                }
647
 
 
648
 
                private String[] splitValueBySemicolon( String value )
649
 
                {
650
 
                        // split string in to parts by semicolon
651
 
                        ArrayList< String > parts = new ArrayList< String >(
652
 
                                Arrays.asList( value.split(  ";" ) ) );
653
 
 
654
 
                        // go through parts
655
 
                        for( int a = 0; a < parts.size(); a++ )
656
 
                        {
657
 
                                String str = parts.get( a );
658
 
 
659
 
                                // look for parts that end in an escape character, but ignore
660
 
                                // the final part. We've already detected escape chars at the
661
 
                                // end of the final part in parseLine() and handled multi-lines
662
 
                                // accordingly.
663
 
                                if( a < parts.size() - 1 &&
664
 
                                        doesStringEndInAnEscapeChar( str ) )
665
 
                                {
666
 
                                        // join the next part to this part and remove the next part
667
 
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
668
 
                                                ';' + parts.get( a + 1 ) );
669
 
                                        parts.remove( a + 1 );
670
 
 
671
 
                                        // re-visit this part
672
 
                                        a--;
673
 
                                        continue;
674
 
                                }
675
 
 
676
 
                                // trim and replace string
677
 
                                str = str.trim();
678
 
                                parts.set( a, str );
679
 
                        }
680
 
 
681
 
                        String[] ret = new String[ parts.size() ];
682
 
                        return parts.toArray( ret );
 
513
                                if( name_and_param_bits[ 0 ].equals( "N" ) )
 
514
                                        parseN( name_and_param_bits, complete_value );
 
515
                                else if( name_and_param_bits[ 0 ].equals( "FN" ) )
 
516
                                        parseFN( name_and_param_bits, complete_value );
 
517
                                else if( name_and_param_bits[ 0 ].equals( "ORG" ) )
 
518
                                        parseORG( name_and_param_bits, complete_value );
 
519
                                else if( name_and_param_bits[ 0 ].equals( "TEL" ) )
 
520
                                        parseTEL( name_and_param_bits, complete_value );
 
521
                                else if( name_and_param_bits[ 0 ].equals( "EMAIL" ) )
 
522
                                        parseEMAIL( name_and_param_bits, complete_value );
 
523
                        }
683
524
                }
684
525
 
685
526
                private void parseN( String[] params, String value )
686
 
                        throws ParseException, SkipContactException,
687
 
                        AbortImportException
 
527
                                throws ParseException, SkipContactException,
 
528
                                AbortImportException
688
529
                {
689
530
                        // already got a better name?
690
531
                        if( _name_level >= NAMELEVEL_N ) return;
691
532
 
692
533
                        // get name parts
693
 
                        String[] name_parts = splitValueBySemicolon( value );
 
534
                        String[] nameparts = value.split( ";" );
 
535
                        for( int i = 0; i < nameparts.length; i++ )
 
536
                                nameparts[ i ] = nameparts[ i ].trim();
694
537
 
695
538
                        // build name
696
539
                        value = "";
697
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
698
 
                                value += name_parts[ 1 ];
699
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
700
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
540
                        if( nameparts.length > 1 && nameparts[ 1 ].length() > 0 )
 
541
                                value += nameparts[ 1 ];
 
542
                        if( nameparts[ 0 ].length() > 0 )
 
543
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
701
544
 
702
545
                        // set name
703
546
                        setName( value );
710
553
                }
711
554
 
712
555
                private void parseFN( String[] params, String value )
713
 
                        throws ParseException, SkipContactException
 
556
                                throws ParseException, SkipContactException
714
557
                {
715
558
                        // already got a better name?
716
559
                        if( _name_level >= NAMELEVEL_FN ) return;
721
564
                }
722
565
 
723
566
                private void parseORG( String[] params, String value )
724
 
                        throws ParseException, SkipContactException
 
567
                                throws ParseException, SkipContactException
725
568
                {
726
569
                        // already got a better name?
727
570
                        if( _name_level >= NAMELEVEL_ORG ) return;
728
571
 
729
572
                        // get org parts
730
 
                        String[] org_parts = splitValueBySemicolon( value );
 
573
                        String[] orgparts = value.split( ";" );
 
574
                        for( int i = 0; i < orgparts.length; i++ )
 
575
                                orgparts[ i ] = orgparts[ i ].trim();
731
576
 
732
577
                        // build name
733
 
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
734
 
                                value = org_parts[ 1 ];
735
 
                        else if( org_parts.length > 1 && org_parts[ 1 ].length() > 0 )
736
 
                                value = org_parts[ 0 ] + ", " + org_parts[ 1 ];
 
578
                        if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
 
579
                                value = orgparts[ 1 ];
737
580
                        else
738
 
                                value = org_parts[ 0 ];
 
581
                                value = orgparts[ 0 ];
739
582
 
740
583
                        // set name
741
584
                        setName( value );
743
586
                }
744
587
 
745
588
                private void parseTEL( String[] params, String value )
746
 
                        throws ParseException
 
589
                                throws ParseException
747
590
                {
748
591
                        if( value.length() == 0 ) return;
749
592
 
750
593
                        Set< String > types = extractTypes( params, Arrays.asList(
751
 
                                "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
752
 
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
 
594
                                        "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
 
595
                                        "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
753
596
 
754
597
                        // here's the logic...
755
598
                        boolean preferred = types.contains( "PREF" );
756
 
                        int type = PhonesColumns.TYPE_MOBILE;
757
599
                        if( types.contains( "VOICE" ) )
758
600
                                if( types.contains( "WORK" ) )
759
 
                                        type = PhonesColumns.TYPE_WORK;
 
601
                                        addPhone( value, PhonesColumns.TYPE_WORK, preferred );
760
602
                                else
761
 
                                        type = PhonesColumns.TYPE_HOME;
 
603
                                        addPhone( value, PhonesColumns.TYPE_HOME, preferred );
762
604
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
763
 
                                type = PhonesColumns.TYPE_MOBILE;
 
605
                                addPhone( value, PhonesColumns.TYPE_MOBILE, preferred );
764
606
                        if( types.contains( "FAX" ) )
765
607
                                if( types.contains( "HOME" ) )
766
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
608
                                        addPhone( value, PhonesColumns.TYPE_FAX_HOME, preferred );
767
609
                                else
768
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
610
                                        addPhone( value, PhonesColumns.TYPE_FAX_WORK, preferred );
769
611
                        if( types.contains( "PAGER" ) )
770
 
                                type = PhonesColumns.TYPE_PAGER;
771
 
 
772
 
                        // add phone number
773
 
                        addPhone( value, type, preferred );
 
612
                                addPhone( value, PhonesColumns.TYPE_PAGER, preferred );
774
613
                }
775
614
 
776
615
                public void parseEMAIL( String[] params, String value )
777
 
                        throws ParseException
 
616
                                throws ParseException
778
617
                {
779
618
                        if( value.length() == 0 ) return;
780
619
 
781
620
                        Set< String > types = extractTypes( params, Arrays.asList(
782
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
621
                                        "PREF", "WORK", "HOME", "INTERNET" ) );
783
622
 
784
 
                        // add email address
 
623
                        // here's the logic...
785
624
                        boolean preferred = types.contains( "PREF" );
786
625
                        if( types.contains( "WORK" ) )
787
626
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
789
628
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
790
629
                }
791
630
 
792
 
                private void parseADR( String[] params, String value )
793
 
                        throws ParseException, SkipContactException
794
 
                {
795
 
                        // get address parts
796
 
                        String[] adr_parts = splitValueBySemicolon( value );
797
 
 
798
 
                        // build address
799
 
                        value = "";
800
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
801
 
                                if( value.length() > 0 ) value += "\n";
802
 
                                value += adr_parts[ a ].trim();
803
 
                        }
804
 
 
805
 
                        Set< String > types = extractTypes( params, Arrays.asList(
806
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
807
 
 
808
 
                        // add address
809
 
                        if( types.contains( "WORK" ) )
810
 
                                addAddress( value, Contacts.ContactMethods.TYPE_WORK );
811
 
                        else
812
 
                                addAddress( value, Contacts.ContactMethods.TYPE_HOME);
813
 
                }
814
 
 
815
631
                public void finaliseParsing()
816
 
                        throws ParseException, SkipContactException,
817
 
                        AbortImportException
 
632
                                throws ParseException, SkipContactException,
 
633
                                AbortImportException
818
634
                {
819
635
                        // missing version (and data is present)
820
636
                        if( _version == null && _buffers != null )
833
649
 
834
650
                private String checkParam( String[] params, String name )
835
651
                {
836
 
                        Pattern p = Pattern.compile(
837
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
 
652
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
838
653
                        for( int i = 0; i < params.length; i++ ) {
839
654
                                Matcher m = p.matcher( params[ i ] );
840
655
                                if( m.matches() )
841
 
                                        return m.group( 2 );
 
656
                                        return m.group( 1 );
842
657
                        }
843
658
                        return null;
844
659
                }
845
660
 
846
661
                private Set< String > extractTypes( String[] params,
847
 
                        List< String > valid_types )
 
662
                                List< String > validTypes )
848
663
                {
849
664
                        HashSet< String > types = new HashSet< String >();
850
665
 
851
666
                        // get 3.0-style TYPE= param
852
 
                        String type_param;
853
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
854
 
                                String[] parts = type_param.split( "," );
855
 
                                for( int i = 0; i < parts.length; i++ )
856
 
                                        if( valid_types.contains( parts[ i ] ) )
857
 
                                                types.add( parts[ i ] );
 
667
                        String typeParam;
 
668
                        if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
 
669
                                String[] bits = typeParam.split( "," );
 
670
                                for( int i = 0; i < bits.length; i++ )
 
671
                                        if( validTypes.contains( bits[ i ] ) )
 
672
                                                types.add( bits[ i ] );
858
673
                        }
859
674
 
860
675
                        // get 2.1-style type param
861
676
                        if( _version.equals( "2.1" ) ) {
862
677
                                for( int i = 1; i < params.length; i++ )
863
 
                                        if( valid_types.contains( params[ i ] ) )
 
678
                                        if( validTypes.contains( params[ i ] ) )
864
679
                                                types.add( params[ i ] );
865
680
                        }
866
681
 
871
686
                {
872
687
                        boolean another = false;
873
688
 
874
 
                        // unencode quoted-printable encoding, as per RFC1521 section 5.1
 
689
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
875
690
                        byte[] out = new byte[ in.limit() - in.position() ];
876
691
                        int j = 0;
877
692
                        for( int i = in.position(); i < in.limit(); i++ )
882
697
                                {
883
698
                                        // we found a =XX format byte, add it
884
699
                                        out[ j ] = (byte)(
885
 
                                                        Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
886
 
                                                        Character.digit( in.array()[ i + 2 ], 16 ) );
 
700
                                                Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
 
701
                                                Character.digit( in.array()[ i + 2 ], 16 ) );
887
702
                                        i += 2;
888
703
                                }
889
704
                                else if( ch == '=' && i == in.limit() - 1 )