/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-05-02 18:28:24 UTC
  • Revision ID: edam@waxworlds.org-20110502182824-acgdi3qfxfzqgely
- fixed logic for vcard field types (home, work, cell, etc) so it works
- updated NEWS and TODO
- rewrote most of ContactsCache, including a new ContactIdentifier class to identify contacts in the cache and new cache building code
- contacts now identified in the same way that Andoid displays them (by name, or organisation, or number, or email, in that order)
- propper handling and support for organisations and titles
- validation of imported contact now done by Importer, not VcfImporter
- separated sanitisation and normalisation (for cache lookups)
- generacised PhoneData, EmailData and AddressData classes
- ContactData is now aware of primary numbers, emails and organisations (defaults to the first prefrred one seen, or the first one seen where none is preferred)

Show diffs side-by-side

added added

removed removed

1
 
package org.waxworlds.importcontacts;
 
1
/*
 
2
 * VCFImporter.java
 
3
 *
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of the Import Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
 
 
24
package org.waxworlds.edam.importcontacts;
2
25
 
3
26
import java.io.BufferedReader;
4
27
import java.io.File;
 
28
import java.io.FileInputStream;
5
29
import java.io.FileNotFoundException;
6
30
import java.io.FileReader;
7
31
import java.io.FilenameFilter;
8
32
import java.io.IOException;
9
33
import java.io.UnsupportedEncodingException;
 
34
import java.nio.ByteBuffer;
 
35
import java.util.ArrayList;
10
36
import java.util.Arrays;
 
37
import java.util.HashMap;
11
38
import java.util.HashSet;
 
39
import java.util.Iterator;
12
40
import java.util.List;
 
41
import java.util.NoSuchElementException;
13
42
import java.util.Set;
14
43
import java.util.Vector;
15
44
import java.util.regex.Matcher;
16
45
import java.util.regex.Pattern;
17
46
 
18
 
import org.waxworlds.importcontacts.Importer.AbortImportException;
 
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
19
48
 
20
49
import android.content.SharedPreferences;
21
50
import android.provider.Contacts;
44
73
                try
45
74
                {
46
75
                        // open directory
47
 
                        String location = prefs.getString( "location", "" );
48
 
                        File dir = new File( location );
49
 
                        if( !dir.exists() || !dir.isDirectory() )
 
76
                        String path = "/sdcard" + prefs.getString( "location", "/" );
 
77
                        File file = new File( path );
 
78
                        if( !file.exists() )
50
79
                                showError( R.string.error_locationnotfound );
51
80
 
52
 
                        // get files
53
 
                        class VCardFilter implements FilenameFilter {
54
 
                            public boolean accept( File dir, String name ) {
55
 
                                return name.toLowerCase().endsWith( ".vcf" );
56
 
                            }
57
 
                        }
58
 
                        files = dir.listFiles( new VCardFilter() );
 
81
                        // directory, or file?
 
82
                        if( file.isDirectory() )
 
83
                        {
 
84
                                // get files
 
85
                                class VCardFilter implements FilenameFilter {
 
86
                                        public boolean accept( File dir, String name ) {
 
87
                                                return name.toLowerCase().endsWith( ".vcf" );
 
88
                                        }
 
89
                                }
 
90
                                files = file.listFiles( new VCardFilter() );
 
91
                        }
 
92
                        else
 
93
                        {
 
94
                                // use just this file
 
95
                                files = new File[ 1 ];
 
96
                                files[ 0 ] = file;
 
97
                        }
59
98
                }
60
99
                catch( SecurityException e ) {
61
100
                        showError( R.string.error_locationpermissions );
87
126
                {
88
127
                        // open file
89
128
                        BufferedReader reader = new BufferedReader(
90
 
                                        new FileReader( file ) );
 
129
                                new FileReader( file ) );
91
130
 
92
131
                        // read
93
132
                        String line;
96
135
                        {
97
136
                                if( !inVCard ) {
98
137
                                        // look for vcard beginning
99
 
                                        if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
 
138
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
100
139
                                                inVCard = true;
101
140
                                                _vCardCount++;
102
141
                                        }
103
142
                                }
104
 
                                else if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
 
143
                                else if( line.matches( "^END:VCARD" ) )
105
144
                                        inVCard = false;
106
145
                        }
107
146
 
108
147
                }
109
148
                catch( FileNotFoundException e ) {
110
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
149
                        showError( getText( R.string.error_filenotfound ) +
 
150
                                file.getName() );
111
151
                }
112
152
                catch( IOException e ) {
113
153
                        showError( getText( R.string.error_ioerror ) + file.getName() );
116
156
 
117
157
        private void importVCardFile( File file ) throws AbortImportException
118
158
        {
 
159
                // check file is good
 
160
                if( !file.exists() )
 
161
                        showError( getText( R.string.error_filenotfound ) +
 
162
                                file.getName() );
 
163
                if( file.length() == 0 )
 
164
                        showError( getText( R.string.error_fileisempty ) +
 
165
                                file.getName() );
 
166
 
119
167
                try
120
168
                {
121
 
                        // open file
122
 
                        BufferedReader reader = new BufferedReader(
123
 
                                        new FileReader( file ) );
124
 
 
125
 
                        // read
126
 
                        StringBuffer content = new StringBuffer();
127
 
                        String line;
128
 
                        while( ( line = reader.readLine() ) != null )
129
 
                                content.append( line ).append( "\n" );
130
 
 
131
 
                        importVCardFileContent( content.toString(), file.getName() );
 
169
                        // open/read file
 
170
                        FileInputStream istream = new FileInputStream( file );
 
171
                        byte[] content = new byte[ (int)file.length() ];
 
172
                        istream.read( content );
 
173
 
 
174
                        // import
 
175
                        importVCardFileContent( content, file.getName() );
132
176
                }
133
177
                catch( FileNotFoundException e ) {
134
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
178
                        showError( getText( R.string.error_filenotfound ) +
 
179
                                file.getName() );
135
180
                }
136
181
                catch( IOException e ) {
137
182
                        showError( getText( R.string.error_ioerror ) + file.getName() );
138
183
                }
139
184
        }
140
185
 
141
 
        private void importVCardFileContent( String content, String fileName )
142
 
                        throws AbortImportException
 
186
        private void importVCardFileContent( byte[] content, String fileName )
 
187
                throws AbortImportException
143
188
        {
144
 
                // unfold RFC2425 section 5.8.1 folded lines, except that we must also
145
 
                // handle embedded Quoted-Printable encodings that have a trailing '='.
146
 
                // So we remove these first before doing RFC2425 unfolding.
147
 
                content = content.replaceAll( "=\n[ \\t]", "" )
148
 
                                .replaceAll( "\n[ \\t]", "" );
149
 
 
150
 
                // get lines and parse them
151
 
                String[] lines = content.split( "\n" );
 
189
                // go through lines
152
190
                VCard vCard = null;
153
 
                for( int i = 0; i < lines.length; i++ )
 
191
                ContentLineIterator cli = new ContentLineIterator( content );
 
192
                while( cli.hasNext() )
154
193
                {
155
 
                        String line = lines[ i ];
 
194
                        ByteBuffer buffer = cli.next();
 
195
 
 
196
                        // get a US-ASCII version of the line for processing
 
197
                        String line;
 
198
                        try {
 
199
                                line = new String( buffer.array(), buffer.position(),
 
200
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
201
                        }
 
202
                        catch( UnsupportedEncodingException e ) {
 
203
                                // we know US-ASCII is supported, so appease the compiler...
 
204
                                line = "";
 
205
                        }
156
206
 
157
207
                        if( vCard == null ) {
158
208
                                // look for vcard beginning
159
 
                                if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
 
209
                                if( line.matches( "^BEGIN:VCARD" ) ) {
160
210
                                        setProgress( ++_progress );
161
211
                                        vCard = new VCard();
162
212
                                }
163
213
                        }
164
214
                        else {
165
215
                                // look for vcard content or ending
166
 
                                if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
 
216
                                if( line.matches( "^END:VCARD" ) )
167
217
                                {
168
218
                                        // store vcard and do away with it
169
219
                                        try {
173
223
                                        catch( VCard.ParseException e ) {
174
224
                                                skipContact();
175
225
                                                if( !showContinue(
176
 
                                                                getText( R.string.error_vcf_parse ).toString()
177
 
                                                                + fileName + "\n" + e.getMessage() ) )
 
226
                                                        getText( R.string.error_vcf_parse ).toString()
 
227
                                                        + fileName + "\n" + e.getMessage() ) )
 
228
                                                {
178
229
                                                        finish( ACTION_ABORT );
 
230
                                                }
179
231
                                        }
180
232
                                        catch( VCard.SkipContactException e ) {
181
233
                                                skipContact();
187
239
                                {
188
240
                                        // try giving the line to the vcard
189
241
                                        try {
190
 
                                                vCard.parseLine( line );
 
242
                                                vCard.parseLine( buffer, line,
 
243
                                                        cli.doesNextLineLookFolded() );
191
244
                                        }
192
245
                                        catch( VCard.ParseException e ) {
193
246
                                                skipContact();
194
247
                                                if( !showContinue(
195
 
                                                                getText( R.string.error_vcf_parse ).toString()
196
 
                                                                + fileName + "\n" + e.getMessage() ) )
 
248
                                                        getText( R.string.error_vcf_parse ).toString()
 
249
                                                        + fileName + "\n" + e.getMessage() ) )
 
250
                                                {
197
251
                                                        finish( ACTION_ABORT );
 
252
                                                }
198
253
 
199
254
                                                // although we're continuing, we still need to abort
200
255
                                                // this vCard. Further lines will be ignored until we
212
267
                }
213
268
        }
214
269
 
 
270
        class ContentLineIterator implements Iterator< ByteBuffer >
 
271
        {
 
272
                protected byte[] _content = null;
 
273
                protected int _pos = 0;
 
274
 
 
275
                public ContentLineIterator( byte[] content )
 
276
                {
 
277
                        _content = content;
 
278
                }
 
279
 
 
280
                @Override
 
281
                public boolean hasNext()
 
282
                {
 
283
                        return _pos < _content.length;
 
284
                }
 
285
 
 
286
                @Override
 
287
                public ByteBuffer next()
 
288
                {
 
289
                        int initial_pos = _pos;
 
290
 
 
291
                        // find newline
 
292
                        for( ; _pos < _content.length; _pos++ )
 
293
                                if( _content[ _pos ] == '\n' )
 
294
                                {
 
295
                                        // adjust for a \r preceding the \n
 
296
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
 
297
                                                _pos > initial_pos )? _pos - 1 : _pos;
 
298
                                        _pos++;
 
299
                                        return ByteBuffer.wrap( _content, initial_pos,
 
300
                                                to - initial_pos );
 
301
                                }
 
302
 
 
303
                        // we didn't find one, but were there bytes left?
 
304
                        if( _pos != initial_pos ) {
 
305
                                int to = _pos;
 
306
                                _pos++;
 
307
                                return ByteBuffer.wrap( _content, initial_pos,
 
308
                                        to - initial_pos );
 
309
                        }
 
310
 
 
311
                        // no bytes left
 
312
                        throw new NoSuchElementException();
 
313
                }
 
314
 
 
315
                @Override
 
316
                public void remove()
 
317
                {
 
318
                        throw new UnsupportedOperationException();
 
319
                }
 
320
 
 
321
                /**
 
322
                 * Does the next line, if there is one, look like it should be folded
 
323
                 * onto the end of this one?
 
324
                 * @return
 
325
                 */
 
