209
if( vcard == null ) {
204
if( vCard == null ) {
210
205
// look for vcard beginning
211
206
if( line.matches( "^BEGIN:VCARD" ) ) {
212
setProgress( _progress++ );
214
vcard_start_line = cli.getLineNumber();
207
setProgress( ++_progress );
218
212
// look for vcard content or ending
219
213
if( line.matches( "^END:VCARD" ) )
221
// finalise the vcard/contact
215
// store vcard and do away with it
223
vcard.finaliseVcard();
225
// pass the finalised contact to the importer
226
importContact( vcard );
228
catch( Vcard.ParseException e ) {
230
getText( R.string.error_vcf_parse ).toString()
232
getText( R.string.error_vcf_parse_line ).toString()
233
+ cli.getLineNumber() + ":\n" + e.getMessage() ) )
235
finish( ACTION_ABORT );
240
catch( ContactData.ContactNotIdentifiableException e ) {
242
getText( R.string.error_vcf_parse ).toString()
244
getText( R.string.error_vcf_parse_line ).toString()
245
+ vcard_start_line + ":\n" + getText(
246
R.string.error_vcf_notenoughinfo ).toString()
249
finish( ACTION_ABORT );
255
// 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 ) {
260
237
// try giving the line to the vcard
262
vcard.parseLine( buffer, line,
239
vCard.parseLine( buffer, line,
263
240
cli.doesNextLineLookFolded() );
265
catch( Vcard.ParseException e ) {
242
catch( VCard.ParseException e ) {
267
244
if( !showContinue(
268
245
getText( R.string.error_vcf_parse ).toString()
270
getText( R.string.error_vcf_parse_line ).toString()
271
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
246
+ fileName + "\n" + e.getMessage() ) )
273
248
finish( ACTION_ABORT );
608
552
// unencoding_result = unencodeBase64( props[ 1 ], charset );
609
553
if( unencoding_result != null ) {
610
554
value = unencoding_result.getBuffer();
611
if( unencoding_result.isAnotherLineRequired() )
612
_parser_multiline_state = MULTILINE_ENCODED;
555
_parser_in_encoded_multiline =
556
unencoding_result.isAnotherLineRequired();
615
// convert 8-bit US-ASCII charset to UTF-8 (where no charset is
616
// specified for a v2.1 vcard entry, we assume it's US-ASCII)
617
if( ( charset == null && _version.equals( "2.1" ) ) ||
618
( charset != null && (
619
charset.equals( "ASCII" ) ||
620
charset.equals( "US-ASCII" ) ) ) )
559
// convert 8-bit ASCII charset to US-ASCII
560
if( charset == null || charset.equals( "ASCII" ) ) {
622
561
value = transcodeAsciiToUtf8( value );
625
// process charset (value is now in UTF-8)
626
566
String string_value;
628
568
string_value = new String( value.array(), value.position(),
629
value.limit() - value.position(), "UTF-8" );
569
value.limit() - value.position(), charset );
630
570
} catch( UnsupportedEncodingException e ) {
631
571
throw new ParseException( R.string.error_vcf_charset );
634
// for some entries that have semicolon-separated value parts,
635
// check to see if the value ends in an escape character, which
636
// indicates that we have a multi-line value
637
if( ( name_param_parts[ 0 ].equals( "N" ) ||
638
name_param_parts[ 0 ].equals( "ORG" ) ||
639
name_param_parts[ 0 ].equals( "ADR" ) ) &&
640
doesStringEndInAnEscapeChar( string_value ) )
642
_parser_multiline_state = MULTILINE_ESCAPED;
643
string_value = string_value.substring( 0,
644
string_value.length() - 1 );
647
574
// now we know whether we're in an encoding multi-line,
648
575
// determine if we're in a v3 folded multi-line or not
649
if( _parser_multiline_state == MULTILINE_NONE &&
650
_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 )
652
_parser_multiline_state = MULTILINE_FOLDED;
655
// handle multi-lines by buffering them and parsing them when we
656
// are processing the last line in a multi-line sequence
657
if( _parser_multiline_state != MULTILINE_NONE ) {
658
583
_parser_buffered_value_so_far += string_value;
587
// add on buffered multi-line content
661
588
String complete_value =
662
( _parser_buffered_value_so_far + string_value ).trim();
589
_parser_buffered_value_so_far + string_value;
664
591
// ignore empty values
665
592
if( complete_value.length() < 1 ) return;
671
598
parseFN( name_param_parts, complete_value );
672
599
else if( name_param_parts[ 0 ].equals( "ORG" ) )
673
600
parseORG( name_param_parts, complete_value );
674
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
675
parseTITLE( name_param_parts, complete_value );
676
601
else if( name_param_parts[ 0 ].equals( "TEL" ) )
677
602
parseTEL( name_param_parts, complete_value );
678
603
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
679
604
parseEMAIL( name_param_parts, complete_value );
680
else if( name_param_parts[ 0 ].equals( "ADR" ) )
681
parseADR( name_param_parts, complete_value );
682
else if( name_param_parts[ 0 ].equals( "LABEL" ) )
683
parseLABEL( name_param_parts, complete_value );
684
else if( name_param_parts[ 0 ].equals( "NOTE" ) )
685
parseNOTE( name_param_parts, complete_value );
689
private boolean doesStringEndInAnEscapeChar( String string )
691
// count the number of backslashes at the end of the string
693
for( int a = string.length() - 1; a >= 0; a-- )
694
if( string.charAt( a ) == '\\' )
699
// if there are an even number of backslashes then the final one
701
return ( count & 1 ) == 1;
704
private String[] splitValueByCharacter( String value, char character )
706
// split string in to parts by specified character
707
ArrayList< String > parts = new ArrayList< String >(
708
Arrays.asList( value.split( "" + character ) ) );
711
for( int a = 0; a < parts.size(); a++ )
713
String str = parts.get( a );
715
// look for parts that end in an escape character, but ignore
716
// the final part. We've already detected escape chars at the
717
// end of the final part in parseLine() and handled multi-lines
719
if( a < parts.size() - 1 &&
720
doesStringEndInAnEscapeChar( str ) )
722
// append the escaped character, join the next part to this
723
// part and remove the next part
724
parts.set( a, str.substring( 0, str.length() - 1 ) +
725
character + parts.get( a + 1 ) );
726
parts.remove( a + 1 );
728
// re-visit this part
733
// trim and replace string
738
String[] ret = new String[ parts.size() ];
739
return parts.toArray( ret );
742
private String unescapeValue( String value )
744
StringBuilder ret = new StringBuilder( value.length() );
745
boolean in_escape = false;
746
for( int a = 0; a < value.length(); a++ )
748
int c = value.codePointAt( a );
750
// process a normal character
755
ret.append( Character.toChars( c ) );
759
// process an escape sequence
765
// add tab (invalid/non-standard, but accepted)
776
// add escaped character
777
ret.append( Character.toChars( c ) );
780
// unknown escape sequence, so add it unescaped
781
// (invalid/non-standard, but accepted)
783
ret.append( Character.toChars( c ) );
788
return ret.toString();
791
608
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
793
612
// already got a better name?
794
613
if( _name_level >= NAMELEVEL_N ) return;
796
615
// get name parts
797
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();
801
final int[] part_order = { 3, 1, 2, 0, 4 };
802
for( int a = 0; a < part_order.length; a++ )
803
if( name_parts.length > part_order[ a ] &&
804
name_parts[ part_order[ a ] ].length() > 0 )
806
// split this part in to it's comma-separated bits
807
String[] name_part_parts = splitValueByCharacter(
808
name_parts[ part_order[ a ] ], ',' );
809
for( int b = 0; b < name_part_parts.length; b++ )
810
if( name_part_parts[ b ].length() > 0 )
812
if( value.length() == 0 ) value += " ";
813
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 ];
818
setName( unescapeValue( value ) );
819
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();
822
637
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
824
640
// already got a better name?
825
641
if( _name_level >= NAMELEVEL_FN ) return;
828
setName( unescapeValue( value ) );
829
645
_name_level = NAMELEVEL_FN;
832
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;
835
String[] org_parts = splitValueByCharacter( value, ';' );
836
if( org_parts == null || org_parts.length < 1 ) return;
838
// build organisation name
839
StringBuilder builder = new StringBuilder(
840
String.valueOf( org_parts[ 0 ] ) );
841
for( int a = 1; a < org_parts.length; a++ )
842
builder.append( ", " ).append( org_parts[ a ] );
843
String organisation = unescapeValue( builder.toString() );
845
// set organisation name (using a title we've previously found)
846
addOrganisation( organisation, _cached_title, true );
848
// if we've not previously found a title, store this organisation
849
// name (we'll need it when we find a title to update the
850
// organisation, by name), else if we *have* previously found a
851
// title, clear it (since we just used it)
852
if( _cached_title == null )
853
_cached_organisation = organisation;
855
_cached_title = null;
858
private void parseTITLE( String[] params, String value )
860
value = unescapeValue( value );
862
// if we previously had an organisation, look it up and append this
864
if( _cached_organisation != null && hasOrganisations() ) {
865
HashMap< String, ExtraDetail > datas = getOrganisations();
866
ExtraDetail detail = datas.get( _cached_organisation );
868
detail.setExtra( value );
871
// same as when handling organisation, if we've not previously found
872
// an organisation we store this title, else we clear it (since we
873
// just appended this title to it)
874
if( _cached_organisation == null )
875
_cached_title = value;
877
_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;
880
670
private void parseTEL( String[] params, String value )
671
throws ParseException
882
673
if( value.length() == 0 ) return;
886
677
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
888
679
// here's the logic...
889
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;
891
689
if( types.contains( "FAX" ) )
892
690
if( types.contains( "HOME" ) )
893
type = TYPE_FAX_HOME;
691
type = PhonesColumns.TYPE_FAX_HOME;
895
type = TYPE_FAX_WORK;
896
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
898
else if( types.contains( "PAGER" ) )
900
else if( types.contains( "WORK" ) )
693
type = PhonesColumns.TYPE_FAX_WORK;
694
if( types.contains( "PAGER" ) )
695
type = PhonesColumns.TYPE_PAGER;
905
697
// add phone number
906
addNumber( value, type, is_preferred );
698
addPhone( value, type, preferred );
909
701
public void parseEMAIL( String[] params, String value )
702
throws ParseException
911
704
if( value.length() == 0 ) return;
913
706
Set< String > types = extractTypes( params, Arrays.asList(
914
707
"PREF", "WORK", "HOME", "INTERNET" ) );
917
boolean is_preferred = types.contains( "PREF" );
919
if( types.contains( "WORK" ) )
924
addEmail( unescapeValue( value ), type, is_preferred );
927
private void parseADR( String[] params, String value )
930
String[] adr_parts = splitValueByCharacter( value, ';' );
934
for( int a = 0; a < adr_parts.length; a++ )
935
if( adr_parts[ a ].length() > 0 )
937
// split this part in to it's comma-separated bits
938
String[] adr_part_parts =
939
splitValueByCharacter( adr_parts[ a ], ',' );
940
for( int b = 0; b < adr_part_parts.length; b++ )
941
if( adr_part_parts[ b ].length() > 0 )
943
if( value.length() > 0 ) value += "\n";
944
value += adr_part_parts[ b ];
948
Set< String > types = extractTypes( params, Arrays.asList(
949
"PREF", "WORK", "HOME" ) );
953
if( types.contains( "WORK" ) )
958
addAddress( unescapeValue( value ), type );
961
private void parseLABEL( String[] params, String value )
963
Set< String > types = extractTypes( params, Arrays.asList(
964
"PREF", "WORK", "HOME" ) );
968
if( types.contains( "WORK" ) )
973
addAddress( unescapeValue( value ), type );
976
private void parseNOTE( String[] params, String value )
978
addNote( unescapeValue( value ) );
981
public void finaliseVcard()
982
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,
984
721
// missing version (and data is present)
985
722
if( _version == null && _buffers != null )
986
723
throw new ParseException( R.string.error_vcf_malformed );
988
// 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();
993
* Amongst the params, find the value of the first, only, of any with
997
* @return a value, or null
999
736
private String checkParam( String[] params, String name )
1001
String[] res = checkParams( params, name );
1002
return res.length > 0? res[ 0 ] : null;
1006
* Amongst the params, find the values of any with the specified name
1009
* @return an array of values, or null
1011
private String[] checkParams( String[] params, String name )
1013
HashSet< String > ret = new HashSet< String >();
1015
738
Pattern p = Pattern.compile(
1016
739
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1017
740
for( int i = 0; i < params.length; i++ ) {
1018
741
Matcher m = p.matcher( params[ i ] );
1019
742
if( m.matches() )
1020
ret.add( m.group( 2 ) );
1023
return (String[]) ret.toArray( new String[ ret.size() ] );
1027
* Amongst the params, return any type values present. For v2.1 vCards,
1028
* those types are just parameters. For v3.0, they are prefixed with
1029
* "TYPE=". There may also be multiple type parameters.
1031
* @param a list of type values to look for
1032
* @return a set of present type values
1034
748
private Set< String > extractTypes( String[] params,
1035
749
List< String > valid_types )
1037
751
HashSet< String > types = new HashSet< String >();
1039
753
// get 3.0-style TYPE= param
1040
String type_params[] = checkParams( params, "TYPE" );
1041
for( int a = 0; a < type_params.length; a++ )
1043
// check for a comma-separated list of types (why? this isn't in
1045
String[] parts = type_params[ a ].split( "," );
755
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
String[] parts = type_param.split( "," );
1046
757
for( int i = 0; i < parts.length; i++ )
1047
758
if( valid_types.contains( parts[ i ] ) )
1048
759
types.add( parts[ i ] );