/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-07-13 19:13:38 UTC
  • Revision ID: edam@waxworlds.org-20100713191338-e23wre83uellvtt4
Tags: 1.0
- updated NEWS, TODO and manifest

Show diffs side-by-side

added added

removed removed

25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
28
 
import java.io.FileInputStream;
29
28
import java.io.FileNotFoundException;
30
29
import java.io.FileReader;
31
30
import java.io.FilenameFilter;
32
31
import java.io.IOException;
33
32
import java.io.UnsupportedEncodingException;
34
 
import java.nio.ByteBuffer;
35
33
import java.util.Arrays;
36
34
import java.util.HashSet;
37
35
import java.util.List;
67
65
                try
68
66
                {
69
67
                        // open directory
70
 
                        String path = "/sdcard" + prefs.getString( "location", "/" );
71
 
                        File file = new File( path );
72
 
                        if( !file.exists() )
 
68
                        String location = prefs.getString( "location", "" );
 
69
                        File dir = new File( location );
 
70
                        if( !dir.exists() )
73
71
                                showError( R.string.error_locationnotfound );
74
72
 
75
73
                        // directory, or file?
76
 
                        if( file.isDirectory() )
 
74
                        if( dir.isDirectory() )
77
75
                        {
78
76
                                // get files
79
77
                                class VCardFilter implements FilenameFilter {
81
79
                                                return name.toLowerCase().endsWith( ".vcf" );
82
80
                                        }
83
81
                                }
84
 
                                files = file.listFiles( new VCardFilter() );
 
82
                                files = dir.listFiles( new VCardFilter() );
85
83
                        }
86
84
                        else
87
85
                        {
88
86
                                // use just this file
89
87
                                files = new File[ 1 ];
90
 
                                files[ 0 ] = file;
 
88
                                files[ 0 ] = dir;
91
89
                        }
92
90
                }
93
91
                catch( SecurityException e ) {
140
138
 
141
139
                }
142
140
                catch( FileNotFoundException e ) {
143
 
                        showError( getText( R.string.error_filenotfound ) +
144
 
                                file.getName() );
 
141
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
145
142
                }
