204
if( vCard == null ) {
209
if( vcard == null ) {
205
210
// look for vcard beginning
206
211
if( line.matches( "^BEGIN:VCARD" ) ) {
207
setProgress( ++_progress );
212
setProgress( _progress++ );
214
vcard_start_line = cli.getLineNumber();
212
218
// look for vcard content or ending
213
219
if( line.matches( "^END:VCARD" ) )
215
// store vcard and do away with it
221
// finalise the vcard/contact
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 ) {
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
237
260
// try giving the line to the vcard
239
vCard.parseLine( buffer, line,
262
vcard.parseLine( buffer, line,
240
263
cli.doesNextLineLookFolded() );
242
catch( VCard.ParseException e ) {
265
catch( Vcard.ParseException e ) {
244
267
if( !showContinue(
245
268
getText( R.string.error_vcf_parse ).toString()
246
+ fileName + "\n" + e.getMessage() ) )
270
getText( R.string.error_vcf_parse_line ).toString()
271
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
248
273
finish( ACTION_ABORT );
325
353
return _pos > 0 && _pos < _content.length &&
326
354
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
357
public int getLineNumber()
330
private class VCard extends ContactData
363
private class Vcard extends ContactData
332
365
private final static int NAMELEVEL_NONE = 0;
333
private final static int NAMELEVEL_ORG = 1;
366
private final static int NAMELEVEL_N = 1;
334
367
private final static int NAMELEVEL_FN = 2;
335
private final static int NAMELEVEL_N = 3;
369
private final static int MULTILINE_NONE = 0;
370
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
371
private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
372
private final static int MULTILINE_FOLDED = 3; // v3.0 folding
337
374
private String _version = null;
338
375
private Vector< ByteBuffer > _buffers = null;
339
376
private int _name_level = NAMELEVEL_NONE;
340
private boolean _parser_in_encoded_multiline = false;
341
private boolean _parser_in_folded_multiline = false;
377
private int _parser_multiline_state = MULTILINE_NONE;
342
378
private String _parser_current_name_and_params = null;
343
379
private String _parser_buffered_value_so_far = "";
380
private String _cached_organisation = null;
381
private String _cached_title = null;
345
383
protected class UnencodeResult
552
608
// unencoding_result = unencodeBase64( props[ 1 ], charset );
553
609
if( unencoding_result != null ) {
554
610
value = unencoding_result.getBuffer();
555
_parser_in_encoded_multiline =
556
unencoding_result.isAnotherLineRequired();
611
if( unencoding_result.isAnotherLineRequired() )
612
_parser_multiline_state = MULTILINE_ENCODED;
559
// convert 8-bit ASCII charset to US-ASCII
560
if( charset == null || charset.equals( "ASCII" ) ) {
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" ) ) ) )
561
622
value = transcodeAsciiToUtf8( value );
625
// process charset (value is now in UTF-8)
566
626
String string_value;
568
628
string_value = new String( value.array(), value.position(),
569
value.limit() - value.position(), charset );
629
value.limit() - value.position(), "UTF-8" );
570
630
} catch( UnsupportedEncodingException e ) {
571
631
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 );
574
647
// now we know whether we're in an encoding multi-line,
575
648
// 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;
649
if( _parser_multiline_state == MULTILINE_NONE &&
650
_version.equals( "3.0" ) && next_line_looks_folded )
652
_parser_multiline_state = MULTILINE_FOLDED;
579
// handle multi-line requests
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
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 ) {
583
658
_parser_buffered_value_so_far += string_value;
587
// add on buffered multi-line content
588
661
String complete_value =
589
_parser_buffered_value_so_far + string_value;
662
( _parser_buffered_value_so_far + string_value ).trim();
591
664
// ignore empty values
592
665
if( complete_value.length() < 1 ) return;
598
671
parseFN( name_param_parts, complete_value );
599
672
else if( name_param_parts[ 0 ].equals( "ORG" ) )
600
673
parseORG( name_param_parts, complete_value );
674
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
675
parseTITLE( name_param_parts, complete_value );
601
676
else if( name_param_parts[ 0 ].equals( "TEL" ) )
602
677
parseTEL( name_param_parts, complete_value );
603
678
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
604
679
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 );
687
private boolean doesStringEndInAnEscapeChar( String string )
689
// count the number of backslashes at the end of the string
691
for( int a = string.length() - 1; a >= 0; a-- )
692
if( string.charAt( a ) == '\\' )
697
// if there are an even number of backslashes then the final one
699
return ( count & 1 ) == 1;
702
private String[] splitValueByCharacter( String value, char character )
704
// split string in to parts by specified character
705
ArrayList< String > parts = new ArrayList< String >(
706
Arrays.asList( value.split( "" + character ) ) );
709
for( int a = 0; a < parts.size(); a++ )
711
String str = parts.get( a );
713
// look for parts that end in an escape character, but ignore
714
// the final part. We've already detected escape chars at the
715
// end of the final part in parseLine() and handled multi-lines
717
if( a < parts.size() - 1 &&
718
doesStringEndInAnEscapeChar( str ) )
720
// append the escaped character, join the next part to this
721
// part and remove the next part
722
parts.set( a, str.substring( 0, str.length() - 1 ) +
723
character + parts.get( a + 1 ) );
724
parts.remove( a + 1 );
726
// re-visit this part
731
// trim and replace string
736
String[] ret = new String[ parts.size() ];
737
return parts.toArray( ret );
740
private String unescapeValue( String value )
742
StringBuilder ret = new StringBuilder( value.length() );
743
boolean in_escape = false;
744
for( int a = 0; a < value.length(); a++ )
746
int c = value.codePointAt( a );
748
// process a normal character
753
ret.append( Character.toChars( c ) );
757
// process an escape sequence
769
// add escaped character
770
ret.append( Character.toChars( c ) );
773
// unknown escape sequence, so add it unescaped
775
ret.append( Character.toChars( c ) );
780
return ret.toString();
608
783
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
612
785
// already got a better name?
613
786
if( _name_level >= NAMELEVEL_N ) return;
615
788
// get name parts
616
String[] name_parts = value.split( ";" );
617
for( int i = 0; i < name_parts.length; i++ )
618
name_parts[ i ] = name_parts[ i ].trim();
789
String[] name_parts = splitValueByCharacter( value, ';' );
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 ];
793
final int[] part_order = { 3, 1, 2, 0, 4 };
794
for( int a = 0; a < part_order.length; a++ )
795
if( name_parts.length > part_order[ a ] &&
796
name_parts[ part_order[ a ] ].length() > 0 )
798
// split this part in to it's comma-separated bits
799
String[] name_part_parts = splitValueByCharacter(
800
name_parts[ part_order[ a ] ], ',' );
801
for( int b = 0; b < name_part_parts.length; b++ )
802
if( name_part_parts[ b ].length() > 0 )
804
if( value.length() == 0 ) value += " ";
805
value += name_part_parts[ b ];
810
setName( unescapeValue( value ) );
629
811
_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();
637
814
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
640
816
// already got a better name?
641
817
if( _name_level >= NAMELEVEL_FN ) return;
820
setName( unescapeValue( value ) );
645
821
_name_level = NAMELEVEL_FN;
648
824
private void parseORG( String[] params, String value )
649
throws ParseException, SkipContactException
651
// already got a better name?
652
if( _name_level >= NAMELEVEL_ORG ) return;
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;
827
String[] org_parts = splitValueByCharacter( value, ';' );
828
if( org_parts == null || org_parts.length < 1 ) return;
830
// build organisation name
831
StringBuilder builder = new StringBuilder(
832
String.valueOf( org_parts[ 0 ] ) );
833
for( int a = 1; a < org_parts.length; a++ )
834
builder.append( ", " ).append( org_parts[ a ] );
835
String organisation = unescapeValue( builder.toString() );
837
// set organisation name (using a title we've previously found)
838
addOrganisation( organisation, _cached_title, true );
840
// if we've not previously found a title, store this organisation
841
// name (we'll need it when we find a title to update the
842
// organisation, by name), else if we *have* previously found a
843
// title, clear it (since we just used it)
844
if( _cached_title == null )
845
_cached_organisation = organisation;
847
_cached_title = null;
850
private void parseTITLE( String[] params, String value )
852
value = unescapeValue( value );
854
// if we previously had an organisation, look it up and append this
856
if( _cached_organisation != null && hasOrganisations() ) {
857
HashMap< String, ExtraDetail > datas = getOrganisations();
858
ExtraDetail detail = datas.get( _cached_organisation );
860
detail.setExtra( value );
863
// same as when handling organisation, if we've not previously found
864
// an organisation we store this title, else we clear it (since we
865
// just appended this title to it)
866
if( _cached_organisation == null )
867
_cached_title = value;
869
_cached_organisation = null;
670
872
private void parseTEL( String[] params, String value )
671
throws ParseException
673
874
if( value.length() == 0 ) return;
677
878
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
679
880
// here's the logic...
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;
881
boolean is_preferred = types.contains( "PREF" );
689
883
if( types.contains( "FAX" ) )
690
884
if( types.contains( "HOME" ) )
691
type = PhonesColumns.TYPE_FAX_HOME;
885
type = TYPE_FAX_HOME;
693
type = PhonesColumns.TYPE_FAX_WORK;
694
if( types.contains( "PAGER" ) )
695
type = PhonesColumns.TYPE_PAGER;
887
type = TYPE_FAX_WORK;
888
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
890
else if( types.contains( "PAGER" ) )
892
else if( types.contains( "WORK" ) )
697
897
// add phone number
698
addPhone( value, type, preferred );
898
addNumber( value, type, is_preferred );
701
901
public void parseEMAIL( String[] params, String value )
702
throws ParseException
704
903
if( value.length() == 0 ) return;
706
905
Set< String > types = extractTypes( params, Arrays.asList(
707
906
"PREF", "WORK", "HOME", "INTERNET" ) );
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,
909
boolean is_preferred = types.contains( "PREF" );
911
if( types.contains( "WORK" ) )
916
addEmail( unescapeValue( value ), type, is_preferred );
919
private void parseADR( String[] params, String value )
922
String[] adr_parts = splitValueByCharacter( value, ';' );
926
for( int a = 0; a < adr_parts.length; a++ )
927
if( adr_parts[ a ].length() > 0 )
929
// split this part in to it's comma-separated bits
930
String[] adr_part_parts =
931
splitValueByCharacter( adr_parts[ a ], ',' );
932
for( int b = 0; b < adr_part_parts.length; b++ )
933
if( adr_part_parts[ b ].length() > 0 )
935
if( value.length() > 0 ) value += "\n";
936
value += adr_part_parts[ b ];
940
Set< String > types = extractTypes( params, Arrays.asList(
941
"PREF", "WORK", "HOME" ) );
945
if( types.contains( "WORK" ) )
950
addAddress( unescapeValue( value ), type );
953
private void parseLABEL( String[] params, String value )
955
Set< String > types = extractTypes( params, Arrays.asList(
956
"PREF", "WORK", "HOME" ) );
960
if( types.contains( "WORK" ) )
965
addAddress( unescapeValue( value ), type );
968
public void finaliseVcard()
969
throws ParseException, ContactNotIdentifiableException
721
971
// missing version (and data is present)
722
972
if( _version == null && _buffers != null )
723
973
throw new ParseException( R.string.error_vcf_malformed );
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();
975
// finalise the parent class
980
* Amongst the params, find the value of the first, only, of any with
984
* @return a value, or null
736
986
private String checkParam( String[] params, String name )
988
String[] res = checkParams( params, name );
989
return res.length > 0? res[ 0 ] : null;
993
* Amongst the params, find the values of any with the specified name
996
* @return an array of values, or null
998
private String[] checkParams( String[] params, String name )
1000
HashSet< String > ret = new HashSet< String >();
738
1002
Pattern p = Pattern.compile(
739
1003
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
740
1004
for( int i = 0; i < params.length; i++ ) {
741
1005
Matcher m = p.matcher( params[ i ] );
742
1006
if( m.matches() )
1007
ret.add( m.group( 2 ) );
1010
return (String[]) ret.toArray( new String[ ret.size() ] );
1014
* Amongst the params, return any type values present. For v2.1 vCards,
1015
* those types are just parameters. For v3.0, they are prefixed with
1016
* "TYPE=". There may also be multiple type parameters.
1018
* @param a list of type values to look for
1019
* @return a set of present type values
748
1021
private Set< String > extractTypes( String[] params,
749
1022
List< String > valid_types )
751
1024
HashSet< String > types = new HashSet< String >();
753
1026
// get 3.0-style TYPE= param
755
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
String[] parts = type_param.split( "," );
1027
String type_params[] = checkParams( params, "TYPE" );
1028
for( int a = 0; a < type_params.length; a++ )
1030
// check for a comma-separated list of types (why? this isn't in
1032
String[] parts = type_params[ a ].split( "," );
757
1033
for( int i = 0; i < parts.length; i++ )
758
1034
if( valid_types.contains( parts[ i ] ) )
759
1035
types.add( parts[ i ] );