/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-20 17:16:08 UTC
  • Revision ID: tim@ed.am-20121220171608-vx41zykf4krel9xf
slightly improved the efficiency of the cache identifier factory function

Show diffs side-by-side

added added

removed removed

208
208
 
209
209
                        if( vcard == null ) {
210
210
                                // look for vcard beginning
211
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
 
211
                                if( line.matches( "^BEGIN:VCARD" ) ) {
212
212
                                        setProgress( _progress++ );
213
213
                                        vcard = new Vcard();
214
214
                                        vcard_start_line = cli.getLineNumber();
216
216
                        }
217
217
                        else {
218
218
                                // look for vcard content or ending
219
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
 
219
                                if( line.matches( "^END:VCARD" ) )
220
220
                                {
221
221
                                        // finalise the vcard/contact
222
222
                                        try {
351
351
                public boolean doesNextLineLookFolded()
352
352
                {
353
353
                        return _pos > 0 && _pos < _content.length &&
354
 
                                _content[ _pos - 1 ] == '\n' &&
355
 
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
 
354
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
355
                }
357
356
 
358
357
                public int getLineNumber()
370
369
                private final static int MULTILINE_NONE = 0;
371
370
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
372
371
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
373
 
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
 
372
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
374
373
 
375
374
                private String _version = null;
376
375
                private Vector< ByteBuffer > _buffers = null;
474
473
 
475
474
                                // is it a version line?
476
475
                                if( name_and_params != null &&
477
 
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
 
476
                                        name_and_params.equals( "VERSION" ) )
478
477
                                {
479
478
                                        // yes, get it!
480
479
                                        String value = extractValueFromLine( buffer, line );
581
580
                                if( encoding != null )
582
581
                                        encoding = encoding.toUpperCase( Locale.US );
583
582
                                if( is_interesting_field && encoding != null &&
584
 
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
585
 
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
586
 
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
 
583
                                        !encoding.equals( "8BIT" ) &&
 
584
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
585
                                        //&& !encoding.equals( "BASE64" ) )
587
586
                                {
588
587
                                        throw new ParseException( R.string.error_vcf_encoding );
589
588
                                }
593
592
                                if( charset != null )
594
593
                                        charset = charset.toUpperCase( Locale.US );
595
594
                                if( charset != null &&
596
 
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
597
 
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
598
 
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
 
595
                                        !charset.equals( "US-ASCII" ) &&
 
596
                                        !charset.equals( "ASCII" ) &&
 
597
                                        !charset.equals( "UTF-8" ) )
599
598
                                {
600
599
                                        throw new ParseException( R.string.error_vcf_charset );
601
600
                                }
603
602
                                // do unencoding (or default to a fake unencoding result with
604
603
                                // the raw string)
605
604
                                UnencodeResult unencoding_result = null;
606
 
                                if( encoding != null &&
607
 
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
608
 
                                {
 
605
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
609
606
                                        unencoding_result = unencodeQuotedPrintable( value );
610
 
                                }
611
 
//                              else if( encoding != null &&
612
 
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
613
 
//                              {
 
607
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
614
608
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
615
 
//                              }
616
609
                                if( unencoding_result != null ) {
617
610
                                        value = unencoding_result.getBuffer();
618
611
                                        if( unencoding_result.isAnotherLineRequired() )
623
616
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
624
617
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
625
618
                                        ( charset != null && (
626
 
                                                charset.equalsIgnoreCase( "ASCII" ) ||
627
 
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
 
619
                                                charset.equals( "ASCII" ) ||
 
620
                                                charset.equals( "US-ASCII" ) ) ) )
628
621
                                {
629
622
                                        value = transcodeAsciiToUtf8( value );
630
623
                                }
641
634
                                // for some entries that have semicolon-separated value parts,
642
635
                                // check to see if the value ends in an escape character, which
643
636
                                // indicates that we have a multi-line value
644
 
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
645
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
646
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
 
637
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
 
638
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
 
639
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
647
640
                                        doesStringEndInAnEscapeChar( string_value ) )
648
641
                                {
649
642
                                        _parser_multiline_state = MULTILINE_ESCAPED;
651
644
                                                string_value.length() - 1 );
652
645
                                }
653
646
 
654
 
                                // if we know we're not in an encoding-based multi-line, check
655
 
                                // to see if we're in a folded multi-line
 
647
                                // now we know whether we're in an encoding multi-line,
 
648
                                // determine if we're in a v3 folded multi-line or not
656
649
                                if( _parser_multiline_state == MULTILINE_NONE &&
657
 
                                        next_line_looks_folded )
 
650
                                        _version.equals( "3.0" ) && next_line_looks_folded )
658
651
                                {
659
652
                                        _parser_multiline_state = MULTILINE_FOLDED;
660
653
                                }
672
665
                                if( complete_value.length() < 1 ) return;
673
666
 
674
667
                                // parse some properties
675
 
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
 
668
                                if( name_param_parts[ 0 ].equals( "N" ) )
676
669
                                        parseN( name_param_parts, complete_value );
677
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
 
670
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
678
671
                                        parseFN( name_param_parts, complete_value );
679
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
 
672
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
680
673
                                        parseORG( name_param_parts, complete_value );
681
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
 
674
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
682
675
                                        parseTITLE( name_param_parts, complete_value );
683
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
 
676
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
684
677
                                        parseTEL( name_param_parts, complete_value );
685
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
 
678
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
686
679
                                        parseEMAIL( name_param_parts, complete_value );
687
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
 
680
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
688
681
                                        parseADR( name_param_parts, complete_value );
689
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
 
682
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
690
683
                                        parseLABEL( name_param_parts, complete_value );
691
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
 
684
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
692
685
                                        parseNOTE( name_param_parts, complete_value );
693
686
                        }
694
687
                }
941
934
                        for( int a = 0; a < adr_parts.length; a++ )
942
935
                                if( adr_parts[ a ].length() > 0 )
943
936
                                {
944
 
                                        // version 3.0 vCards allow further splitting by comma
945
 
                                        if( _version.equals( "3.0" ) )
946
 
                                        {
947
 
                                                // split this part in to it's comma-separated bits and
948
 
                                                // add them on individual lines
949
 
                                                String[] adr_part_parts =
950
 
                                                        splitValueByCharacter( adr_parts[ a ], ',' );
951
 
                                                for( int b = 0; b < adr_part_parts.length; b++ )
952
 
                                                        if( adr_part_parts[ b ].length() > 0 )
953
 
                                                        {
954
 
                                                                if( value.length() > 0 ) value += "\n";
955
 
                                                                value += adr_part_parts[ b ];
956
 
                                                        }
957
 
                                        }
958
 
                                        else
959
 
                                        {
960
 
                                                // add this part on an individual line
961
 
                                                if( value.length() > 0 ) value += "\n";
962
 
                                                value += adr_parts[ a ];
963
 
                                        }
 
937
                                        // split this part in to it's comma-separated bits
 
938
                                        String[] adr_part_parts =
 
939
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
940
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
941
                                                if( adr_part_parts[ b ].length() > 0 )
 
942
                                                {
 
943
                                                        if( value.length() > 0 ) value += "\n";
 
944
                                                        value += adr_part_parts[ b ];
 
945
                                                }
964
946
                                }
965
947
 
966
948
                        Set< String > types = extractTypes( params, Arrays.asList(
1031
1013
                        HashSet< String > ret = new HashSet< String >();
1032
1014
 
1033
1015
                        Pattern p = Pattern.compile(
1034
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
1035
 
                                Pattern.CASE_INSENSITIVE );
 
1016
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1036
1017
                        for( int i = 0; i < params.length; i++ ) {
1037
1018
                                Matcher m = p.matcher( params[ i ] );
1038
1019
                                if( m.matches() )
1046
1027
                 * Amongst the params, return any type values present. For v2.1 vCards,
1047
1028
                 * those types are just parameters. For v3.0, they are prefixed with
1048
1029
                 * "TYPE=". There may also be multiple type parameters.
1049
 
                 * @param params an array of params to look for types in
1050
 
                 * @param valid_types an list of upper-case type values to look for
 
1030
                 * @param params
 
1031
                 * @param a list of type values to look for
1051
1032
                 * @return a set of present type values
1052
1033
                 */
1053
1034
                private Set< String > extractTypes( String[] params,
1063
1044
                                // the specs!)
1064
1045
                                String[] parts = type_params[ a ].split( "," );
1065
1046
                                for( int i = 0; i < parts.length; i++ )
1066
 
                                        parts[ i ] = parts[ i ].toUpperCase( Locale.US );
1067
 
                                for( int i = 0; i < parts.length; i++ )
1068
1047
                                        if( valid_types.contains( parts[ i ] ) )
1069
1048
                                                types.add( parts[ i ] );
1070
1049
                        }