/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-23 09:57:22 UTC
  • Revision ID: tim@ed.am-20121223095722-ep12gv4433ahh6vz
Tags: 1.3
fixed version no. in news

Show diffs side-by-side

added added

removed removed

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