362
362
private class Vcard extends ContactData
364
364
private final static int NAMELEVEL_NONE = 0;
365
private final static int NAMELEVEL_N = 1;
366
private final static int NAMELEVEL_FN = 2;
365
private final static int NAMELEVEL_FN = 1;
366
private final static int NAMELEVEL_N = 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[] { "N",
572
"FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
571
new HashSet< String >( Arrays.asList( new String[]
572
{ "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
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 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" ) ) ) )
612
// convert 8-bit ASCII charset to US-ASCII
613
if( charset == null || charset.equals( "ASCII" ) ) {
619
614
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
623
619
String string_value;
625
621
string_value = new String( value.array(), value.position(),
626
value.limit() - value.position(), "UTF-8" );
622
value.limit() - value.position(), charset );
627
623
} catch( UnsupportedEncodingException e ) {
628
624
throw new ParseException( R.string.error_vcf_charset );
676
672
parseEMAIL( name_param_parts, complete_value );
677
673
else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
674
parseADR( name_param_parts, complete_value );
679
else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
parseLABEL( name_param_parts, complete_value );
696
690
return ( count & 1 ) == 1;
699
private String[] splitValueByCharacter( String value, char character )
693
private String[] splitValueBySemicolon( String value )
701
// split string in to parts by specified character
695
// split string in to parts by semicolon
702
696
ArrayList< String > parts = new ArrayList< String >(
703
Arrays.asList( value.split( "" + character ) ) );
697
Arrays.asList( value.split( ";" ) ) );
705
699
// go through parts
706
700
for( int a = 0; a < parts.size(); a++ )
714
708
if( a < parts.size() - 1 &&
715
709
doesStringEndInAnEscapeChar( str ) )
717
// append the escaped character, join the next part to this
718
// part and remove the next part
711
// join the next part to this part and remove the next part
719
712
parts.set( a, str.substring( 0, str.length() - 1 ) +
720
character + parts.get( a + 1 ) );
713
';' + parts.get( a + 1 ) );
721
714
parts.remove( a + 1 );
723
716
// re-visit this part
734
727
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
730
private void parseN( String[] params, String value )
782
732
// already got a better name?
783
733
if( _name_level >= NAMELEVEL_N ) return;
785
735
// get name parts
786
String[] name_parts = splitValueByCharacter( value, ';' );
736
String[] name_parts = splitValueBySemicolon( value );
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 ];
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 ];
807
setName( unescapeValue( value ) );
808
747
_name_level = NAMELEVEL_N;
814
753
if( _name_level >= NAMELEVEL_FN ) return;
817
setName( unescapeValue( value ) );
818
757
_name_level = NAMELEVEL_FN;
821
760
private void parseORG( String[] params, String value )
824
String[] org_parts = splitValueByCharacter( value, ';' );
763
String[] org_parts = splitValueBySemicolon( value );
825
764
if( org_parts == null || org_parts.length < 1 ) return;
827
766
// build organisation name
829
768
String.valueOf( org_parts[ 0 ] ) );
830
769
for( int a = 1; a < org_parts.length; a++ )
831
770
builder.append( ", " ).append( org_parts[ a ] );
832
String organisation = unescapeValue( builder.toString() );
771
String organisation = builder.toString();
834
773
// set organisation name (using a title we've previously found)
835
774
addOrganisation( organisation, _cached_title, true );
911
848
type = Contacts.ContactMethods.TYPE_HOME;
913
addEmail( unescapeValue( value ), type, is_preferred );
850
addEmail( value, type, is_preferred );
916
853
private void parseADR( String[] params, String value )
918
855
// get address parts
919
String[] adr_parts = splitValueByCharacter( value, ';' );
856
String[] adr_parts = splitValueBySemicolon( 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 );
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 );
965
878
public void finaliseVcard()
977
* Amongst the params, find the value of the first, only, of any with
981
* @return a value, or null
983
889
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
891
Pattern p = Pattern.compile(
1000
892
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1001
893
for( int i = 0; i < params.length; i++ ) {
1002
894
Matcher m = p.matcher( params[ i ] );
1003
895
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
901
private Set< String > extractTypes( String[] params,
1019
902
List< String > valid_types )
1021
904
HashSet< String > types = new HashSet< String >();
1023
906
// 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( "," );
908
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
909
String[] parts = type_param.split( "," );
1030
910
for( int i = 0; i < parts.length; i++ )
1031
911
if( valid_types.contains( parts[ i ] ) )
1032
912
types.add( parts[ i ] );