326
                public boolean doesNextLineLookFolded()
 
327
                {
 
328
                        return _pos > 0 && _pos < _content.length &&
 
329
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
 
330
                }
 
331
        }
 
332
 
215
333
        private class VCard extends ContactData
216
334
        {
217
335
                private final static int NAMELEVEL_NONE = 0;
218
 
                private final static int NAMELEVEL_ORG = 1;
219
 
                private final static int NAMELEVEL_FN = 2;
220
 
                private final static int NAMELEVEL_N = 3;
 
336
                private final static int NAMELEVEL_FN = 1;
 
337
                private final static int NAMELEVEL_N = 2;
 
338
 
 
339
                private final static int MULTILINE_NONE = 0;
 
340
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
 
341
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
 
342
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
221
343
 
222
344
                private String _version = null;
223
 
                private Vector< String > _lines = null;
224
 
                private int _nameLevel = NAMELEVEL_NONE;
225
 
 
 
345
                private Vector< ByteBuffer > _buffers = null;
 
346
                private int _name_level = NAMELEVEL_NONE;
 
347
                private int _parser_multiline_state = MULTILINE_NONE;
 
348
                private String _parser_current_name_and_params = null;
 
349
                private String _parser_buffered_value_so_far = "";
 
350
                private String _cached_organisation = null;
 
351
                private String _cached_title = null;
 
352
 
 
353
                protected class UnencodeResult
 
354
                {
 
355
                        private boolean _another_line_required;
 
356
                        private ByteBuffer _buffer;
 
357
 
 
358
                        public UnencodeResult( boolean another_line_required,
 
359
                                ByteBuffer buffer )
 
360
                        {
 
361
                                _another_line_required = another_line_required;
 
362
                                _buffer = buffer;
 
363
                        }
 
364
 
 
365
                        public boolean isAnotherLineRequired()
 
366
                        {
 
367
                                return _another_line_required;
 
368
                        }
 
369
 
 
370
                        public ByteBuffer getBuffer()
 
371
                        {
 
372
                                return _buffer;
 
373
                        }
 
374
                }
 
375
 
 
376
                @SuppressWarnings("serial")
226
377
                protected class ParseException extends Exception
227
378
                {
 
379
                        @SuppressWarnings("unused")
228
380
                        public ParseException( String error )
229
381
                        {
230
382
                                super( error );
236
388
                        }
237
389
                }
238
390
 
 
391
                @SuppressWarnings("serial")
239
392
                protected class SkipContactException extends Exception { }
240
393
 
241
 
                public void parseLine( String line )
242
 
                                throws ParseException, SkipContactException,
243
 
                                AbortImportException
244
 
                {
245
 
                        // get property halves
246
 
                        String[] props = line.split( ":" );
247
 
                        for( int i = 0; i < props.length; i++ )
248
 
                                props[ i ] = props[ i ].trim();
249
 
                        if( props.length < 2 ||
250
 
                                        props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
251
 
                                throw new ParseException( R.string.error_vcf_malformed );
252
 
 
 
394
                private String extractCollonPartFromLine( ByteBuffer buffer,
 
395
                        String line, boolean former )
 
396
                {
 
397
                        String ret = null;
 
398
 
 
399
                        // get a US-ASCII version of the line for processing, unless we were
 
400
                        // supplied with one
 
401
                        if( line == null ) {
 
402
                                try {
 
403
                                        line = new String( buffer.array(), buffer.position(),
 
404
                                                buffer.limit() - buffer.position(), "US-ASCII" );
 
405
                                }
 
406
                                catch( UnsupportedEncodingException e ) {
 
407
                                        // we know US-ASCII is supported, so appease the compiler...
 
408
                                        line = "";
 
409
                                }
 
410
                        }
 
411
 
 
412
                        // split line into name and value parts and check to make sure we
 
413
                        // only got 2 parts and that the first part is not zero in length
 
414
                        String[] parts = line.split( ":", 2 );
 
415
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
 
416
                                ret = parts[ former? 0 : 1 ];
 
417
 
 
418
                        return ret;
 
419
                }
 
420
 
 
421
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
 
422
                        String line )
 
423
                {
 
424
                        return extractCollonPartFromLine( buffer, line, true );
 
425
                }
 
426
 
 
427
                private String extractValueFromLine( ByteBuffer buffer, String line )
 
428
                {
 
429
                        return extractCollonPartFromLine( buffer, line, false );
 
430
                }
 
431
 
 
432
                public void parseLine( ByteBuffer buffer, String line,
 
433
                        boolean next_line_looks_folded )
 
434
                        throws ParseException, SkipContactException,
 
435
                        AbortImportException
 
436
                {
 
437
                        // do we have a version yet?
253
438
                        if( _version == null )
254
439
                        {
255
 
                                if( props[ 0 ].equals( "VERSION" ) )
 
440
                                // tentatively get name and params from line
 
441
                                String name_and_params =
 
442
                                        extractNameAndParamsFromLine( buffer, line );
 
443
 
 
444
                                // is it a version line?
 
445
                                if( name_and_params != null &&
 
446
                                        name_and_params.equals( "VERSION" ) )
256
447
                                {
257
 
                                        // get version
258
 
                                        if( !props[ 1 ].equals( "2.1" ) &&
259
 
                                                        !props[ 1 ].equals( "3.0" ) )
 
448
                                        // yes, get it!
 
449
                                        String value = extractValueFromLine( buffer, line );
 
450
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
260
451
                                                throw new ParseException( R.string.error_vcf_version );
261
 
                                        _version = props[ 1 ];
 
452
                                        _version = value;
262
453
 
263
 
                                        // parse any other lines we've accumulated so far
264
 
                                        if( _lines != null )
265
 
                                                for( int i = 0; i < _lines.size(); i++ )
266
 
                                                        parseLine( _lines.get( i ) );
267
 
                                        _lines = null;
 
454
                                        // parse any buffers we've been accumulating while we waited
 
455
                                        // for a version
 
456
                                        if( _buffers != null )
 
457
                                                for( int i = 0; i < _buffers.size(); i++ )
 
458
                                                        parseLine( _buffers.get( i ), null,
 
459
                                                                i + 1 < _buffers.size() &&
 
460
                                                                _buffers.get( i + 1 ).hasRemaining() &&
 
461
                                                                _buffers.get( i + 1 ).get(
 
462
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
 
463
                                        _buffers = null;
268
464
                                }
269
465
                                else
270
466
                                {
271
 
                                        // stash this line till we have a version
272
 
                                        if( _lines == null )
273
 
                                                _lines = new Vector< String >();
274
 
                                        _lines.add( line );
 
467
                                        // no, so stash this line till we get a version
 
468
                                        if( _buffers == null )
 
469
                                                _buffers = new Vector< ByteBuffer >();
 
470
                                        _buffers.add( buffer );
275
471
                                }
276
472
                        }
277
473
                        else
278
474
                        {
 
475
                                // name and params and the position in the buffer where the
 
476
                                // "value" part of the line start
 
477
                                String name_and_params;
 
478
                                int pos;
 
479
 
 
480
                                if( _parser_multiline_state != MULTILINE_NONE )
 
481
                                {
 
482
                                        // if we're currently in a multi-line value, use the stored
 
483
                                        // property name and parameters
 
484
                                        name_and_params = _parser_current_name_and_params;
 
485
 
 
486
                                        // skip some initial line characters, depending on the type
 
487
                                        // of multi-line we're handling
 
488
                                        pos = buffer.position();
 
489
                                        switch( _parser_multiline_state )
 
490
                                        {
 
491
                                        case MULTILINE_FOLDED:
 
492
                                                pos++;
 
493
                                                break;
 
494
                                        case MULTILINE_ENCODED:
 
495
                                                while( pos < buffer.limit() && (
 
496
                                                        buffer.get( pos ) == ' ' ||
 
497
                                                        buffer.get( pos ) == '\t' ) )
 
498
                                                {
 
499
                                                        pos++;
 
500
                                                }
 
501
                                                break;
 
502
                                        default:
 
503
                                                // do nothing
 
504
                                        }
 
505
 
 
506
                                        // take us out of multi-line so that we can re-detect that
 
507
                                        // this line is a multi-line or not
 
508
                                        _parser_multiline_state = MULTILINE_NONE;
 
509
                                }
 
510
                                else
 
511
                                {
 
512
                                        // get name and params from line, and since we're not
 
513
                                        // parsing a subsequent line in a multi-line, this should
 
514
                                        // not fail, or it's an error
 
515
                                        name_and_params =
 
516
                                                extractNameAndParamsFromLine( buffer, line );
 
517
                                        if( name_and_params == null )
 
518
                                                throw new ParseException(
 
519
                                                        R.string.error_vcf_malformed );
 
520
 
 
521
                                        // calculate how many chars to skip from beginning of line
 
522
                                        // so we skip the property "name:" part
 
523
                                        pos = buffer.position() + name_and_params.length() + 1;
 
524
 
 
525
                                        // reset the saved multi-line state
 
526
                                        _parser_current_name_and_params = name_and_params;
 
527
                                        _parser_buffered_value_so_far = "";
 
528
                                }
 
529
 
 
530
                                // get value from buffer, as raw bytes
 
531
                                ByteBuffer value;
 
532
                                value = ByteBuffer.wrap( buffer.array(), pos,
 
533
                                        buffer.limit() - pos );
 
534
 
279
535
                                // get parameter parts
280
 
                                String[] params = props[ 0 ].split( ";" );
281
 
                                for( int i = 0; i < params.length; i++ )
282
 
                                        params[ i ] = params[ i ].trim();
 
536
                                String[] name_param_parts = name_and_params.split( ";", -1 );
 
537
                                for( int i = 0; i < name_param_parts.length; i++ )
 
538
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
 
539
 
 
540
                                // parse encoding parameter
 
541
                                String encoding = checkParam( name_param_parts, "ENCODING" );
 
542
                                if( encoding != null ) encoding = encoding.toUpperCase();
 
543
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
544
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
545
                                        //&& !encoding.equals( "BASE64" ) )
 
546
                                {
 
547
                                        throw new ParseException( R.string.error_vcf_encoding );
 
548
                                }
 
549
 
 
550
                                // parse charset parameter
 
551
                                String charset = checkParam( name_param_parts, "CHARSET" );
 
552
                                if( charset != null ) charset = charset.toUpperCase();
 
553
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
554
                                        !charset.equals( "ASCII" ) &&
 
555
                                        !charset.equals( "UTF-8" ) )
 
556
                                {
 
557
                                        throw new ParseException( R.string.error_vcf_charset );
 
558
                                }
 
559
 
 
560
                                // do unencoding (or default to a fake unencoding result with
 
561
                                // the raw string)
 
562
                                UnencodeResult unencoding_result = null;
 
563
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
564
                                        unencoding_result = unencodeQuotedPrintable( value );
 
565
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
 
566
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
 
567
                                if( unencoding_result != null ) {
 
568
                                        value = unencoding_result.getBuffer();
 
569
                                        if( unencoding_result.isAnotherLineRequired() )
 
570
                                                _parser_multiline_state = MULTILINE_ENCODED;
 
571
                                }
 
572
 
 
573
                                // convert 8-bit ASCII charset to US-ASCII
 
574
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
575
                                        value = transcodeAsciiToUtf8( value );
 
576
                                        charset = "UTF-8";
 
577
                                }
 
578
 
 
579
                                // process charset
 
580
                                String string_value;
 
581
                                try {
 
582
                                        string_value = new String( value.array(), value.position(),
 
583
                                                value.limit() - value.position(), charset );
 
584
                                } catch( UnsupportedEncodingException e ) {
 
585
                                        throw new ParseException( R.string.error_vcf_charset );
 
586
                                }
 
587
 
 
588
                                // for some entries that have semicolon-separated value parts,
 
589
                                // check to see if the value ends in an escape character, which
 
590
                                // indicates that we have a multi-line value
 
591
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
 
592
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
 
593
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
 
594
                                        doesStringEndInAnEscapeChar( string_value ) )
 
595
                                {
 
596
                                        _parser_multiline_state = MULTILINE_ESCAPED;
 
597
                                        string_value = string_value.substring( 0,
 
598
                                                string_value.length() - 1 );
 
599
                                }
 
600
 
 
601
                                // now we know whether we're in an encoding multi-line,
 
602
                                // determine if we're in a v3 folded multi-line or not
 
603
                                if( _parser_multiline_state == MULTILINE_NONE &&
 
604
                                        _version.equals( "3.0" ) && next_line_looks_folded )
 
605
                                {
 
606
                                        _parser_multiline_state = MULTILINE_FOLDED;
 
607
                                }
 
608
 
 
609
                                // handle multi-lines by buffering them and parsing them when we
 
610
                                // are processing the last line in a multi-line sequence
 
611
                                if( _parser_multiline_state != MULTILINE_NONE ) {
 
612
                                        _parser_buffered_value_so_far += string_value;
 
613
                                        return;
 
614
                                }
 
615
                                String complete_value =
 
616
                                        ( _parser_buffered_value_so_far + string_value ).trim();
 
617
 
 
618
                                // ignore empty values
 
619
                                if( complete_value.length() < 1 ) return;
283
620
 
284
621
                                // parse some properties
285
 
                                if( params[ 0 ].equals( "N" ) )
286
 
                                        parseN( params, props[ 1 ] );
287
 
                                else if( params[ 0 ].equals( "FN" ) )
288
 
                                        parseFN( params, props[ 1 ] );
289
 
                                else if( params[ 0 ].equals( "ORG" ) )
290
 
                                        parseORG( params, props[ 1 ] );
291
 
                                else if( params[ 0 ].equals( "TEL" ) )
292
 
                                        parseTEL( params, props[ 1 ] );
293
 
                                else if( params[ 0 ].equals( "EMAIL" ) )
294
 
                                        parseEMAIL( params, props[ 1 ] );
295
 
                        }
 
622
                                if( name_param_parts[ 0 ].equals( "N" ) )
 
623
                                        parseN( name_param_parts, complete_value );
 
624
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
 
625
                                        parseFN( name_param_parts, complete_value );
 
626
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
 
627
                                        parseORG( name_param_parts, complete_value );
 
628
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
629
                                        parseTITLE( name_param_parts, complete_value );
 
630
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
 
631
                                        parseTEL( name_param_parts, complete_value );
 
632
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
 
633
                                        parseEMAIL( name_param_parts, complete_value );
 
634
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
 
635
                                        parseADR( name_param_parts, complete_value );
 
636
                        }
 
637
                }
 
