/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/VCFImporter.java

  • Committer: edam
  • Date: 2010-12-11 23:57:07 UTC
  • Revision ID: edam@waxworlds.org-20101211235707-czyw48tt3hcopuwf
- read vCard files in as raw bytes now and convert to string only tentatively to check for version no.s and property names and params
- ASCII is now (correctly) the default charset
- added conversion from 8-bit ASCII to UTF-8 (not used on 7-bit US-ASCII) which works on raw bytes, not chars
- unencode quoted-printable now works on raw bytes, not chars

Show diffs side-by-side

added added

removed removed

1
 
package org.waxworlds.importcontacts;
 
1
/*
 
2
 * VCFImporter.java
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of the Import Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
 
 
24
package org.waxworlds.edam.importcontacts;
2
25
 
3
26
import java.io.BufferedReader;
4
27
import java.io.File;
 
28
import java.io.FileInputStream;
5
29
import java.io.FileNotFoundException;
6
30
import java.io.FileReader;
7
31
import java.io.FilenameFilter;
8
32
import java.io.IOException;
9
33
import java.io.UnsupportedEncodingException;
 
34
import java.nio.ByteBuffer;
10
35
import java.util.Arrays;
11
36
import java.util.HashSet;
12
37
import java.util.List;
15
40
import java.util.regex.Matcher;
16
41
import java.util.regex.Pattern;
17
42
 
18
 
import org.waxworlds.importcontacts.Importer.AbortImportException;
19
 
 
20
43
import android.content.SharedPreferences;
21
44
import android.provider.Contacts;
22
45
import android.provider.Contacts.PhonesColumns;
44
67
                try
45
68
                {
46
69
                        // open directory
47
 
                        String location = prefs.getString( "location", "" );
48
 
                        File dir = new File( location );
49
 
                        if( !dir.exists() || !dir.isDirectory() )
 
70
                        String path = "/sdcard" + prefs.getString( "location", "/" );
 
71
                        File file = new File( path );
 
72
                        if( !file.exists() )
50
73
                                showError( R.string.error_locationnotfound );
51
74
 
52
 
                        // get files
53
 
                        class VCardFilter implements FilenameFilter {
54
 
                            public boolean accept( File dir, String name ) {
55
 
                                return name.toLowerCase().endsWith( ".vcf" );
56
 
                            }
57
 
                        }
58
 
                        files = dir.listFiles( new VCardFilter() );
 
75
                        // directory, or file?
 
76
                        if( file.isDirectory() )
 
77
                        {
 
78
                                // get files
 
79
                                class VCardFilter implements FilenameFilter {
 
80
                                        public boolean accept( File dir, String name ) {
 
81
                                                return name.toLowerCase().endsWith( ".vcf" );
 
82
                                        }
 
83
                                }
 
84
                                files = file.listFiles( new VCardFilter() );
 
85
                        }
 
86
                        else
 
87
                        {
 
88
                                // use just this file
 
89
                                files = new File[ 1 ];
 
90
                                files[ 0 ] = file;
 
91
                        }
59
92
                }
60
93
                catch( SecurityException e ) {
61
94
                        showError( R.string.error_locationpermissions );
107
140
 
108
141
                }
109
142
                catch( FileNotFoundException e ) {
110
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
143
                        showError( getText( R.string.error_filenotfound ) +
 
144
                                file.getName() );
111
145
                }
112
146
                catch( IOException e ) {
113
147
                        showError( getText( R.string.error_ioerror ) + file.getName() );
116
150
 
117
151
        private void importVCardFile( File file ) throws AbortImportException
118
152
        {
 
153
                // check file is good
 
154
                if( !file.exists() )
 
155
                        showError( getText( R.string.error_filenotfound ) +
 
156
                                file.getName() );
 
157
                if( file.length() == 0 )
 
158
                        showError( getText( R.string.error_fileisempty ) +
 
159
                                file.getName() );
 
160
 
119
161
                try
120
162
                {
121
 
                        // open file
122
 
                        BufferedReader reader = new BufferedReader(
123
 
                                        new FileReader( file ) );
124
 
 
125
 
                        // read
126
 
                        StringBuffer content = new StringBuffer();
127
 
                        String line;
128
 
                        while( ( line = reader.readLine() ) != null )
129
 
                                content.append( line ).append( "\n" );
130
 
 
131
 
                        importVCardFileContent( content.toString(), file.getName() );
 
163
                        // open/read file
 
164
                        FileInputStream istream = new FileInputStream( file );
 
165
                        byte[] content = new byte[ (int)file.length() ];
 
166
                        istream.read( content );
 
167
 
 
168
                        // import
 
169
                        importVCardFileContent( content, file.getName() );
132
170
                }
133
171
                catch( FileNotFoundException e ) {
134
 
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
 
172
                        showError( getText( R.string.error_filenotfound ) +
 
173
                                file.getName() );
135
174
                }
136
175
                catch( IOException e ) {
137
176
                        showError( getText( R.string.error_ioerror ) + file.getName() );
138
177
                }
139
178
        }
140
179
 
141
 
        private void importVCardFileContent( String content, String fileName )
 
180
        private void importVCardFileContent( byte[] content, String fileName )
142
181
                        throws AbortImportException
143
182
        {
144
 
                // unfold RFC2425 section 5.8.1 folded lines, except that we must also
145
 
                // handle embedded Quoted-Printable encodings that have a trailing '='.
146
 
                // So we remove these first before doing RFC2425 unfolding.
147
 
                content = content.replaceAll( "=\n[ \\t]", "" )
148
 
                                .replaceAll( "\n[ \\t]", "" );
 
183
                ByteBuffer buffers[] = getLinesFromContent( content );
149
184
 
150
 
                // get lines and parse them
151
 
                String[] lines = content.split( "\n" );
 
185
                // go through lines
152
186
                VCard vCard = null;
153
 
                for( int i = 0; i < lines.length; i++ )
 
187
                for( int i = 0; i < buffers.length; i++ )
154
188
                {
155
 
                        String line = lines[ i ];
 
189
                        // get a US-ASCII version of the line for processing
 
190
                        String line;
 
191
                        try {
 
192
                                line = new String( buffers[ i ].array(), buffers[ i ].position(),
 
193
                                        buffers[ i ].limit() - buffers[ i ].position(), "US-ASCII" );
 
194
                        }
 
195
                        catch( UnsupportedEncodingException e ) {
 
196
                                // we know US-ASCII is supported, so appease the compiler...
 
197
                                line = "";
 
198
                        }
156
199
 
157
200
                        if( vCard == null ) {
158
201
                                // look for vcard beginning
187
230
                                {
188
231
                                        // try giving the line to the vcard
189
232
                                        try {
190
 
                                                vCard.parseLine( line );
 
233
                                                vCard.parseLine( buffers[ i ] );
191
234
                                        }
192
235
                                        catch( VCard.ParseException e ) {
193
236
                                                skipContact();
212
255
                }
213
256
        }
214
257
 
 
258
        private ByteBuffer[] getLinesFromContent( byte[] content )
 
259
        {
 
260
                // count lines in data
 
261
                int num_lines = 1;
 
262
                for( int a = 0; a < content.length; a++ )
 
263
                        if( content[ a ] == '\n' )
 
264
                                num_lines++;
 
265
 
 
266
                // get lines, removing \r's and \n's as we go
 
267
                ByteBuffer lines[] = new ByteBuffer[ num_lines ];
 
268
                int last = 0;
 
269
                for( int a = 0, b = 0; a < content.length; a++ )
 
270
                        if( content[ a ] == '\n' ) {
 
271
                                int to = ( a > 0 && content[ a - 1 ] == '\r' &&
 
272
                                        a - 1 >= last )? a - 1 : a;
 
273
                                lines[ b++ ] = ByteBuffer.wrap( content, last, to - last );
 
274
                                last = a + 1;
 
275
                        }
 
276
                lines[ lines.length - 1 ] = ByteBuffer.wrap( content, last,
 
277
                        content.length - last );
 
278
 
 
279
                return lines;
 
280
        }
 
281
 
215
282
        private class VCard extends ContactData
216
283
        {
217
284
                private final static int NAMELEVEL_NONE = 0;
220
287
                private final static int NAMELEVEL_N = 3;
221
288
 
222
289
                private String _version = null;
223
 
                private Vector< String > _lines = null;
224
 
                private int _nameLevel = NAMELEVEL_NONE;
225
 
 
 
290
                private Vector< ByteBuffer > _buffers = null;
 
291
                private int _name_level = NAMELEVEL_NONE;
 
292
                private boolean _parser_in_multiline = false;
 
293
                private String _parser_current_name_and_params = null;
 
294
                private String _parser_buffered_value_so_far = "";
 
295
 
 
296
                protected class UnencodeResult
 
297
                {
 
298
                        private boolean _another_line_required;
 
299
                        private ByteBuffer _buffer;
 
300
 
 
301
                        public UnencodeResult( boolean another_line_required,
 
302
                                ByteBuffer buffer )
 
303
                        {
 
304
                                _another_line_required = another_line_required;
 
305
                                _buffer = buffer;
 
306
                        }
 
307
 
 
308
                        public boolean isAnotherLineRequired()
 
309
                        {
 
310
                                return _another_line_required;
 
311
                        }
 
312
 
 
313
                        public ByteBuffer getBuffer()
 
314
                        {
 
315
                                return _buffer;
 
316
                        }
 
317
                }
 
318
 
 
319
                @SuppressWarnings("serial")
226
320
                protected class ParseException extends Exception
227
321
                {
 
322
                        @SuppressWarnings("unused")
228
323
                        public ParseException( String error )
229
324
                        {
230
325
                                super( error );
236
331
                        }
237
332
                }
238
333
 
 
334
                @SuppressWarnings("serial")
239
335
                protected class SkipContactException extends Exception { }
240
336
 
241
 
                public void parseLine( String line )
 
337
                public void parseLine( ByteBuffer buffer )
242
338
                                throws ParseException, SkipContactException,
243
339
                                AbortImportException
244
340
                {
245
 
                        // get property halves
246
 
                        String[] props = line.split( ":" );
247
 
                        for( int i = 0; i < props.length; i++ )
248
 
                                props[ i ] = props[ i ].trim();
249
 
                        if( props.length < 2 ||
250
 
                                        props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
251
 
                                throw new ParseException( R.string.error_vcf_malformed );
252
 
 
 
341
                        // get a US-ASCII version of the line for processing
 
342
                        String line;
 
343
                        try {
 
344
                                line = new String( buffer.array(), buffer.position(),
 
345
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
346
                        }
 
347
                        catch( UnsupportedEncodingException e ) {
 
348
                                // we know US-ASCII is supported, so appease the compiler...
 
349
                                line = "";
 
350
                        }
 
351
 
 
352
                        // ignore empty lines
 
353
                        if( line.trim() == "" ) return;
 
354
 
 
355
                        // split line into name and value parts (this may turn out to be
 
356
                        // unwanted if the line is a subsequent line in a multi-line
 
357
                        // value, but we have to do this now to check for and handle VCF
 
358
                        // versions first). Also, the value part is only created tentatively
 
359
                        // because it may have an encoding/charset. Since we're treating it
 
360
                        // as UTF-8 (which is compatible with 7-bit US-ASCII) this is ok
 
361
                        // though so long as we later use the raw bytes. ALso we check for
 
362
                        // malformed property:name pairs.
 
363
                        String name_and_params, string_value;
 
364
                        {
 
365
                                String[] bits = line.split(  ":", 2 );
 
366
                                if( bits.length == 2 ) {
 
367
                                        name_and_params = bits[ 0 ].trim();
 
368
                                        string_value = bits[ 1 ].trim();
 
369
                                        if( name_and_params.length() == 0 )
 
370
                                                throw new ParseException( R.string.error_vcf_malformed );
 
371
                                }
 
372
                                else
 
373
                                {
 
374
                                        if( !_parser_in_multiline )
 
375
                                                throw new ParseException( R.string.error_vcf_malformed );
 
376
                                        name_and_params = null;
 
377
                                        string_value = null;
 
378
                                }
 
379
                        }
 
380
 
 
381
                        // if we haven't yet got a version, we won't be paring anything!
253
382
                        if( _version == null )
254
383
                        {
255
 
                                if( props[ 0 ].equals( "VERSION" ) )
 
384
                                // is this a version?
 
385
                                if( name_and_params.equals( "VERSION" ) )
256
386
                                {
257
 
                                        // get version
258
 
                                        if( !props[ 1 ].equals( "2.1" ) &&
259
 
                                                        !props[ 1 ].equals( "3.0" ) )
 
387
                                        // yes, check/store it
 
388
                                        if( !string_value.equals( "2.1" ) &&
 
389
                                                        !string_value.equals( "3.0" ) )
260
390
                                                throw new ParseException( R.string.error_vcf_version );
261
 
                                        _version = props[ 1 ];
 
391
                                        _version = string_value;
262
392
 
263
 
                                        // parse any other lines we've accumulated so far
264
 
                                        if( _lines != null )
265
 
                                                for( int i = 0; i < _lines.size(); i++ )
266
 
                                                        parseLine( _lines.get( i ) );
267
 
                                        _lines = null;
 
393
                                        // parse any other buffers we've accumulated so far
 
394
                                        if( _buffers != null )
 
395
                                                for( int i = 0; i < _buffers.size(); i++ )
 
396
                                                        parseLine( _buffers.get( i ) );
 
397
                                        _buffers = null;
268
398
                                }
269
399
                                else
270
400
                                {
271
 
                                        // stash this line till we have a version
272
 
                                        if( _lines == null )
273
 
                                                _lines = new Vector< String >();
274
 
                                        _lines.add( line );
 
401
                                        // no, so stash this buffer till we have a version
 
402
                                        if( _buffers == null )
 
403
                                                _buffers = new Vector< ByteBuffer >();
 
404
                                        _buffers.add( buffer );
275
405
                                }
276
406
                        }
277
407
                        else
278
408
                        {
 
409
                                // value bytes, for processing
 
410
                                ByteBuffer value;
 
411
 
 
412
                                if( _parser_in_multiline )
 
413
                                {
 
414
                                        // if we're currently in a multi-line value, use the stored
 
415
                                        // property name and parameters
 
416
                                        name_and_params = _parser_current_name_and_params;
 
417
 
 
418
                                        // find start of string (skip spaces/tabs)
 
419
                                        int pos = buffer.position();
 
420
                                        byte[] buffer_array = buffer.array();
 
421
                                        while( pos < buffer.limit() && (
 
422
                                                buffer_array[ pos ] == ' ' ||
 
423
                                                buffer_array[ pos ] == '\t' ) )
 
424
                                        {
 
425
                                                pos++;
 
426
                                        }
 
427
 
 
428
                                        // get value from buffer
 
429
                                        value = ByteBuffer.wrap( buffer.array(), pos,
 
430
                                                buffer.limit() - pos );
 
431
                                }
 
432
                                else
 
433
                                {
 
434
                                        // ignore empty values
 
435
                                        if( string_value.length() < 1 ) return;
 
436
 
 
437
                                        // calculate how many chars to skip from beginning of line
 
438
                                        // so we skip the property "name:" part
 
439
                                        int pos = buffer.position() + name_and_params.length() + 1;
 
440
 
 
441
                                        // get value from buffer
 
442
                                        value = ByteBuffer.wrap( buffer.array(), pos,
 
443
                                                buffer.limit() - pos );
 
444
 
 
445
                                        // reset the saved multi-line state
 
446
                                        _parser_current_name_and_params = name_and_params;
 
447
                                        _parser_buffered_value_so_far = "";
 
448
                                }
 
449
 
279
450
                                // get parameter parts
280
 
                                String[] params = props[ 0 ].split( ";" );
281
 
                                for( int i = 0; i < params.length; i++ )
282
 
                                        params[ i ] = params[ i ].trim();
 
451
                                String[] name_and_param_bits = name_and_params.split( ";" );
 
452
                                for( int i = 0; i < name_and_param_bits.length; i++ )
 
453
                                        name_and_param_bits[ i ] = name_and_param_bits[ i ].trim();
 
454
 
 
455
                                // parse encoding parameter
 
456
                                String encoding = checkParam( name_and_param_bits, "ENCODING" );
 
457
                                if( encoding != null ) encoding = encoding.toUpperCase();
 
458
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
459
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
460
                                        //&& !encoding.equals( "BASE64" ) )
 
461
                                {
 
462
                                        throw new ParseException( R.string.error_vcf_encoding );
 
463
                                }
 
464
 
 
465
                                // parse charset parameter
 
466
                                String charset = checkParam( name_and_param_bits, "CHARSET" );
 
467
                                if( charset != null ) charset = charset.toUpperCase();
 
468
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
469
                                        !charset.equals( "ASCII" ) && !charset.equals( "UTF-8" ) )
 
470
                                {
 
471
                                        throw new ParseException( R.string.error_vcf_charset );
 
472
                                }
 
473
 
 
474
                                // do unencoding (or default to a fake unencoding result with
 
475
                                // the raw string)
 
476
                                UnencodeResult unencoding_result = null;
 
477
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
478
                                        unencoding_result = unencodeQuotedPrintable( value );
 
479
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
 
480
//                                      result = unencodeBase64( props[ 1 ], charset );
 
481
                                if( unencoding_result != null ) {
 
482
                                        value = unencoding_result.getBuffer();
 
483
                                        _parser_in_multiline =
 
484
                                                unencoding_result.isAnotherLineRequired();
 
485
                                }
 
486
 
 
487
                                // convert 8-bit ASCII charset to US-ASCII
 
488
                                if( charset == null || charset == "ASCII" ) {
 
489
                                        value = transcodeAsciiToUtf8( value );
 
490
                                        charset = "UTF-8";
 
491
                                }
 
492
 
 
493
                                // process charset
 
494
                                try {
 
495
                                        string_value =
 
496
                                                new String( value.array(), value.position(),
 
497
                                                        value.limit() - value.position(), charset );
 
498
                                } catch( UnsupportedEncodingException e ) {
 
499
                                        throw new ParseException( R.string.error_vcf_charset );
 
500
                                }
 
501
 
 
502
                                // handle multi-line requests
 
503
                                if( _parser_in_multiline ) {
 
504
                                        _parser_buffered_value_so_far += string_value;
 
505
                                        return;
 
506
                                }
 
507
 
 
508
                                // add on buffered multi-line content
 
509
                                String complete_value =
 
510
                                        _parser_buffered_value_so_far + string_value;
283
511
 
284
512
                                // parse some properties
285
 
                                if( params[ 0 ].equals( "N" ) )
286
 
                                        parseN( params, props[ 1 ] );
287
 
                                else if( params[ 0 ].equals( "FN" ) )
288
 
                                        parseFN( params, props[ 1 ] );
289
 
                                else if( params[ 0 ].equals( "ORG" ) )
290
 
                                        parseORG( params, props[ 1 ] );
291
 
                                else if( params[ 0 ].equals( "TEL" ) )
292
 
                                        parseTEL( params, props[ 1 ] );
293
 
                                else if( params[ 0 ].equals( "EMAIL" ) )
294
 
                                        parseEMAIL( params, props[ 1 ] );
 
513
                                if( name_and_param_bits[ 0 ].equals( "N" ) )
 
514
                                        parseN( name_and_param_bits, complete_value );
 
515
                                else if( name_and_param_bits[ 0 ].equals( "FN" ) )
 
516
                                        parseFN( name_and_param_bits, complete_value );
 
517
                                else if( name_and_param_bits[ 0 ].equals( "ORG" ) )
 
518
                                        parseORG( name_and_param_bits, complete_value );
 
519
                                else if( name_and_param_bits[ 0 ].equals( "TEL" ) )
 
520
                                        parseTEL( name_and_param_bits, complete_value );
 
521
                                else if( name_and_param_bits[ 0 ].equals( "EMAIL" ) )
 
522
                                        parseEMAIL( name_and_param_bits, complete_value );
295
523
                        }
296
524
                }
297
525
 
300
528
                                AbortImportException
301
529
                {
302
530
                        // already got a better name?
303
 
                        if( _nameLevel >= NAMELEVEL_N ) return;
 
531
                        if( _name_level >= NAMELEVEL_N ) return;
304
532
 
305
533
                        // get name parts
306
534
                        String[] nameparts = value.split( ";" );
315
543
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
316
544
 
317
545
                        // set name
318
 
                        setName( undoCharsetAndEncoding( params, value ) );
319
 
                        _nameLevel = NAMELEVEL_N;
 
546
                        setName( value );
 
547
                        _name_level = NAMELEVEL_N;
320
548
 
321
549
                        // check now to see if we need to import this contact (to avoid
322
550
                        // parsing the rest of the vCard unnecessarily)
328
556
                                throws ParseException, SkipContactException
329
557
                {
330
558
                        // already got a better name?
331
 
                        if( _nameLevel >= NAMELEVEL_FN ) return;
 
559
                        if( _name_level >= NAMELEVEL_FN ) return;
332
560
 
333
561
                        // set name
334
 
                        setName( undoCharsetAndEncoding( params, value ) );
335
 
                        _nameLevel = NAMELEVEL_FN;
 
562
                        setName( value );
 
563
                        _name_level = NAMELEVEL_FN;
336
564
                }
337
565
 
338
566
                private void parseORG( String[] params, String value )
339
567
                                throws ParseException, SkipContactException
340
568
                {
341
569
                        // already got a better name?
342
 
                        if( _nameLevel >= NAMELEVEL_ORG ) return;
 
570
                        if( _name_level >= NAMELEVEL_ORG ) return;
343
571
 
344
572
                        // get org parts
345
573
                        String[] orgparts = value.split( ";" );
353
581
                                value = orgparts[ 0 ];
354
582
 
355
583
                        // set name
356
 
                        setName( undoCharsetAndEncoding( params, value ) );
357
 
                        _nameLevel = NAMELEVEL_ORG;
 
584
                        setName( value );
 
585
                        _name_level = NAMELEVEL_ORG;
358
586
                }
359
587
 
360
588
                private void parseTEL( String[] params, String value )
385
613
                }
386
614
 
387
615
                public void parseEMAIL( String[] params, String value )
 
616
                                throws ParseException
388
617
                {
389
618
                        if( value.length() == 0 ) return;
390
619
 
404
633
                                AbortImportException
405
634
                {
406
635
                        // missing version (and data is present)
407
 
                        if( _version == null && _lines != null )
 
636
                        if( _version == null && _buffers != null )
408
637
                                throw new ParseException( R.string.error_vcf_malformed );
409
638
 
410
639
                        //  missing name properties?
411
 
                        if( _nameLevel == NAMELEVEL_NONE )
 
640
                        if( _name_level == NAMELEVEL_NONE )
412
641
                                throw new ParseException( R.string.error_vcf_noname );
413
642
 
414
643
                        // check if we should import this one? If we've already got an 'N'-
415
644
                        // type name, this will already have been done by parseN() so we
416
645
                        // mustn't do this here (or it could prompt twice!)
417
 
                        if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
 
646
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
418
647
                                throw new SkipContactException();
419
648
                }
420
649
 
421
 
                private String undoCharsetAndEncoding( String[] params, String value )
422
 
                                throws ParseException
423
 
                {
424
 
                        // check encoding/charset
425
 
                        String charset, encoding;
426
 
                        if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
427
 
                                        !charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
428
 
                                throw new ParseException( R.string.error_vcf_charset );
429
 
                        if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
430
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
431
 
                                throw new ParseException( R.string.error_vcf_encoding );
432
 
 
433
 
                        // do decoding?
434
 
                        if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
435
 
                                return unencodeQuotedPrintable( value, charset );
436
 
 
437
 
                        // nothing to do!
438
 
                        return value;
439
 
                }
440
 
 
441
650
                private String checkParam( String[] params, String name )
442
651
                {
443
652
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
473
682
                        return types;
474
683
                }
475
684
 
476
 
                private String unencodeQuotedPrintable( String str, String charset )
 
685
                private UnencodeResult unencodeQuotedPrintable( ByteBuffer in )
477
686
                {
478
 
                        // default encoding scheme
479
 
                        if( charset == null ) charset = "UTF-8";
 
687
                        boolean another = false;
480
688
 
481
689
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
482
 
                        byte[] bytes = new byte[ str.length() ];
 
690
                        byte[] out = new byte[ in.limit() - in.position() ];
483
691
                        int j = 0;
484
 
                        for( int i = 0; i < str.length(); i++, j++ ) {
485
 
                                char ch = str.charAt( i );
486
 
                                if( ch == '=' && i < str.length() - 2 ) {
487
 
                                        bytes[ j ] = (byte)(
488
 
                                                        Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
489
 
                                                        Character.digit( str.charAt( i + 2 ), 16 ) );
 
692
                        for( int i = in.position(); i < in.limit(); i++ )
 
693
                        {
 
694
                                // get next char and process...
 
695
                                byte ch = in.array()[ i ];
 
696
                                if( ch == '=' && i < in.limit() - 2 )
 
697
                                {
 
698
                                        // we found a =XX format byte, add it
 
699
                                        out[ j ] = (byte)(
 
700
                                                Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
 
701
                                                Character.digit( in.array()[ i + 2 ], 16 ) );
490
702
                                        i += 2;
491
703
                                }
 
704
                                else if( ch == '=' && i == in.limit() - 1 )
 
705
                                {
 
706
                                        // we found a '=' at the end of a line signifying a multi-
 
707
                                        // line string, so we don't add it.
 
708
                                        another = true;
 
709
                                        continue;
 
710
                                }
492
711
                                else
493
 
                                        bytes[ j ] = (byte)ch;
494
 
                        }
495
 
                        try {
496
 
                                return new String( bytes, 0, j, charset );
497
 
                        } catch( UnsupportedEncodingException e ) { }
498
 
                        return null;
 
712
                                        // just a normal char...
 
713
                                        out[ j ] = (byte)ch;
 
714
                                j++;
 
715
                        }
 
716
 
 
717
                        return new UnencodeResult( another, ByteBuffer.wrap( out, 0, j ) );
 
718
                }
 
719
 
 
720
                private ByteBuffer transcodeAsciiToUtf8( ByteBuffer in )
 
721
                {
 
722
                        // transcode
 
723
                        byte[] out = new byte[ ( in.limit() - in.position() ) * 2 ];
 
724
                        int j = 0;
 
725
                        for( int a = in.position(); a < in.limit(); a++ )
 
726
                        {
 
727
                                // if char is < 127, keep it as-is
 
728
                                if( in.array()[ a ] >= 0 )
 
729
                                        out[ j++ ] = in.array()[ a ];
 
730
 
 
731
                                // else, convert it to UTF-8
 
732
                                else {
 
733
                                        int b = 0xff & (int)in.array()[ a ];
 
734
                                        out[ j++ ] = (byte)( 0xc0 | ( b >> 6 ) );
 
735
                                        out[ j++ ] = (byte)( 0x80 | ( b & 0x3f ) );
 
736
                                }
 
737
                        }
 
738
 
 
739
                        return ByteBuffer.wrap( out, 0, j );
499
740
                }
500
741
        }
501
742
}