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

  • Committer: edam
  • Date: 2011-05-30 14:04:51 UTC
  • Revision ID: edam@waxworlds.org-20110530140451-d99fy3zoi6zq1jf2
- renamed VCFImporter to VcardImporter and VCard to Vcard

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 <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
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://ed.am/dev/android/import-contacts
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
116
116
                setProgress( 0 );
117
117
                for( int i = 0; i < files.length; i++ )
118
118
                        importVCardFile( files[ i ] );
119
 
                setProgress( _vcard_count );
120
119
        }
121
120
 
122
121
        private void countVCardFile( File file ) throws AbortImportException
169
168
                        FileInputStream istream = new FileInputStream( file );
170
169
                        byte[] content = new byte[ (int)file.length() ];
171
170
                        istream.read( content );
172
 
                        istream = null;
173
171
 
174
172
                        // import
175
173
                        importVCardFileContent( content, file.getName() );
188
186
        {
189
187
                // go through lines
190
188
                Vcard vcard = null;
191
 
                int vcard_start_line = 0;
192
189
                ContentLineIterator cli = new ContentLineIterator( content );
193
190
                while( cli.hasNext() )
194
191
                {
208
205
                        if( vcard == null ) {
209
206
                                // look for vcard beginning
210
207
                                if( line.matches( "^BEGIN:VCARD" ) ) {
211
 
                                        setProgress( _progress++ );
 
208
                                        setProgress( ++_progress );
212
209
                                        vcard = new Vcard();
213
 
                                        vcard_start_line = cli.getLineNumber();
214
210
                                }
215
211
                        }
216
212
                        else {
217
213
                                // look for vcard content or ending
218
214
                                if( line.matches( "^END:VCARD" ) )
219
215
                                {
220
 
                                        // finalise the vcard/contact
 
216
                                        // store vcard and do away with it
221
217
                                        try {
222
 
                                                vcard.finaliseVcard();
223
 
 
224
 
                                                // pass the finalised contact to the importer
 
218
                                                vcard.finaliseParsing();
225
219
                                                importContact( vcard );
226
220
                                        }
227
221
                                        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
 
222
                                                skipContact();
 
223
                                                if( !showContinue(
 
224
                                                        getText( R.string.error_vcf_parse ).toString()
 
225
                                                        + fileName + "\n" + e.getMessage() ) )
 
226
                                                {
 
227
                                                        finish( ACTION_ABORT );
 
228
                                                }
 
229
                                        }
 
230
                                        catch( Vcard.SkipContactException e ) {
 
231
                                                skipContact();
 
232
                                                // do nothing
 
233
                                        }
255
234
                                        vcard = null;
256
235
                                }
257
236
                                else
265
244
                                                skipContact();
266
245
                                                if( !showContinue(
267
246
                                                        getText( R.string.error_vcf_parse ).toString()
268
 
                                                        + fileName +
269
 
                                                        getText( R.string.error_vcf_parse_line ).toString()
270
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
247
                                                        + fileName + "\n" + e.getMessage() ) )
271
248
                                                {
272
249
                                                        finish( ACTION_ABORT );
273
250
                                                }
277
254
                                                // get to another BEGIN:VCARD line.
278
255
                                                vcard = null;
279
256
                                        }
280
 
                                        catch( Vcard.SkipImportException e ) {
 
257
                                        catch( Vcard.SkipContactException e ) {
281
258
                                                skipContact();
282
259
                                                // abort this vCard. Further lines will be ignored until
283
260
                                                // we get to another BEGIN:VCARD line.
292
269
        {
293
270
                protected byte[] _content = null;
294
271
                protected int _pos = 0;
295
 
                protected int _line = 0;
296
272
 
297
273
                public ContentLineIterator( byte[] content )
298
274
                {
318
294
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
319
295
                                                _pos > initial_pos )? _pos - 1 : _pos;
320
296
                                        _pos++;
321
 
                                        _line++;
322
297
                                        return ByteBuffer.wrap( _content, initial_pos,
323
298
                                                to - initial_pos );
324
299
                                }
327
302
                        if( _pos != initial_pos ) {
328
303
                                int to = _pos;
329
304
                                _pos++;
330
 
                                _line++;
331
305
                                return ByteBuffer.wrap( _content, initial_pos,
332
306
                                        to - initial_pos );
333
307
                        }
352
326
                        return _pos > 0 && _pos < _content.length &&
353
327
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
354
328
                }
355
 
 
356
 
                public int getLineNumber()
357
 
                {
358
 
                        return _line;
359
 
                }
360
329
        }
361
330
 
362
331
        private class Vcard extends ContactData
363
332
        {
364
333
                private final static int NAMELEVEL_NONE = 0;
365
 
                private final static int NAMELEVEL_N = 1;
366
 
                private final static int NAMELEVEL_FN = 2;
 
334
                private final static int NAMELEVEL_FN = 1;
 
335
                private final static int NAMELEVEL_N = 2;
367
336
 
368
337
                private final static int MULTILINE_NONE = 0;
369
338
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
418
387
                }
419
388
 
420
389
                @SuppressWarnings("serial")
421
 
                protected class SkipImportException extends Exception { }
 
390
                protected class SkipContactException extends Exception { }
422
391
 
423
392
                private String extractCollonPartFromLine( ByteBuffer buffer,
424
393
                        String line, boolean former )
460
429
 
461
430
                public void parseLine( ByteBuffer buffer, String line,
462
431
                        boolean next_line_looks_folded )
463
 
                        throws ParseException, SkipImportException,
 
432
                        throws ParseException, SkipContactException,
464
433
                        AbortImportException
465
434
                {
466
435
                        // do we have a version yet?
566
535
                                for( int i = 0; i < name_param_parts.length; i++ )
567
536
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
568
537
 
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
 
 
577
538
                                // parse encoding parameter
578
539
                                String encoding = checkParam( name_param_parts, "ENCODING" );
579
540
                                if( encoding != null ) encoding = encoding.toUpperCase();
580
 
                                if( is_interesting_field && encoding != null &&
581
 
                                        !encoding.equals( "8BIT" ) &&
 
541
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
582
542
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
583
543
                                        //&& !encoding.equals( "BASE64" ) )
584
544
                                {
588
548
                                // parse charset parameter
589
549
                                String charset = checkParam( name_param_parts, "CHARSET" );
590
550
                                if( charset != null ) charset = charset.toUpperCase();
591
 
                                if( charset != null &&
592
 
                                        !charset.equals( "US-ASCII" ) &&
 
551
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
593
552
                                        !charset.equals( "ASCII" ) &&
594
553
                                        !charset.equals( "UTF-8" ) )
595
554
                                {
609
568
                                                _parser_multiline_state = MULTILINE_ENCODED;
610
569
                                }
611
570
 
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
 
                                {
 
571
                                // convert 8-bit ASCII charset to US-ASCII
 
572
                                if( charset == null || charset.equals( "ASCII" ) ) {
619
573
                                        value = transcodeAsciiToUtf8( value );
 
574
                                        charset = "UTF-8";
620
575
                                }
621
576
 
622
 
                                // process charset (value is now in UTF-8)
 
577
                                // process charset
623
578
                                String string_value;
624
579
                                try {
625
580
                                        string_value = new String( value.array(), value.position(),
626
 
                                                value.limit() - value.position(), "UTF-8" );
 
581
                                                value.limit() - value.position(), charset );
627
582
                                } catch( UnsupportedEncodingException e ) {
628
583
                                        throw new ParseException( R.string.error_vcf_charset );
629
584
                                }
676
631
                                        parseEMAIL( name_param_parts, complete_value );
677
632
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
633
                                        parseADR( name_param_parts, complete_value );
679
 
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
 
                                        parseLABEL( name_param_parts, complete_value );
681
634
                        }
682
635
                }
683
636
 
696
649
                        return ( count & 1 ) == 1;
697
650
                }
698
651
 
699
 
                private String[] splitValueByCharacter( String value, char character )
 
652
                private String[] splitValueBySemicolon( String value )
700
653
                {
701
 
                        // split string in to parts by specified character
 
654
                        // split string in to parts by semicolon
702
655
                        ArrayList< String > parts = new ArrayList< String >(
703
 
                                Arrays.asList( value.split( "" + character ) ) );
 
656
                                Arrays.asList( value.split(  ";" ) ) );
704
657
 
705
658
                        // go through parts
706
659
                        for( int a = 0; a < parts.size(); a++ )
714
667
                                if( a < parts.size() - 1 &&
715
668
                                        doesStringEndInAnEscapeChar( str ) )
716
669
                                {
717
 
                                        // append the escaped character, join the next part to this
718
 
                                        // part and remove the next part
 
670
                                        // join the next part to this part and remove the next part
719
671
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
720
 
                                                character + parts.get( a + 1 ) );
 
672
                                                ';' + parts.get( a + 1 ) );
