/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/am/ed/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2012-12-18 13:00:43 UTC
  • Revision ID: tim@ed.am-20121218130043-4zhj1cdeaqko3c4b
cleanup; fixed some typos; updated TODO

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/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 org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
44
44
import java.util.regex.Matcher;
45
45
import java.util.regex.Pattern;
46
46
 
47
 
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
48
 
 
49
47
import android.content.SharedPreferences;
50
48
import android.provider.Contacts;
51
49
import android.provider.Contacts.PhonesColumns;
52
50
 
53
 
public class VCFImporter extends Importer
 
51
public class VcardImporter extends Importer
54
52
{
55
 
        private int _vCardCount = 0;
 
53
        private int _vcard_count = 0;
56
54
        private int _progress = 0;
57
55
 
58
 
        public VCFImporter( Doit doit )
 
56
        public VcardImporter( Doit doit )
59
57
        {
60
58
                super( doit );
61
59
        }
112
110
                        countVCardFile( files[ i ] );
113
111
                        setTmpProgress( i );
114
112
                }
115
 
                setProgressMax( _vCardCount );  // will also update tmp progress
 
113
                setProgressMax( _vcard_count ); // will also update tmp progress
116
114
 
117
115
                // import them
118
116
                setProgress( 0 );
119
117
                for( int i = 0; i < files.length; i++ )
120
118
                        importVCardFile( files[ i ] );
 
119
                setProgress( _vcard_count );
121
120
        }
122
121
 
123
122
        private void countVCardFile( File file ) throws AbortImportException
130
129
 
131
130
                        // read
132
131
                        String line;
133
 
                        boolean inVCard = false;
 
132
                        boolean in_vcard = false;
134
133
                        while( ( line = reader.readLine() ) != null )
135
134
                        {
136
 
                                if( !inVCard ) {
 
135
                                if( !in_vcard ) {
137
136
                                        // look for vcard beginning
138
137
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
139
 
                                                inVCard = true;
140
 
                                                _vCardCount++;
 
138
                                                in_vcard = true;
 
139
                                                _vcard_count++;
141
140
                                        }
142
141
                                }
143
142
                                else if( line.matches( "^END:VCARD" ) )
144
 
                                        inVCard = false;
 
143
                                        in_vcard = false;
145
144
                        }
146
145
 
147
146
                }
170
169
                        FileInputStream istream = new FileInputStream( file );
171
170
                        byte[] content = new byte[ (int)file.length() ];
172
171
                        istream.read( content );
 
172
                        istream = null;
173
173
 
174
174
                        // import
175
175
                        importVCardFileContent( content, file.getName() );
187
187
                throws AbortImportException
