/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/org/waxworlds/edam/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2011-06-04 13:16:20 UTC
  • Revision ID: edam@waxworlds.org-20110604131620-hean9j66r3f5i72h
- fix UTF-8 unencoding by default on v3.0 vCards
- proper unescaping of strings in v3.0 vCards (also works for v2.1, although the 2.1 specs don't mention it)

Show diffs side-by-side

added added

removed removed

956
956
                        finalise();
957
957
                }
958
958
 
959
 
                /**
960
 
                 * Amongst the params, find the value of the first, only, of any with
961
 
                 * the specified name
962
 
                 * @param params
963
 
                 * @param name
964
 
                 * @return a value, or null
965
 
                 */
966
959
                private String checkParam( String[] params, String name )
967
960
                {
968
 
                        String[] res = checkParams( params, name );
969
 
                        return res.length > 0? res[ 0 ] : null;
970
 
                }
971
 
 
972
 
                /**
973
 
                 * Amongst the params, find the values of any with the specified name
974
 
                 * @param params
975
 
                 * @param name
976
 
                 * @return an array of values, or null
977
 
                 */
978
 
                private String[] checkParams( String[] params, String name )
979
 
                {
980
 
                        HashSet< String > ret = new HashSet< String >();
981
 
 
982
961
                        Pattern p = Pattern.compile(
983
962
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
984
963
                        for( int i = 0; i < params.length; i++ ) {
985
964
                                Matcher m = p.matcher( params[ i ] );
986
965
                                if( m.matches() )
987
 
                                        ret.add( m.group( 2 ) );
 
966
                                        return m.group( 2 );
988
967
                        }
989
 
 
990
 
                        return (String[]) ret.toArray( new String[ ret.size() ] );
 
968
                        return null;
991
969
                }
992
970
 
993
 
                /**
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.
997
 
                 * @param params
998
 
                 * @param a list of type values to look for
999
 
                 * @return a set of present type values
1000
 
                 */
1001
971
                private Set< String > extractTypes( String[] params,
1002
972
                        List< String > valid_types )
1003
973
                {
1004
974
                        HashSet< String > types = new HashSet< String >();
1005
975
 
1006
976
                        // get 3.0-style TYPE= param
1007
 
                        String type_params[] = checkParams( params, "TYPE" );
1008
 
                        for( int a = 0; a < type_params.length; a++ )
1009
 
                        {
1010
 
                                // check for a comma-separated list of types (why? this isn't in
1011
 
                                // the specs!)
1012
 
                                String[] parts = type_params[ a ].split( "," );
 
977
                        String type_param;
 
978
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
979
                                String[] parts = type_param.split( "," );
1013
980
                                for( int i = 0; i < parts.length; i++ )
1014
981
                                        if( valid_types.contains( parts[ i ] ) )
1015
982
                                                types.add( parts[ i ] );