4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/import-contacts
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package org.waxworlds.edam.importcontacts;
1
package org.waxworlds.importcontacts;
26
3
import java.io.BufferedReader;
27
4
import java.io.File;
28
import java.io.FileInputStream;
29
5
import java.io.FileNotFoundException;
30
6
import java.io.FileReader;
31
7
import java.io.FilenameFilter;
32
8
import java.io.IOException;
33
9
import java.io.UnsupportedEncodingException;
34
import java.nio.ByteBuffer;
35
10
import java.util.Arrays;
36
11
import java.util.HashSet;
37
12
import java.util.List;
151
117
private void importVCardFile( File file ) throws AbortImportException
153
// check file is good
155
showError( getText( R.string.error_filenotfound ) +
157
if( file.length() == 0 )
158
showError( getText( R.string.error_fileisempty ) +
164
FileInputStream istream = new FileInputStream( file );
165
byte[] content = new byte[ (int)file.length() ];
166
istream.read( content );
169
importVCardFileContent( content, file.getName() );
122
BufferedReader reader = new BufferedReader(
123
new FileReader( file ) );
126
StringBuffer content = new StringBuffer();
128
while( ( line = reader.readLine() ) != null )
129
content.append( line ).append( "\n" );
131
importVCardFileContent( content.toString(), file.getName() );
171
133
catch( FileNotFoundException e ) {
172
showError( getText( R.string.error_filenotfound ) +
134
showError( getText( R.string.error_filenotfound ) + file.getName() );
175
136
catch( IOException e ) {
176
137
showError( getText( R.string.error_ioerror ) + file.getName() );
180
private void importVCardFileContent( byte[] content, String fileName )
141
private void importVCardFileContent( String content, String fileName )
181
142
throws AbortImportException
183
ByteBuffer buffers[] = getLinesFromContent( content );
144
// unfold RFC2425 section 5.8.1 folded lines, except that we must also
145
// handle embedded Quoted-Printable encodings that have a trailing '='.
146
// So we remove these first before doing RFC2425 unfolding.
147
content = content.replaceAll( "=\n[ \\t]", "" )
148
.replaceAll( "\n[ \\t]", "" );
150
// get lines and parse them
151
String[] lines = content.split( "\n" );
186
152
VCard vCard = null;
187
for( int i = 0; i < buffers.length; i++ )
153
for( int i = 0; i < lines.length; i++ )
189
// get a US-ASCII version of the line for processing
192
line = new String( buffers[ i ].array(), buffers[ i ].position(),
193
buffers[ i ].limit() - buffers[ i ].position(), "US-ASCII" );
195
catch( UnsupportedEncodingException e ) {
196
// we know US-ASCII is supported, so appease the compiler...
155
String line = lines[ i ];
200
157
if( vCard == null ) {
201
158
// look for vcard beginning
334
@SuppressWarnings("serial")
335
239
protected class SkipContactException extends Exception { }
337
public void parseLine( ByteBuffer buffer )
241
public void parseLine( String line )
338
242
throws ParseException, SkipContactException,
339
243
AbortImportException
341
// get a US-ASCII version of the line for processing
344
line = new String( buffer.array(), buffer.position(),
345
buffer.limit() - buffer.position(), "US-ASCII" );
347
catch( UnsupportedEncodingException e ) {
348
// we know US-ASCII is supported, so appease the compiler...
352
// ignore empty lines
353
if( line.trim() == "" ) return;
355
// split line into name and value parts (this may turn out to be
356
// unwanted if the line is a subsequent line in a multi-line
357
// value, but we have to do this now to check for and handle VCF
358
// versions first). Also, the value part is only created tentatively
359
// because it may have an encoding/charset. Since we're treating it
360
// as UTF-8 (which is compatible with 7-bit US-ASCII) this is ok
361
// though so long as we later use the raw bytes. ALso we check for
362
// malformed property:name pairs.
363
String name_and_params, string_value;
365
String[] parts = line.split( ":", 2 );
366
if( parts.length == 2 ) {
367
name_and_params = parts[ 0 ].trim();
368
string_value = parts[ 1 ].trim();
369
if( name_and_params.length() == 0 )
370
throw new ParseException( R.string.error_vcf_malformed );
374
if( !_parser_in_multiline )
375
throw new ParseException( R.string.error_vcf_malformed );
376
name_and_params = null;
381
// if we haven't yet got a version, we won't be paring anything!
245
// get property halves
246
String[] props = line.split( ":" );
247
for( int i = 0; i < props.length; i++ )
248
props[ i ] = props[ i ].trim();
249
if( props.length < 2 ||
250
props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
251
throw new ParseException( R.string.error_vcf_malformed );
382
253
if( _version == null )
384
// is this a version?
385
if( name_and_params.equals( "VERSION" ) )
255
if( props[ 0 ].equals( "VERSION" ) )
387
// yes, check/store it
388
if( !string_value.equals( "2.1" ) &&
389
!string_value.equals( "3.0" ) )
258
if( !props[ 1 ].equals( "2.1" ) &&
259
!props[ 1 ].equals( "3.0" ) )
390
260
throw new ParseException( R.string.error_vcf_version );
391
_version = string_value;
261
_version = props[ 1 ];
393
// parse any other buffers we've accumulated so far
394
if( _buffers != null )
395
for( int i = 0; i < _buffers.size(); i++ )
396
parseLine( _buffers.get( i ) );
263
// parse any other lines we've accumulated so far
265
for( int i = 0; i < _lines.size(); i++ )
266
parseLine( _lines.get( i ) );
401
// no, so stash this buffer till we have a version
402
if( _buffers == null )
403
_buffers = new Vector< ByteBuffer >();
404
_buffers.add( buffer );
271
// stash this line till we have a version
273
_lines = new Vector< String >();
409
// value bytes, for processing
412
if( _parser_in_multiline )
414
// if we're currently in a multi-line value, use the stored
415
// property name and parameters
416
name_and_params = _parser_current_name_and_params;
418
// find start of string (skip spaces/tabs)
419
int pos = buffer.position();
420
byte[] buffer_array = buffer.array();
421
while( pos < buffer.limit() && (
422
buffer_array[ pos ] == ' ' ||
423
buffer_array[ pos ] == '\t' ) )
428
// get value from buffer
429
value = ByteBuffer.wrap( buffer.array(), pos,
430
buffer.limit() - pos );
434
// ignore empty values
435
if( string_value.length() < 1 ) return;
437
// calculate how many chars to skip from beginning of line
438
// so we skip the property "name:" part
439
int pos = buffer.position() + name_and_params.length() + 1;
441
// get value from buffer
442
value = ByteBuffer.wrap( buffer.array(), pos,
443
buffer.limit() - pos );
445
// reset the saved multi-line state
446
_parser_current_name_and_params = name_and_params;
447
_parser_buffered_value_so_far = "";
450
279
// get parameter parts
451
String[] name_param_parts = name_and_params.split( ";", -1 );
452
for( int i = 0; i < name_param_parts.length; i++ )
453
name_param_parts[ i ] = name_param_parts[ i ].trim();
455
// parse encoding parameter
456
String encoding = checkParam( name_param_parts, "ENCODING" );
457
if( encoding != null ) encoding = encoding.toUpperCase();
458
if( encoding != null && !encoding.equals( "8BIT" ) &&
459
!encoding.equals( "QUOTED-PRINTABLE" ) )
460
//&& !encoding.equals( "BASE64" ) )
462
throw new ParseException( R.string.error_vcf_encoding );
465
// parse charset parameter
466
String charset = checkParam( name_param_parts, "CHARSET" );
467
if( charset != null ) charset = charset.toUpperCase();
468
if( charset != null && !charset.equals( "US-ASCII" ) &&
469
!charset.equals( "ASCII" ) && !charset.equals( "UTF-8" ) )
471
throw new ParseException( R.string.error_vcf_charset );
474
// do unencoding (or default to a fake unencoding result with
476
UnencodeResult unencoding_result = null;
477
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
478
unencoding_result = unencodeQuotedPrintable( value );
479
// else if( encoding != null && encoding.equals( "BASE64" ) )
480
// result = unencodeBase64( props[ 1 ], charset );
481
if( unencoding_result != null ) {
482
value = unencoding_result.getBuffer();
483
_parser_in_multiline =
484
unencoding_result.isAnotherLineRequired();
487
// convert 8-bit ASCII charset to US-ASCII
488
if( charset == null || charset == "ASCII" ) {
489
value = transcodeAsciiToUtf8( value );
496
new String( value.array(), value.position(),
497
value.limit() - value.position(), charset );
498
} catch( UnsupportedEncodingException e ) {
499
throw new ParseException( R.string.error_vcf_charset );
502
// handle multi-line requests
503
if( _parser_in_multiline ) {
504
_parser_buffered_value_so_far += string_value;
508
// add on buffered multi-line content
509
String complete_value =
510
_parser_buffered_value_so_far + string_value;
280
String[] params = props[ 0 ].split( ";" );
281
for( int i = 0; i < params.length; i++ )
282
params[ i ] = params[ i ].trim();
512
284
// parse some properties
513
if( name_param_parts[ 0 ].equals( "N" ) )
514
parseN( name_param_parts, complete_value );
515
else if( name_param_parts[ 0 ].equals( "FN" ) )
516
parseFN( name_param_parts, complete_value );
517
else if( name_param_parts[ 0 ].equals( "ORG" ) )
518
parseORG( name_param_parts, complete_value );
519
else if( name_param_parts[ 0 ].equals( "TEL" ) )
520
parseTEL( name_param_parts, complete_value );
521
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
522
parseEMAIL( name_param_parts, complete_value );
285
if( params[ 0 ].equals( "N" ) )
286
parseN( params, props[ 1 ] );
287
else if( params[ 0 ].equals( "FN" ) )
288
parseFN( params, props[ 1 ] );
289
else if( params[ 0 ].equals( "ORG" ) )
290
parseORG( params, props[ 1 ] );
291
else if( params[ 0 ].equals( "TEL" ) )
292
parseTEL( params, props[ 1 ] );
293
else if( params[ 0 ].equals( "EMAIL" ) )
294
parseEMAIL( params, props[ 1 ] );
556
328
throws ParseException, SkipContactException
558
330
// already got a better name?
559
if( _name_level >= NAMELEVEL_FN ) return;
331
if( _nameLevel >= NAMELEVEL_FN ) return;
563
_name_level = NAMELEVEL_FN;
334
setName( undoCharsetAndEncoding( params, value ) );
335
_nameLevel = NAMELEVEL_FN;
566
338
private void parseORG( String[] params, String value )
567
339
throws ParseException, SkipContactException
569
341
// already got a better name?
570
if( _name_level >= NAMELEVEL_ORG ) return;
342
if( _nameLevel >= NAMELEVEL_ORG ) return;
573
String[] org_parts = value.split( ";" );
574
for( int i = 0; i < org_parts.length; i++ )
575
org_parts[ i ] = org_parts[ i ].trim();
345
String[] orgparts = value.split( ";" );
346
for( int i = 0; i < orgparts.length; i++ )
347
orgparts[ i ] = orgparts[ i ].trim();
578
if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
579
value = org_parts[ 1 ];
350
if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
351
value = orgparts[ 1 ];
581
value = org_parts[ 0 ];
353
value = orgparts[ 0 ];
585
_name_level = NAMELEVEL_ORG;
356
setName( undoCharsetAndEncoding( params, value ) );
357
_nameLevel = NAMELEVEL_ORG;
588
360
private void parseTEL( String[] params, String value )
597
369
// here's the logic...
598
370
boolean preferred = types.contains( "PREF" );
599
int type = PhonesColumns.TYPE_MOBILE;
600
371
if( types.contains( "VOICE" ) )
601
372
if( types.contains( "WORK" ) )
602
type = PhonesColumns.TYPE_WORK;
373
addPhone( value, PhonesColumns.TYPE_WORK, preferred );
604
type = PhonesColumns.TYPE_HOME;
375
addPhone( value, PhonesColumns.TYPE_HOME, preferred );
605
376
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
606
type = PhonesColumns.TYPE_MOBILE;
377
addPhone( value, PhonesColumns.TYPE_MOBILE, preferred );
607
378
if( types.contains( "FAX" ) )
608
379
if( types.contains( "HOME" ) )
609
type = PhonesColumns.TYPE_FAX_HOME;
380
addPhone( value, PhonesColumns.TYPE_FAX_HOME, preferred );
611
type = PhonesColumns.TYPE_FAX_WORK;
382
addPhone( value, PhonesColumns.TYPE_FAX_WORK, preferred );
612
383
if( types.contains( "PAGER" ) )
613
type = PhonesColumns.TYPE_PAGER;
616
addPhone( value, type, preferred );
384
addPhone( value, PhonesColumns.TYPE_PAGER, preferred );
619
387
public void parseEMAIL( String[] params, String value )
620
throws ParseException
622
389
if( value.length() == 0 ) return;
637
404
AbortImportException
639
406
// missing version (and data is present)
640
if( _version == null && _buffers != null )
407
if( _version == null && _lines != null )
641
408
throw new ParseException( R.string.error_vcf_malformed );
643
410
// missing name properties?
644
if( _name_level == NAMELEVEL_NONE )
411
if( _nameLevel == NAMELEVEL_NONE )
645
412
throw new ParseException( R.string.error_vcf_noname );
647
414
// check if we should import this one? If we've already got an 'N'-
648
415
// type name, this will already have been done by parseN() so we
649
416
// mustn't do this here (or it could prompt twice!)
650
if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
417
if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
651
418
throw new SkipContactException();
421
private String undoCharsetAndEncoding( String[] params, String value )
422
throws ParseException
424
// check encoding/charset
425
String charset, encoding;
426
if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
427
!charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
428
throw new ParseException( R.string.error_vcf_charset );
429
if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
430
!encoding.equals( "QUOTED-PRINTABLE" ) )
431
throw new ParseException( R.string.error_vcf_encoding );
434
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
435
return unencodeQuotedPrintable( value, charset );
654
441
private String checkParam( String[] params, String name )
656
443
Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
665
452
private Set< String > extractTypes( String[] params,
666
List< String > valid_types )
453
List< String > validTypes )
668
455
HashSet< String > types = new HashSet< String >();
670
457
// get 3.0-style TYPE= param
672
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
673
String[] parts = type_param.split( "," );
674
for( int i = 0; i < parts.length; i++ )
675
if( valid_types.contains( parts[ i ] ) )
676
types.add( parts[ i ] );
459
if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
460
String[] bits = typeParam.split( "," );
461
for( int i = 0; i < bits.length; i++ )
462
if( validTypes.contains( bits[ i ] ) )
463
types.add( bits[ i ] );
679
466
// get 2.1-style type param
680
467
if( _version.equals( "2.1" ) ) {
681
468
for( int i = 1; i < params.length; i++ )
682
if( valid_types.contains( params[ i ] ) )
469
if( validTypes.contains( params[ i ] ) )
683
470
types.add( params[ i ] );
689
private UnencodeResult unencodeQuotedPrintable( ByteBuffer in )
476
private String unencodeQuotedPrintable( String str, String charset )
691
boolean another = false;
478
// default encoding scheme
479
if( charset == null ) charset = "UTF-8";
693
481
// unencode quoted-pritable encoding, as per RFC1521 section 5.1
694
byte[] out = new byte[ in.limit() - in.position() ];
482
byte[] bytes = new byte[ str.length() ];
696
for( int i = in.position(); i < in.limit(); i++ )
698
// get next char and process...
699
byte ch = in.array()[ i ];
700
if( ch == '=' && i < in.limit() - 2 )
702
// we found a =XX format byte, add it
704
Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
705
Character.digit( in.array()[ i + 2 ], 16 ) );
484
for( int i = 0; i < str.length(); i++, j++ ) {
485
char ch = str.charAt( i );
486
if( ch == '=' && i < str.length() - 2 ) {
488
Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
489
Character.digit( str.charAt( i + 2 ), 16 ) );
708
else if( ch == '=' && i == in.limit() - 1 )
710
// we found a '=' at the end of a line signifying a multi-
711
// line string, so we don't add it.
716
// just a normal char...
721
return new UnencodeResult( another, ByteBuffer.wrap( out, 0, j ) );
724
private ByteBuffer transcodeAsciiToUtf8( ByteBuffer in )
727
byte[] out = new byte[ ( in.limit() - in.position() ) * 2 ];
729
for( int a = in.position(); a < in.limit(); a++ )
731
// if char is < 127, keep it as-is
732
if( in.array()[ a ] >= 0 )
733
out[ j++ ] = in.array()[ a ];
735
// else, convert it to UTF-8
737
int b = 0xff & (int)in.array()[ a ];
738
out[ j++ ] = (byte)( 0xc0 | ( b >> 6 ) );
739
out[ j++ ] = (byte)( 0x80 | ( b & 0x3f ) );
743
return ByteBuffer.wrap( out, 0, j );
493
bytes[ j ] = (byte)ch;
496
return new String( bytes, 0, j, charset );
497
} catch( UnsupportedEncodingException e ) { }