638
 
 
639
                private boolean doesStringEndInAnEscapeChar( String string )
 
640
                {
 
641
                        // count the number of backslashes at the end of the string
 
642
                        int count = 0;
 
643
                        for( int a = string.length() - 1; a >= 0; a-- )
 
644
                                if( string.charAt( a ) == '\\' )
 
645
                                        count++;
 
646
                                else
 
647
                                        break;
 
648
 
 
649
                        // if there are an even number of backslashes then the final one
 
650
                        // doesn't count
 
651
                        return ( count & 1 ) == 1;
 
652
                }
 
653
 
 
654
                private String[] splitValueBySemicolon( String value )
 
655
                {
 
656
                        // split string in to parts by semicolon
 
657
                        ArrayList< String > parts = new ArrayList< String >(
 
658
                                Arrays.asList( value.split(  ";" ) ) );
 
659
 
 
660
                        // go through parts
 
661
                        for( int a = 0; a < parts.size(); a++ )
 
662
                        {
 
663
                                String str = parts.get( a );
 
664
 
 
665
                                // look for parts that end in an escape character, but ignore
 
666
                                // the final part. We've already detected escape chars at the
 
667
                                // end of the final part in parseLine() and handled multi-lines
 
668
                                // accordingly.
 
669
                                if( a < parts.size() - 1 &&
 
670
                                        doesStringEndInAnEscapeChar( str ) )
 
671
                                {
 
672
                                        // join the next part to this part and remove the next part
 
673
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
 
674
                                                ';' + parts.get( a + 1 ) );
 
675
                                        parts.remove( a + 1 );
 
676
 
 
677
                                        // re-visit this part
 
678
                                        a--;
 
679
                                        continue;
 
680
                                }
 
681
 
 
682
                                // trim and replace string
 
683
                                str = str.trim();
 
684
                                parts.set( a, str );
 
685
                        }
 
