183
180
private void importVCardFileContent( byte[] content, String fileName )
184
throws AbortImportException
181
throws AbortImportException
183
ByteBuffer buffers[] = getLinesFromContent( content );
186
185
// go through lines
187
186
VCard vCard = null;
188
ContentLineIterator cli = new ContentLineIterator( content );
189
while( cli.hasNext() )
187
for( int i = 0; i < buffers.length; i++ )
191
ByteBuffer buffer = cli.next();
193
189
// get a US-ASCII version of the line for processing
196
line = new String( buffer.array(), buffer.position(),
197
buffer.limit() - buffer.position(), "US-ASCII" );
192
line = new String( buffers[ i ].array(), buffers[ i ].position(),
193
buffers[ i ].limit() - buffers[ i ].position(), "US-ASCII" );
199
195
catch( UnsupportedEncodingException e ) {
200
196
// we know US-ASCII is supported, so appease the compiler...
267
class ContentLineIterator implements Iterator< ByteBuffer >
258
private ByteBuffer[] getLinesFromContent( byte[] content )
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,
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 );
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 ] == ' ';
276
lines[ lines.length - 1 ] = ByteBuffer.wrap( content, last,
277
content.length - last );
330
282
private class VCard extends ContactData
387
334
@SuppressWarnings("serial")
388
335
protected class SkipContactException extends Exception { }
390
private String extractCollonPartFromLine( ByteBuffer buffer,
391
String line, boolean former )
395
// get a US-ASCII version of the line for processing, unless we were
399
line = new String( buffer.array(), buffer.position(),
400
buffer.limit() - buffer.position(), "US-ASCII" );
402
catch( UnsupportedEncodingException e ) {
403
// we know US-ASCII is supported, so appease the compiler...
408
// split line into name and value parts and check to make sure we
409
// only got 2 parts and that the first part is not zero in length
410
String[] parts = line.split( ":", 2 );
411
if( parts.length == 2 && parts[ 0 ].length() > 0 )
412
ret = parts[ former? 0 : 1 ];
417
private String extractNameAndParamsFromLine( ByteBuffer buffer,
420
return extractCollonPartFromLine( buffer, line, true );
423
private String extractValueFromLine( ByteBuffer buffer, String line )
425
return extractCollonPartFromLine( buffer, line, false );
428
public void parseLine( ByteBuffer buffer, String line,
429
boolean next_line_looks_folded )
430
throws ParseException, SkipContactException,
433
// do we have a version yet?
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[] 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!
434
382
if( _version == null )
436
// tentatively get name and params from line
437
String name_and_params =
438
extractNameAndParamsFromLine( buffer, line );
440
// is it a version line?
441
if( name_and_params != null &&
442
name_and_params.equals( "VERSION" ) )
384
// is this a version?
385
if( name_and_params.equals( "VERSION" ) )
445
String value = extractValueFromLine( buffer, line );
446
if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
387
// yes, check/store it
388
if( !string_value.equals( "2.1" ) &&
389
!string_value.equals( "3.0" ) )
447
390
throw new ParseException( R.string.error_vcf_version );
391
_version = string_value;
450
// parse any buffers we've been accumulating while we waited
393
// parse any other buffers we've accumulated so far
452
394
if( _buffers != null )
453
395
for( int i = 0; i < _buffers.size(); i++ )
454
parseLine( _buffers.get( i ), null,
455
i + 1 < _buffers.size() &&
456
_buffers.get( i + 1 ).hasRemaining() &&
457
_buffers.get( i + 1 ).get(
458
_buffers.get( i + 1 ).position() ) == ' ' );
396
parseLine( _buffers.get( i ) );
463
// no, so stash this line till we get a version
401
// no, so stash this buffer till we have a version
464
402
if( _buffers == null )
465
403
_buffers = new Vector< ByteBuffer >();
466
404
_buffers.add( buffer );
471
// name and params and the position in the buffer where the
472
// "value" part of the line start
473
String name_and_params;
409
// value bytes, for processing
476
if( _parser_multiline_state != MULTILINE_NONE )
412
if( _parser_in_multiline )
478
414
// if we're currently in a multi-line value, use the stored
479
415
// property name and parameters
480
416
name_and_params = _parser_current_name_and_params;
482
// skip some initial line characters, depending on the type
483
// of multi-line we're handling
484
pos = buffer.position();
485
switch( _parser_multiline_state )
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' ) )
487
case MULTILINE_FOLDED:
490
case MULTILINE_ENCODED:
491
while( pos < buffer.limit() && (
492
buffer.get( pos ) == ' ' ||
493
buffer.get( pos ) == '\t' ) )
502
// take us out of multi-line so that we can re-detect that
503
// this line is a multi-line or not
504
_parser_multiline_state = MULTILINE_NONE;
428
// get value from buffer
429
value = ByteBuffer.wrap( buffer.array(), pos,
430
buffer.limit() - pos );
508
// get name and params from line, and since we're not
509
// parsing a subsequent line in a multi-line, this should
510
// not fail, or it's an error
512
extractNameAndParamsFromLine( buffer, line );
513
if( name_and_params == null )
514
throw new ParseException(
515
R.string.error_vcf_malformed );
434
// ignore empty values
435
if( string_value.length() < 1 ) return;
517
437
// calculate how many chars to skip from beginning of line
518
438
// so we skip the property "name:" part
519
pos = buffer.position() + name_and_params.length() + 1;
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 );
521
445
// reset the saved multi-line state
522
446
_parser_current_name_and_params = name_and_params;
523
447
_parser_buffered_value_so_far = "";
526
// get value from buffer, as raw bytes
528
value = ByteBuffer.wrap( buffer.array(), pos,
529
buffer.limit() - pos );
531
450
// get parameter parts
532
451
String[] name_param_parts = name_and_params.split( ";", -1 );
533
452
for( int i = 0; i < name_param_parts.length; i++ )
559
477
if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
560
478
unencoding_result = unencodeQuotedPrintable( value );
561
479
// else if( encoding != null && encoding.equals( "BASE64" ) )
562
// unencoding_result = unencodeBase64( props[ 1 ], charset );
480
// result = unencodeBase64( props[ 1 ], charset );
563
481
if( unencoding_result != null ) {
564
482
value = unencoding_result.getBuffer();
565
if( unencoding_result.isAnotherLineRequired() )
566
_parser_multiline_state = MULTILINE_ENCODED;
483
_parser_in_multiline =
484
unencoding_result.isAnotherLineRequired();
569
487
// convert 8-bit ASCII charset to US-ASCII
570
if( charset == null || charset.equals( "ASCII" ) ) {
488
if( charset == null || charset == "ASCII" ) {
571
489
value = transcodeAsciiToUtf8( value );
572
490
charset = "UTF-8";
575
493
// process charset
578
string_value = new String( value.array(), value.position(),
579
value.limit() - value.position(), charset );
496
new String( value.array(), value.position(),
497
value.limit() - value.position(), charset );
580
498
} catch( UnsupportedEncodingException e ) {
581
499
throw new ParseException( R.string.error_vcf_charset );
584
// for some entries that have semicolon-separated value parts,
585
// check to see if the value ends in an escape character, which
586
// indicates that we have a multi-line value
587
if( ( name_param_parts[ 0 ].equals( "N" ) ||
588
name_param_parts[ 0 ].equals( "ORG" ) ||
589
name_param_parts[ 0 ].equals( "ADR" ) ) &&
590
doesStringEndInAnEscapeChar( string_value ) )
592
_parser_multiline_state = MULTILINE_ESCAPED;
593
string_value = string_value.substring( 0,
594
string_value.length() - 1 );
597
// now we know whether we're in an encoding multi-line,
598
// determine if we're in a v3 folded multi-line or not
599
if( _parser_multiline_state == MULTILINE_NONE &&
600
_version.equals( "3.0" ) && next_line_looks_folded )
602
_parser_multiline_state = MULTILINE_FOLDED;
605
// handle multi-lines by buffering them and parsing them when we
606
// are processing the last line in a multi-line sequence
607
if( _parser_multiline_state != MULTILINE_NONE ) {
502
// handle multi-line requests
503
if( _parser_in_multiline ) {
608
504
_parser_buffered_value_so_far += string_value;
508
// add on buffered multi-line content
611
509
String complete_value =
612
( _parser_buffered_value_so_far + string_value ).trim();
614
// ignore empty values
615
if( complete_value.length() < 1 ) return;
510
_parser_buffered_value_so_far + string_value;
617
512
// parse some properties
618
513
if( name_param_parts[ 0 ].equals( "N" ) )
625
520
parseTEL( name_param_parts, complete_value );
626
521
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
627
522
parseEMAIL( name_param_parts, complete_value );
628
else if( name_param_parts[ 0 ].equals( "ADR" ) )
629
parseADR( name_param_parts, complete_value );
633
private boolean doesStringEndInAnEscapeChar( String string )
635
// count the number of backslashes at the end of the string
637
for( int a = string.length() - 1; a >= 0; a-- )
638
if( string.charAt( a ) == '\\' )
643
// if there are an even number of backslashes then the final one
645
return ( count & 1 ) == 1;
648
private String[] splitValueBySemicolon( String value )
650
// split string in to parts by semicolon
651
ArrayList< String > parts = new ArrayList< String >(
652
Arrays.asList( value.split( ";" ) ) );
655
for( int a = 0; a < parts.size(); a++ )
657
String str = parts.get( a );
659
// look for parts that end in an escape character, but ignore
660
// the final part. We've already detected escape chars at the
661
// end of the final part in parseLine() and handled multi-lines
663
if( a < parts.size() - 1 &&
664
doesStringEndInAnEscapeChar( str ) )
666
// join the next part to this part and remove the next part
667
parts.set( a, str.substring( 0, str.length() - 1 ) +
668
';' + parts.get( a + 1 ) );
669
parts.remove( a + 1 );
671
// re-visit this part
676
// trim and replace string
681
String[] ret = new String[ parts.size() ];
682
return parts.toArray( ret );
685
526
private void parseN( String[] params, String value )
686
throws ParseException, SkipContactException,
527
throws ParseException, SkipContactException,
689
530
// already got a better name?
690
531
if( _name_level >= NAMELEVEL_N ) return;
692
533
// get name parts
693
String[] name_parts = splitValueBySemicolon( value );
534
String[] name_parts = value.split( ";" );
535
for( int i = 0; i < name_parts.length; i++ )
536
name_parts[ i ] = name_parts[ i ].trim();
745
588
private void parseTEL( String[] params, String value )
746
throws ParseException
589
throws ParseException
748
591
if( value.length() == 0 ) return;
750
593
Set< String > types = extractTypes( params, Arrays.asList(
751
"PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
752
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
594
"PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
595
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
754
597
// here's the logic...
755
598
boolean preferred = types.contains( "PREF" );
789
632
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
792
private void parseADR( String[] params, String value )
793
throws ParseException, SkipContactException
796
String[] adr_parts = splitValueBySemicolon( value );
800
for( int a = 0; a < adr_parts.length; a++ ) {
801
if( value.length() > 0 ) value += "\n";
802
value += adr_parts[ a ].trim();
805
Set< String > types = extractTypes( params, Arrays.asList(
806
"PREF", "WORK", "HOME", "INTERNET" ) );
809
if( types.contains( "WORK" ) )
810
addAddress( value, Contacts.ContactMethods.TYPE_WORK );
812
addAddress( value, Contacts.ContactMethods.TYPE_HOME);
815
635
public void finaliseParsing()
816
throws ParseException, SkipContactException,
636
throws ParseException, SkipContactException,
819
639
// missing version (and data is present)
820
640
if( _version == null && _buffers != null )