205
208
if( vcard == null ) {
206
209
// look for vcard beginning
207
210
if( line.matches( "^BEGIN:VCARD" ) ) {
208
setProgress( ++_progress );
211
setProgress( _progress++ );
209
212
vcard = new Vcard();
213
vcard_start_line = cli.getLineNumber();
216
220
// finalise the vcard/contact
218
222
vcard.finaliseVcard();
224
// pass the finalised contact to the importer
225
importContact( vcard );
220
227
catch( Vcard.ParseException e ) {
223
getText( R.string.error_vcf_parse ).toString()
224
+ fileName + "\n" + e.getMessage() ) )
226
finish( ACTION_ABORT );
230
// pass the finalised contact to the importer
231
importContact( vcard );
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
326
352
return _pos > 0 && _pos < _content.length &&
327
353
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
331
362
private class Vcard extends ContactData
333
364
private final static int NAMELEVEL_NONE = 0;
334
private final static int NAMELEVEL_FN = 1;
335
private final static int NAMELEVEL_N = 2;
365
private final static int NAMELEVEL_N = 1;
366
private final static int NAMELEVEL_FN = 2;
337
368
private final static int MULTILINE_NONE = 0;
338
369
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
535
566
for( int i = 0; i < name_param_parts.length; i++ )
536
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 ] );
538
577
// parse encoding parameter
539
578
String encoding = checkParam( name_param_parts, "ENCODING" );
540
579
if( encoding != null ) encoding = encoding.toUpperCase();
541
if( encoding != null && !encoding.equals( "8BIT" ) &&
580
if( is_interesting_field && encoding != null &&
581
!encoding.equals( "8BIT" ) &&
542
582
!encoding.equals( "QUOTED-PRINTABLE" ) )
543
583
//&& !encoding.equals( "BASE64" ) )
548
588
// parse charset parameter
549
589
String charset = checkParam( name_param_parts, "CHARSET" );
550
590
if( charset != null ) charset = charset.toUpperCase();
551
if( charset != null && !charset.equals( "US-ASCII" ) &&
591
if( charset != null &&
592
!charset.equals( "US-ASCII" ) &&
552
593
!charset.equals( "ASCII" ) &&
553
594
!charset.equals( "UTF-8" ) )
568
609
_parser_multiline_state = MULTILINE_ENCODED;
571
// convert 8-bit ASCII charset to US-ASCII
572
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" ) ) ) )
573
619
value = transcodeAsciiToUtf8( value );
622
// process charset (value is now in UTF-8)
578
623
String string_value;
580
625
string_value = new String( value.array(), value.position(),
581
value.limit() - value.position(), charset );
626
value.limit() - value.position(), "UTF-8" );
582
627
} catch( UnsupportedEncodingException e ) {
583
628
throw new ParseException( R.string.error_vcf_charset );
649
694
return ( count & 1 ) == 1;
652
private String[] splitValueBySemicolon( String value )
697
private String[] splitValueByCharacter( String value, char character )
654
// split string in to parts by semicolon
699
// split string in to parts by specified character
655
700
ArrayList< String > parts = new ArrayList< String >(
656
Arrays.asList( value.split( ";" ) ) );
701
Arrays.asList( value.split( "" + character ) ) );
658
703
// go through parts
659
704
for( int a = 0; a < parts.size(); a++ )
667
712
if( a < parts.size() - 1 &&
668
713
doesStringEndInAnEscapeChar( str ) )
670
// 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
671
717
parts.set( a, str.substring( 0, str.length() - 1 ) +
672
';' + parts.get( a + 1 ) );
718
character + parts.get( a + 1 ) );
673
719
parts.remove( a + 1 );
675
721
// re-visit this part
686
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();
689
778
private void parseN( String[] params, String value )
691
780
// already got a better name?
692
781
if( _name_level >= NAMELEVEL_N ) return;
694
783
// get name parts
695
String[] name_parts = splitValueBySemicolon( value );
784
String[] name_parts = splitValueByCharacter( value, ';' );
699
if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
700
value += name_parts[ 1 ];
701
if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
702
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 ) );
706
806
_name_level = NAMELEVEL_N;
712
812
if( _name_level >= NAMELEVEL_FN ) return;
815
setName( unescapeValue( value ) );
716
816
_name_level = NAMELEVEL_FN;
719
819
private void parseORG( String[] params, String value )
722
String[] org_parts = splitValueBySemicolon( value );
822
String[] org_parts = splitValueByCharacter( value, ';' );
723
823
if( org_parts == null || org_parts.length < 1 ) return;
725
825
// build organisation name
727
827
String.valueOf( org_parts[ 0 ] ) );
728
828
for( int a = 1; a < org_parts.length; a++ )
729
829
builder.append( ", " ).append( org_parts[ a ] );
730
String organisation = builder.toString();
830
String organisation = unescapeValue( builder.toString() );
732
832
// set organisation name (using a title we've previously found)
733
833
addOrganisation( organisation, _cached_title, true );
745
845
private void parseTITLE( String[] params, String value )
847
value = unescapeValue( value );
747
849
// if we previously had an organisation, look it up and append this
749
851
if( _cached_organisation != null && hasOrganisations() ) {
807
909
type = Contacts.ContactMethods.TYPE_HOME;
809
addEmail( value, type, is_preferred );
911
addEmail( unescapeValue( value ), type, is_preferred );
812
914
private void parseADR( String[] params, String value )
814
916
// get address parts
815
String[] adr_parts = splitValueBySemicolon( value );
917
String[] adr_parts = splitValueByCharacter( value, ';' );
819
for( int a = 0; a < adr_parts.length; a++ ) {
820
if( value.length() > 0 ) value += "\n";
821
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 ];
824
935
Set< String > types = extractTypes( params, Arrays.asList(
825
936
"PREF", "WORK", "HOME", "INTERNET" ) );
832
943
type = Contacts.ContactMethods.TYPE_HOME;
834
addAddress( value, type );
945
addAddress( unescapeValue( value ), type );
837
948
public void finaliseVcard()
838
throws ParseException
949
throws ParseException, ContactNotIdentifiableException
840
951
// missing version (and data is present)
841
952
if( _version == null && _buffers != null )
842
953
throw new ParseException( R.string.error_vcf_malformed );
844
955
// finalise the parent class
848
catch( ContactNotIdentifiableException e ) {
849
throw new ParseException( R.string.error_vcf_notenoughinfo );
853
959
private String checkParam( String[] params, String name )