4
* Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
6
6
* This file is part of the Import Contacts program (hereafter referred
7
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/import-contacts
8
* http://ed.am/dev/android/import-contacts
10
10
* This program is free software: you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License as published by
362
362
private class Vcard extends ContactData
364
364
private final static int NAMELEVEL_NONE = 0;
365
private final static int NAMELEVEL_FN = 1;
366
private final static int NAMELEVEL_N = 2;
365
private final static int NAMELEVEL_N = 1;
366
private final static int NAMELEVEL_FN = 2;
368
368
private final static int MULTILINE_NONE = 0;
369
369
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
569
569
// determine whether we care about this entry
570
570
final HashSet< String > interesting_fields =
571
new HashSet< String >( Arrays.asList( new String[]
572
{ "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
571
new HashSet< String >( Arrays.asList( new String[] { "N",
572
"FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
574
574
boolean is_interesting_field =
575
575
interesting_fields.contains( name_param_parts[ 0 ] );
609
609
_parser_multiline_state = MULTILINE_ENCODED;
612
// convert 8-bit ASCII charset to US-ASCII
613
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" ) ) ) )
614
619
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
619
623
String string_value;
621
625
string_value = new String( value.array(), value.position(),
622
value.limit() - value.position(), charset );
626
value.limit() - value.position(), "UTF-8" );
623
627
} catch( UnsupportedEncodingException e ) {
624
628
throw new ParseException( R.string.error_vcf_charset );
672
676
parseEMAIL( name_param_parts, complete_value );
673
677
else if( name_param_parts[ 0 ].equals( "ADR" ) )
674
678
parseADR( name_param_parts, complete_value );
679
else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
parseLABEL( name_param_parts, complete_value );
690
696
return ( count & 1 ) == 1;
693
private String[] splitValueBySemicolon( String value )
699
private String[] splitValueByCharacter( String value, char character )
695
// split string in to parts by semicolon
701
// split string in to parts by specified character
696
702
ArrayList< String > parts = new ArrayList< String >(
697
Arrays.asList( value.split( ";" ) ) );
703
Arrays.asList( value.split( "" + character ) ) );
699
705
// go through parts
700
706
for( int a = 0; a < parts.size(); a++ )
708
714
if( a < parts.size() - 1 &&
709
715
doesStringEndInAnEscapeChar( str ) )
711
// join the next part to this part and remove the next part
717
// append the escaped character, join the next part to this
718
// part and remove the next part
712
719
parts.set( a, str.substring( 0, str.length() - 1 ) +
713
';' + parts.get( a + 1 ) );
720
character + parts.get( a + 1 ) );
714
721
parts.remove( a + 1 );
716
723
// re-visit this part
727
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();
730
780
private void parseN( String[] params, String value )
732
782
// already got a better name?
733
783
if( _name_level >= NAMELEVEL_N ) return;
735
785
// get name parts
736
String[] name_parts = splitValueBySemicolon( value );
786
String[] name_parts = splitValueByCharacter( value, ';' );
740
if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
741
value += name_parts[ 1 ];
742
if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
743
value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
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 ];
807
setName( unescapeValue( value ) );
747
808
_name_level = NAMELEVEL_N;
753
814
if( _name_level >= NAMELEVEL_FN ) return;
817
setName( unescapeValue( value ) );
757
818
_name_level = NAMELEVEL_FN;
760
821
private void parseORG( String[] params, String value )
763
String[] org_parts = splitValueBySemicolon( value );
824
String[] org_parts = splitValueByCharacter( value, ';' );
764
825
if( org_parts == null || org_parts.length < 1 ) return;
766
827
// build organisation name
768
829
String.valueOf( org_parts[ 0 ] ) );
769
830
for( int a = 1; a < org_parts.length; a++ )
770
831
builder.append( ", " ).append( org_parts[ a ] );
771
String organisation = builder.toString();
832
String organisation = unescapeValue( builder.toString() );
773
834
// set organisation name (using a title we've previously found)
774
835
addOrganisation( organisation, _cached_title, true );
848
911
type = Contacts.ContactMethods.TYPE_HOME;
850
addEmail( value, type, is_preferred );
913
addEmail( unescapeValue( value ), type, is_preferred );
853
916
private void parseADR( String[] params, String value )
855
918
// get address parts
856
String[] adr_parts = splitValueBySemicolon( value );
919
String[] adr_parts = splitValueByCharacter( value, ';' );
860
for( int a = 0; a < adr_parts.length; a++ ) {
861
if( value.length() > 0 ) value += "\n";
862
value += adr_parts[ a ].trim();
865
Set< String > types = extractTypes( params, Arrays.asList(
866
"PREF", "WORK", "HOME", "INTERNET" ) );
870
if( types.contains( "WORK" ) )
871
type = Contacts.ContactMethods.TYPE_WORK;
873
type = Contacts.ContactMethods.TYPE_HOME;
875
addAddress( value, type );
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 );
878
965
public void finaliseVcard()
977
* Amongst the params, find the value of the first, only, of any with
981
* @return a value, or null
889
983
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 >();
891
999
Pattern p = Pattern.compile(
892
1000
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
893
1001
for( int i = 0; i < params.length; i++ ) {
894
1002
Matcher m = p.matcher( params[ i ] );
895
1003
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
901
1018
private Set< String > extractTypes( String[] params,
902
1019
List< String > valid_types )
904
1021
HashSet< String > types = new HashSet< String >();
906
1023
// get 3.0-style TYPE= param
908
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
909
String[] parts = type_param.split( "," );
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( "," );
910
1030
for( int i = 0; i < parts.length; i++ )
911
1031
if( valid_types.contains( parts[ i ] ) )
912
1032
types.add( parts[ i ] );