188
188
        {
189
189
                // go through lines
190
 
                VCard vCard = null;
 
190
                Vcard vcard = null;
 
191
                int vcard_start_line = 0;
191
192
                ContentLineIterator cli = new ContentLineIterator( content );
192
193
                while( cli.hasNext() )
193
194
                {
204
205
                                line = "";
205
206
                        }
206
207
 
207
 
                        if( vCard == null ) {
 
208
                        if( vcard == null ) {
208
209
                                // look for vcard beginning
209
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
210
 
                                        setProgress( ++_progress );
211
 
                                        vCard = new VCard();
 
211
                                        setProgress( _progress++ );
 
212
                                        vcard = new Vcard();
 
213
                                        vcard_start_line = cli.getLineNumber();
212
214
                                }
213
215
                        }
214
216
                        else {
215
217
                                // look for vcard content or ending
216
218
                                if( line.matches( "^END:VCARD" ) )
217
219
                                {
218
 
                                        // store vcard and do away with it
 
220
                                        // finalise the vcard/contact
219
221
                                        try {
220
 
                                                vCard.finaliseParsing();
221
 
                                                importContact( vCard );
222
 
                                        }
223
 
                                        catch( VCard.ParseException e ) {
224
 
                                                skipContact();
225
 
                                                if( !showContinue(
226
 
                                                        getText( R.string.error_vcf_parse ).toString()
227
 
                                                        + fileName + "\n" + e.getMessage() ) )
228
 
                                                {
229
 
                                                        finish( ACTION_ABORT );
230
 
                                                }
231
 
                                        }
232
 
                                        catch( VCard.SkipContactException e ) {
233
 
                                                skipContact();
234
 
                                                // do nothing
235
 
                                        }
236
 
                                        vCard = null;
 
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
 
225
                                                importContact( vcard );
 
226
                                        }
 
227
                                        catch( Vcard.ParseException e ) {
 
228
                                                if( !showContinue(
 
229
                                                        getText( R.string.error_vcf_parse ).toString()
 
230
                                                        + fileName +
 
231
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
232
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
233
                                                {
 
234
                                                        finish( ACTION_ABORT );
 
235
                                                }
 
236
                                                else
 
237
                                                        skipContact();
 
238
                                        }
 
239
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
240
                                                if( !showContinue(
 
241
                                                        getText( R.string.error_vcf_parse ).toString()
 
242
                                                        + fileName +
 
243
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
244
                                                        + vcard_start_line + ":\n" + getText(
 
245
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
246
                                                ) )
 
247
                                                {
 
248
                                                        finish( ACTION_ABORT );
 
249
                                                }
 
250
                                                else
 
251
                                                        skipContact();
 
252
                                        }
 
253
 
 
254
                                        // discard this vcard
 
255
                                        vcard = null;
237
256
                                }
238
257
                                else
239
258
                                {
240
259
                                        // try giving the line to the vcard
241
260
                                        try {
242
 
                                                vCard.parseLine( buffer, line,
 
261
                                                vcard.parseLine( buffer, line,
243
262
                                                        cli.doesNextLineLookFolded() );
244
263
                                        }
245
 
                                        catch( VCard.ParseException e ) {
 
264
                                        catch( Vcard.ParseException e ) {
246
265
                                                skipContact();
247
266
                                                if( !showContinue(
248
267
                                                        getText( R.string.error_vcf_parse ).toString()
249
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
268
                                                        + fileName +
 
269
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
270
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
250
271
                                                {
251
272
                                                        finish( ACTION_ABORT );
252
273
                                                }
254
275
                                                // although we're continuing, we still need to abort
255
276
                                                // this vCard. Further lines will be ignored until we
256
277
                                                // get to another BEGIN:VCARD line.
257
 
                                                vCard = null;
 
278
                                                vcard = null;
258
279
                                        }
259
 
                                        catch( VCard.SkipContactException e ) {
 
280
                                        catch( Vcard.SkipImportException e ) {
260
281
                                                skipContact();
261
282
                                                // abort this vCard. Further lines will be ignored until
262
283
                                                // we get to another BEGIN:VCARD line.
263
 
                                                vCard = null;
 
284
                                                vcard = null;
264
285
                                        }
265
286
                                }
266
287
                        }
271
292
        {
272
293
                protected byte[] _content = null;
273
294
                protected int _pos = 0;
 
295
                protected int _line = 0;
274
296
 
275
297
                public ContentLineIterator( byte[] content )
276
298
                {
296
318
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
297
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
298
320
                                        _pos++;
 
321
                                        _line++;
299
322
                                        return ByteBuffer.wrap( _content, initial_pos,
300
323
                                                to - initial_pos );
301
324
                                }
304
327
                        if( _pos != initial_pos ) {
305
328
                                int to = _pos;
306
329
                                _pos++;
 
330
                                _line++;
307
331
                                return ByteBuffer.wrap( _content, initial_pos,
308
332
                                        to - initial_pos );
309
333
                        }
328
352
                        return _pos > 0 && _pos < _content.length &&
329
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
330
354
                }
 
355
 
 
356
                public int getLineNumber()
 
357
                {
 
358
                        return _line;
 
359
                }
331
360
        }
332
361
 
333
 
        private class VCard extends ContactData
 
362
        private class Vcard extends ContactData
334
363
        {
335
364
                private final static int NAMELEVEL_NONE = 0;
336
 
                private final static int NAMELEVEL_FN = 1;
337
 
                private final static int NAMELEVEL_N = 2;
 
365
                private final static int NAMELEVEL_N = 1;
 
366
                private final static int NAMELEVEL_FN = 2;
338
367
 
339
368
                private final static int MULTILINE_NONE = 0;
340
369
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
384
413
 
385
414
                        public ParseException( int res )
386
415
                        {
387
 
                                super( VCFImporter.this.getText( res ).toString() );
 
416
                                super( VcardImporter.this.getText( res ).toString() );
388
417
                        }
389
418
                }
390
419
 
391
420
                @SuppressWarnings("serial")
392
 
                protected class SkipContactException extends Exception { }
 
421
                protected class SkipImportException extends Exception { }
393
422
 
394
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
395
424
                        String line, boolean former )
431
460
 
432
461
                public void parseLine( ByteBuffer buffer, String line,
433
462
                        boolean next_line_looks_folded )
434
 
                        throws ParseException, SkipContactException,
 
463
                        throws ParseException, SkipImportException,
435
464
                        AbortImportException
436
465
                {
437
466
                        // do we have a version yet?
537
566
                                for( int i = 0; i < name_param_parts.length; i++ )
538
567
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
539
568
 
 
569
                                // determine whether we care about this entry
 
570
                                final HashSet< String > interesting_fields =
 
571
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
572
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
573
                                ) );
 
574
                                boolean is_interesting_field =
 
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
 
576
 
540
577
                                // parse encoding parameter
541
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
542
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
543
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
580
                                if( is_interesting_field && encoding != null &&
 
581
                                        !encoding.equals( "8BIT" ) &&
544
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
545
583
                                        //&& !encoding.equals( "BASE64" ) )
546
584
                                {
550
588
                                // parse charset parameter
551
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
552
590
                                if( charset != null ) charset = charset.toUpperCase();
553
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
591
                                if( charset != null &&
 
592
                                        !charset.equals( "US-ASCII" ) &&
554
593
                                        !charset.equals( "ASCII" ) &&
555
594
                                        !charset.equals( "UTF-8" ) )
556
595
                                {
570
609
                                                _parser_multiline_state = MULTILINE_ENCODED;
571
610
                                }
572
611
 
573
 
                                // convert 8-bit ASCII charset to US-ASCII
574
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
612
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
613
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
614
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
615
                                        ( charset != null && (
 
616
                                                charset.equals( "ASCII" ) ||
 
617
                                                charset.equals( "US-ASCII" ) ) ) )
 
618
                                {
575
619
                                        value = transcodeAsciiToUtf8( value );
576
 
                                        charset = "UTF-8";
577
620
                                }
578
621
 
579
 
                                // process charset
 
622
                                // process charset (value is now in UTF-8)
580
623
                                String string_value;
581
624
                                try {
582
625
                                        string_value = new String( value.array(), value.position(),
583
 
                                                value.limit() - value.position(), charset );
 
626
                                                value.limit() - value.position(), "UTF-8" );
584
627
                                } catch( UnsupportedEncodingException e ) {
585
628
                                        throw new ParseException( R.string.error_vcf_charset );
586
629
                                }
633
676
                                        parseEMAIL( name_param_parts, complete_value );
634
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
635
678
                                        parseADR( name_param_parts, complete_value );
 
679
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
680
                                        parseLABEL( name_param_parts, complete_value );
636
681
                        }
637
682
                }
638
683
 
651
696
                        return ( count & 1 ) == 1;
652
697
                }
653
698
 
654
 
                private String[] splitValueBySemicolon( String value )
 
699
                private String[] splitValueByCharacter( String value, char character )
655
700
                {
656
 
                        // split string in to parts by semicolon
 
701
                        // split string in to parts by specified character
657
702
                        ArrayList< String > parts = new ArrayList< String >(
658
 
                                Arrays.asList( value.split(  ";" ) ) );
 
703
                                Arrays.asList( value.split( "" + character ) ) );
659
704
 
660
705
                        // go through parts
661
706
                        for( int a = 0; a < parts.size(); a++ )
669
714
                                if( a < parts.size() - 1 &&
670
715
                                        doesStringEndInAnEscapeChar( str ) )
671
716
                                {
672
 
                                        // join the next part to this part and remove the next part
 
717
                                        // append the escaped character, join the next part to this
 
718
                                        // part and remove the next part
673
719
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
674
 
                                                ';' + parts.get( a + 1 ) );
 
720
                                                character + parts.get( a + 1 ) );
