/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2012-12-19 17:41:29 UTC
  • Revision ID: tim@ed.am-20121219174129-41i7vrz0jviideqi
fix intro strings

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
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
9
9
 *
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
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
362
362
        private class Vcard extends ContactData
363
363
        {
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;
367
367
 
368
368
                private final static int MULTILINE_NONE = 0;
369
369
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
568
568
 
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" }
573
573
                                ) );
574
574
                                boolean is_interesting_field =
575
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
609
609
                                                _parser_multiline_state = MULTILINE_ENCODED;
610
610
                                }
611
611
 
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" ) ) ) )
 
618
                                {
614
619
                                        value = transcodeAsciiToUtf8( value );
615
 
                                        charset = "UTF-8";
616
620
                                }
617
621
 
618
 
                                // process charset
 
622
                                // process charset (value is now in UTF-8)
619
623
                                String string_value;
620
624
                                try {
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 );
625
629
                                }
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 );
675
681
                        }
676
682
                }
677
683
 
690
696
                        return ( count & 1 ) == 1;
691
697
                }
692
698
 
693
 
                private String[] splitValueBySemicolon( String value )
 
699
                private String[] splitValueByCharacter( String value, char character )
694
700
                {
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 ) ) );
698
704
 
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 ) )
710
716
                                {
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 );
715
722
 
716
723
                                        // re-visit this part
727
734
                        return parts.toArray( ret );
728
735
                }
729
736
 
 
737
                private String unescapeValue( String value )
 
738
                {
 
739
                        StringBuilder ret = new StringBuilder( value.length() );
 
740
                        boolean in_escape = false;
 
741
                        for( int a = 0; a < value.length(); a++ )
 
742
                        {
 
743
                                int c = value.codePointAt( a );
 
744
 
 
745
                                // process a normal character
 
746
                                if( !in_escape ) {
 
747
                                        if( c == '\\' )
 
748
                                                in_escape = true;
 
749
                                        else
 
750
                                                ret.append( Character.toChars( c ) );
 
751
                                        continue;
 
752
                                }
 
753
 
 
754
                                // process an escape sequence
 
755
                                in_escape = false;
 
756
                                switch( c )
 
757
                                {
 
758
                                case 'N':
 
759
                                case 'n':
 
760
                                        // add newline
 
761
                                        ret.append( '\n' );
 
762
                                        break;
 
763
                                case '\\':
 
764
                                case ',':
 
765
                                case ';':
 
766
                                        // add escaped character
 
767
                                        ret.append( Character.toChars( c ) );
 
768
                                        break;
 
769
                                default:
 
770
                                        // unknown escape sequence, so add it unescaped
 
771
                                        ret.append( "\\" );
 
772
                                        ret.append( Character.toChars( c ) );
 
773
                                        break;
 
774
                                }
 
775
                        }
 
776
 
 
777
                        return ret.toString();
 
778
                }
 
779
 
730
780
                private void parseN( String[] params, String value )
731
781
                {
732
782
                        // already got a better name?
733
783
                        if( _name_level >= NAMELEVEL_N ) return;
734
784
 
735
785
                        // get name parts
736
 
                        String[] name_parts = splitValueBySemicolon( value );
 
786
                        String[] name_parts = splitValueByCharacter( value, ';' );
737
787
 
738
788
                        // build name
739
789
                        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 )
 
794
                                {
 
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 )
 
800
                                                {
 
801
                                                        if( value.length() == 0 ) value += " ";
 
802
                                                        value += name_part_parts[ b ];
 
803
                                                }
 
804
                                }
744
805
 
745
806
                        // set name
746
 
                        setName( value );
 
807
                        setName( unescapeValue( value ) );
747
808
                        _name_level = NAMELEVEL_N;
748
809
                }
749
810
 
753
814
                        if( _name_level >= NAMELEVEL_FN ) return;
754
815
 
755
816
                        // set name
756
 
                        setName( value );
 
817
                        setName( unescapeValue( value ) );
757
818
                        _name_level = NAMELEVEL_FN;
758
819
                }
759
820
 
760
821
                private void parseORG( String[] params, String value )
761
822
                {
762
823
                        // get org parts
763
 
                        String[] org_parts = splitValueBySemicolon( value );
 
824
                        String[] org_parts = splitValueByCharacter( value, ';' );
764
825
                        if( org_parts == null || org_parts.length < 1 ) return;
765
826
 
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() );
772
833
 
773
834
                        // set organisation name (using a title we've previously found)
774
835
                        addOrganisation( organisation, _cached_title, true );
785
846
 
786
847
                private void parseTITLE( String[] params, String value )