686
 
 
687
                        String[] ret = new String[ parts.size() ];
 
688
                        return parts.toArray( ret );
296
689
                }
297
690
 
298
691
                private void parseN( String[] params, String value )
299
 
                                throws ParseException, SkipContactException,
300
 
                                AbortImportException
301
692
                {
302
693
                        // already got a better name?
303
 
                        if( _nameLevel >= NAMELEVEL_N ) return;
 
694
                        if( _name_level >= NAMELEVEL_N ) return;
304
695
 
305
696
                        // get name parts
306
 
                        String[] nameparts = value.split( ";" );
307
 
                        for( int i = 0; i < nameparts.length; i++ )
308
 
                                nameparts[ i ] = nameparts[ i ].trim();
 
697
                        String[] name_parts = splitValueBySemicolon( value );
309
698
 
310
699
                        // build name
311
700
                        value = "";
312
 
                        if( nameparts.length > 1 && nameparts[ 1 ].length() > 0 )
313
 
                                value += nameparts[ 1 ];
314
 
                        if( nameparts[ 0 ].length() > 0 )
315
 
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
 
701
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
 
702
                                value += name_parts[ 1 ];
 
703
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
 
704
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
316
705
 
317
706
                        // set name
318
 
                        setName( undoCharsetAndEncoding( params, value ) );
319
 
                        _nameLevel = NAMELEVEL_N;
320
 
 
321
 
                        // check now to see if we need to import this contact (to avoid
322
 
                        // parsing the rest of the vCard unnecessarily)
323
 
                        if( !isImportRequired( getName() ) )
324
 
                                throw new SkipContactException();
 
707
                        setName( value );
 
708
                        _name_level = NAMELEVEL_N;
325
709
                }
