180
183
private void importVCardFileContent( byte[] content, String fileName )
181
throws AbortImportException
184
throws AbortImportException
183
ByteBuffer buffers[] = getLinesFromContent( content );
185
186
// go through lines
186
187
VCard vCard = null;
187
for( int i = 0; i < buffers.length; i++ )
188
ContentLineIterator cli = new ContentLineIterator( content );
189
while( cli.hasNext() )
191
ByteBuffer buffer = cli.next();
189
193
// 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" );
196
line = new String( buffer.array(), buffer.position(),
197
buffer.limit() - buffer.position(), "US-ASCII" );
195
199
catch( UnsupportedEncodingException e ) {
196
200
// we know US-ASCII is supported, so appease the compiler...
258
private ByteBuffer[] getLinesFromContent( byte[] content )
267
class ContentLineIterator implements Iterator< ByteBuffer >
260
// count lines in data
262
for( int a = 0; a < content.length; a++ )
263
if( content[ a ] == '\n' )
266
// get lines, removing \r's and \n's as we go
267
ByteBuffer lines[] = new ByteBuffer[ num_lines ];
269
for( int a = 0, b = 0; a < content.length; a++ )
270
if( content[ a ] == '\n' ) {
271
int to = ( a > 0 && content[ a - 1 ] == '\r' &&
272
a - 1 >= last )? a - 1 : a;
273
lines[ b++ ] = ByteBuffer.wrap( content, last, to - last );
269
protected byte[] _content = null;
270
protected int _pos = 0;
272
public ContentLineIterator( byte[] content )
278
public boolean hasNext()
280
return _pos < _content.length;
284
public ByteBuffer next()
286
int initial_pos = _pos;
289
for( ; _pos < _content.length; _pos++ )
290
if( _content[ _pos ] == '\n' )
292
// adjust for a \r preceding the \n
293
int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
294
_pos > initial_pos )? _pos - 1 : _pos;
296
return ByteBuffer.wrap( _content, initial_pos,
300
// we didn't find one, but were there bytes left?
301
if( _pos != initial_pos ) {
304
return ByteBuffer.wrap( _content, initial_pos,
276
lines[ lines.length - 1 ] = ByteBuffer.wrap( content, last,
277
content.length - last );
309
throw new NoSuchElementException();
315
throw new UnsupportedOperationException();
319
* Does the next line, if there is one, look like it should be folded
320
* onto the end of this one?
323
public boolean doesNextLineLookFolded()
325
return _pos > 0 && _pos < _content.length &&
326
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
282
330
private class VCard extends ContactData
334
383
@SuppressWarnings("serial")
335
384
protected class SkipContactException extends Exception { }
337
public void parseLine( ByteBuffer buffer )
338
throws ParseException, SkipContactException,
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[] bits = line.split( ":", 2 );
366
if( bits.length == 2 ) {
367
name_and_params = bits[ 0 ].trim();
368
string_value = bits[ 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!
386
private String extractCollonPartFromLine( ByteBuffer buffer,
387
String line, boolean former )
391
// get a US-ASCII version of the line for processing, unless we were
395
line = new String( buffer.array(), buffer.position(),
396
buffer.limit() - buffer.position(), "US-ASCII" );
398
catch( UnsupportedEncodingException e ) {
399
// we know US-ASCII is supported, so appease the compiler...
404
// split line into name and value parts and check to make sure we
405
// only got 2 parts and that the first part is not zero in length
406
String[] parts = line.split( ":", 2 );
407
if( parts.length == 2 && parts[ 0 ].length() > 0 )
408
ret = parts[ former? 0 : 1 ];
413
private String extractNameAndParamsFromLine( ByteBuffer buffer,
416
return extractCollonPartFromLine( buffer, line, true );
419
private String extractValueFromLine( ByteBuffer buffer, String line )
421
return extractCollonPartFromLine( buffer, line, false );
424
public void parseLine( ByteBuffer buffer, String line,
425
boolean next_line_looks_folded )
426
throws ParseException, SkipContactException,
429
// do we have a version yet?
382
430
if( _version == null )
384
// is this a version?
385
if( name_and_params.equals( "VERSION" ) )
432
// tentatively get name and params from line
433
String name_and_params =
434
extractNameAndParamsFromLine( buffer, line );
436
// is it a version line?
437
if( name_and_params != null &&
438
name_and_params.equals( "VERSION" ) )
387
// yes, check/store it
388
if( !string_value.equals( "2.1" ) &&
389
!string_value.equals( "3.0" ) )
441
String value = extractValueFromLine( buffer, line );
442
if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
390
443
throw new ParseException( R.string.error_vcf_version );
391
_version = string_value;
393
// parse any other buffers we've accumulated so far
446
// parse any buffers we've been accumulating while we waited
394
448
if( _buffers != null )
395
449
for( int i = 0; i < _buffers.size(); i++ )
396
parseLine( _buffers.get( i ) );
450
parseLine( _buffers.get( i ), null,
451
i + 1 < _buffers.size() &&
452
_buffers.get( i + 1 ).hasRemaining() &&
453
_buffers.get( i + 1 ).get(
454
_buffers.get( i + 1 ).position() ) == ' ' );
401
// no, so stash this buffer till we have a version
459
// no, so stash this line till we get a version
402
460
if( _buffers == null )
403
461
_buffers = new Vector< ByteBuffer >();
404
462
_buffers.add( buffer );
409
// value bytes, for processing
467
// name and params and the position in the buffer where the
468
// "value" part of the line start
469
String name_and_params;
412
if( _parser_in_multiline )
472
if( _parser_in_encoded_multiline ||
473
_parser_in_folded_multiline )
414
475
// if we're currently in a multi-line value, use the stored
415
476
// property name and parameters
416
477
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' ) )
479
pos = buffer.position();
481
// for folded multi-lines, skip the single space at the
482
// start of the next line
483
if( _parser_in_folded_multiline )
428
// get value from buffer
429
value = ByteBuffer.wrap( buffer.array(), pos,
430
buffer.limit() - pos );
486
// else, this must be an encoded multi-line, so skip any
487
// whitespace we find at the start of the next line
489
while( pos < buffer.limit() && (
490
buffer.get( pos ) == ' ' ||
491
buffer.get( pos ) == '\t' ) )
434
// ignore empty values
435
if( string_value.length() < 1 ) return;
498
// get name and params from line, and since we're not
499
// parsing a subsequent line in a multi-line, this should
500
// not fail, or it's an error
502
extractNameAndParamsFromLine( buffer, line );
503
if( name_and_params == null )
504
throw new ParseException(
505
R.string.error_vcf_malformed );
437
507
// calculate how many chars to skip from beginning of line
438
508
// 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 );
509
pos = buffer.position() + name_and_params.length() + 1;
445
511
// reset the saved multi-line state
446
512
_parser_current_name_and_params = name_and_params;
447
513
_parser_buffered_value_so_far = "";
516
// get value from buffer, as raw bytes
518
value = ByteBuffer.wrap( buffer.array(), pos,
519
buffer.limit() - pos );
450
521
// get parameter parts
451
String[] name_and_param_bits = name_and_params.split( ";" );
452
for( int i = 0; i < name_and_param_bits.length; i++ )
453
name_and_param_bits[ i ] = name_and_param_bits[ i ].trim();
522
String[] name_param_parts = name_and_params.split( ";", -1 );
523
for( int i = 0; i < name_param_parts.length; i++ )
524
name_param_parts[ i ] = name_param_parts[ i ].trim();
455
526
// parse encoding parameter
456
String encoding = checkParam( name_and_param_bits, "ENCODING" );
527
String encoding = checkParam( name_param_parts, "ENCODING" );
457
528
if( encoding != null ) encoding = encoding.toUpperCase();
458
529
if( encoding != null && !encoding.equals( "8BIT" ) &&
459
530
!encoding.equals( "QUOTED-PRINTABLE" ) )
477
549
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
478
550
unencoding_result = unencodeQuotedPrintable( value );
479
551
// else if( encoding != null && encoding.equals( "BASE64" ) )
480
// result = unencodeBase64( props[ 1 ], charset );
552
// unencoding_result = unencodeBase64( props[ 1 ], charset );
481
553
if( unencoding_result != null ) {
482
554
value = unencoding_result.getBuffer();
483
_parser_in_multiline =
555
_parser_in_encoded_multiline =
484
556
unencoding_result.isAnotherLineRequired();
487
559
// convert 8-bit ASCII charset to US-ASCII
488
if( charset == null || charset == "ASCII" ) {
560
if( charset == null || charset.equals( "ASCII" ) ) {
489
561
value = transcodeAsciiToUtf8( value );
490
562
charset = "UTF-8";
493
565
// process charset
496
new String( value.array(), value.position(),
497
value.limit() - value.position(), charset );
568
string_value = new String( value.array(), value.position(),
569
value.limit() - value.position(), charset );
498
570
} catch( UnsupportedEncodingException e ) {
499
571
throw new ParseException( R.string.error_vcf_charset );
574
// now we know whether we're in an encoding multi-line,
575
// determine if we're in a v3 folded multi-line or not
576
_parser_in_folded_multiline = !_parser_in_encoded_multiline &&
577
_version.equals( "3.0" ) && next_line_looks_folded;
502
579
// handle multi-line requests
503
if( _parser_in_multiline ) {
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
504
583
_parser_buffered_value_so_far += string_value;
509
588
String complete_value =
510
589
_parser_buffered_value_so_far + string_value;
591
// ignore empty values
592
if( complete_value.length() < 1 ) return;
512
594
// parse some properties
513
if( name_and_param_bits[ 0 ].equals( "N" ) )
514
parseN( name_and_param_bits, complete_value );
515
else if( name_and_param_bits[ 0 ].equals( "FN" ) )
516
parseFN( name_and_param_bits, complete_value );
517
else if( name_and_param_bits[ 0 ].equals( "ORG" ) )
518
parseORG( name_and_param_bits, complete_value );
519
else if( name_and_param_bits[ 0 ].equals( "TEL" ) )
520
parseTEL( name_and_param_bits, complete_value );
521
else if( name_and_param_bits[ 0 ].equals( "EMAIL" ) )
522
parseEMAIL( name_and_param_bits, complete_value );
595
if( name_param_parts[ 0 ].equals( "N" ) )
596
parseN( name_param_parts, complete_value );
597
else if( name_param_parts[ 0 ].equals( "FN" ) )
598
parseFN( name_param_parts, complete_value );
599
else if( name_param_parts[ 0 ].equals( "ORG" ) )
600
parseORG( name_param_parts, complete_value );
601
else if( name_param_parts[ 0 ].equals( "TEL" ) )
602
parseTEL( name_param_parts, complete_value );
603
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
604
parseEMAIL( name_param_parts, complete_value );
526
608
private void parseN( String[] params, String value )
527
throws ParseException, SkipContactException,
609
throws ParseException, SkipContactException,
530
612
// already got a better name?
531
613
if( _name_level >= NAMELEVEL_N ) return;
533
615
// get name parts
534
String[] nameparts = value.split( ";" );
535
for( int i = 0; i < nameparts.length; i++ )
536
nameparts[ i ] = nameparts[ i ].trim();
616
String[] name_parts = value.split( ";" );
617
for( int i = 0; i < name_parts.length; i++ )
618
name_parts[ i ] = name_parts[ i ].trim();
540
if( nameparts.length > 1 && nameparts[ 1 ].length() > 0 )
541
value += nameparts[ 1 ];
542
if( nameparts[ 0 ].length() > 0 )
543
value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
622
if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
623
value += name_parts[ 1 ];
624
if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
625
value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
546
628
setName( value );
566
648
private void parseORG( String[] params, String value )
567
throws ParseException, SkipContactException
649
throws ParseException, SkipContactException
569
651
// already got a better name?
570
652
if( _name_level >= NAMELEVEL_ORG ) return;
573
String[] orgparts = value.split( ";" );
574
for( int i = 0; i < orgparts.length; i++ )
575
orgparts[ i ] = orgparts[ i ].trim();
655
String[] org_parts = value.split( ";" );
656
for( int i = 0; i < org_parts.length; i++ )
657
org_parts[ i ] = org_parts[ i ].trim();
578
if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
579
value = orgparts[ 1 ];
660
if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
661
value = org_parts[ 1 ];
581
value = orgparts[ 0 ];
663
value = org_parts[ 0 ];
584
666
setName( value );
588
670
private void parseTEL( String[] params, String value )
589
throws ParseException
671
throws ParseException
591
673
if( value.length() == 0 ) return;
593
675
Set< String > types = extractTypes( params, Arrays.asList(
594
"PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
595
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
676
"PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
677
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
597
679
// here's the logic...
598
680
boolean preferred = types.contains( "PREF" );
654
736
private String checkParam( String[] params, String name )
656
Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
738
Pattern p = Pattern.compile(
739
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
657
740
for( int i = 0; i < params.length; i++ ) {
658
741
Matcher m = p.matcher( params[ i ] );
659
742
if( m.matches() )
665
748
private Set< String > extractTypes( String[] params,
666
List< String > validTypes )
749
List< String > valid_types )
668
751
HashSet< String > types = new HashSet< String >();
670
753
// get 3.0-style TYPE= param
672
if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
673
String[] bits = typeParam.split( "," );
674
for( int i = 0; i < bits.length; i++ )
675
if( validTypes.contains( bits[ i ] ) )
676
types.add( bits[ i ] );
755
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
String[] parts = type_param.split( "," );
757
for( int i = 0; i < parts.length; i++ )
758
if( valid_types.contains( parts[ i ] ) )
759
types.add( parts[ i ] );
679
762
// get 2.1-style type param
680
763
if( _version.equals( "2.1" ) ) {
681
764
for( int i = 1; i < params.length; i++ )
682
if( validTypes.contains( params[ i ] ) )
765
if( valid_types.contains( params[ i ] ) )
683
766
types.add( params[ i ] );