208
if( vcard == null ) {
204
if( vCard == null ) {
209
205
// look for vcard beginning
210
206
if( line.matches( "^BEGIN:VCARD" ) ) {
211
setProgress( _progress++ );
213
vcard_start_line = cli.getLineNumber();
207
setProgress( ++_progress );
217
212
// look for vcard content or ending
218
213
if( line.matches( "^END:VCARD" ) )
220
// finalise the vcard/contact
215
// store vcard and do away with it
222
vcard.finaliseVcard();
224
// pass the finalised contact to the importer
225
importContact( vcard );
227
catch( Vcard.ParseException e ) {
229
getText( R.string.error_vcf_parse ).toString()
231
getText( R.string.error_vcf_parse_line ).toString()
232
+ cli.getLineNumber() + ":\n" + e.getMessage() ) )
234
finish( ACTION_ABORT );
239
catch( ContactData.ContactNotIdentifiableException e ) {
241
getText( R.string.error_vcf_parse ).toString()
243
getText( R.string.error_vcf_parse_line ).toString()
244
+ vcard_start_line + ":\n" + getText(
245
R.string.error_vcf_notenoughinfo ).toString()
248
finish( ACTION_ABORT );
254
// discard this vcard
217
vCard.finaliseParsing();
218
importContact( vCard );
220
catch( VCard.ParseException e ) {
223
getText( R.string.error_vcf_parse ).toString()
224
+ fileName + "\n" + e.getMessage() ) )
226
finish( ACTION_ABORT );
229
catch( VCard.SkipContactException e ) {
259
237
// try giving the line to the vcard
261
vcard.parseLine( buffer, line,
239
vCard.parseLine( buffer, line,
262
240
cli.doesNextLineLookFolded() );
264
catch( Vcard.ParseException e ) {
242
catch( VCard.ParseException e ) {
266
244
if( !showContinue(
267
245
getText( R.string.error_vcf_parse ).toString()
269
getText( R.string.error_vcf_parse_line ).toString()
270
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
246
+ fileName + "\n" + e.getMessage() ) )
272
248
finish( ACTION_ABORT );
352
325
return _pos > 0 && _pos < _content.length &&
353
326
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
362
private class Vcard extends ContactData
330
private class VCard extends ContactData
364
332
private final static int NAMELEVEL_NONE = 0;
365
private final static int NAMELEVEL_N = 1;
333
private final static int NAMELEVEL_ORG = 1;
366
334
private final static int NAMELEVEL_FN = 2;
368
private final static int MULTILINE_NONE = 0;
369
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
370
private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
371
private final static int MULTILINE_FOLDED = 3; // v3.0 folding
335
private final static int NAMELEVEL_N = 3;
373
337
private String _version = null;
374
338
private Vector< ByteBuffer > _buffers = null;
375
339
private int _name_level = NAMELEVEL_NONE;
376
private int _parser_multiline_state = MULTILINE_NONE;
340
private boolean _parser_in_encoded_multiline = false;
341
private boolean _parser_in_folded_multiline = false;
377
342
private String _parser_current_name_and_params = null;
378
343
private String _parser_buffered_value_so_far = "";
379
private String _cached_organisation = null;
380
private String _cached_title = null;
382
345
protected class UnencodeResult
605
552
// unencoding_result = unencodeBase64( props[ 1 ], charset );
606
553
if( unencoding_result != null ) {
607
554
value = unencoding_result.getBuffer();
608
if( unencoding_result.isAnotherLineRequired() )
609
_parser_multiline_state = MULTILINE_ENCODED;
555
_parser_in_encoded_multiline =
556
unencoding_result.isAnotherLineRequired();
612
// convert 8-bit US-ASCII charset to UTF-8 (where no charset is
613
// specified for a v2.1 vcard entry, we assume it's US-ASCII)
614
if( ( charset == null && _version.equals( "2.1" ) ) ||
615
( charset != null && (
616
charset.equals( "ASCII" ) ||
617
charset.equals( "US-ASCII" ) ) ) )
559
// convert 8-bit ASCII charset to US-ASCII
560
if( charset == null || charset.equals( "ASCII" ) ) {
619
561
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
623
566
String string_value;
625
568
string_value = new String( value.array(), value.position(),
626
value.limit() - value.position(), "UTF-8" );
569
value.limit() - value.position(), charset );
627
570
} catch( UnsupportedEncodingException e ) {
628
571
throw new ParseException( R.string.error_vcf_charset );
631
// for some entries that have semicolon-separated value parts,
632
// check to see if the value ends in an escape character, which
633
// indicates that we have a multi-line value
634
if( ( name_param_parts[ 0 ].equals( "N" ) ||
635
name_param_parts[ 0 ].equals( "ORG" ) ||
636
name_param_parts[ 0 ].equals( "ADR" ) ) &&
637
doesStringEndInAnEscapeChar( string_value ) )
639
_parser_multiline_state = MULTILINE_ESCAPED;
640
string_value = string_value.substring( 0,
641
string_value.length() - 1 );
644
574
// now we know whether we're in an encoding multi-line,
645
575
// determine if we're in a v3 folded multi-line or not
646
if( _parser_multiline_state == MULTILINE_NONE &&
647
_version.equals( "3.0" ) && next_line_looks_folded )
576
_parser_in_folded_multiline = !_parser_in_encoded_multiline &&
577
_version.equals( "3.0" ) && next_line_looks_folded;
579
// handle multi-line requests
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
649
_parser_multiline_state = MULTILINE_FOLDED;
652
// handle multi-lines by buffering them and parsing them when we
653
// are processing the last line in a multi-line sequence
654
if( _parser_multiline_state != MULTILINE_NONE ) {
655
583
_parser_buffered_value_so_far += string_value;
587
// add on buffered multi-line content
658
588
String complete_value =
659
( _parser_buffered_value_so_far + string_value ).trim();
589
_parser_buffered_value_so_far + string_value;
661
591
// ignore empty values
662
592
if( complete_value.length() < 1 ) return;
668
598
parseFN( name_param_parts, complete_value );
669
599
else if( name_param_parts[ 0 ].equals( "ORG" ) )
670
600
parseORG( name_param_parts, complete_value );
671
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
672
parseTITLE( name_param_parts, complete_value );
673
601
else if( name_param_parts[ 0 ].equals( "TEL" ) )
674
602
parseTEL( name_param_parts, complete_value );
675
603
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
676
604
parseEMAIL( name_param_parts, complete_value );
677
else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
parseADR( name_param_parts, complete_value );
679
else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
parseLABEL( name_param_parts, complete_value );
684
private boolean doesStringEndInAnEscapeChar( String string )
686
// count the number of backslashes at the end of the string
688
for( int a = string.length() - 1; a >= 0; a-- )
689
if( string.charAt( a ) == '\\' )
694
// if there are an even number of backslashes then the final one
696
return ( count & 1 ) == 1;
699
private String[] splitValueByCharacter( String value, char character )
701
// split string in to parts by specified character
702
ArrayList< String > parts = new ArrayList< String >(
703
Arrays.asList( value.split( "" + character ) ) );
706
for( int a = 0; a < parts.size(); a++ )
708
String str = parts.get( a );
710
// look for parts that end in an escape character, but ignore
711
// the final part. We've already detected escape chars at the
712
// end of the final part in parseLine() and handled multi-lines
714
if( a < parts.size() - 1 &&
715
doesStringEndInAnEscapeChar( str ) )
717
// append the escaped character, join the next part to this
718
// part and remove the next part
719
parts.set( a, str.substring( 0, str.length() - 1 ) +
720
character + parts.get( a + 1 ) );
721
parts.remove( a + 1 );
723
// re-visit this part
728
// trim and replace string
733
String[] ret = new String[ parts.size() ];
734
return parts.toArray( ret );
737
private String unescapeValue( String value )
739
StringBuilder ret = new StringBuilder( value.length() );
740
boolean in_escape = false;
741
for( int a = 0; a < value.length(); a++ )
743
int c = value.codePointAt( a );
745
// process a normal character
750
ret.append( Character.toChars( c ) );
754
// process an escape sequence
766
// add escaped character
767
ret.append( Character.toChars( c ) );
770
// unknown escape sequence, so add it unescaped
772
ret.append( Character.toChars( c ) );
777
return ret.toString();
780
608
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
782
612
// already got a better name?
783
613
if( _name_level >= NAMELEVEL_N ) return;
785
615
// get name parts
786
String[] name_parts = splitValueByCharacter( value, ';' );
616
String[] name_parts = value.split( ";" );
617
for( int i = 0; i < name_parts.length; i++ )
618
name_parts[ i ] = name_parts[ i ].trim();
790
final int[] part_order = { 3, 1, 2, 0, 4 };
791
for( int a = 0; a < part_order.length; a++ )
792
if( name_parts.length > part_order[ a ] &&
793
name_parts[ part_order[ a ] ].length() > 0 )
795
// split this part in to it's comma-separated bits
796
String[] name_part_parts = splitValueByCharacter(
797
name_parts[ part_order[ a ] ], ',' );
798
for( int b = 0; b < name_part_parts.length; b++ )
799
if( name_part_parts[ b ].length() > 0 )
801
if( value.length() == 0 ) value += " ";
802
value += name_part_parts[ b ];
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 ];
807
setName( unescapeValue( value ) );
808
629
_name_level = NAMELEVEL_N;
631
// check now to see if we need to import this contact (to avoid
632
// parsing the rest of the vCard unnecessarily)
633
if( !isImportRequired( getName() ) )
634
throw new SkipContactException();
811
637
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
813
640
// already got a better name?
814
641
if( _name_level >= NAMELEVEL_FN ) return;
817
setName( unescapeValue( value ) );
818
645
_name_level = NAMELEVEL_FN;
821
648
private void parseORG( String[] params, String value )
649
throws ParseException, SkipContactException
651
// already got a better name?
652
if( _name_level >= NAMELEVEL_ORG ) return;
824
String[] org_parts = splitValueByCharacter( value, ';' );
825
if( org_parts == null || org_parts.length < 1 ) return;
827
// build organisation name
828
StringBuilder builder = new StringBuilder(
829
String.valueOf( org_parts[ 0 ] ) );
830
for( int a = 1; a < org_parts.length; a++ )
831
builder.append( ", " ).append( org_parts[ a ] );
832
String organisation = unescapeValue( builder.toString() );
834
// set organisation name (using a title we've previously found)
835
addOrganisation( organisation, _cached_title, true );
837
// if we've not previously found a title, store this organisation
838
// name (we'll need it when we find a title to update the
839
// organisation, by name), else if we *have* previously found a
840
// title, clear it (since we just used it)
841
if( _cached_title == null )
842
_cached_organisation = organisation;
844
_cached_title = null;
847
private void parseTITLE( String[] params, String value )
849
value = unescapeValue( value );
851
// if we previously had an organisation, look it up and append this
853
if( _cached_organisation != null && hasOrganisations() ) {
854
HashMap< String, ExtraDetail > datas = getOrganisations();
855
ExtraDetail detail = datas.get( _cached_organisation );
857
detail.setExtra( value );
860
// same as when handling organisation, if we've not previously found
861
// an organisation we store this title, else we clear it (since we
862
// just appended this title to it)
863
if( _cached_organisation == null )
864
_cached_title = value;
866
_cached_organisation = null;
655
String[] org_parts = value.split( ";" );
656
for( int i = 0; i < org_parts.length; i++ )
657
org_parts[ i ] = org_parts[ i ].trim();
660
if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
661
value = org_parts[ 1 ];
663
value = org_parts[ 0 ];
667
_name_level = NAMELEVEL_ORG;
869
670
private void parseTEL( String[] params, String value )
671
throws ParseException
871
673
if( value.length() == 0 ) return;
875
677
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
877
679
// here's the logic...
878
boolean is_preferred = types.contains( "PREF" );
680
boolean preferred = types.contains( "PREF" );
681
int type = PhonesColumns.TYPE_MOBILE;
682
if( types.contains( "VOICE" ) )
683
if( types.contains( "WORK" ) )
684
type = PhonesColumns.TYPE_WORK;
686
type = PhonesColumns.TYPE_HOME;
687
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
688
type = PhonesColumns.TYPE_MOBILE;
880
689
if( types.contains( "FAX" ) )
881
690
if( types.contains( "HOME" ) )
882
691
type = PhonesColumns.TYPE_FAX_HOME;
884
693
type = PhonesColumns.TYPE_FAX_WORK;
885
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
886
type = PhonesColumns.TYPE_MOBILE;
887
else if( types.contains( "PAGER" ) )
694
if( types.contains( "PAGER" ) )
888
695
type = PhonesColumns.TYPE_PAGER;
889
else if( types.contains( "WORK" ) )
890
type = PhonesColumns.TYPE_WORK;
892
type = PhonesColumns.TYPE_HOME;
894
697
// add phone number
895
addNumber( value, type, is_preferred );
698
addPhone( value, type, preferred );
898
701
public void parseEMAIL( String[] params, String value )
702
throws ParseException
900
704
if( value.length() == 0 ) return;
902
706
Set< String > types = extractTypes( params, Arrays.asList(
903
707
"PREF", "WORK", "HOME", "INTERNET" ) );
906
boolean is_preferred = types.contains( "PREF" );
908
if( types.contains( "WORK" ) )
909
type = Contacts.ContactMethods.TYPE_WORK;
911
type = Contacts.ContactMethods.TYPE_HOME;
913
addEmail( unescapeValue( value ), type, is_preferred );
916
private void parseADR( String[] params, String value )
919
String[] adr_parts = splitValueByCharacter( value, ';' );
923
for( int a = 0; a < adr_parts.length; a++ )
924
if( adr_parts[ a ].length() > 0 )
926
// split this part in to it's comma-separated bits
927
String[] adr_part_parts =
928
splitValueByCharacter( adr_parts[ a ], ',' );
929
for( int b = 0; b < adr_part_parts.length; b++ )
930
if( adr_part_parts[ b ].length() > 0 )
932
if( value.length() > 0 ) value += "\n";
933
value += adr_part_parts[ b ];
937
Set< String > types = extractTypes( params, Arrays.asList(
938
"PREF", "WORK", "HOME" ) );
942
if( types.contains( "WORK" ) )
943
type = Contacts.ContactMethods.TYPE_WORK;
945
type = Contacts.ContactMethods.TYPE_HOME;
947
addAddress( unescapeValue( value ), type );
950
private void parseLABEL( String[] params, String value )
952
Set< String > types = extractTypes( params, Arrays.asList(
953
"PREF", "WORK", "HOME" ) );
957
if( types.contains( "WORK" ) )
958
type = Contacts.ContactMethods.TYPE_WORK;
960
type = Contacts.ContactMethods.TYPE_HOME;
962
addAddress( unescapeValue( value ), type );
965
public void finaliseVcard()
966
throws ParseException, ContactNotIdentifiableException
709
// here's the logic...
710
boolean preferred = types.contains( "PREF" );
711
if( types.contains( "WORK" ) )
712
addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
714
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
717
public void finaliseParsing()
718
throws ParseException, SkipContactException,
968
721
// missing version (and data is present)
969
722
if( _version == null && _buffers != null )
970
723
throw new ParseException( R.string.error_vcf_malformed );
972
// finalise the parent class
725
// missing name properties?
726
if( _name_level == NAMELEVEL_NONE )
727
throw new ParseException( R.string.error_vcf_noname );
729
// check if we should import this one? If we've already got an 'N'-
730
// type name, this will already have been done by parseN() so we
731
// mustn't do this here (or it could prompt twice!)
732
if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
733
throw new SkipContactException();
977
* Amongst the params, find the value of the first, only, of any with
981
* @return a value, or null
983
736
private String checkParam( String[] params, String name )
985
String[] res = checkParams( params, name );
986
return res.length > 0? res[ 0 ] : null;
990
* Amongst the params, find the values of any with the specified name
993
* @return an array of values, or null
995
private String[] checkParams( String[] params, String name )
997
HashSet< String > ret = new HashSet< String >();
999
738
Pattern p = Pattern.compile(
1000
739
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1001
740
for( int i = 0; i < params.length; i++ ) {
1002
741
Matcher m = p.matcher( params[ i ] );
1003
742
if( m.matches() )
1004
ret.add( m.group( 2 ) );
1007
return (String[]) ret.toArray( new String[ ret.size() ] );
1011
* Amongst the params, return any type values present. For v2.1 vCards,
1012
* those types are just parameters. For v3.0, they are prefixed with
1013
* "TYPE=". There may also be multiple type parameters.
1015
* @param a list of type values to look for
1016
* @return a set of present type values
1018
748
private Set< String > extractTypes( String[] params,
1019
749
List< String > valid_types )
1021
751
HashSet< String > types = new HashSet< String >();
1023
753
// get 3.0-style TYPE= param
1024
String type_params[] = checkParams( params, "TYPE" );
1025
for( int a = 0; a < type_params.length; a++ )
1027
// check for a comma-separated list of types (why? this isn't in
1029
String[] parts = type_params[ a ].split( "," );
755
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
String[] parts = type_param.split( "," );
1030
757
for( int i = 0; i < parts.length; i++ )
1031
758
if( valid_types.contains( parts[ i ] ) )
1032
759
types.add( parts[ i ] );