/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-05-10 12:05:10 UTC
  • Revision ID: tim@ed.am-20130510120510-xmjdrd7c7gje5shl
make checks for BEGIN:VCARD and END:VCARD case insensitive

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
149
149
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
150
150
                                        in_vcard = false;
151
151
                        }
152
 
                        reader.close();
153
152
 
154
153
                }
155
154
                catch( FileNotFoundException e ) {
177
176
                        FileInputStream istream = new FileInputStream( file );
178
177
                        byte[] content = new byte[ (int)file.length() ];
179
178
                        istream.read( content );
180
 
                        istream.close();
 
179
                        istream = null;
181
180
 
182
181
                        // import
183
182
                        importVCardFileContent( content, file.getName() );
274
273
                                                        finish( ACTION_ABORT );
275
274
                                                }
276
275
 
277
 
                                                // Although we're continuing, we still need to abort
278
 
                                                // 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
279
278
                                                // get to another BEGIN:VCARD line.
280
279
                                                vcard = null;
281
280
                                        }
282
281
                                        catch( Vcard.SkipImportException e ) {
283
282
                                                skipContact();
284
 
                                                // Abort this vCard.  Further lines will be ignored until
 
283
                                                // abort this vCard. Further lines will be ignored until
285
284
                                                // we get to another BEGIN:VCARD line.
286
285
                                                vcard = null;
287
286
                                        }
472
471
                private String extractCollonPartFromLine( ContentLine content_line,
473
472
                        boolean former )
474
473
                {
 
474
                        String ret = null;
 
475
 
475
476
                        // split line into name and value parts and check to make sure we
476
477
                        // only got 2 parts and that the first part is not zero in length
477
478
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
478
479
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
479
 
                                return parts[ former? 0 : 1 ].trim();
 
480
                                ret = parts[ former? 0 : 1 ];
480
481
 
481
 
                        return null;
 
482
                        return ret;
482
483
                }
483
484
 
484
485
                private String extractNameAndParamsFromLine( ContentLine content_line )
485
486
                {
486
 
                        return extractCollonPartFromLine( content_line, true );
 
487
                        return extractCollonPartFromLine( content_line, true ).trim();
487
488
                }
488
489
 
489
490
                private String extractValueFromLine( ContentLine content_line )
507
508
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
508
509
                                {
509
510
                                        // yes, get it!
510
 
                                        String value = extractValueFromLine( content_line );
511
 
                                        if( value == null || (
512
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
513
 
                                        {
 
511
                                        String value = extractValueFromLine( content_line ).trim();
 
512
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
514
513
                                                throw new ParseException( R.string.error_vcf_version );
515
 
                                        }
516
514
                                        _version = value;
517
515
 
518
516
                                        // parse any buffers we've been accumulating while we waited
725
723
                                        parseLABEL( name_param_parts, complete_value );
726
724
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
727
725
                                        parseNOTE( name_param_parts, complete_value );
728
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
729
 
                                        parseBDAY( name_param_parts, complete_value );
730
726
                        }
731
727
                }
732
728
 
756
752
                        {
757
753
                                String str = parts.get( a );
758
754
 
759
 
                                // Look for parts that end in an escape character, but ignore
760
 
                                // the final part.  We've already detected escape chars at the
 
755
                                // look for parts that end in an escape character, but ignore
 
756
                                // the final part. We've already detected escape chars at the
761
757
                                // end of the final part in parseLine() and handled multi-lines
762
758
                                // accordingly.
763
759
                                if( a < parts.size() - 1 &&
1033
1029
                        addNote( unescapeValue( value ) );
1034
1030
                }
1035
1031
 
1036
 
                private void parseBDAY( String[] params, String value )
1037
 
                {
1038
 
                        setBirthday( value );
1039
 
                }
1040
 
 
1041
1032
                public void finaliseVcard()
1042
1033
                        throws ParseException, ContactNotIdentifiableException
1043
1034
                {
1051
1042
 
1052
1043
                /**
1053
1044
                 * Amongst the params, find the value of the first, only, of any with
1054
 
                 * the specified name.
1055
 
                 *
 
1045
                 * the specified name
1056
1046
                 * @param params
1057
1047
                 * @param name
1058
1048
                 * @return a value, or null
1064
1054
                }
1065
1055
 
1066
1056
                /**
1067
 
                 * Amongst the params, find the values of any with the specified name.
1068
 
                 *
 
1057
                 * Amongst the params, find the values of any with the specified name
1069
1058
                 * @param params
1070
1059
                 * @param name
1071
1060
                 * @return an array of values, or null
1087
1076
                }
1088
1077
 
1089
1078
                /**
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
 
                 *
 
1079
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1080
                 * those types are just parameters. For v3.0, they are prefixed with
 
1081
                 * "TYPE=". There may also be multiple type parameters.
1094
1082
                 * @param params an array of params to look for types in
1095
1083
                 * @param valid_types an list of upper-case type values to look for
1096
1084
                 * @return a set of present type values
1148
1136
                                else if( ch == '=' && i == in.limit() - 1 )
1149
1137
                                {
1150
1138
                                        // we found a '=' at the end of a line signifying a multi-
1151
 
                                        // line string, so we don't add it
 
1139
                                        // line string, so we don't add it.
1152
1140
                                        another = true;
1153
1141
                                        continue;
1154
1142
                                }