721
673
                                        parts.remove( a + 1 );
722
674
 
723
675
                                        // re-visit this part
734
686
                        return parts.toArray( ret );
735
687
                }
736
688
 
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
 
 
780
689
                private void parseN( String[] params, String value )
781
690
                {
782
691
                        // already got a better name?
783
692
                        if( _name_level >= NAMELEVEL_N ) return;
784
693
 
785
694
                        // get name parts
786
 
                        String[] name_parts = splitValueByCharacter( value, ';' );
 
695
                        String[] name_parts = splitValueBySemicolon( value );
787
696
 
788
697
                        // build name
789
698
                        value = "";
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
 
                                }
 
699
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
 
700
                                value += name_parts[ 1 ];
 
701
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
 
702
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
805
703
 
806
704
                        // set name
807
 
                        setName( unescapeValue( value ) );
 
705
                        setName( value );
808
706
                        _name_level = NAMELEVEL_N;
809
707
                }
810
708
 
814
712
                        if( _name_level >= NAMELEVEL_FN ) return;
815
713
 
816
714
                        // set name
817
 
                        setName( unescapeValue( value ) );
 
715
                        setName( value );
818
716
                        _name_level = NAMELEVEL_FN;
819
717
                }
820
718
 
821
719
                private void parseORG( String[] params, String value )
