44
44
import java.util.regex.Matcher;
45
45
import java.util.regex.Pattern;
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
49
47
import android.content.SharedPreferences;
50
48
import android.provider.Contacts;
51
49
import android.provider.Contacts.PhonesColumns;
53
public class VCFImporter extends Importer
51
public class VcardImporter extends Importer
55
private int _vCardCount = 0;
53
private int _vcard_count = 0;
56
54
private int _progress = 0;
58
public VCFImporter( Doit doit )
56
public VcardImporter( Doit doit )
207
if( vCard == null ) {
208
if( vcard == null ) {
208
209
// look for vcard beginning
209
210
if( line.matches( "^BEGIN:VCARD" ) ) {
210
setProgress( ++_progress );
211
setProgress( _progress++ );
213
vcard_start_line = cli.getLineNumber();
215
217
// look for vcard content or ending
216
218
if( line.matches( "^END:VCARD" ) )
218
// store vcard and do away with it
220
// finalise the vcard/contact
220
vCard.finaliseParsing();
221
importContact( vCard );
223
catch( VCard.ParseException e ) {
226
getText( R.string.error_vcf_parse ).toString()
227
+ fileName + "\n" + e.getMessage() ) )
229
finish( ACTION_ABORT );
232
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
240
259
// try giving the line to the vcard
242
vCard.parseLine( buffer, line,
261
vcard.parseLine( buffer, line,
243
262
cli.doesNextLineLookFolded() );
245
catch( VCard.ParseException e ) {
264
catch( Vcard.ParseException e ) {
247
266
if( !showContinue(
248
267
getText( R.string.error_vcf_parse ).toString()
249
+ fileName + "\n" + e.getMessage() ) )
269
getText( R.string.error_vcf_parse_line ).toString()
270
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
251
272
finish( ACTION_ABORT );
328
352
return _pos > 0 && _pos < _content.length &&
329
353
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
333
private class VCard extends ContactData
362
private class Vcard extends ContactData
335
364
private final static int NAMELEVEL_NONE = 0;
336
private final static int NAMELEVEL_FN = 1;
337
private final static int NAMELEVEL_N = 2;
365
private final static int NAMELEVEL_N = 1;
366
private final static int NAMELEVEL_FN = 2;
339
368
private final static int MULTILINE_NONE = 0;
340
369
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
385
414
public ParseException( int res )
387
super( VCFImporter.this.getText( res ).toString() );
416
super( VcardImporter.this.getText( res ).toString() );
391
420
@SuppressWarnings("serial")
392
protected class SkipContactException extends Exception { }
421
protected class SkipImportException extends Exception { }
394
423
private String extractCollonPartFromLine( ByteBuffer buffer,
395
424
String line, boolean former )
537
566
for( int i = 0; i < name_param_parts.length; i++ )
538
567
name_param_parts[ i ] = name_param_parts[ i ].trim();
569
// determine whether we care about this entry
570
final HashSet< String > interesting_fields =
571
new HashSet< String >( Arrays.asList( new String[]
572
{ "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
574
boolean is_interesting_field =
575
interesting_fields.contains( name_param_parts[ 0 ] );
540
577
// parse encoding parameter
541
578
String encoding = checkParam( name_param_parts, "ENCODING" );
542
579
if( encoding != null ) encoding = encoding.toUpperCase();
543
if( encoding != null && !encoding.equals( "8BIT" ) &&
580
if( is_interesting_field && encoding != null &&
581
!encoding.equals( "8BIT" ) &&
544
582
!encoding.equals( "QUOTED-PRINTABLE" ) )
545
583
//&& !encoding.equals( "BASE64" ) )
570
609
_parser_multiline_state = MULTILINE_ENCODED;
573
// convert 8-bit ASCII charset to US-ASCII
574
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" ) ) ) )
575
619
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
580
623
String string_value;
582
625
string_value = new String( value.array(), value.position(),
583
value.limit() - value.position(), charset );
626
value.limit() - value.position(), "UTF-8" );
584
627
} catch( UnsupportedEncodingException e ) {
585
628
throw new ParseException( R.string.error_vcf_charset );
651
694
return ( count & 1 ) == 1;
654
private String[] splitValueBySemicolon( String value )
697
private String[] splitValueByCharacter( String value, char character )
656
// split string in to parts by semicolon
699
// split string in to parts by specified character
657
700
ArrayList< String > parts = new ArrayList< String >(
658
Arrays.asList( value.split( ";" ) ) );
701
Arrays.asList( value.split( "" + character ) ) );
660
703
// go through parts
661
704
for( int a = 0; a < parts.size(); a++ )
669
712
if( a < parts.size() - 1 &&
670
713
doesStringEndInAnEscapeChar( str ) )
672
// join the next part to this part and remove the next part
715
// append the escaped character, join the next part to this
716
// part and remove the next part
673
717
parts.set( a, str.substring( 0, str.length() - 1 ) +
674
';' + parts.get( a + 1 ) );
718
character + parts.get( a + 1 ) );
675
719
parts.remove( a + 1 );
677
721
// re-visit this part
688
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();
691
778
private void parseN( String[] params, String value )
693
780
// already got a better name?
694
781
if( _name_level >= NAMELEVEL_N ) return;
696
783
// get name parts
697
String[] name_parts = splitValueBySemicolon( value );
784
String[] name_parts = splitValueByCharacter( value, ';' );
701
if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
702
value += name_parts[ 1 ];
703
if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
704
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 ) );
708
806
_name_level = NAMELEVEL_N;
714
812
if( _name_level >= NAMELEVEL_FN ) return;
815
setName( unescapeValue( value ) );
718
816
_name_level = NAMELEVEL_FN;
721
819
private void parseORG( String[] params, String value )
724
String[] org_parts = splitValueBySemicolon( value );
822
String[] org_parts = splitValueByCharacter( value, ';' );
725
823
if( org_parts == null || org_parts.length < 1 ) return;
727
825
// build organisation name
729
827
String.valueOf( org_parts[ 0 ] ) );
730
828
for( int a = 1; a < org_parts.length; a++ )
731
829
builder.append( ", " ).append( org_parts[ a ] );
732
String organisation = builder.toString();
830
String organisation = unescapeValue( builder.toString() );
734
832
// set organisation name (using a title we've previously found)
735
833
addOrganisation( organisation, _cached_title, true );
801
901
"PREF", "WORK", "HOME", "INTERNET" ) );
803
903
// add email address
804
boolean preferred = types.contains( "PREF" );
904
boolean is_preferred = types.contains( "PREF" );
806
906
if( types.contains( "WORK" ) )
807
907
type = Contacts.ContactMethods.TYPE_WORK;
809
909
type = Contacts.ContactMethods.TYPE_HOME;
811
addEmail( value, type, preferred );
911
addEmail( unescapeValue( value ), type, is_preferred );
814
914
private void parseADR( String[] params, String value )
816
916
// get address parts
817
String[] adr_parts = splitValueBySemicolon( value );
917
String[] adr_parts = splitValueByCharacter( value, ';' );
821
for( int a = 0; a < adr_parts.length; a++ ) {
822
if( value.length() > 0 ) value += "\n";
823
value += adr_parts[ a ].trim();
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 ];
826
935
Set< String > types = extractTypes( params, Arrays.asList(
827
936
"PREF", "WORK", "HOME", "INTERNET" ) );
834
943
type = Contacts.ContactMethods.TYPE_HOME;
836
addAddress( value, type );
945
addAddress( unescapeValue( value ), type );
839
public void finaliseParsing()
840
throws ParseException, SkipContactException,
948
public void finaliseVcard()
949
throws ParseException, ContactNotIdentifiableException
843
951
// missing version (and data is present)
844
952
if( _version == null && _buffers != null )
845
953
throw new ParseException( R.string.error_vcf_malformed );
847
// check if we should import this contact
849
if( !isImportRequired( this ) )
850
throw new SkipContactException();
852
catch( ContactNeedsMoreInfoException e ) {
853
throw new ParseException( R.string.error_vcf_notenoughinfo );
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
857
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 >();
859
982
Pattern p = Pattern.compile(
860
983
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
861
984
for( int i = 0; i < params.length; i++ ) {
862
985
Matcher m = p.matcher( params[ i ] );
863
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
869
1001
private Set< String > extractTypes( String[] params,
870
1002
List< String > valid_types )
872
1004
HashSet< String > types = new HashSet< String >();
874
1006
// get 3.0-style TYPE= param
876
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
877
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( "," );
878
1013
for( int i = 0; i < parts.length; i++ )
879
1014
if( valid_types.contains( parts[ i ] ) )
880
1015
types.add( parts[ i ] );