146
143
                catch( IOException e ) {
147
144
                        showError( getText( R.string.error_ioerror ) + file.getName() );
150
147
 
151
148
        private void importVCardFile( File file ) throws AbortImportException
152
149
        {
153
 
                // check file is good
154
 
                if( !file.exists() )
155
 
                        showError( getText( R.string.error_filenotfound ) +
156
 
                                file.getName() );
157
 
                if( file.length() == 0 )
158
 
                        showError( getText( R.string.error_fileisempty ) +
159
 
                                file.getName() );
160
 
 
161
150
                try
162
151
                {
163
 
                        // open/read file
164
 
                        FileInputStream istream = new FileInputStream( file );
165
 
                        byte[] content = new byte[ (int)file.length() ];
166
 
                        istream.read( content );
167
 
 
168
 
                        // import
169
 
                        importVCardFileContent( content, file.getName() );
 
152
                        // open file
 
153
                        BufferedReader reader = new BufferedReader(
 
154
                                        new FileReader( file ) );
 
155
 
 
156
                        // read
 
157
                        StringBuffer content = new StringBuffer();
 
158
                        String line;
 
159
                        while( ( line = reader.readLine() ) != null )
 
160
                                content.append( line ).append( "\n" );
 
161
 
 
162
                        importVCardFileContent( content.toString(), file.getName() );
170
163
                }
171
164
                catch( FileNotFoundException e ) {
172
 
                        showError( getText( R.string.error_filenotfound ) +
173
 
                                file.getName() );
 
165
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
174
166
                }
175
167
                catch( IOException e ) {
176
168
                        showError( getText( R.string.error_ioerror ) + file.getName() );
177
169
                }
178
170
        }
179
171
 
180
 
        private void importVCardFileContent( byte[] content, String fileName )
 
172
        private void importVCardFileContent( String content, String fileName )
181
173
                        throws AbortImportException
182
174
        {
183
 
                ByteBuffer buffers[] = getLinesFromContent( content );
 
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]", "" );
184
180
 
185
 
                // go through lines
 
181
                // get lines and parse them
 
182
                String[] lines = content.split( "\n" );
186
183
                VCard vCard = null;
187
 
                for( int i = 0; i < buffers.length; i++ )
 
184
                for( int i = 0; i < lines.length; i++ )
188
185
                {
189
 
                        // get a US-ASCII version of the line for processing
190
 
                        String line;
191
 
                        try {
192
 
                                line = new String( buffers[ i ].array(), buffers[ i ].position(),
193
 
                                        buffers[ i ].limit() - buffers[ i ].position(), "US-ASCII" );
194
 
                        }
195
 
                        catch( UnsupportedEncodingException e ) {
196
 
                                // we know US-ASCII is supported, so appease the compiler...
197
 
                                line = "";
198
 
                        }
 
186
                        String line = lines[ i ];
199
187
 
200
188
                        if( vCard == null ) {
201
189
                                // look for vcard beginning
230
218
                                {
231
219
                                        // try giving the line to the vcard
232
220
                                        try {
233
 
                                                vCard.parseLine( buffers[ i ] );
 
221
                                                vCard.parseLine( line );
234
222
                                        }
235
223
                                        catch( VCard.ParseException e ) {
236
224
                                                skipContact();
255
243
                }
256
244
        }
257
245
 
258
 
        private ByteBuffer[] getLinesFromContent( byte[] content )
259
 
        {
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;
275
 
                        }
276
 
                lines[ lines.length - 1 ] = ByteBuffer.wrap( content, last,
277
 
                        content.length - last );
278
 
 
279
 
                return lines;
280
 
        }
281
 
 
282
246
        private class VCard extends ContactData
283
247
        {
284
248
                private final static int NAMELEVEL_NONE = 0;
287
251
                private final static int NAMELEVEL_N = 3;
288
252
 
289
253
                private String _version = null;
290
 
                private Vector< ByteBuffer > _buffers = null;
291
 
                private int _name_level = NAMELEVEL_NONE;
292
 
                private boolean _parser_in_multiline = false;
293
 
                private String _parser_current_name_and_params = null;
294
 
                private String _parser_buffered_value_so_far = "";
295
 
 
296
 
                protected class UnencodeResult
297
 
                {
298
 
                        private boolean _another_line_required;
299
 
                        private ByteBuffer _buffer;
300
 
 
301
 
                        public UnencodeResult( boolean another_line_required,
302
 
                                ByteBuffer buffer )
303
 
                        {
304
 
                                _another_line_required = another_line_required;
305
 
                                _buffer = buffer;
306
 
                        }
307
 
 
308
 
                        public boolean isAnotherLineRequired()
309
 
                        {
310
 
                                return _another_line_required;
311
 
                        }
312
 
 
313
 
                        public ByteBuffer getBuffer()
314
 
                        {
315
 
                                return _buffer;
316
 
                        }
317
 
                }
 
254
                private Vector< String > _lines = null;
 
255
                private int _nameLevel = NAMELEVEL_NONE;
318
256
 
319
257
                @SuppressWarnings("serial")
320
258
                protected class ParseException extends Exception
334
272
                @SuppressWarnings("serial")
335
273
                protected class SkipContactException extends Exception { }
336
274
 
337
 
                public void parseLine( ByteBuffer buffer )
 
275
                public void parseLine( String line )
338
276
                                throws ParseException, SkipContactException,
339
277
                                AbortImportException
340
278
                {
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[] parts = line.split( ":", 2 );
366
 
                                if( parts.length == 2 ) {
367
 
                                        name_and_params = parts[ 0 ].trim();
368
 
                                        string_value = parts[ 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!
 
279
                        // get property halves
 
280
                        String[] props = line.split( ":" );
 
281
                        for( int i = 0; i < props.length; i++ )
 
282
                                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
 
382
287
                        if( _version == null )
383
288
                        {
384
 
                                // is this a version?
385
 
                                if( name_and_params.equals( "VERSION" ) )
 
289
                                if( props[ 0 ].equals( "VERSION" ) )
386
290
                                {
387
 
                                        // yes, check/store it
388
 
                                        if( !string_value.equals( "2.1" ) &&
389
 
                                                        !string_value.equals( "3.0" ) )
 
291
                                        // get version
 
292
                                        if( !props[ 1 ].equals( "2.1" ) &&
 
293
                                                        !props[ 1 ].equals( "3.0" ) )
390
294
                                                throw new ParseException( R.string.error_vcf_version );
391
 
                                        _version = string_value;
 
295
                                        _version = props[ 1 ];
392
296
 
393
 
                                        // parse any other buffers we've accumulated so far
394
 
                                        if( _buffers != null )
395
 
                                                for( int i = 0; i < _buffers.size(); i++ )
396
 
                                                        parseLine( _buffers.get( i ) );
397
 
                                        _buffers = null;
 
297
                                        // parse any other lines we've accumulated so far
 
298
                                        if( _lines != null )
 
299
                                                for( int i = 0; i < _lines.size(); i++ )
 
300
                                                        parseLine( _lines.get( i ) );
 
301
                                        _lines = null;
398
302
                                }
399
303
                                else
400
304
                                {
401
 
                                        // no, so stash this buffer till we have a version
402
 
                                        if( _buffers == null )
403
 
                                                _buffers = new Vector< ByteBuffer >();
404
 
                                        _buffers.add( buffer );
 
305
                                        // stash this line till we have a version
 
306
                                        if( _lines == null )
 
307
                                                _lines = new Vector< String >();
 
308
                                        _lines.add( line );
405
309
                                }
406
310
                        }
407
311
                        else
408
312
                        {
409
 
                                // value bytes, for processing
410
 
                                ByteBuffer value;
411
 
 
412
 
                                if( _parser_in_multiline )
413
 
                                {
414
 
                                        // if we're currently in a multi-line value, use the stored
415
 
                                        // property name and parameters
416
 
                                        name_and_params = _parser_current_name_and_params;
417
 
 
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' ) )
424
 
                                        {
425
 
                                                pos++;
426
 
                                        }
427
 
 
428
 
                                        // get value from buffer
429
 
                                        value = ByteBuffer.wrap( buffer.array(), pos,
430
 
                                                buffer.limit() - pos );
431
 
                                }
432
 
                                else
433
 
                                {
434
 
                                        // ignore empty values
435
 
                                        if( string_value.length() < 1 ) return;
436
 
 
437
 
                                        // calculate how many chars to skip from beginning of line
438
 
                                        // so we skip the property "name:" part
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 );
444
 
 
445
 
                                        // reset the saved multi-line state
446
 
                                        _parser_current_name_and_params = name_and_params;
447
 
                                        _parser_buffered_value_so_far = "";
448
 
                                }
449
 
 
450
313
                                // get parameter parts
451
 
                                String[] name_param_parts = name_and_params.split( ";", -1 );
452
 
                                for( int i = 0; i < name_param_parts.length; i++ )
453
 
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
454
 
 
455
 
                                // parse encoding parameter
456
 
                                String encoding = checkParam( name_param_parts, "ENCODING" );
457
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
458
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
459
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
460
 
                                        //&& !encoding.equals( "BASE64" ) )
461
 
                                {
462
 
                                        throw new ParseException( R.string.error_vcf_encoding );
463
 
                                }
464
 
 
465
 
                                // parse charset parameter
466
 
                                String charset = checkParam( name_param_parts, "CHARSET" );
467
 
                                if( charset != null ) charset = charset.toUpperCase();
468
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
469
 
                                        !charset.equals( "ASCII" ) && !charset.equals( "UTF-8" ) )
470
 
                                {
471
 
                                        throw new ParseException( R.string.error_vcf_charset );
472
 
                                }
473
 
 
474
 
                                // do unencoding (or default to a fake unencoding result with
475
 
                                // the raw string)
476
 
                                UnencodeResult unencoding_result = null;
477
 
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
478
 
                                        unencoding_result = unencodeQuotedPrintable( value );
479
 
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
480
 
//                                      result = unencodeBase64( props[ 1 ], charset );
481
 
                                if( unencoding_result != null ) {
482
 
                                        value = unencoding_result.getBuffer();
483
 
                                        _parser_in_multiline =
484
 
                                                unencoding_result.isAnotherLineRequired();
485
 
                                }
486
 
 
487
 
                                // convert 8-bit ASCII charset to US-ASCII
488
 
                                if( charset == null || charset == "ASCII" ) {
489
 
                                        value = transcodeAsciiToUtf8( value );
490
 
                                        charset = "UTF-8";
491
 
                                }
492
 
 
493
 
                                // process charset
494
 
                                try {
495
 
                                        string_value =
496
 
                                                new String( value.array(), value.position(),
497
 
                                                        value.limit() - value.position(), charset );
498
 
                                } catch( UnsupportedEncodingException e ) {
499
 
                                        throw new ParseException( R.string.error_vcf_charset );
500
 
                                }
501
 
 
502
 
                                // handle multi-line requests
503
 
                                if( _parser_in_multiline ) {
504
 
                                        _parser_buffered_value_so_far += string_value;
505
 
                                        return;
506
 
                                }
507
 
 
508
 
                                // add on buffered multi-line content
509
 
                                String complete_value =
510
 
                                        _parser_buffered_value_so_far + string_value;
 
314
                                String[] params = props[ 0 ].split( ";" );
 
315
                                for( int i = 0; i < params.length; i++ )
 
316
                                        params[ i ] = params[ i ].trim();
511
317
 
512
318
                                // parse some properties
513
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
514
 
                                        parseN( name_param_parts, complete_value );
515
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
516
 
                                        parseFN( name_param_parts, complete_value );
517
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
518
 
                                        parseORG( name_param_parts, complete_value );
519
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
520
 
                                        parseTEL( name_param_parts, complete_value );
521
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
522
 
                                        parseEMAIL( name_param_parts, complete_value );
 
319
                                if( params[ 0 ].equals( "N" ) )
 
320
                                        parseN( params, props[ 1 ] );
 
321
                                else if( params[ 0 ].equals( "FN" ) )
 
322
                                        parseFN( params, props[ 1 ] );
 
323
                                else if( params[ 0 ].equals( "ORG" ) )
 
324
                                        parseORG( params, props[ 1 ] );
 
325
                                else if( params[ 0 ].equals( "TEL" ) )
 
326
                                        parseTEL( params, props[ 1 ] );
 
327
                                else if( params[ 0 ].equals( "EMAIL" ) )
 
328
                                        parseEMAIL( params, props[ 1 ] );
523
329
                        }
524
330
                }
525
331
 
528
334
                                AbortImportException
529
335
                {
530
336
                        // already got a better name?
531
 
                        if( _name_level >= NAMELEVEL_N ) return;
 
337
                        if( _nameLevel >= NAMELEVEL_N ) return;
532
338
 
533
339
                        // get name parts
534
 
                        String[] name_parts = value.split( ";" );
535
 
                        for( int i = 0; i < name_parts.length; i++ )
536
 
                                name_parts[ i ] = name_parts[ i ].trim();
 
340
                        String[] nameparts = value.split( ";" );
 
341
                        for( int i = 0; i < nameparts.length; i++ )
 
342
                                nameparts[ i ] = nameparts[ i ].trim();
537
343
 
538
344
                        // build name
539
345
                        value = "";
540
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
541
 
                                value += name_parts[ 1 ];
542
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
543
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
346
                        if( nameparts.length > 1 && nameparts[ 1 ].length() > 0 )
 
347
                                value += nameparts[ 1 ];
 
348
                        if( nameparts[ 0 ].length() > 0 )
 
349
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
544
350
 
545
351
                        // set name
546
 
                        setName( value );
547
 
                        _name_level = NAMELEVEL_N;
 
352
                        setName( undoCharsetAndEncoding( params, value ) );
 
353
                        _nameLevel = NAMELEVEL_N;
548
354
 
549
355
                        // check now to see if we need to import this contact (to avoid
550
356
                        // parsing the rest of the vCard unnecessarily)
556
362
                                throws ParseException, SkipContactException
557
363
                {
558
364
                        // already got a better name?
559
 
                        if( _name_level >= NAMELEVEL_FN ) return;
 
365
                        if( _nameLevel >= NAMELEVEL_FN ) return;
560
366
 
561
367
                        // set name
562
 
                        setName( value );
563
 
                        _name_level = NAMELEVEL_FN;
 
368
                        setName( undoCharsetAndEncoding( params, value ) );
 
369
                        _nameLevel = NAMELEVEL_FN;
564
370
                }
565
371
 
566
372
                private void parseORG( String[] params, String value )
567
373
                                throws ParseException, SkipContactException
568
374
                {
569
375
                        // already got a better name?
570
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
 
376
                        if( _nameLevel >= NAMELEVEL_ORG ) return;
571
377
 
572
378
                        // get org parts
573
 
                        String[] org_parts = value.split( ";" );
574
 
                        for( int i = 0; i < org_parts.length; i++ )
575
 
                                org_parts[ i ] = org_parts[ i ].trim();
 
379
                        String[] orgparts = value.split( ";" );
 
380
                        for( int i = 0; i < orgparts.length; i++ )
 
381
                                orgparts[ i ] = orgparts[ i ].trim();
576
382
 
577
383
                        // build name
578
 
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
579
 
                                value = org_parts[ 1 ];
 
384
                        if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
 
385
                                value = orgparts[ 1 ];
580
386
                        else
581
 
                                value = org_parts[ 0 ];
 
387
                                value = orgparts[ 0 ];
582
388
 
583
389
                        // set name
584
 
                        setName( value );
585
 
                        _name_level = NAMELEVEL_ORG;
 
390
                        setName( undoCharsetAndEncoding( params, value ) );
 
391
                        _nameLevel = NAMELEVEL_ORG;
586
392
                }
587
393
 
588
394
                private void parseTEL( String[] params, String value )
596
402
 
597
403
                        // here's the logic...
598
404
                        boolean preferred = types.contains( "PREF" );
599
 
                        int type = PhonesColumns.TYPE_MOBILE;
600
405
                        if( types.contains( "VOICE" ) )
601
406
                                if( types.contains( "WORK" ) )
602
 
                                        type = PhonesColumns.TYPE_WORK;
 
407
                                        addPhone( value, PhonesColumns.TYPE_WORK, preferred );
603
408
                                else
604
 
                                        type = PhonesColumns.TYPE_HOME;
 
409
                                        addPhone( value, PhonesColumns.TYPE_HOME, preferred );
605
410
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
606
 
                                type = PhonesColumns.TYPE_MOBILE;
 
411
                                addPhone( value, PhonesColumns.TYPE_MOBILE, preferred );
607
412
                        if( types.contains( "FAX" ) )
608
413
                                if( types.contains( "HOME" ) )
609
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
414
                                        addPhone( value, PhonesColumns.TYPE_FAX_HOME, preferred );
610
415
                                else
611
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
416
                                        addPhone( value, PhonesColumns.TYPE_FAX_WORK, preferred );
612
417
                        if( types.contains( "PAGER" ) )
613
 
                                type = PhonesColumns.TYPE_PAGER;
614
 
 
615
 
                        // add phone number
616
 
                        addPhone( value, type, preferred );
 
418
                                addPhone( value, PhonesColumns.TYPE_PAGER, preferred );
617
419
                }
618
420
 
619
421
                public void parseEMAIL( String[] params, String value )
620
 
                                throws ParseException
621
422
                {
622
423
                        if( value.length() == 0 ) return;
623
424
 
637
438
                                AbortImportException
638
439
                {
639
440
                        // missing version (and data is present)
640
 
                        if( _version == null && _buffers != null )
 
441
                        if( _version == null && _lines != null )
641
442
                                throw new ParseException( R.string.error_vcf_malformed );
642
443
 
643
444
                        //  missing name properties?
644
 
                        if( _name_level == NAMELEVEL_NONE )
 
445
                        if( _nameLevel == NAMELEVEL_NONE )
645
446
                                throw new ParseException( R.string.error_vcf_noname );
646
447
 
647
448
                        // check if we should import this one? If we've already got an 'N'-
648
449
                        // type name, this will already have been done by parseN() so we
649
450
                        // mustn't do this here (or it could prompt twice!)
650
 
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
 
451
                        if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
651
452
                                throw new SkipContactException();
652
453
                }
653
454
 
 
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
 
654
478
                private String checkParam( String[] params, String name )
655
479
                {
656
480
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
663
487
                }
664
488
 
665
489
                private Set< String > extractTypes( String[] params,
666
 
                                List< String > valid_types )
 
490
                                List< String > validTypes )
667
491
                {
668
492
                        HashSet< String > types = new HashSet< String >();
669
493
 
670
494
                        // get 3.0-style TYPE= param
671
 
                        String type_param;
672
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
673
 
                                String[] parts = type_param.split( "," );
674
 
                                for( int i = 0; i < parts.length; i++ )
675
 
                                        if( valid_types.contains( parts[ i ] ) )
676
 
                                                types.add( parts[ i ] );
 
495
                        String typeParam;
 
496
                        if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
 
497
                                String[] bits = typeParam.split( "," );
 
498
                                for( int i = 0; i < bits.length; i++ )
 
499
                                        if( validTypes.contains( bits[ i ] ) )
 
500
                                                types.add( bits[ i ] );
677
501
                        }
678
502
 
679
503
                        // get 2.1-style type param
680
504
                        if( _version.equals( "2.1" ) ) {
681
505
                                for( int i = 1; i < params.length; i++ )
682
 
                                        if( valid_types.contains( params[ i ] ) )
 
506
                                        if( validTypes.contains( params[ i ] ) )
683
507
                                                types.add( params[ i ] );
684
508
                        }
685
509
 
686
510
                        return types;
687
511
                }
688
512
 
689
 
                private UnencodeResult unencodeQuotedPrintable( ByteBuffer in )
 
513
                private String unencodeQuotedPrintable( String str, String charset )
690
514
                {
691
 
                        boolean another = false;
 
515
                        // default encoding scheme
 
516
                        if( charset == null ) charset = "UTF-8";
692
517
 
693
518
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
694
 
                        byte[] out = new byte[ in.limit() - in.position() ];
 
519
                        byte[] bytes = new byte[ str.length() ];
695
520
                        int j = 0;
696
 
                        for( int i = in.position(); i < in.limit(); i++ )
697
 
                        {
698
 
                                // get next char and process...
699
 
                                byte ch = in.array()[ i ];
700
 
                                if( ch == '=' && i < in.limit() - 2 )
701
 
                                {
702
 
                                        // we found a =XX format byte, add it
703
 
                                        out[ j ] = (byte)(
704
 
                                                Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
705
 
                                                Character.digit( in.array()[ i + 2 ], 16 ) );
 
521
                        for( int i = 0; i < str.length(); i++, j++ ) {
 
522
                                char ch = str.charAt( i );
 
523
                                if( ch == '=' && i < str.length() - 2 ) {
 
524
                                        bytes[ j ] = (byte)(
 
525
                                                        Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
 
526
                                                        Character.digit( str.charAt( i + 2 ), 16 ) );
706
527
                                        i += 2;
707
528
                                }
708
 
                                else if( ch == '=' && i == in.limit() - 1 )
709
 
                                {
710
 
                                        // we found a '=' at the end of a line signifying a multi-
711
 
                                        // line string, so we don't add it.
712
 
                                        another = true;
713
 
                                        continue;
714
 
                                }
715
529
                                else
716
 
                                        // just a normal char...
717
 
                                        out[ j ] = (byte)ch;
718
 
                                j++;
719
 
                        }
720
 
 
721
 
                        return new UnencodeResult( another, ByteBuffer.wrap( out, 0, j ) );
722
 
                }
723
 
 
724
 
                private ByteBuffer transcodeAsciiToUtf8( ByteBuffer in )
725
 
                {
726
 
                        // transcode
727
 
                        byte[] out = new byte[ ( in.limit() - in.position() ) * 2 ];
728
 
                        int j = 0;
729
 
                        for( int a = in.position(); a < in.limit(); a++ )
730
 
                        {
731
 
                                // if char is < 127, keep it as-is
732
 
                                if( in.array()[ a ] >= 0 )
733
 
                                        out[ j++ ] = in.array()[ a ];
734
 
 
735
 
                                // else, convert it to UTF-8
736
 
                                else {
737
 
                                        int b = 0xff & (int)in.array()[ a ];
738
 
                                        out[ j++ ] = (byte)( 0xc0 | ( b >> 6 ) );
739
 
                                        out[ j++ ] = (byte)( 0x80 | ( b & 0x3f ) );
740
 
                                }
741
 
                        }
742
 
 
743
 
                        return ByteBuffer.wrap( out, 0, j );
 
530
                                        bytes[ j ] = (byte)ch;
 
531
                        }
 
532
                        try {
 
533
                                return new String( bytes, 0, j, charset );
 
534
                        } catch( UnsupportedEncodingException e ) { }
 
535
                        return null;
744
536
                }
745
537
        }
746
538
}