326
710
 
327
711
                private void parseFN( String[] params, String value )
328
 
                                throws ParseException, SkipContactException
329
712
                {
330
713
                        // already got a better name?
331
 
                        if( _nameLevel >= NAMELEVEL_FN ) return;
 
714
                        if( _name_level >= NAMELEVEL_FN ) return;
332
715
 
333
716
                        // set name
334
 
                        setName( undoCharsetAndEncoding( params, value ) );
335
 
                        _nameLevel = NAMELEVEL_FN;
 
717
                        setName( value );
 
718
                        _name_level = NAMELEVEL_FN;
336
719
                }
337
720
 
338
721
                private void parseORG( String[] params, String value )
339
 
                                throws ParseException, SkipContactException
340
722
                {
341
 
                        // already got a better name?
342
 
                        if( _nameLevel >= NAMELEVEL_ORG ) return;
343
 
 
344
723
                        // get org parts
345
 
                        String[] orgparts = value.split( ";" );
346
 
                        for( int i = 0; i < orgparts.length; i++ )
347
 
                                orgparts[ i ] = orgparts[ i ].trim();
348
 
 
349
 
                        // build name
350
 
                        if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
351
 
                                value = orgparts[ 1 ];
352
 
                        else
353
 
                                value = orgparts[ 0 ];
354
 
 
355
 
                        // set name
356
 
                        setName( undoCharsetAndEncoding( params, value ) );
357
 
                        _nameLevel = NAMELEVEL_ORG;
 
724
                        String[] org_parts = splitValueBySemicolon( value );
 
725
                        if( org_parts == null || org_parts.length < 1 ) return;
 
726
 
 
727
                        // build organisation name
 
728
                        StringBuilder builder = new StringBuilder(
 
729
                                String.valueOf( org_parts[ 0 ] ) );
 
730
                        for( int a = 1; a < org_parts.length; a++ )
 
731
                                builder.append( ", " ).append( org_parts[ a ] );
 
732
                        String organisation = builder.toString();
 
733
 
 
734
                        // set organisation name (using a title we've previously found)
 
735
                        addOrganisation( organisation, _cached_title, true );
 
736
 
 
737
                        // if we've not previously found a title, store this organisation
 
738
                        // name (we'll need it when we find a title to update the
 
739
                        // organisation, by name), else if we *have* previously found a
 
740
                        // title, clear it (since we just used it)
 
741
                        if( _cached_title == null )
 
742
                                _cached_organisation = organisation;
 
743
                        else
 
744
                                _cached_title = null;
 
745
                }
 
