/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: 2011-03-19 20:33:09 UTC
  • Revision ID: edam@waxworlds.org-20110319203309-5dzfyqrxwk94jtin
- formatting: removed some double-indents on overrunning lines
- updated TODO and NEWS
- rewrote central logic of parser so it makes more sense, looks nicer and has a small optimisation (getting name and params from line only when necessary)
- optimised unnecessary mutliple converting of lines to US-ASCII
- re-wrote line extraction from vcards so that we can lookahead for v3 folded lines
- added support for v3 folded lines

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
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
 
import java.util.HashMap;
38
36
import java.util.HashSet;
39
37
import java.util.Iterator;
40
38
import java.util.List;
41
 
import java.util.Locale;
42
 
import java.util.NoSuchElementException;
43
39
import java.util.Set;
44
40
import java.util.Vector;
45
41
import java.util.regex.Matcher;
46
42
import java.util.regex.Pattern;
 
43
import java.util.NoSuchElementException;
 
44
import java.lang.UnsupportedOperationException;
47
45
 
48
 
import android.annotation.SuppressLint;
49
46
import android.content.SharedPreferences;
 
47
import android.provider.Contacts;
 
48
import android.provider.Contacts.PhonesColumns;
50
49
 
51
 
public class VcardImporter extends Importer
 
50
public class VCFImporter extends Importer
52
51
{
53
 
        private int _vcard_count = 0;
 
52
        private int _vCardCount = 0;
54
53
        private int _progress = 0;
55
54
 
56
 
        public VcardImporter( Doit doit )
 
55
        public VCFImporter( Doit doit )
57
56
        {
58
57
                super( doit );
59
58
        }
60
59
 
61
 
        @SuppressLint( "SdCardPath" )
62
60
        @Override
63
61
        protected void onImport() throws AbortImportException
64
62
        {
83
81
                                // get files
84
82
                                class VCardFilter implements FilenameFilter {
85
83
                                        public boolean accept( File dir, String name ) {
86
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
84
                                                return name.toLowerCase().endsWith( ".vcf" );
87
85
                                        }
88
86
                                }
89
87
                                files = file.listFiles( new VCardFilter() );
111
109
                        countVCardFile( files[ i ] );
112
110
                        setTmpProgress( i );
113
111
                }
114
 
                setProgressMax( _vcard_count ); // will also update tmp progress
 
112
                setProgressMax( _vCardCount );  // will also update tmp progress
115
113
 
116
114
                // import them
117
115
                setProgress( 0 );
118
116
                for( int i = 0; i < files.length; i++ )
119
117
                        importVCardFile( files[ i ] );
120
 
                setProgress( _vcard_count );
121
118
        }
122
119
 
123
120
        private void countVCardFile( File file ) throws AbortImportException
130
127
 
131
128
                        // read
132
129
                        String line;
133
 
                        boolean in_vcard = false;
 
130
                        boolean inVCard = false;
134
131
                        while( ( line = reader.readLine() ) != null )
135
132
                        {
136
 
                                if( !in_vcard )
137
 
                                {
 
133
                                if( !inVCard ) {
138
134
                                        // look for vcard beginning
139
 
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
140
 
                                                in_vcard = true;
141
 
                                                _vcard_count++;
142
 
                                        }
143
 
                                        // check for vMsg files
144
 
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
145
 
                                                showError( getText( R.string.error_vcf_vmsgfile )
146
 
                                                        + file.getName() );
 
135
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
136
                                                inVCard = true;
 
137
                                                _vCardCount++;
147
138
                                        }
148
139
                                }
149
 
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
150
 
                                        in_vcard = false;
 
140
                                else if( line.matches( "^END:VCARD" ) )
 
141
                                        inVCard = false;
151
142
                        }
152
143
 
153
144
                }
176
167
                        FileInputStream istream = new FileInputStream( file );
177
168
                        byte[] content = new byte[ (int)file.length() ];
178
169
                        istream.read( content );
179
 
                        istream = null;
180
170
 
181
171
                        // import
182
172
                        importVCardFileContent( content, file.getName() );
183
173
                }
184
 
                catch( OutOfMemoryError e ) {
185
 
                        showError( R.string.error_outofmemory );
186
 
                }
