/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: Tim Marston
  • Date: 2013-10-20 17:52:21 UTC
  • Revision ID: tim@ed.am-20131020175221-7f9i6dhl55xz39ui
eclipse file changes (who knows!)

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 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
 
7
 * to as "this program").  For more information, see
8
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
133
133
                        boolean in_vcard = false;
134
134
                        while( ( line = reader.readLine() ) != null )
135
135
                        {
136
 
                                if( !in_vcard ) {
 
136
                                if( !in_vcard )
 
137
                                {
137
138
                                        // look for vcard beginning
138
 
                                        if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
139
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
139
140
                                                in_vcard = true;
140
141
                                                _vcard_count++;
141
142
                                        }
 
143
                                        // check for vMsg files
 
144
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
 
145
                                                showError( getText( R.string.error_vcf_vmsgfile )
 
146
                                                        + file.getName() );
 
147
                                        }
142
148
                                }
143
 
                                else if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
149
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
144
150
                                        in_vcard = false;
145
151
                        }
146
152
 
203
209
 
204
210
                        if( vcard == null ) {
205
211
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
212
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
207
213
                                        setProgress( _progress++ );
208
214
                                        vcard = new Vcard();
209
215
                                        vcard_start_line = cli.getLineNumber();
211
217
                        }
212
218
                        else {
213
219
                                // look for vcard content or ending
214
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
220
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
215
221
                                {
216
222
                                        // finalise the vcard/contact
217
223
                                        try {
267
273
                                                        finish( ACTION_ABORT );
268
274
                                                }
269
275
 
270
 
                                                // although we're continuing, we still need to abort
271
 
                                                // this vCard. Further lines will be ignored until we
 
276
                                                // Although we're continuing, we still need to abort
 
277
                                                // this vCard.  Further lines will be ignored until we
272
278
                                                // get to another BEGIN:VCARD line.
273
279
                                                vcard = null;
274
280
                                        }
275
281
                                        catch( Vcard.SkipImportException e ) {
276
282
                                                skipContact();
277
 
                                                // abort this vCard. Further lines will be ignored until
 
283
                                                // Abort this vCard.  Further lines will be ignored until
278
284
                                                // we get to another BEGIN:VCARD line.
279
285
                                                vcard = null;
280
286
                                        }
465
471
                private String extractCollonPartFromLine( ContentLine content_line,
466
472
                        boolean former )
467
473
                {
468
 
                        String ret = null;
469
 
 
470
474
                        // split line into name and value parts and check to make sure we
471
475
                        // only got 2 parts and that the first part is not zero in length
472
476
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
473
477
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
474
 
                                ret = parts[ former? 0 : 1 ];
 
478
                                return parts[ former? 0 : 1 ].trim();
475
479
 
476
 
                        return ret;
 
480
                        return null;
477
481
                }
478
482
 
479
483
                private String extractNameAndParamsFromLine( ContentLine content_line )
480
484
                {
481
 
                        return extractCollonPartFromLine( content_line, true ).trim();
 
485
                        return extractCollonPartFromLine( content_line, true );
482
486
                }
483
487
 
484
488
                private String extractValueFromLine( ContentLine content_line )
502
506
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
503
507
                                {
504
508
                                        // yes, get it!
505
 
                                        String value = extractValueFromLine( content_line ).trim();
506
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
509
                                        String value = extractValueFromLine( content_line );
 
510
                                        if( value == null || (
 
511
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
512
                                        {
507
513
                                                throw new ParseException( R.string.error_vcf_version );
 
514
                                        }
508
515
                                        _version = value;
509
516
 
510
517
                                        // parse any buffers we've been accumulating while we waited
717
724
                                        parseLABEL( name_param_parts, complete_value );
718
725
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
719
726
                                        parseNOTE( name_param_parts, complete_value );
 
727
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
728
                                        parseBDAY( name_param_parts, complete_value );
720
729
                        }
721
730
                }
722
731
 
746
755
                        {
747
756
                                String str = parts.get( a );
748
757
 
749
 
                                // look for parts that end in an escape character, but ignore
750
 
                                // the final part. We've already detected escape chars at the
 
758
                                // Look for parts that end in an escape character, but ignore
 
759
                                // the final part.  We've already detected escape chars at the
751
760
                                // end of the final part in parseLine() and handled multi-lines
752
761
                                // accordingly.
753
762
                                if( a < parts.size() - 1 &&
1023
1032
                        addNote( unescapeValue( value ) );
1024
1033
                }
1025
1034
 
 
1035
                private void parseBDAY( String[] params, String value )
 
1036
                {
 
1037
                        setBirthday( value );
 
1038
                }
 
1039
 
1026
1040
                public void finaliseVcard()
1027
1041
                        throws ParseException, ContactNotIdentifiableException
1028
1042
                {
1036
1050
 
1037
1051
                /**
1038
1052
                 * Amongst the params, find the value of the first, only, of any with
1039
 
                 * the specified name
 
1053
                 * the specified name.
 
1054
                 *
1040
1055
                 * @param params
1041
1056
                 * @param name
1042
1057
                 * @return a value, or null
1048
1063
                }
1049
1064
 
1050
1065
                /**
1051
 
                 * Amongst the params, find the values of any with the specified name
 
1066
                 * Amongst the params, find the values of any with the specified name.
 
1067
                 *
1052
1068
                 * @param params
1053
1069
                 * @param name
1054
1070
                 * @return an array of values, or null
1070
1086
                }
1071
1087
 
1072
1088
                /**
1073
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1074
 
                 * those types are just parameters. For v3.0, they are prefixed with
1075
 
                 * "TYPE=". There may also be multiple type parameters.
 
1089
                 * Amongst the params, return any type values present.  For v2.1 vCards,
 
1090
                 * those types are just parameters.  For v3.0, they are prefixed with
 
1091
                 * "TYPE=".  There may also be multiple type parameters.
 
1092
                 *
1076
1093
                 * @param params an array of params to look for types in
1077
1094
                 * @param valid_types an list of upper-case type values to look for
1078
1095
                 * @return a set of present type values
1130
1147
                                else if( ch == '=' && i == in.limit() - 1 )
1131
1148
                                {
1132
1149
                                        // we found a '=' at the end of a line signifying a multi-
1133
 
                                        // line string, so we don't add it.
 
1150
                                        // line string, so we don't add it
1134
1151
                                        another = true;
1135
1152
                                        continue;
1136
1153
                                }