675
721
                                        parts.remove( a + 1 );
676
722
 
677
723
                                        // re-visit this part
688
734
                        return parts.toArray( ret );
689
735
                }
690
736
 
 
737
                private String unescapeValue( String value )
 
738
                {
 
739
                        StringBuilder ret = new StringBuilder( value.length() );
 
740
                        boolean in_escape = false;
 
741
                        for( int a = 0; a < value.length(); a++ )
 
742
                        {
 
743
                                int c = value.codePointAt( a );
 
744
 
 
745
                                // process a normal character
 
746
                                if( !in_escape ) {
 
747
                                        if( c == '\\' )
 
748
                                                in_escape = true;
 
749
                                        else
 
750
                                                ret.append( Character.toChars( c ) );
 
751
                                        continue;
 
752
                                }
 
753
 
 
754
                                // process an escape sequence
 
755
                                in_escape = false;
 
756
                                switch( c )
 
757
                                {
 
758
                                case 'N':
 
759
                                case 'n':
 
760
                                        // add newline
 
761
                                        ret.append( '\n' );
 
762
                                        break;
 
763
                                case '\\':
 
764
                                case ',':
 
765
                                case ';':
 
766
                                        // add escaped character
 
767
                                        ret.append( Character.toChars( c ) );
 
768
                                        break;
 
769
                                default:
 
770
                                        // unknown escape sequence, so add it unescaped
 
771
                                        ret.append( "\\" );
 
772
                                        ret.append( Character.toChars( c ) );
 
773
                                        break;
 
774
                                }
 
775
                        }
 
776
 
 
777
                        return ret.toString();
 
778
                }
 