787
848
                {
 
849
                        value = unescapeValue( value );
 
850
 
788
851
                        // if we previously had an organisation, look it up and append this
789
852
                        // title to it
790
853
                        if( _cached_organisation != null && hasOrganisations() ) {
847
910
                        else
848
911
                                type = Contacts.ContactMethods.TYPE_HOME;
849
912
 
850
 
                        addEmail( value, type, is_preferred );
 
913
                        addEmail( unescapeValue( value ), type, is_preferred );
851
914
                }
852
915
 
853
916
                private void parseADR( String[] params, String value )
854
917
                {
855
918
                        // get address parts
856
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
919
                        String[] adr_parts = splitValueByCharacter( value, ';' );
857
920
 
858
921
                        // build address
859
922
                        value = "";
860
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
861
 
                                if( value.length() > 0 ) value += "\n";
862
 
                                value += adr_parts[ a ].trim();
863
 
                        }
864
 
 
865
 
                        Set< String > types = extractTypes( params, Arrays.asList(
866
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
867
 
 
868
 
                        // add address
869
 
                        int type;
870
 
                        if( types.contains( "WORK" ) )
871
 
                                type = Contacts.ContactMethods.TYPE_WORK;
872
 
                        else
873
 
                                type = Contacts.ContactMethods.TYPE_HOME;
874
 
 
875
 
                        addAddress( value, type );
 
923
                        for( int a = 0; a < adr_parts.length; a++ )
 
924
                                if( adr_parts[ a ].length() > 0 )
 
925
                                {
 
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 )
 
931
                                                {
 
932
                                                        if( value.length() > 0 ) value += "\n";
 
933
                                                        value += adr_part_parts[ b ];
 
934
                                                }
 
935
                                }
 
936
 
 
937
                        Set< String > types = extractTypes( params, Arrays.asList(
 
938
                                "PREF", "WORK", "HOME" ) );
 
939
 
 
940
                        // add address
 
941
                        int type;
 
942
                        if( types.contains( "WORK" ) )
 
943
                                type = Contacts.ContactMethods.TYPE_WORK;
 
944
                        else
 
945
                                type = Contacts.ContactMethods.TYPE_HOME;
 
946
 
 
947
                        addAddress( unescapeValue( value ), type );
 
948
                }
 
949
 
 
950
                private void parseLABEL( String[] params, String value )
 
951
                {
 
952
                        Set< String > types = extractTypes( params, Arrays.asList(
 
953
                                "PREF", "WORK", "HOME" ) );
 
954
 
 
955
                        // add address
 
956
                        int type;
 
957
                        if( types.contains( "WORK" ) )
 
958
                                type = Contacts.ContactMethods.TYPE_WORK;
 
959
                        else
 
960
                                type = Contacts.ContactMethods.TYPE_HOME;
 
961
 
 
962
                        addAddress( unescapeValue( value ), type );
876
963
                }
877
964
 
878
965
                public void finaliseVcard()
886
973
                        finalise();
887
974
                }
888
975
 
 
976
                /**
 
977
                 * Amongst the params, find the value of the first, only, of any with
 
978
                 * the specified name
 
979
                 * @param params
 
980
                 * @param name
 
981
                 * @return a value, or null
 
982
                 */
889
983
                private String checkParam( String[] params, String name )
890
984
                {
 
985
                        String[] res = checkParams( params, name );
 
986
                        return res.length > 0? res[ 0 ] : null;
 
987
                }
 
988
 
 
989
                /**
 
990
                 * Amongst the params, find the values of any with the specified name
 
991
                 * @param params
 
992
                 * @param name
 
993
                 * @return an array of values, or null
 
994
                 */
 
995
                private String[] checkParams( String[] params, String name )
 
996
                {
 
997
                        HashSet< String > ret = new HashSet< String >();
 
998
 
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() )
896
 
                                        return m.group( 2 );
 
1004
                                        ret.add( m.group( 2 ) );
897
1005
                        }
898
 
                        return null;
 
1006
 
 
1007
                        return (String[]) ret.toArray( new String[ ret.size() ] );
899
1008
                }
900
1009
 
 
1010
                /**
 
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.
 
1014
                 * @param params
 
1015
                 * @param a list of type values to look for
 
1016
                 * @return a set of present type values
 
1017
                 */
901
1018
                private Set< String > extractTypes( String[] params,
902
1019
                        List< String > valid_types )
903
1020
                {
904
1021
                        HashSet< String > types = new HashSet< String >();
905
1022
 
906
1023
                        // get 3.0-style TYPE= param
907
 
                        String 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++ )
 
1026
                        {
 
1027
                                // check for a comma-separated list of types (why? this isn't in
 
1028
                                // the specs!)
 
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 ] );