187
174
                catch( FileNotFoundException e ) {
188
175
                        showError( getText( R.string.error_filenotfound ) +
189
176
                                file.getName() );
197
184
                throws AbortImportException
198
185
        {
199
186
                // go through lines
200
 
                Vcard vcard = null;
201
 
                int vcard_start_line = 0;
 
187
                VCard vCard = null;
202
188
                ContentLineIterator cli = new ContentLineIterator( content );
203
189
                while( cli.hasNext() )
204
190
                {
205
 
                        ContentLine content_line = cli.next();
206
 
 
207
 
                        // get a US-ASCII version of the string, for processing
208
 
                        String line = content_line.getUsAsciiLine();
209
 
 
210
 
                        if( vcard == null ) {
 
191
                        ByteBuffer buffer = cli.next();
 
192
 
 
193
                        // get a US-ASCII version of the line for processing
 
194
                        String line;
 
195
                        try {
 
196
                                line = new String( buffer.array(), buffer.position(),
 
197
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
198
                        }
 
199
                        catch( UnsupportedEncodingException e ) {
 
200
                                // we know US-ASCII is supported, so appease the compiler...
 
201
                                line = "";
 
202
                        }
 
203
 
 
204
                        if( vCard == null ) {
211
205
                                // look for vcard beginning
212
 
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
213
 
                                        setProgress( _progress++ );
214
 
                                        vcard = new Vcard();
215
 
                                        vcard_start_line = cli.getLineNumber();
 
206
                                if( line.matches( "^BEGIN:VCARD" ) ) {
 
207
                                        setProgress( ++_progress );
 
208
                                        vCard = new VCard();
216
209
                                }
217
210
                        }
218
211
                        else {
219
212
                                // look for vcard content or ending
220
 
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
 
213
                                if( line.matches( "^END:VCARD" ) )
221
214
                                {
222
 
                                        // finalise the vcard/contact
 
215
                                        // store vcard and do away with it
223
216
                                        try {
224
 
                                                vcard.finaliseVcard();
225
 
 
226
 
                                                // pass the finalised contact to the importer
227
 
                                                importContact( vcard );
228
 
                                        }
229
 
                                        catch( Vcard.ParseException e ) {
230
 
                                                if( !showContinue(
231
 
                                                        getText( R.string.error_vcf_parse ).toString()
232
 
                                                        + fileName +
233
 
                                                        getText( R.string.error_vcf_parse_line ).toString()
234
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
235
 
                                                {
236
 
                                                        finish( ACTION_ABORT );
237
 
                                                }
238
 
                                                else
239
 
                                                        skipContact();
240
 
                                        }
241
 
                                        catch( ContactData.ContactNotIdentifiableException e ) {
242
 
                                                if( !showContinue(
243
 
                                                        getText( R.string.error_vcf_parse ).toString()
244
 
                                                        + fileName +
245
 
                                                        getText( R.string.error_vcf_parse_line ).toString()
246
 
                                                        + vcard_start_line + ":\n" + getText(
247
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
248
 
                                                ) )
249
 
                                                {
250
 
                                                        finish( ACTION_ABORT );
251
 
                                                }
252
 
                                                else
253
 
                                                        skipContact();
254
 
                                        }
255
 
 
256
 
                                        // discard this vcard
257
 
                                        vcard = null;
 
217
                                                vCard.finaliseParsing();
 
218
                                                importContact( vCard );
 
219
                                        }
 
220
                                        catch( VCard.ParseException e ) {
 
221
                                                skipContact();
 
222
                                                if( !showContinue(
 
223
                                                        getText( R.string.error_vcf_parse ).toString()
 
224
                                                        + fileName + "\n" + e.getMessage() ) )
 
225
                                                {
 
226
                                                        finish( ACTION_ABORT );
 
227
                                                }
 
228
                                        }
 
229
                                        catch( VCard.SkipContactException e ) {
 
230
                                                skipContact();
 
231
                                                // do nothing
 
232
                                        }
 
233
                                        vCard = null;
258
234
                                }
259
235
                                else
260
236
                                {
261
237
                                        // try giving the line to the vcard
262
238
                                        try {
263
 
                                                vcard.parseLine( content_line );
 
239
                                                vCard.parseLine( buffer, line,
 
240
                                                        cli.doesNextLineLookFolded() );
264
241
                                        }
265
 
                                        catch( Vcard.ParseException e ) {
 
242
                                        catch( VCard.ParseException e ) {
266
243
                                                skipContact();
267
244
                                                if( !showContinue(
268
245
                                                        getText( R.string.error_vcf_parse ).toString()
269
 
                                                        + fileName +
270
 
                                                        getText( R.string.error_vcf_parse_line ).toString()
271
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
246
                                                        + fileName + "\n" + e.getMessage() ) )
272
247
                                                {
273
248
                                                        finish( ACTION_ABORT );
274
249
                                                }
275
250
 
276
 
                                                // Although we're continuing, we still need to abort
277
 
                                                // this vCard.  Further lines will be ignored until we
 
251
                                                // although we're continuing, we still need to abort
 
252
                                                // this vCard. Further lines will be ignored until we
278
253
                                                // get to another BEGIN:VCARD line.
279
 
                                                vcard = null;
 
254
                                                vCard = null;
280
255
                                        }
281
 
                                        catch( Vcard.SkipImportException e ) {
 
256
                                        catch( VCard.SkipContactException e ) {
282
257
                                                skipContact();
283
 
                                                // Abort this vCard.  Further lines will be ignored until
 
258
                                                // abort this vCard. Further lines will be ignored until
284
259
                                                // we get to another BEGIN:VCARD line.
285
 
                                                vcard = null;
 
260
                                                vCard = null;
286
261
                                        }
287
262
                                }
288
263
                        }
289
264
                }
290
265
        }
291
266
 
292
 
        class ContentLine
293
 
        {
294
 
                private ByteBuffer _buffer;
295
 
                private boolean _folded_next;
296
 
                private String _line;
297
 
 
298
 
                public ContentLine( ByteBuffer buffer, boolean folded_next )
299
 
                {
300
 
                        _buffer = buffer;
301
 
                        _folded_next = folded_next;
302
 
                        _line = null;
303
 
                }
304
 
 
305
 
                public ByteBuffer getBuffer()
306
 
                {
307
 
                        return _buffer;
308
 
                }
309
 
 
310
 
                public boolean doesNextLineLookFolded()
311
 
                {
312
 
                        return _folded_next;
313
 
                }
314
 
 
315
 
                public String getUsAsciiLine()
316
 
                {
317
 
                        // generated line and cache it
318
 
                        if( _line == null ) {
319
 
                                try {
320
 
                                        _line = new String( _buffer.array(), _buffer.position(),
321
 
                                                _buffer.limit() - _buffer.position(), "US-ASCII" );
322
 
                                }
323
 
                                catch( UnsupportedEncodingException e ) {
324
 
                                        // we know US-ASCII *is* supported, so appease the
325
 
                                        // compiler...
326
 
                                }
327
 
                        }
328
 
 
329
 
                        // return cached line
330
 
                        return _line;
331
 
                }
332
 
        }
333
 
 
334
 
        class ContentLineIterator implements Iterator< ContentLine >
 
267
        class ContentLineIterator implements Iterator< ByteBuffer >
335
268
        {
336
269
                protected byte[] _content = null;
337
270
                protected int _pos = 0;
338
 
                protected int _line = 0;
339
271
 
340
272
                public ContentLineIterator( byte[] content )
341
273
                {
349
281
                }
350
282
 
351
283
                @Override
352
 
                public ContentLine next()
 
284
                public ByteBuffer next()
353
285
                {
354
286
                        int initial_pos = _pos;
355
287
 
361
293
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
362
294
                                                _pos > initial_pos )? _pos - 1 : _pos;
363
295
                                        _pos++;
364
 
                                        _line++;
365
 
                                        return new ContentLine(
366
 
                                                ByteBuffer.wrap( _content, initial_pos,
367
 
                                                        to - initial_pos ),
368
 
                                                doesNextLineLookFolded() );
 
296
                                        return ByteBuffer.wrap( _content, initial_pos,
 
297
                                                to - initial_pos );
369
298
                                }
370
299
 
371
300
                        // we didn't find one, but were there bytes left?
372
301
                        if( _pos != initial_pos ) {
373
302
                                int to = _pos;
374
303
                                _pos++;
375
 
                                _line++;
376
 
                                return new ContentLine(
377
 
                                        ByteBuffer.wrap( _content, initial_pos,
378
 
                                                to - initial_pos ),
379
 
                                        doesNextLineLookFolded() );
 
304
                                return ByteBuffer.wrap( _content, initial_pos,
 
305
                                        to - initial_pos );
380
306
                        }
381
307
 
382
308
                        // no bytes left
394
320
                 * onto the end of this one?
395
321
                 * @return
396
322
                 */
397
 
                private boolean doesNextLineLookFolded()
 
323
                public boolean doesNextLineLookFolded()
398
324
                {
399
325
                        return _pos > 0 && _pos < _content.length &&
400
 
                                _content[ _pos - 1 ] == '\n' &&
401
 
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
402
 
                }
403
 
 
404
 
                public int getLineNumber()
405
 
                {
406
 
                        return _line;
 
326
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
407
327
                }
408
328
        }
409
329
 
410
 
        private class Vcard extends ContactData
 
330
        private class VCard extends ContactData
411
331
        {
412
332
                private final static int NAMELEVEL_NONE = 0;
413
 
                private final static int NAMELEVEL_N = 1;
 
333
                private final static int NAMELEVEL_ORG = 1;
414
334
                private final static int NAMELEVEL_FN = 2;
415
 
 
416
 
                private final static int MULTILINE_NONE = 0;
417
 
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
418
 
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
419
 
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
 
335
                private final static int NAMELEVEL_N = 3;
420
336
 
421
337
                private String _version = null;
422
 
                private Vector< ContentLine > _content_lines = null;
 
338
                private Vector< ByteBuffer > _buffers = null;
423
339
                private int _name_level = NAMELEVEL_NONE;
424
 
                private int _parser_multiline_state = MULTILINE_NONE;
 
340
                private boolean _parser_in_encoded_multiline = false;
 
341
                private boolean _parser_in_folded_multiline = false;
425
342
                private String _parser_current_name_and_params = null;
426
343
                private String _parser_buffered_value_so_far = "";
427
 
                private String _cached_organisation = null;
428
 
                private String _cached_title = null;
429
344
 
430
345
                protected class UnencodeResult
431
346
                {
461
376
 
462
377
                        public ParseException( int res )
463
378
                        {
464
 
                                super( VcardImporter.this.getText( res ).toString() );
 
379
                                super( VCFImporter.this.getText( res ).toString() );
465
380
                        }
466
381
                }
467
382
 
468
383
                @SuppressWarnings("serial")
469
 
                protected class SkipImportException extends Exception { }
 
384
                protected class SkipContactException extends Exception { }
470
385
 
471
 
                private String extractCollonPartFromLine( ContentLine content_line,
472
 
                        boolean former )
 
386
                private String extractCollonPartFromLine( ByteBuffer buffer,
 
387
                        String line, boolean former )
473
388
                {
 
389
                        String ret = null;
 
390
 
 
391
                        // get a US-ASCII version of the line for processing, unless we were
 
392
                        // supplied with one
 
393
                        if( line == null ) {
 
394
                                try {
 
395
                                        line = new String( buffer.array(), buffer.position(),
 
396
                                                buffer.limit() - buffer.position(), "US-ASCII" );
 
397
                                }
 
398
                                catch( UnsupportedEncodingException e ) {
 
399
                                        // we know US-ASCII is supported, so appease the compiler...
 
400
                                        line = "";
 
401
                                }
 
402
                        }
 
403
 
474
404
                        // split line into name and value parts and check to make sure we
475
405
                        // only got 2 parts and that the first part is not zero in length
476
 
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
 
406
                        String[] parts = line.split( ":", 2 );
477
407
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
478
 
                                return parts[ former? 0 : 1 ].trim();
479
 
 
480
 
                        return null;
481
 
                }
482
 
 
483
 
                private String extractNameAndParamsFromLine( ContentLine content_line )
484
 
                {
485
 
                        return extractCollonPartFromLine( content_line, true );
486
 
                }
487
 
 
488
 
                private String extractValueFromLine( ContentLine content_line )
489
 
                {
490
 
                        return extractCollonPartFromLine( content_line, false );
491
 
                }
492
 
 
493
 
                public void parseLine( ContentLine content_line )
494
 
                        throws ParseException, SkipImportException,
 
408
                                ret = parts[ former? 0 : 1 ];
 
409
 
 
410
                        return ret;
 
411
                }
 
412
 
 
413
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
 
414
                        String line )
 
415
                {
 
416
                        return extractCollonPartFromLine( buffer, line, true );
 
417
                }
 
418
 
 
419
                private String extractValueFromLine( ByteBuffer buffer, String line )
 
420
                {
 
421
                        return extractCollonPartFromLine( buffer, line, false );
 
422
                }
 
423
 
 
424
                public void parseLine( ByteBuffer buffer, String line,
 
425
                        boolean next_line_looks_folded )
 
426
                        throws ParseException, SkipContactException,
495
427
                        AbortImportException
496
428
                {
497
429
                        // do we have a version yet?
499
431
                        {
500
432
                                // tentatively get name and params from line
501
433
                                String name_and_params =
502
 
                                        extractNameAndParamsFromLine( content_line );
 
434
                                        extractNameAndParamsFromLine( buffer, line );
503
435
 
504
436
                                // is it a version line?
505
437
                                if( name_and_params != null &&
506
 
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
 
438
                                        name_and_params.equals( "VERSION" ) )
507
439
                                {
508
440
                                        // yes, get it!
509
 
                                        String value = extractValueFromLine( content_line );
510
 
                                        if( value == null || (
511
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
512
 
                                        {
 
441
                                        String value = extractValueFromLine( buffer, line );
 
442
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
513
443
                                                throw new ParseException( R.string.error_vcf_version );
514
 
                                        }
515
444
                                        _version = value;
516
445
 
517
446
                                        // parse any buffers we've been accumulating while we waited
518
447
                                        // for a version
519
 
                                        if( _content_lines != null )
520
 
                                                for( int i = 0; i < _content_lines.size(); i++ )
521
 
                                                        parseLine( _content_lines.get( i ) );
522
 
                                        _content_lines = null;
 
448
                                        if( _buffers != null )
 
449
                                                for( int i = 0; i < _buffers.size(); i++ )
 
450
                                                        parseLine( _buffers.get( i ), null,
 
451
                                                                i + 1 < _buffers.size() &&
 
452
                                                                _buffers.get( i + 1 ).hasRemaining() &&
 
453
                                                                _buffers.get( i + 1 ).get(
 
454
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
 
455
                                        _buffers = null;
523
456
                                }
524
457
                                else
525
458
                                {
526
459
                                        // no, so stash this line till we get a version
527
 
                                        if( _content_lines == null )
528
 
                                                _content_lines = new Vector< ContentLine >();
529
 
                                        _content_lines.add( content_line );
 
460
                                        if( _buffers == null )
 
461
                                                _buffers = new Vector< ByteBuffer >();
 
462
                                        _buffers.add( buffer );
530
463
                                }
531
464
                        }
532
465
                        else
533
466
                        {
534
467
                                // name and params and the position in the buffer where the
535
 
                                // "value" part of the line starts
 
468
                                // "value" part of the line start
536
469
                                String name_and_params;
537
470
                                int pos;
538
471
 
539
 
                                if( _parser_multiline_state != MULTILINE_NONE )
 
472
                                if( _parser_in_encoded_multiline ||
 
473
                                        _parser_in_folded_multiline )
540
474
                                {
541
475
                                        // if we're currently in a multi-line value, use the stored
542
476
                                        // property name and parameters
543
477
                                        name_and_params = _parser_current_name_and_params;
544
478
 
545
 
                                        // skip some initial line characters, depending on the type
546
 
                                        // of multi-line we're handling
547
 
                                        pos = content_line.getBuffer().position();
548
 
                                        switch( _parser_multiline_state )
549
 
                                        {
550
 
                                        case MULTILINE_FOLDED:
 
479
                                        pos = buffer.position();
 
480
 
 
481
                                        // for folded multi-lines, skip the single space at the
 
482
                                        // start of the next line
 
483
                                        if( _parser_in_folded_multiline )
551
484
                                                pos++;
552
 
                                                break;
553
 
                                        case MULTILINE_ENCODED:
554
 
                                                while( pos < content_line.getBuffer().limit() && (
555
 
                                                        content_line.getBuffer().get( pos ) == ' ' ||
556
 
                                                        content_line.getBuffer().get( pos ) == '\t' ) )
 
485
 
 
486
                                        // else, this must be an encoded multi-line, so skip any
 
487
                                        // whitespace we find at the start of the next line
 
488
                                        else
 
489
                                                while( pos < buffer.limit() && (
 
490
                                                        buffer.get( pos ) == ' ' ||
 
491
                                                        buffer.get( pos ) == '\t' ) )
557
492
                                                {
558
493
                                                        pos++;
559
494
                                                }
560
 
                                                break;
561
 
                                        default:
562
 
                                                // do nothing
563
 
                                        }
564
 
 
565
 
                                        // take us out of multi-line so that we can re-detect that
566
 
                                        // this line is a multi-line or not
567
 
                                        _parser_multiline_state = MULTILINE_NONE;
568
495
                                }
569
496
                                else
570
497
                                {
571
 
                                        // skip empty lines
572
 
                                        if( content_line.getUsAsciiLine().trim().length() == 0 )
573
 
                                                return;
574
 
 
575
498
                                        // get name and params from line, and since we're not
576
499
                                        // parsing a subsequent line in a multi-line, this should
577
500
                                        // not fail, or it's an error
578
501
                                        name_and_params =
579
 
                                                extractNameAndParamsFromLine( content_line );
 
502
                                                extractNameAndParamsFromLine( buffer, line );
580
503
                                        if( name_and_params == null )
581
504
                                                throw new ParseException(
582
505
                                                        R.string.error_vcf_malformed );
583
506
 
584
507
                                        // calculate how many chars to skip from beginning of line
585
508
                                        // so we skip the property "name:" part
586
 
                                        pos = content_line.getBuffer().position() +
587
 
                                                name_and_params.length() + 1;
 
509
                                        pos = buffer.position() + name_and_params.length() + 1;
588
510
 
589
511
                                        // reset the saved multi-line state
590
512
                                        _parser_current_name_and_params = name_and_params;
593
515
 
594
516
                                // get value from buffer, as raw bytes
595
517
                                ByteBuffer value;
596
 
                                value = ByteBuffer.wrap( content_line.getBuffer().array(), pos,
597
 
                                        content_line.getBuffer().limit() - pos );
 
518
                                value = ByteBuffer.wrap( buffer.array(), pos,
 
519
                                        buffer.limit() - pos );
598
520
 
599
521
                                // get parameter parts
600
522
                                String[] name_param_parts = name_and_params.split( ";", -1 );
601
523
                                for( int i = 0; i < name_param_parts.length; i++ )
602
524
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
603
525
 
604
 
                                // determine whether we care about this entry
605
 
                                final HashSet< String > interesting_fields =
606
 
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
607
 
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
608
 
                                ) );
609
 
                                boolean is_interesting_field =
610
 
                                        interesting_fields.contains(
611
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
612
 
 
613
526
                                // parse encoding parameter
614
527
                                String encoding = checkParam( name_param_parts, "ENCODING" );
615
 
                                if( encoding != null )
616
 
                                        encoding = encoding.toUpperCase( Locale.US );
617
 
                                if( is_interesting_field && encoding != null &&
618
 
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
619
 
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
620
 
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
 
528
                                if( encoding != null ) encoding = encoding.toUpperCase();
 
529
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
530
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
531
                                        //&& !encoding.equals( "BASE64" ) )
621
532
                                {
622
533
                                        throw new ParseException( R.string.error_vcf_encoding );
623
534
                                }
624
535
 
625
536
                                // parse charset parameter
626
537
                                String charset = checkParam( name_param_parts, "CHARSET" );
627
 
                                if( charset != null )
628
 
                                        charset = charset.toUpperCase( Locale.US );
629
 
                                if( charset != null &&
630
 
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
631
 
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
632
 
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
 
538
                                if( charset != null ) charset = charset.toUpperCase();
 
539
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
540
                                        !charset.equals( "ASCII" ) &&
 
541
                                        !charset.equals( "UTF-8" ) )
633
542
                                {
634
543
                                        throw new ParseException( R.string.error_vcf_charset );
635
544
                                }
637
546
                                // do unencoding (or default to a fake unencoding result with
638
547
                                // the raw string)
639
548
                                UnencodeResult unencoding_result = null;
640
 
                                if( encoding != null &&
641
 
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
642
 
                                {
 
549
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
643
550
                                        unencoding_result = unencodeQuotedPrintable( value );
644
 
                                }
645
 
//                              else if( encoding != null &&
646
 
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
647
 
//                              {
 
551
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
648
552
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
649
 
//                              }
650
553
                                if( unencoding_result != null ) {
651
554
                                        value = unencoding_result.getBuffer();
652
 
                                        if( unencoding_result.isAnotherLineRequired() )
653
 
                                                _parser_multiline_state = MULTILINE_ENCODED;
 
555
                                        _parser_in_encoded_multiline =
 
556
                                                unencoding_result.isAnotherLineRequired();
654
557
                                }
655
558
 
656
 
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
657
 
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
658
 
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
659
 
                                        ( charset != null && (
660
 
                                                charset.equalsIgnoreCase( "ASCII" ) ||
661
 
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
662
 
                                {
 
559
                                // convert 8-bit ASCII charset to US-ASCII
 
560
                                if( charset == null || charset.equals( "ASCII" ) ) {
663
561
                                        value = transcodeAsciiToUtf8( value );
 
562
                                        charset = "UTF-8";
664
563
                                }
665
564
 
666
 
                                // process charset (value is now in UTF-8)
 
565
                                // process charset
667
566
                                String string_value;
668
567
                                try {
669
568
                                        string_value = new String( value.array(), value.position(),
670
 
                                                value.limit() - value.position(), "UTF-8" );
 
569
                                                value.limit() - value.position(), charset );
671
570
                                } catch( UnsupportedEncodingException e ) {
672
571
                                        throw new ParseException( R.string.error_vcf_charset );
673
572
                                }
674
573
 
675
 
                                // for some entries that have semicolon-separated value parts,
676
 
                                // check to see if the value ends in an escape character, which
677
 
                                // indicates that we have a multi-line value
678
 
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
679
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
680
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
681
 
                                        doesStringEndInAnEscapeChar( string_value ) )
682
 
                                {
683
 
                                        _parser_multiline_state = MULTILINE_ESCAPED;
684
 
                                        string_value = string_value.substring( 0,
685
 
                                                string_value.length() - 1 );
686
 
                                }
687
 
 
688
 
                                // if we know we're not in an encoding-based multi-line, check
689
 
                                // to see if we're in a folded multi-line
690
 
                                if( _parser_multiline_state == MULTILINE_NONE &&
691
 
                                        content_line.doesNextLineLookFolded() )
692
 
                                {
693
 
                                        _parser_multiline_state = MULTILINE_FOLDED;
694
 
                                }
695
 
 
696
 
                                // handle multi-lines by buffering them and parsing them when we
697
 
                                // are processing the last line in a multi-line sequence
698
 
                                if( _parser_multiline_state != MULTILINE_NONE ) {
 
574
                                // now we know whether we're in an encoding multi-line,
 
575
                                // determine if we're in a v3 folded multi-line or not
 
576
                                _parser_in_folded_multiline = !_parser_in_encoded_multiline &&
 
577
                                        _version.equals( "3.0" ) && next_line_looks_folded;
 
578
 
 
579
                                // handle multi-line requests
 
580
                                if( _parser_in_encoded_multiline ||
 
581
                                        _parser_in_folded_multiline )
 
582
                                {
699
583
                                        _parser_buffered_value_so_far += string_value;
700
584
                                        return;
701
585
                                }
 
586
 
 
587
                                // add on buffered multi-line content
702
588
                                String complete_value =
703
 
                                        ( _parser_buffered_value_so_far + string_value ).trim();
 
589
                                        _parser_buffered_value_so_far + string_value;
704
590
 
705
591
                                // ignore empty values
706
592
                                if( complete_value.length() < 1 ) return;
707
593
 
708
594
                                // parse some properties
709
 
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
 
595
                                if( name_param_parts[ 0 ].equals( "N" ) )
710
596
                                        parseN( name_param_parts, complete_value );
711
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
 
597
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
712
598
                                        parseFN( name_param_parts, complete_value );
713
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
 
599
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
714
600
                                        parseORG( name_param_parts, complete_value );
715
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
716
 
                                        parseTITLE( name_param_parts, complete_value );
717
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
 
601
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
718
602
                                        parseTEL( name_param_parts, complete_value );
719
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
 
603
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
720
604
                                        parseEMAIL( name_param_parts, complete_value );
721
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
722
 
                                        parseADR( name_param_parts, complete_value );
723
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
724
 
                                        parseLABEL( name_param_parts, complete_value );
725
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
726
 
                                        parseNOTE( name_param_parts, complete_value );
727
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
728
 
                                        parseBDAY( name_param_parts, complete_value );
729
 
                        }
730
 
                }
731
 
 
732
 
                private boolean doesStringEndInAnEscapeChar( String string )
733
 
                {
734
 
                        // count the number of backslashes at the end of the string
735
 
                        int count = 0;
736
 
                        for( int a = string.length() - 1; a >= 0; a-- )
737
 
                                if( string.charAt( a ) == '\\' )
738
 
                                        count++;
739
 
                                else
740
 
                                        break;
741
 
 
742
 
                        // if there are an even number of backslashes then the final one
743
 
                        // doesn't count
744
 
                        return ( count & 1 ) == 1;
745
 
                }
746
 
 
747
 
                private String[] splitValueByCharacter( String value, char character )
748
 
                {
749
 
                        // split string in to parts by specified character
750
 
                        ArrayList< String > parts = new ArrayList< String >(
751
 
                                Arrays.asList( value.split( "" + character ) ) );
752
 
 
753
 
                        // go through parts
754
 
                        for( int a = 0; a < parts.size(); a++ )
755
 
                        {
756
 
                                String str = parts.get( a );
757
 
 
758
 
                                // Look for parts that end in an escape character, but ignore
759
 
                                // the final part.  We've already detected escape chars at the
760
 
                                // end of the final part in parseLine() and handled multi-lines
761
 
                                // accordingly.
762
 
                                if( a < parts.size() - 1 &&
763
 
                                        doesStringEndInAnEscapeChar( str ) )
764
 
                                {
765
 
                                        // append the escaped character, join the next part to this
766
 
                                        // part and remove the next part
767
 
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
768
 
                                                character + parts.get( a + 1 ) );
769
 
                                        parts.remove( a + 1 );
770
 
 
771
 
                                        // re-visit this part
772
 
                                        a--;
773
 
                                        continue;
774
 
                                }
775
 
 
776
 
                                // trim and replace string
777
 
                                str = str.trim();
778
 
                                parts.set( a, str );
779
 
                        }
780
 
 
781
 
                        String[] ret = new String[ parts.size() ];
782
 
                        return parts.toArray( ret );
783
 
                }
784
 
 
785
 
                private String unescapeValue( String value )
786
 
                {
787
 
                        StringBuilder ret = new StringBuilder( value.length() );
788
 
                        boolean in_escape = false;
789
 
                        for( int a = 0; a < value.length(); a++ )
790
 
                        {
791
 
                                int c = value.codePointAt( a );
792
 
 
793
 
                                // process a normal character
794
 
                                if( !in_escape ) {
795
 
                                        if( c == '\\' )
796
 
                                                in_escape = true;
797
 
                                        else
798
 
                                                ret.append( Character.toChars( c ) );
799
 
                                        continue;
800
 
                                }
801
 
 
802
 
                                // process an escape sequence
803
 
                                in_escape = false;
804
 
                                switch( c )
805
 
                                {
806
 
                                case 'T':
807
 
                                case 't':
808
 
                                        // add tab (invalid/non-standard, but accepted)
809
 
                                        ret.append( '\t' );
810
 
                                        break;
811
 
                                case 'N':
812
 
                                case 'n':
813
 
                                        // add newline
814
 
                                        ret.append( '\n' );
815
 
                                        break;
816
 
                                case '\\':
817
 
                                case ',':
818
 
                                case ';':
819
 
                                        // add escaped character
820
 
                                        ret.append( Character.toChars( c ) );
821
 
                                        break;
822
 
                                default:
823
 
                                        // unknown escape sequence, so add it unescaped
824
 
                                        // (invalid/non-standard, but accepted)
825
 
                                        ret.append( "\\" );
826
 
                                        ret.append( Character.toChars( c ) );
827
 
                                        break;
828
 
                                }
829
 
                        }
830
 
 
831
 
                        return ret.toString();
 
605
                        }
832
606
                }
833
607
 
834
608
                private void parseN( String[] params, String value )
 
609
                        throws ParseException, SkipContactException,
 
610
                        AbortImportException
835
611
                {
836
612
                        // already got a better name?
837
613
                        if( _name_level >= NAMELEVEL_N ) return;
838
614
 
839
615
                        // get name parts
840
 
                        String[] name_parts = splitValueByCharacter( value, ';' );
 
616
                        String[] name_parts = value.split( ";" );
 
617
                        for( int i = 0; i < name_parts.length; i++ )
 
618
                                name_parts[ i ] = name_parts[ i ].trim();
841
619
 
842
620
                        // build name
843
621
                        value = "";
844
 
                        final int[] part_order = { 3, 1, 2, 0, 4 };
845
 
                        for( int a = 0; a < part_order.length; a++ )
846
 
                                if( name_parts.length > part_order[ a ] &&
847
 
                                        name_parts[ part_order[ a ] ].length() > 0 )
848
 
                                {
849
 
                                        // split this part in to it's comma-separated bits
850
 
                                        String[] name_part_parts = splitValueByCharacter(
851
 
                                                name_parts[ part_order[ a ] ], ',' );
852
 
                                        for( int b = 0; b < name_part_parts.length; b++ )
853
 
                                                if( name_part_parts[ b ].length() > 0 )
854
 
                                                {
855
 
                                                        if( value.length() > 0 ) value += " ";
856
 
                                                        value += name_part_parts[ b ];
857
 
                                                }
858
 
                                }
 
622
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
 
623
                                value += name_parts[ 1 ];
 
624
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
 
625
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
859
626
 
860
627
                        // set name
861
 
                        setName( unescapeValue( value ) );
 
628
                        setName( value );
862
629
                        _name_level = NAMELEVEL_N;
 
630
 
 
631
                        // check now to see if we need to import this contact (to avoid
 
632
                        // parsing the rest of the vCard unnecessarily)
 
633
                        if( !isImportRequired( getName() ) )
 
634
                                throw new SkipContactException();
863
635
                }
864
636
 
865
637
                private void parseFN( String[] params, String value )
 
638
                        throws ParseException, SkipContactException
866
639
                {
867
640
                        // already got a better name?
868
641
                        if( _name_level >= NAMELEVEL_FN ) return;
869
642
 
870
643
                        // set name
871
 
                        setName( unescapeValue( value ) );
 
644
                        setName( value );
872
645
                        _name_level = NAMELEVEL_FN;
873
646
                }
874
647
 
875
648
                private void parseORG( String[] params, String value )
 
649
                        throws ParseException, SkipContactException
876
650
                {
 
651
                        // already got a better name?
 
652
                        if( _name_level >= NAMELEVEL_ORG ) return;
 
653
 
877
654
                        // get org parts
878
 
                        String[] org_parts = splitValueByCharacter( value, ';' );
879
 
                        if( org_parts == null || org_parts.length < 1 ) return;
880
 
 
881
 
                        // build organisation name
882
 
                        StringBuilder builder = new StringBuilder(
883
 
                                String.valueOf( org_parts[ 0 ] ) );
884
 
                        for( int a = 1; a < org_parts.length; a++ )
885
 
                                builder.append( ", " ).append( org_parts[ a ] );
886
 
                        String organisation = unescapeValue( builder.toString() );
887
 
 
888
 
                        // set organisation name (using a title we've previously found)
889
 
                        addOrganisation( organisation, _cached_title, true );
890
 
 
891
 
                        // if we've not previously found a title, store this organisation
892
 
                        // name (we'll need it when we find a title to update the
893
 
                        // organisation, by name), else if we *have* previously found a
894
 
                        // title, clear it (since we just used it)
895
 
                        if( _cached_title == null )
896
 
                                _cached_organisation = organisation;
897
 
                        else
898
 
                                _cached_title = null;
899
 
                }
900
 
 
901
 
                private void parseTITLE( String[] params, String value )
902
 
                {
903
 
                        value = unescapeValue( value );
904
 
 
905
 
                        // if we previously had an organisation, look it up and append this
906
 
                        // title to it
907
 
                        if( _cached_organisation != null && hasOrganisations() ) {
908
 
                                HashMap< String, ExtraDetail > datas = getOrganisations();
909
 
                                ExtraDetail detail = datas.get( _cached_organisation );
910
 
                                if( detail != null )
911
 
                                        detail.setExtra( value );
912
 
                        }
913
 
 
914
 
                        // same as when handling organisation, if we've not previously found
915
 
                        // an organisation we store this title, else we clear it (since we
916
 
                        // just appended this title to it)
917
 
                        if( _cached_organisation == null )
918
 
                                _cached_title = value;
919
 
                        else
920
 
                                _cached_organisation = null;
 
655
                        String[] org_parts = value.split( ";" );
 
656
                        for( int i = 0; i < org_parts.length; i++ )
 
657
                                org_parts[ i ] = org_parts[ i ].trim();
 
658
 
 
659
                        // build name
 
660
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
 
661
                                value = org_parts[ 1 ];
 
662
                        else
 
663
                                value = org_parts[ 0 ];
 
664
 
 
665
                        // set name
 
666
                        setName( value );
 
667
                        _name_level = NAMELEVEL_ORG;
921
668
                }
922
669
 
923
670
                private void parseTEL( String[] params, String value )
 
671
                        throws ParseException
924
672
                {
925
673
                        if( value.length() == 0 ) return;
926
674
 
929
677
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
930
678
 
931
679
                        // here's the logic...
932
 
                        boolean is_preferred = types.contains( "PREF" );
933
 
                        int type;
 
680
                        boolean preferred = types.contains( "PREF" );
 
681
                        int type = PhonesColumns.TYPE_MOBILE;
 
682
                        if( types.contains( "VOICE" ) )
 
683
                                if( types.contains( "WORK" ) )
 
684
                                        type = PhonesColumns.TYPE_WORK;
 
685
                                else
 
686
                                        type = PhonesColumns.TYPE_HOME;
 
687
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
688
                                type = PhonesColumns.TYPE_MOBILE;
934
689
                        if( types.contains( "FAX" ) )
935
690
                                if( types.contains( "HOME" ) )
936
 
                                        type = TYPE_FAX_HOME;
 
691
                                        type = PhonesColumns.TYPE_FAX_HOME;
937
692
                                else
938
 
                                        type = TYPE_FAX_WORK;
939
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
940
 
                                type = TYPE_MOBILE;
941
 
                        else if( types.contains( "PAGER" ) )
942
 
                                type = TYPE_PAGER;
943
 
                        else if( types.contains( "WORK" ) )
944
 
                                type = TYPE_WORK;
945
 
                        else
946
 
                                type = TYPE_HOME;
 
693
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
694
                        if( types.contains( "PAGER" ) )
 
695
                                type = PhonesColumns.TYPE_PAGER;
947
696
 
948
697
                        // add phone number
949
 
                        addNumber( value, type, is_preferred );
 
698
                        addPhone( value, type, preferred );
950
699
                }
951
700
 
952
701
                public void parseEMAIL( String[] params, String value )
 
702
                        throws ParseException
953
703
                {
954
704
                        if( value.length() == 0 ) return;
955
705
 
956
706
                        Set< String > types = extractTypes( params, Arrays.asList(
957
707
                                "PREF", "WORK", "HOME", "INTERNET" ) );
958
708
 
959
 
                        // add email address
960
 
                        boolean is_preferred = types.contains( "PREF" );
961
 
                        int type;
962
 
                        if( types.contains( "WORK" ) )
963
 
                                type = TYPE_WORK;
964
 
                        else
965
 
                                type = TYPE_HOME;
966
 
 
967
 
                        addEmail( unescapeValue( value ), type, is_preferred );
968
 
                }
969
 
 
970
 
                private void parseADR( String[] params, String value )
971
 
                {
972
 
                        // get address parts
973
 
                        String[] adr_parts = splitValueByCharacter( value, ';' );
974
 
 
975
 
                        // build address
976
 
                        value = "";
977
 
                        for( int a = 0; a < adr_parts.length; a++ )
978
 
                                if( adr_parts[ a ].length() > 0 )
979
 
                                {
980
 
                                        // version 3.0 vCards allow further splitting by comma
981
 
                                        if( _version.equals( "3.0" ) )
982
 
                                        {
983
 
                                                // split this part in to it's comma-separated bits and
984
 
                                                // add them on individual lines
985
 
                                                String[] adr_part_parts =
986
 
                                                        splitValueByCharacter( adr_parts[ a ], ',' );
987
 
                                                for( int b = 0; b < adr_part_parts.length; b++ )
988
 
                                                        if( adr_part_parts[ b ].length() > 0 )
989
 
                                                        {
990
 
                                                                if( value.length() > 0 ) value += "\n";
991
 
                                                                value += adr_part_parts[ b ];
992
 
                                                        }
993
 
                                        }
994
 
                                        else
995
 
                                        {
996
 
                                                // add this part on an individual line
997
 
                                                if( value.length() > 0 ) value += "\n";
998
 
                                                value += adr_parts[ a ];
999
 
                                        }
1000
 
                                }
1001
 
 
1002
 
                        Set< String > types = extractTypes( params, Arrays.asList(
1003
 
                                "PREF", "WORK", "HOME" ) );
1004
 
 
1005
 
                        // add address
1006
 
                        int type;
1007
 
                        if( types.contains( "WORK" ) )
1008
 
                                type = TYPE_WORK;
1009
 
                        else
1010
 
                                type = TYPE_HOME;
1011
 
 
1012
 
                        addAddress( unescapeValue( value ), type );
1013
 
                }
1014
 
 
1015
 
                private void parseLABEL( String[] params, String value )
1016
 
                {
1017
 
                        Set< String > types = extractTypes( params, Arrays.asList(
1018
 
                                "PREF", "WORK", "HOME" ) );
1019
 
 
1020
 
                        // add address
1021
 
                        int type;
1022
 
                        if( types.contains( "WORK" ) )
1023
 
                                type = TYPE_WORK;
1024
 
                        else
1025
 
                                type = TYPE_HOME;
1026
 
 
1027
 
                        addAddress( unescapeValue( value ), type );
1028
 
                }
1029
 
 
1030
 
                private void parseNOTE( String[] params, String value )
1031
 
                {
1032
 
                        addNote( unescapeValue( value ) );
1033
 
                }
1034
 
 
1035
 
                private void parseBDAY( String[] params, String value )
1036
 
                {
1037
 
                        setBirthday( value );
1038
 
                }
1039
 
 
1040
 
                public void finaliseVcard()
1041
 
                        throws ParseException, ContactNotIdentifiableException
 
709
                        // here's the logic...
 
710
                        boolean preferred = types.contains( "PREF" );
 
711
                        if( types.contains( "WORK" ) )
 
712
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
 
713
                        else
 
714
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
 
715
                }
 
716
 
 
717
                public void finaliseParsing()
 
718
                        throws ParseException, SkipContactException,
 
719
                        AbortImportException
1042
720
                {
1043
721
                        // missing version (and data is present)
1044
 
                        if( _version == null && _content_lines != null )
 
722
                        if( _version == null && _buffers != null )
1045
723
                                throw new ParseException( R.string.error_vcf_malformed );
1046
724
 
1047
 
                        // finalise the parent class
1048
 
                        finalise();
 
725
                        //  missing name properties?
 
726
                        if( _name_level == NAMELEVEL_NONE )
 
727
                                throw new ParseException( R.string.error_vcf_noname );
 
728
 
 
729
                        // check if we should import this one? If we've already got an 'N'-
 
730
                        // type name, this will already have been done by parseN() so we
 
731
                        // mustn't do this here (or it could prompt twice!)
 
732
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
 
733
                                throw new SkipContactException();
1049
734
                }
1050
735
 
1051
 
                /**
1052
 
                 * Amongst the params, find the value of the first, only, of any with
1053
 
                 * the specified name.
1054
 
                 *
1055
 
                 * @param params
1056
 
                 * @param name
1057
 
                 * @return a value, or null
1058
 
                 */
1059
736
                private String checkParam( String[] params, String name )
1060
737
                {
1061
 
                        String[] res = checkParams( params, name );
1062
 
                        return res.length > 0? res[ 0 ] : null;
1063
 
                }
1064
 
 
1065
 
                /**
1066
 
                 * Amongst the params, find the values of any with the specified name.
1067
 
                 *
1068
 
                 * @param params
1069
 
                 * @param name
1070
 
                 * @return an array of values, or null
1071
 
                 */
1072
 
                private String[] checkParams( String[] params, String name )
1073
 
                {
1074
 
                        HashSet< String > ret = new HashSet< String >();
1075
 
 
1076
738
                        Pattern p = Pattern.compile(
1077
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
1078
 
                                Pattern.CASE_INSENSITIVE );
 
739
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1079
740
                        for( int i = 0; i < params.length; i++ ) {
1080
741
                                Matcher m = p.matcher( params[ i ] );
1081
742
                                if( m.matches() )
1082
 
                                        ret.add( m.group( 2 ) );
 
743
                                        return m.group( 2 );
1083
744
                        }
1084
 
 
1085
 
                        return (String[]) ret.toArray( new String[ ret.size() ] );
 
745
                        return null;
1086
746
                }
1087
747
 
1088
 
                /**
1089
 
                 * Amongst the params, return any type values present.  For v2.1 vCards,
1090
 
                 * those types are just parameters.  For v3.0, they are prefixed with
1091
 
                 * "TYPE=".  There may also be multiple type parameters.
1092
 
                 *
1093
 
                 * @param params an array of params to look for types in
1094
 
                 * @param valid_types an list of upper-case type values to look for
1095
 
                 * @return a set of present type values
1096
 
                 */
1097
748
                private Set< String > extractTypes( String[] params,
1098
749
                        List< String > valid_types )
1099
750
                {
1100
751
                        HashSet< String > types = new HashSet< String >();
1101
752
 
1102
753
                        // get 3.0-style TYPE= param
1103
 
                        String type_params[] = checkParams( params, "TYPE" );
1104
 
                        for( int a = 0; a < type_params.length; a++ )
1105
 
                        {
1106
 
                                // check for a comma-separated list of types (why? I don't think
1107
 
                                // this is in the specs!)
1108
 
                                String[] parts = type_params[ a ].split( "," );
1109
 
                                for( int i = 0; i < parts.length; i++ ) {
1110
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
1111
 
                                        if( valid_types.contains( ucpart ) )
1112
 
                                                types.add( ucpart );
1113
 
                                }
 
754
                        String type_param;
 
755
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
756
                                String[] parts = type_param.split( "," );
 
757
                                for( int i = 0; i < parts.length; i++ )
 
758
                                        if( valid_types.contains( parts[ i ] ) )
 
759
                                                types.add( parts[ i ] );
1114
760
                        }
1115
761
 
1116
762
                        // get 2.1-style type param
1117
763
                        if( _version.equals( "2.1" ) ) {
1118
 
                                for( int i = 1; i < params.length; i++ ) {
1119
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
1120
 
                                        if( valid_types.contains( ucparam ) )
1121
 
                                                types.add( ucparam );
1122
 
                                }
 
764
                                for( int i = 1; i < params.length; i++ )
 
765
                                        if( valid_types.contains( params[ i ] ) )
 
766
                                                types.add( params[ i ] );
1123
767
                        }
1124
768
 
1125
769
                        return types;
1147
791
                                else if( ch == '=' && i == in.limit() - 1 )
1148
792
                                {
1149
793
                                        // we found a '=' at the end of a line signifying a multi-
1150
 
                                        // line string, so we don't add it
 
794
                                        // line string, so we don't add it.
1151
795
                                        another = true;
1152
796
                                        continue;
1153
797
                                }