204
if( vCard == null ) {
208
if( vcard == null ) {
205
209
// look for vcard beginning
206
210
if( line.matches( "^BEGIN:VCARD" ) ) {
207
setProgress( ++_progress );
211
setProgress( _progress++ );
213
vcard_start_line = cli.getLineNumber();
212
217
// look for vcard content or ending
213
218
if( line.matches( "^END:VCARD" ) )
215
// store vcard and do away with it
220
// 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 ) {
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
237
259
// try giving the line to the vcard
239
vCard.parseLine( buffer, line,
261
vcard.parseLine( buffer, line,
240
262
cli.doesNextLineLookFolded() );
242
catch( VCard.ParseException e ) {
264
catch( Vcard.ParseException e ) {
244
266
if( !showContinue(
245
267
getText( R.string.error_vcf_parse ).toString()
246
+ fileName + "\n" + e.getMessage() ) )
269
getText( R.string.error_vcf_parse_line ).toString()
270
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
248
272
finish( ACTION_ABORT );
325
352
return _pos > 0 && _pos < _content.length &&
326
353
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
330
private class VCard extends ContactData
362
private class Vcard extends ContactData
332
364
private final static int NAMELEVEL_NONE = 0;
333
private final static int NAMELEVEL_ORG = 1;
365
private final static int NAMELEVEL_N = 1;
334
366
private final static int NAMELEVEL_FN = 2;
335
private final static int NAMELEVEL_N = 3;
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
337
373
private String _version = null;
338
374
private Vector< ByteBuffer > _buffers = null;
339
375
private int _name_level = NAMELEVEL_NONE;
340
private boolean _parser_in_encoded_multiline = false;
341
private boolean _parser_in_folded_multiline = false;
376
private int _parser_multiline_state = MULTILINE_NONE;
342
377
private String _parser_current_name_and_params = null;
343
378
private String _parser_buffered_value_so_far = "";
379
private String _cached_organisation = null;
380
private String _cached_title = null;
345
382
protected class UnencodeResult
552
605
// unencoding_result = unencodeBase64( props[ 1 ], charset );
553
606
if( unencoding_result != null ) {
554
607
value = unencoding_result.getBuffer();
555
_parser_in_encoded_multiline =
556
unencoding_result.isAnotherLineRequired();
608
if( unencoding_result.isAnotherLineRequired() )
609
_parser_multiline_state = MULTILINE_ENCODED;
559
// convert 8-bit ASCII charset to US-ASCII
560
if( charset == null || charset.equals( "ASCII" ) ) {
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" ) ) ) )
561
619
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
566
623
String string_value;
568
625
string_value = new String( value.array(), value.position(),
569
value.limit() - value.position(), charset );
626
value.limit() - value.position(), "UTF-8" );
570
627
} catch( UnsupportedEncodingException e ) {
571
628
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 );
574
644
// now we know whether we're in an encoding multi-line,
575
645
// 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;
646
if( _parser_multiline_state == MULTILINE_NONE &&
647
_version.equals( "3.0" ) && next_line_looks_folded )
649
_parser_multiline_state = MULTILINE_FOLDED;
579
// handle multi-line requests
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
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 ) {
583
655
_parser_buffered_value_so_far += string_value;
587
// add on buffered multi-line content
588
658
String complete_value =
589
_parser_buffered_value_so_far + string_value;
659
( _parser_buffered_value_so_far + string_value ).trim();
591
661
// ignore empty values
592
662
if( complete_value.length() < 1 ) return;
598
668
parseFN( name_param_parts, complete_value );
599
669
else if( name_param_parts[ 0 ].equals( "ORG" ) )
600
670
parseORG( name_param_parts, complete_value );
671
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
672
parseTITLE( name_param_parts, complete_value );
601
673
else if( name_param_parts[ 0 ].equals( "TEL" ) )
602
674
parseTEL( name_param_parts, complete_value );
603
675
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
604
676
parseEMAIL( name_param_parts, complete_value );
677
else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
parseADR( name_param_parts, complete_value );
682
private boolean doesStringEndInAnEscapeChar( String string )
684
// count the number of backslashes at the end of the string
686
for( int a = string.length() - 1; a >= 0; a-- )
687
if( string.charAt( a ) == '\\' )
692
// if there are an even number of backslashes then the final one
694
return ( count & 1 ) == 1;
697
private String[] splitValueByCharacter( String value, char character )
699
// split string in to parts by specified character
700
ArrayList< String > parts = new ArrayList< String >(
701
Arrays.asList( value.split( "" + character ) ) );
704
for( int a = 0; a < parts.size(); a++ )
706
String str = parts.get( a );
708
// look for parts that end in an escape character, but ignore
709
// the final part. We've already detected escape chars at the
710
// end of the final part in parseLine() and handled multi-lines
712
if( a < parts.size() - 1 &&
713
doesStringEndInAnEscapeChar( str ) )
715
// append the escaped character, join the next part to this
716
// part and remove the next part
717
parts.set( a, str.substring( 0, str.length() - 1 ) +
718
character + parts.get( a + 1 ) );
719
parts.remove( a + 1 );
721
// re-visit this part
726
// trim and replace string
731
String[] ret = new String[ parts.size() ];
732
return parts.toArray( ret );
735
private String unescapeValue( String value )
737
StringBuilder ret = new StringBuilder( value.length() );
738
boolean in_escape = false;
739
for( int a = 0; a < value.length(); a++ )
741
int c = value.codePointAt( a );
743
// process a normal character
748
ret.append( Character.toChars( c ) );
752
// process an escape sequence
764
// add escaped character
765
ret.append( Character.toChars( c ) );
768
// unknown escape sequence, so add it unescaped
770
ret.append( Character.toChars( c ) );
775
return ret.toString();
608
778
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
612
780
// already got a better name?
613
781
if( _name_level >= NAMELEVEL_N ) return;
615
783
// 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();
784
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 ];
788
final int[] part_order = { 3, 1, 2, 0, 4 };
789
for( int a = 0; a < part_order.length; a++ )
790
if( name_parts.length > part_order[ a ] &&
791
name_parts[ part_order[ a ] ].length() > 0 )
793
// split this part in to it's comma-separated bits
794
String[] name_part_parts = splitValueByCharacter(
795
name_parts[ part_order[ a ] ], ',' );
796
for( int b = 0; b < name_part_parts.length; b++ )
797
if( name_part_parts[ b ].length() > 0 )
799
if( value.length() == 0 ) value += " ";
800
value += name_part_parts[ b ];
805
setName( unescapeValue( value ) );
629
806
_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
809
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
640
811
// already got a better name?
641
812
if( _name_level >= NAMELEVEL_FN ) return;
815
setName( unescapeValue( value ) );
645
816
_name_level = NAMELEVEL_FN;
648
819
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;
822
String[] org_parts = splitValueByCharacter( value, ';' );
823
if( org_parts == null || org_parts.length < 1 ) return;
825
// build organisation name
826
StringBuilder builder = new StringBuilder(
827
String.valueOf( org_parts[ 0 ] ) );
828
for( int a = 1; a < org_parts.length; a++ )
829
builder.append( ", " ).append( org_parts[ a ] );
830
String organisation = unescapeValue( builder.toString() );
832
// set organisation name (using a title we've previously found)
833
addOrganisation( organisation, _cached_title, true );
835
// if we've not previously found a title, store this organisation
836
// name (we'll need it when we find a title to update the
837
// organisation, by name), else if we *have* previously found a
838
// title, clear it (since we just used it)
839
if( _cached_title == null )
840
_cached_organisation = organisation;
842
_cached_title = null;
845
private void parseTITLE( String[] params, String value )
847
value = unescapeValue( value );
849
// if we previously had an organisation, look it up and append this
851
if( _cached_organisation != null && hasOrganisations() ) {
852
HashMap< String, ExtraDetail > datas = getOrganisations();
853
ExtraDetail detail = datas.get( _cached_organisation );
855
detail.setExtra( value );
858
// same as when handling organisation, if we've not previously found
859
// an organisation we store this title, else we clear it (since we
860
// just appended this title to it)
861
if( _cached_organisation == null )
862
_cached_title = value;
864
_cached_organisation = null;
670
867
private void parseTEL( String[] params, String value )
671
throws ParseException
673
869
if( value.length() == 0 ) return;
677
873
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
679
875
// 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;
876
boolean is_preferred = types.contains( "PREF" );
689
878
if( types.contains( "FAX" ) )
690
879
if( types.contains( "HOME" ) )
691
880
type = PhonesColumns.TYPE_FAX_HOME;
693
882
type = PhonesColumns.TYPE_FAX_WORK;
694
if( types.contains( "PAGER" ) )
883
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
884
type = PhonesColumns.TYPE_MOBILE;
885
else if( types.contains( "PAGER" ) )
695
886
type = PhonesColumns.TYPE_PAGER;
887
else if( types.contains( "WORK" ) )
888
type = PhonesColumns.TYPE_WORK;
890
type = PhonesColumns.TYPE_HOME;
697
892
// add phone number
698
addPhone( value, type, preferred );
893
addNumber( value, type, is_preferred );
701
896
public void parseEMAIL( String[] params, String value )
702
throws ParseException
704
898
if( value.length() == 0 ) return;
706
900
Set< String > types = extractTypes( params, Arrays.asList(
707
901
"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,
904
boolean is_preferred = types.contains( "PREF" );
906
if( types.contains( "WORK" ) )
907
type = Contacts.ContactMethods.TYPE_WORK;
909
type = Contacts.ContactMethods.TYPE_HOME;
911
addEmail( unescapeValue( value ), type, is_preferred );
914
private void parseADR( String[] params, String value )
917
String[] adr_parts = splitValueByCharacter( value, ';' );
921
for( int a = 0; a < adr_parts.length; a++ )
922
if( adr_parts[ a ].length() > 0 )
924
// split this part in to it's comma-separated bits
925
String[] adr_part_parts =
926
splitValueByCharacter( adr_parts[ a ], ',' );
927
for( int b = 0; b < adr_part_parts.length; b++ )
928
if( adr_part_parts[ b ].length() > 0 )
930
if( value.length() > 0 ) value += "\n";
931
value += adr_part_parts[ b ];
935
Set< String > types = extractTypes( params, Arrays.asList(
936
"PREF", "WORK", "HOME", "INTERNET" ) );
940
if( types.contains( "WORK" ) )
941
type = Contacts.ContactMethods.TYPE_WORK;
943
type = Contacts.ContactMethods.TYPE_HOME;
945
addAddress( unescapeValue( value ), type );
948
public void finaliseVcard()
949
throws ParseException, ContactNotIdentifiableException
721
951
// missing version (and data is present)
722
952
if( _version == null && _buffers != null )
723
953
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();
955
// finalise the parent class
960
* Amongst the params, find the value of the first, only, of any with
964
* @return a value, or null
736
966
private String checkParam( String[] params, String name )
968
String[] res = checkParams( params, name );
969
return res.length > 0? res[ 0 ] : null;
973
* Amongst the params, find the values of any with the specified name
976
* @return an array of values, or null
978
private String[] checkParams( String[] params, String name )
980
HashSet< String > ret = new HashSet< String >();
738
982
Pattern p = Pattern.compile(
739
983
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
740
984
for( int i = 0; i < params.length; i++ ) {
741
985
Matcher m = p.matcher( params[ i ] );
742
986
if( m.matches() )
987
ret.add( m.group( 2 ) );
990
return (String[]) ret.toArray( new String[ ret.size() ] );
994
* Amongst the params, return any type values present. For v2.1 vCards,
995
* those types are just parameters. For v3.0, they are prefixed with
996
* "TYPE=". There may also be multiple type parameters.
998
* @param a list of type values to look for
999
* @return a set of present type values
748
1001
private Set< String > extractTypes( String[] params,
749
1002
List< String > valid_types )
751
1004
HashSet< String > types = new HashSet< String >();
753
1006
// get 3.0-style TYPE= param
755
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
String[] parts = type_param.split( "," );
1007
String type_params[] = checkParams( params, "TYPE" );
1008
for( int a = 0; a < type_params.length; a++ )
1010
// check for a comma-separated list of types (why? this isn't in
1012
String[] parts = type_params[ a ].split( "," );
757
1013
for( int i = 0; i < parts.length; i++ )
758
1014
if( valid_types.contains( parts[ i ] ) )
759
1015
types.add( parts[ i ] );