779
 
691
780
                private void parseN( String[] params, String value )
692
781
                {
693
782
                        // already got a better name?
694
783
                        if( _name_level >= NAMELEVEL_N ) return;
695
784
 
696
785
                        // get name parts
697
 
                        String[] name_parts = splitValueBySemicolon( value );
 
786
                        String[] name_parts = splitValueByCharacter( value, ';' );
698
787
 
699
788
                        // build name
700
789
                        value = "";
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 ];
 
790
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
791
                        for( int a = 0; a < part_order.length; a++ )
 
792
                                if( name_parts.length > part_order[ a ] &&
 
793
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
794
                                {
 
795
                                        // split this part in to it's comma-separated bits
 
796
                                        String[] name_part_parts = splitValueByCharacter(
 
797
                                                name_parts[ part_order[ a ] ], ',' );
 
798
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
799
                                                if( name_part_parts[ b ].length() > 0 )
 
800
                                                {
 
801
                                                        if( value.length() == 0 ) value += " ";
 
802
                                                        value += name_part_parts[ b ];
 
803
                                                }
 
804
                                }
705
805
 
706
806
                        // set name
707
 
                        setName( value );
 
807
                        setName( unescapeValue( value ) );
708
808
                        _name_level = NAMELEVEL_N;
709
809
                }
710
810
 
714
814
                        if( _name_level >= NAMELEVEL_FN ) return;
