/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 18:48:34 UTC
  • Revision ID: tim@ed.am-20121220184834-w9quyrfbtbkp9vhi
compatibility and flexibility fixes (accept spaces in BEGIN/END lines; accept tabs as folding character; ignore case of item names, encodings and charsets; only parse comma-separated subparts of address parts for v3.0 vcards; accept new-line + white-space folding in v2.1 vCards)

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:VCARD" ) ) {
 
211
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*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:VCARD" ) )
 
219
                                if( line.matches( "^END[ \t]*:[ \t]*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' && _content[ _pos ] == ' ';
 
354
                                _content[ _pos - 1 ] == '\n' &&
 
355
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
355
356
                }
356
357
 
357
358
                public int getLineNumber()
369
370
                private final static int MULTILINE_NONE = 0;
370
371
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
371
372
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
372
 
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
 
373
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
373
374
 
374
375
                private String _version = null;
375
376
                private Vector< ByteBuffer > _buffers = null;
473
474
 
474
475
                                // is it a version line?
475
476
                                if( name_and_params != null &&
476
 
                                        name_and_params.equals( "VERSION" ) )
 
477
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
477
478
                                {
478
479
                                        // yes, get it!
479
480
                                        String value = extractValueFromLine( buffer, line );
580
581
                                if( encoding != null )
581
582
                                        encoding = encoding.toUpperCase( Locale.US );
582
583
                                if( is_interesting_field && encoding != null &&
583
 
                                        !encoding.equals( "8BIT" ) &&
584
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
585
 
                                        //&& !encoding.equals( "BASE64" ) )
 
584
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
 
585
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
586
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
586
587
                                {
587
588
                                        throw new ParseException( R.string.error_vcf_encoding );
588
589
                                }
592
593
                                if( charset != null )
593
594
                                        charset = charset.toUpperCase( Locale.US );
594
595
                                if( charset != null &&
595
 
                                        !charset.equals( "US-ASCII" ) &&
596
 
                                        !charset.equals( "ASCII" ) &&
597
 
                                        !charset.equals( "UTF-8" ) )
 
596
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
 
597
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
 
598
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
598
599
                                {
599
600
                                        throw new ParseException( R.string.error_vcf_charset );
600
601
                                }
602
603
                                // do unencoding (or default to a fake unencoding result with
603
604
                                // the raw string)
604
605
                                UnencodeResult unencoding_result = null;
605
 
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
606
                                if( encoding != null &&
 
607
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
608
                                {
606
609
                                        unencoding_result = unencodeQuotedPrintable( value );
607
 
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
 
610
                                }
 
611
//                              else if( encoding != null &&
 
612
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
 
613
//                              {
608
614
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
 
615
//                              }
609
616
                                if( unencoding_result != null ) {
610
617
                                        value = unencoding_result.getBuffer();
611
618
                                        if( unencoding_result.isAnotherLineRequired() )
616
623
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
617
624
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
618
625
                                        ( charset != null && (
619
 
                                                charset.equals( "ASCII" ) ||
620
 
                                                charset.equals( "US-ASCII" ) ) ) )
 
626
                                                charset.equalsIgnoreCase( "ASCII" ) ||
 
627
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
621
628
                                {
622
629
                                        value = transcodeAsciiToUtf8( value );
623
630
                                }
634
641
                                // for some entries that have semicolon-separated value parts,
635
642
                                // check to see if the value ends in an escape character, which
636
643
                                // indicates that we have a multi-line value
637
 
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
638
 
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
639
 
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
 
644
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
 
645
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
 
646
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
640
647
                                        doesStringEndInAnEscapeChar( string_value ) )
641
648
                                {
642
649
                                        _parser_multiline_state = MULTILINE_ESCAPED;
644
651
                                                string_value.length() - 1 );
645
652
                                }
646
653
 
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
 
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
649
656
                                if( _parser_multiline_state == MULTILINE_NONE &&
650
 
                                        _version.equals( "3.0" ) && next_line_looks_folded )
 
657
                                        next_line_looks_folded )
651
658
                                {
652
659
                                        _parser_multiline_state = MULTILINE_FOLDED;
653
660
                                }
665
672
                                if( complete_value.length() < 1 ) return;
666
673
 
667
674
                                // parse some properties
668
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
 
675
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
669
676
                                        parseN( name_param_parts, complete_value );
670
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
 
677
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
671
678
                                        parseFN( name_param_parts, complete_value );
672
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
 
679
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
673
680
                                        parseORG( name_param_parts, complete_value );
674
 
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
681
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
675
682
                                        parseTITLE( name_param_parts, complete_value );
676
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
 
683
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
677
684
                                        parseTEL( name_param_parts, complete_value );
678
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
 
685
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
679
686
                                        parseEMAIL( name_param_parts, complete_value );
680
 
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
 
687
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
681
688
                                        parseADR( name_param_parts, complete_value );
682
 
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
689
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
683
690
                                        parseLABEL( name_param_parts, complete_value );
684
 
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
 
691
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
685
692
                                        parseNOTE( name_param_parts, complete_value );
686
693
                        }
687
694
                }
934
941
                        for( int a = 0; a < adr_parts.length; a++ )
935
942
                                if( adr_parts[ a ].length() > 0 )
936
943
                                {
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
 
                                                }
 
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
                                        }
946
964
                                }
947
965
 
948
966
                        Set< String > types = extractTypes( params, Arrays.asList(