/android/export-contacts

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

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/VcardExporter.java

  • Committer: edam
  • Date: 2012-12-21 15:53:59 UTC
  • Revision ID: tim@ed.am-20121221155359-p8h3n0xhtulmwva0
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Exporter.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Export Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
 
7
 * to as "this program"). For more information, see
8
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
36
36
public class VcardExporter extends Exporter
37
37
{
38
38
        protected FileOutputStream _ostream = null;
39
 
        protected boolean _first_contact = true;
40
39
 
41
40
        public VcardExporter( Doit doit )
42
41
        {
164
163
                return buffer.toString();
165
164
        }
166
165
 
167
 
        /**
168
 
         * Is the provided value a valid date-and-or-time, as per the spec?
169
 
         *
170
 
         * @param value the value
171
 
         * @return true if it is
172
 
         */
173
 
        protected boolean isValidDateAndOrTime( String value )
174
 
        {
175
 
                // ISO 8601:2004 4.1.2 date with 4.1.2.3 a) and b) reduced accuracy
176
 
                String date =
177
 
                        "[0-9]{4}(?:-?[0-9]{2}(?:-?[0-9]{2})?)?";
178
 
 
179
 
                // ISO 8601:2000 5.2.1.3 d), e) and f) truncated date representation
180
 
                String date_trunc =
181
 
                        "--(?:[0-9]{2}(?:-?[0-9]{2})?|-[0-9]{2})";
182
 
 
183
 
                // ISO 8601:2004 4.2.2 time with 4.2.2.3 reduced accuracy, 4.2.4 UTC and
184
 
                // 4.2.5 zone offset, no 4.2.2.4 decimal fraction and no 4.2.3 24:00
185
 
                // midnight
186
 
                String time =
187
 
                        "(?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9](?::?(?:60|[0-5][0-9]))?)?" +
188
 
                        "(?:Z|[-+](?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9])?)?";
189
 
 
190
 
                // ISO 8601:2000 5.3.1.4 a), b) and c) truncated time representation
191
 
                String time_trunc =
192
 
                        "-(?:[0-5][0-9](?::?(?:60|[0-5][0-9]))?|-(?:60|[0-5][0-9]))";
193
 
 
194
 
                // RFC6350 (vCard 3.0) date-and-or-time with mandatory time designator
195
 
                String date_and_or_time =
196
 
                        "(?:" + date + "|" + date_trunc + ")?" +
197
 
                        "(?:T(?:" + time + "|" + time_trunc + "))?";
198
 
 
199
 
                return value.matches( date_and_or_time );
200
 
        }
201
166
 
202
167
        @Override
203
168
        protected boolean exportContact( ContactData contact )
209
174
                if( contact.getPrimaryIdentifier() == null )
210
175
                        return false;
211
176
 
212
 
                // append newline
213
 
                if( _first_contact )
214
 
                        _first_contact = false;
215
 
                else
216
 
                        out.append( "\n" );
217
 
 
218
177
                // append header
219
178
                out.append( "BEGIN:VCARD\n" );
220
179
                out.append( "VERSION:3.0\n" );
323
282
                        for( int a = 0; a < notes.size(); a++ )
324
283
                                out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
325
284
 
326
 
                // append birthday
327
 
                String birthday = contact.getBirthday();
328
 
                if( birthday != null ) {
329
 
                        birthday.trim();
330
 
                        if( isValidDateAndOrTime( birthday ) )
331
 
                                out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
332
 
                        else
333
 
                                out.append(
334
 
                                        fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
335
 
                }
336
 
 
337
285
                // append footer
338
286
                out.append( "END:VCARD\n" );
339
287