822
720
                {
823
721
                        // get org parts
824
 
                        String[] org_parts = splitValueByCharacter( value, ';' );
 
722
                        String[] org_parts = splitValueBySemicolon( value );
825
723
                        if( org_parts == null || org_parts.length < 1 ) return;
826
724
 
827
725
                        // build organisation name
829
727
                                String.valueOf( org_parts[ 0 ] ) );
830
728
                        for( int a = 1; a < org_parts.length; a++ )
831
729
                                builder.append( ", " ).append( org_parts[ a ] );
832
 
                        String organisation = unescapeValue( builder.toString() );
 
730
                        String organisation = builder.toString();
833
731
 
834
732
                        // set organisation name (using a title we've previously found)
835
733
                        addOrganisation( organisation, _cached_title, true );
846
744
 
847
745
                private void parseTITLE( String[] params, String value )
848
746
                {
849
 
                        value = unescapeValue( value );
850
 
 
851
747
                        // if we previously had an organisation, look it up and append this
852
748
                        // title to it
853
749
                        if( _cached_organisation != null && hasOrganisations() ) {
910
806
                        else
911
807
                                type = Contacts.ContactMethods.TYPE_HOME;
912
808
 
913
 
                        addEmail( unescapeValue( value ), type, is_preferred );
 
809
                        addEmail( value, type, is_preferred );
914
810
                }
915
811
 
916
812
                private void parseADR( String[] params, String value )
917
813
                {
918
814
                        // get address parts
919
 
                        String[] adr_parts = splitValueByCharacter( value, ';' );
 
815
                        String[] adr_parts = splitValueBySemicolon( value );
920
816
 
921
817
                        // build address
922
818
                        value = "";
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
 
819
                        for( int a = 0; a < adr_parts.length; a++ ) {
 
820
                                if( value.length() > 0 ) value += "\n";
 
821
                                value += adr_parts[ a ].trim();
 
822
                        }
 
823
 
 
824
                        Set< String > types = extractTypes( params, Arrays.asList(
 
825
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
826
 
 
827
                        // add address
 
828
                        int type;
 
829
                        if( types.contains( "WORK" ) )
 
830
                                type = Contacts.ContactMethods.TYPE_WORK;
 
831
                        else
 
832
                                type = Contacts.ContactMethods.TYPE_HOME;
 
833
 
 
834
                        addAddress( value, type );
 
835
                }
 
836
 
 
837
                public void finaliseParsing()
 
838
                        throws ParseException, SkipContactException,
 
839
                        AbortImportException
967
840
                {
968
841
                        // missing version (and data is present)
969
842
                        if( _version == null && _buffers != null )
970
843
                                throw new ParseException( R.string.error_vcf_malformed );
971
844
 
972
 
                        // finalise the parent class
973
 
                        finalise();
 
845
                        // check if we should import this contact
 
846
                        try {
 
847
                                if( !isImportRequired( this ) )
 
848
                                        throw new SkipContactException();
 
849
                        }
 
850
                        catch( ContactNeedsMoreInfoException e ) {
 
851
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
 
852
                        }
974
853
                }
975
854
 
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
 
                 */
983
855
                private String checkParam( String[] params, String name )
984
856
                {
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
 
 
999
857
                        Pattern p = Pattern.compile(
1000
858
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1001
859
                        for( int i = 0; i < params.length; i++ ) {
1002
860
                                Matcher m = p.matcher( params[ i ] );
1003
861
                                if( m.matches() )
1004
 
                                        ret.add( m.group( 2 ) );
 
862
                                        return m.group( 2 );
1005
863
                        }
1006
 
 
1007
 
                        return (String[]) ret.toArray( new String[ ret.size() ] );
 
864
                        return null;
1008
865
                }
1009
866
 
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
 
                 */
1018
867
                private Set< String > extractTypes( String[] params,
1019
868
                        List< String > valid_types )
1020
869
                {
1021
870
                        HashSet< String > types = new HashSet< String >();
1022
871
 
1023
872
                        // get 3.0-style TYPE= param
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( "," );
 
873
                        String type_param;
 
874
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
875
                                String[] parts = type_param.split( "," );
1030
876
                                for( int i = 0; i < parts.length; i++ )
1031
877
                                        if( valid_types.contains( parts[ i ] ) )
1032
878
                                                types.add( parts[ i ] );