746
 
 
747
                private void parseTITLE( String[] params, String value )
 
748
                {
 
749
                        // if we previously had an organisation, look it up and append this
 
750
                        // title to it
 
751
                        if( _cached_organisation != null && hasOrganisations() ) {
 
752
                                HashMap< String, ExtraDetail > datas = getOrganisations();
 
753
                                ExtraDetail detail = datas.get( _cached_organisation );
 
754
                                if( detail != null )
 
755
                                        detail.setExtra( value );
 
756
                        }
 
757
 
 
758
                        // same as when handling organisation, if we've not previously found
 
759
                        // an organisation we store this title, else we clear it (since we
 
760
                        // just appended this title to it)
 
761
                        if( _cached_organisation == null )
 
762
                                _cached_title = value;
 
763
                        else
 
764
                                _cached_organisation = null;
358
765
                }
359
766
 
360
767
                private void parseTEL( String[] params, String value )
361
 
                                throws ParseException
362
768
                {
363
769
                        if( value.length() == 0 ) return;
364
770
 
365
771
                        Set< String > types = extractTypes( params, Arrays.asList(
366
 
                                        "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
367
 
                                        "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
 
772
                                "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
 
773
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
368
774
 
369
775
                        // here's the logic...
370
776
                        boolean preferred = types.contains( "PREF" );
371
 
                        if( types.contains( "VOICE" ) )
372
 
                                if( types.contains( "WORK" ) )
373
 
                                        addPhone( value, PhonesColumns.TYPE_WORK, preferred );
374
 
                                else
375
 
                                        addPhone( value, PhonesColumns.TYPE_HOME, preferred );
376
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
377
 
                                addPhone( value, PhonesColumns.TYPE_MOBILE, preferred );
 
777
                        int type;
378
778
                        if( types.contains( "FAX" ) )
379
779
                                if( types.contains( "HOME" ) )
380
 
                                        addPhone( value, PhonesColumns.TYPE_FAX_HOME, preferred );
 
780
                                        type = PhonesColumns.TYPE_FAX_HOME;
381
781
                                else
382
 
                                        addPhone( value, PhonesColumns.TYPE_FAX_WORK, preferred );
383
 
                        if( types.contains( "PAGER" ) )
384
 
                                addPhone( value, PhonesColumns.TYPE_PAGER, preferred );
 
782
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
783
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
784
                                type = PhonesColumns.TYPE_MOBILE;
 
785
                        else if( types.contains( "PAGER" ) )
 
786
                                type = PhonesColumns.TYPE_PAGER;
 
787
                        else if( types.contains( "WORK" ) )
 
788
                                type = PhonesColumns.TYPE_WORK;
 
789
                        else
 
790
                                type = PhonesColumns.TYPE_HOME;
 
791
 
 
792
                        // add phone number
 
793
                        addNumber( value, type, preferred );
385
794
                }
386
795
 
387
796
                public void parseEMAIL( String[] params, String value )
389
798
                        if( value.length() == 0 ) return;
390
799
 
391
800
                        Set< String > types = extractTypes( params, Arrays.asList(
392
 
                                        "PREF", "WORK", "HOME", "INTERNET" ) );
 
801
                                "PREF", "WORK", "HOME", "INTERNET" ) );