715
815
 
716
816
                        // set name
717
 
                        setName( value );
 
817
                        setName( unescapeValue( value ) );
718
818
                        _name_level = NAMELEVEL_FN;
719
819
                }
720
820
 
721
821
                private void parseORG( String[] params, String value )
722
822
                {
723
823
                        // get org parts
724
 
                        String[] org_parts = splitValueBySemicolon( value );
 
824
                        String[] org_parts = splitValueByCharacter( value, ';' );
725
825
                        if( org_parts == null || org_parts.length < 1 ) return;
726
826
 
727
827
                        // build organisation name
729
829
                                String.valueOf( org_parts[ 0 ] ) );
730
830
                        for( int a = 1; a < org_parts.length; a++ )
731
831
                                builder.append( ", " ).append( org_parts[ a ] );
732
 
                        String organisation = builder.toString();
 
832
                        String organisation = unescapeValue( builder.toString() );
733
833
 
734
834
                        // set organisation name (using a title we've previously found)
735
835
                        addOrganisation( organisation, _cached_title, true );
746
846
 
747
847
                private void parseTITLE( String[] params, String value )
748
848
                {
 
849
                        value = unescapeValue( value );
 
850
 
749
851
                        // if we previously had an organisation, look it up and append this
750
852
                        // title to it
751
853
                        if( _cached_organisation != null && hasOrganisations() ) {
773
875
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
774
876
 
775
877
                        // here's the logic...
776
 
                        boolean preferred = types.contains( "PREF" );
 
878
                        boolean is_preferred = types.contains( "PREF" );
777
879
                        int type;
778
880
                        if( types.contains( "FAX" ) )
779
881
                                if( types.contains( "HOME" ) )
790
892
                                type = PhonesColumns.TYPE_HOME;
791
893
 
792
894
                        // add phone number
793
 
                        addNumber( value, type, preferred );
 
895
                        addNumber( value, type, is_preferred );
794
896
                }
795
897
 
796
898
                public void parseEMAIL( String[] params, String value )
801
903
                                "PREF", "WORK", "HOME", "INTERNET" ) );
802
904
 
803
905
                        // add email address
804
 
                        boolean preferred = types.contains( "PREF" );
 
906
                        boolean is_preferred = types.contains( "PREF" );
805
907
                        int type;
806
908
                        if( types.contains( "WORK" ) )
807
909
                                type = Contacts.ContactMethods.TYPE_WORK;
808
910
                        else
809
911
                                type = Contacts.ContactMethods.TYPE_HOME;
810
912
 
811
 
                        addEmail( value, type, preferred );
 
913
                        addEmail( unescapeValue( value ), type, is_preferred );
812
914
                }
813
915
 
814
916
                private void parseADR( String[] params, String value )
815
917
                {
816
918
                        // get address parts
817
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
919
                        String[] adr_parts = splitValueByCharacter( value, ';' );
818
920
 
819
921
                        // build address
820
922
                        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 );
837
 
                }
838
 
 
839
 
                public void finaliseParsing()
840
 
                        throws ParseException, SkipContactException,
841
 
                        AbortImportException
 
923
                        for( int a = 0; a < adr_parts.length; a++ )
 
924
                                if( adr_parts[ a ].length() > 0 )
 
925
                                {
 
926
                                        // split this part in to it's comma-separated bits
 
927
                                        String[] adr_part_parts =
 
928
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
929
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
930
                                                if( adr_part_parts[ b ].length() > 0 )
 
931
                                                {
 
932
                                                        if( value.length() > 0 ) value += "\n";
 
933
                                                        value += adr_part_parts[ b ];
 
934
                                                }
 
935
                                }
 
936
 
 
937
                        Set< String > types = extractTypes( params, Arrays.asList(
 
938
                                "PREF", "WORK", "HOME" ) );
 
939
 
 
940
                        // add address
 
941
                        int type;
 
