/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-12-08 18:43:52 UTC
  • Revision ID: tim@ed.am-20131208184352-0giww6zoy58bx07h
close some streams properly

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