393
802
 
394
 
                        // here's the logic...
 
803
                        // add email address
395
804
                        boolean preferred = types.contains( "PREF" );
396
 
                        if( types.contains( "WORK" ) )
397
 
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
398
 
                        else
399
 
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
 
805
                        int type;
 
806
                        if( types.contains( "WORK" ) )
 
807
                                type = Contacts.ContactMethods.TYPE_WORK;
 
808
                        else
 
809
                                type = Contacts.ContactMethods.TYPE_HOME;
 
810
 
 
811
                        addEmail( value, type, preferred );
 
812
                }
 
813
 
 
814
                private void parseADR( String[] params, String value )
 
815
                {
 
816
                        // get address parts
 
817
                        String[] adr_parts = splitValueBySemicolon( value );
 
818
 
 
819
                        // build address
 
820
                        value = "";
 
821
                        for( int a = 0; a < adr_parts.length; a++ ) {
 
822
                                if( value.length() > 0 ) value += "\n";
 
823
                                value += adr_parts[ a ].trim();
 
824
                        }
 
825
 
 
826
                        Set< String > types = extractTypes( params, Arrays.asList(
 
827
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
828
 
 
829
                        // add address
 
830
                        int type;
 
831
                        if( types.contains( "WORK" ) )
 
832
                                type = Contacts.ContactMethods.TYPE_WORK;
 
833
                        else
 
834
                                type = Contacts.ContactMethods.TYPE_HOME;
 
835
 
 
836
                        addAddress( value, type );
400
837
                }
401
838
 
402
839
                public void finaliseParsing()
403
 
                                throws ParseException, SkipContactException,
404
 
                                AbortImportException
 
840
                        throws ParseException, SkipContactException,
 
841
                        AbortImportException
405
842
                {
406
843
                        // missing version (and data is present)
407
 
                        if( _version == null && _lines != null )
 
844
                        if( _version == null && _buffers != null )
408
845
                                throw new ParseException( R.string.error_vcf_malformed );
409
846
 
410
 
                        //  missing name properties?
411
 
                        if( _nameLevel == NAMELEVEL_NONE )
412
 
                                throw new ParseException( R.string.error_vcf_noname );
413
 
 
414
 
                        // check if we should import this one? If we've already got an 'N'-
415
 
                        // type name, this will already have been done by parseN() so we
416
 
                        // mustn't do this here (or it could prompt twice!)
417
 
                        if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
418
 
                                throw new SkipContactException();
419
 
                }
420
 
 
421
 
                private String undoCharsetAndEncoding( String[] params, String value )
422
 
                                throws ParseException
423
 
                {
424
 
                        // check encoding/charset
425
 
                        String charset, encoding;
426
 
                        if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
427
 
                                        !charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
428
 
                                throw new ParseException( R.string.error_vcf_charset );
429
 
                        if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
430
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
431
 
                                throw new ParseException( R.string.error_vcf_encoding );
432
 
 
433
 
                        // do decoding?
434
 
                        if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
435
 
                                return unencodeQuotedPrintable( value, charset );
436
 
 
437
 
                        // nothing to do!
438
 
                        return value;
 
847
                        // check if we should import this contact
 
848
                        try {
 
849
                                if( !isImportRequired( this ) )
 
850
                                        throw new SkipContactException();
 
851
                        }
 
852
                        catch( ContactNeedsMoreInfoException e ) {
 
853
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
 
854
                        }
439
855
                }
440
856
 
441
857
                private String checkParam( String[] params, String name )
442
858
                {
443
 
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
 
859
                        Pattern p = Pattern.compile(
 
860
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
444
861
                        for( int i = 0; i < params.length; i++ ) {
445
862
                                Matcher m = p.matcher( params[ i ] );
446
863
                                if( m.matches() )
447
 
                                        return m.group( 1 );
 
864
                                        return m.group( 2 );
448
865
                        }
449
866
                        return null;
450
867
                }
451
868
 
452
869
                private Set< String > extractTypes( String[] params,
453
 
                                List< String > validTypes )
 
870
                        List< String > valid_types )
454
871
                {
455
872
                        HashSet< String > types = new HashSet< String >();
456
873
 
457
874
                        // get 3.0-style TYPE= param
458
 
                        String typeParam;
459
 
                        if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
460
 
                                String[] bits = typeParam.split( "," );
461
 
                                for( int i = 0; i < bits.length; i++ )
462
 
                                        if( validTypes.contains( bits[ i ] ) )
463
 
                                                types.add( bits[ i ] );
 
875
                        String type_param;
 
876
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
877
                                String[] parts = type_param.split( "," );
 
878
                                for( int i = 0; i < parts.length; i++ )
 
879
                                        if( valid_types.contains( parts[ i ] ) )
 
880
                                                types.add( parts[ i ] );
464
881
                        }
465
882
 
466
883
                        // get 2.1-style type param
467
884
                        if( _version.equals( "2.1" ) ) {
468
885
                                for( int i = 1; i < params.length; i++ )
469
 
                                        if( validTypes.contains( params[ i ] ) )
 
886
                                        if( valid_types.contains( params[ i ] ) )
470
887
                                                types.add( params[ i ] );
471
888
                        }
472
889
 
473
890
                        return types;
474
891
                }
475
892
 
476
 
                private String unencodeQuotedPrintable( String str, String charset )
 
893
                private UnencodeResult unencodeQuotedPrintable( ByteBuffer in )
477
894
                {
478
 
                        // default encoding scheme
479
 
                        if( charset == null ) charset = "UTF-8";
 
895
                        boolean another = false;
480
896
 
481
 
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
482
 
                        byte[] bytes = new byte[ str.length() ];
 
897
                        // unencode quoted-printable encoding, as per RFC1521 section 5.1
 
898
                        byte[] out = new byte[ in.limit() - in.position() ];
483
899
                        int j = 0;
484
 
                        for( int i = 0; i < str.length(); i++, j++ ) {
485
 
                                char ch = str.charAt( i );
486
 
                                if( ch == '=' && i < str.length() - 2 ) {
487
 
                                        bytes[ j ] = (byte)(
488
 
                                                        Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
489
 
                                                        Character.digit( str.charAt( i + 2 ), 16 ) );
 
900
                        for( int i = in.position(); i < in.limit(); i++ )
 
901
                        {
 
902
                                // get next char and process...
 
903
                                byte ch = in.array()[ i ];
 
904
                                if( ch == '=' && i < in.limit() - 2 )
 
905
                                {
 
906
                                        // we found a =XX format byte, add it
 
907
                                        out[ j ] = (byte)(
 
908
                                                        Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
 
909
                                                        Character.digit( in.array()[ i + 2 ], 16 ) );
490
910
                                        i += 2;
491
911
                                }
 
912
                                else if( ch == '=' && i == in.limit() - 1 )
 
913
                                {
 
914
                                        // we found a '=' at the end of a line signifying a multi-
 
915
                                        // line string, so we don't add it.
 
916
                                        another = true;
 
917
                                        continue;
 
918
                                }
492
919
                                else
493
 
                                        bytes[ j ] = (byte)ch;
494
 
                        }
495
 
                        try {
496
 
                                return new String( bytes, 0, j, charset );
497
 
                        } catch( UnsupportedEncodingException e ) { }
498
 
                        return null;
 
920
                                        // just a normal char...
 
921
                                        out[ j ] = (byte)ch;
 
922
                                j++;
 
923
                        }
 
924
 
 
925
                        return new UnencodeResult( another, ByteBuffer.wrap( out, 0, j ) );
 
926
                }
 
927
 
 
928
                private ByteBuffer transcodeAsciiToUtf8( ByteBuffer in )
 
929
                {
 
930
                        // transcode
 
931
                        byte[] out = new byte[ ( in.limit() - in.position() ) * 2 ];
 
932
                        int j = 0;
 
933
                        for( int a = in.position(); a < in.limit(); a++ )
 
934
                        {
 
935
                                // if char is < 127, keep it as-is
 
936
                                if( in.array()[ a ] >= 0 )
 
937
                                        out[ j++ ] = in.array()[ a ];
 
938
 
 
939
                                // else, convert it to UTF-8
 
940
                                else {
 
941
                                        int b = 0xff & (int)in.array()[ a ];
 
942
                                        out[ j++ ] = (byte)( 0xc0 | ( b >> 6 ) );
 
943
                                        out[ j++ ] = (byte)( 0x80 | ( b & 0x3f ) );
 
944
                                }
 
945
                        }
 
946
 
 
947
                        return ByteBuffer.wrap( out, 0, j );
499
948
                }
500
949
        }
501
950
}