942
                        if( types.contains( "WORK" ) )
 
943
                                type = Contacts.ContactMethods.TYPE_WORK;
 
944
                        else
 
945
                                type = Contacts.ContactMethods.TYPE_HOME;
 
946
 
 
947
                        addAddress( unescapeValue( value ), type );
 
948
                }
 
949
 
 
950
                private void parseLABEL( String[] params, String value )
 
951
                {
 
952
                        Set< String > types = extractTypes( params, Arrays.asList(
 
953
                                "PREF", "WORK", "HOME" ) );
 
954
 
 
955
                        // add address
 
956
                        int type;
 
957
                        if( types.contains( "WORK" ) )
 
958
                                type = Contacts.ContactMethods.TYPE_WORK;
 
959
                        else
 
960
                                type = Contacts.ContactMethods.TYPE_HOME;
 
961
 
 
962
                        addAddress( unescapeValue( value ), type );
 
963
                }
 
964
 
 
965
                public void finaliseVcard()
 
966
                        throws ParseException, ContactNotIdentifiableException
842
967
                {
843
968
                        // missing version (and data is present)
844
969
                        if( _version == null && _buffers != null )
845
970
                                throw new ParseException( R.string.error_vcf_malformed );
846
971
 
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
 
                        }
 
972
                        // finalise the parent class
 
973
                        finalise();
855
974
                }
856
975
 
 
976
                /**
 
977
                 * Amongst the params, find the value of the first, only, of any with
 
978
                 * the specified name
 
979
                 * @param params
 
980
                 * @param name
 
981
                 * @return a value, or null
 
982
                 */
857
983
                private String checkParam( String[] params, String name )
858
984
                {
 
985
                        String[] res = checkParams( params, name );
 
986
                        return res.length > 0? res[ 0 ] : null;
 
987
                }
 
988
 
 
989
                /**
 
990
                 * Amongst the params, find the values of any with the specified name
 
991
                 * @param params
 
992
                 * @param name
 
993
                 * @return an array of values, or null
 
994
                 */
 
995
                private String[] checkParams( String[] params, String name )
 
996
                {
 
997
                        HashSet< String > ret = new HashSet< String >();
 
998
 
859
999
                        Pattern p = Pattern.compile(
860
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
861
1001
                        for( int i = 0; i < params.length; i++ ) {
862
1002
                                Matcher m = p.matcher( params[ i ] );
863
1003
                                if( m.matches() )
864
 
                                        return m.group( 2 );
 
1004
                                        ret.add( m.group( 2 ) );
865
1005
                        }
866
 
                        return null;
 
1006
 
 
1007
                        return (String[]) ret.toArray( new String[ ret.size() ] );
867
1008
                }
868
1009
 
 
1010
                /**
 
1011
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1012
                 * those types are just parameters. For v3.0, they are prefixed with
 
1013
                 * "TYPE=". There may also be multiple type parameters.
 
1014
                 * @param params
 
1015
                 * @param a list of type values to look for
 
1016
                 * @return a set of present type values
 
1017
                 */
869
1018
                private Set< String > extractTypes( String[] params,
870
1019
                        List< String > valid_types )
871
1020
                {
872
1021
                        HashSet< String > types = new HashSet< String >();
873
1022
 
874
1023
                        // get 3.0-style TYPE= param
875
 
                        String type_param;
876
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
877
 
                                String[] parts = type_param.split( "," );
 
1024
                        String type_params[] = checkParams( params, "TYPE" );
 
1025
                        for( int a = 0; a < type_params.length; a++ )
 
1026
                        {
 
1027
                                // check for a comma-separated list of types (why? this isn't in
 
1028
                                // the specs!)
 
1029
                                String[] parts = type_params[ a ].split( "," );
878
1030
                                for( int i = 0; i < parts.length; i++ )
879
1031
                                        if( valid_types.contains( parts[ i ] ) )
880
1032
                                                types.add( parts[ i ] );