~ubuntu-branches/ubuntu/trusty/freeguide/trusty

« back to all changes in this revision

Viewing changes to src/freeguide/plugins/importexport/mobile/ExpMobile.java

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2007-09-11 16:52:59 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070911165259-4r32oke21i1ezbmv
Tags: 0.10.5-1
* New upstream release.
* Update the watch file.
* Change Debian policy to version 3.7.2.2. No changes necessary.
* Add ant-optional to build dependencies. Closes: #441762.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import freeguide.common.gui.FileChooserExtension;
4
4
 
 
5
import freeguide.common.lib.fgspecific.Application;
5
6
import freeguide.common.lib.fgspecific.data.TVChannel;
6
7
import freeguide.common.lib.fgspecific.data.TVData;
7
8
import freeguide.common.lib.fgspecific.data.TVIteratorChannels;
23
24
 
24
25
import java.util.ArrayList;
25
26
import java.util.Calendar;
26
 
import java.util.Date;
27
 
import java.util.GregorianCalendar;
28
 
import java.util.Iterator;
29
27
import java.util.List;
30
28
import java.util.Map;
31
29
import java.util.Set;
 
30
import java.util.TimeZone;
32
31
import java.util.TreeMap;
33
32
import java.util.regex.Pattern;
34
33
import java.util.zip.GZIPOutputStream;
44
43
public class ExpMobile extends BaseModule implements IModuleExport
45
44
{
46
45
    protected static Time DAY_BEGIN = new Time( 5 );
 
46
    protected static final long MSEC_PER_DAY = 24L * 60L * 60L * 1000L;
 
47
    protected static final String FILE_LIST = "list";
47
48
 
48
49
    /** Pattern for data files. */
49
 
    protected static Pattern DATA_FILE_RE =
 
50
    protected static final Pattern DATA_FILE_RE =
50
51
        Pattern.compile( "\\d{4}-\\d{2}-\\d{2}" );
 
52
    protected static final String DATEFORMAT_MASK = "yyyy-MM-dd";
 
53
    protected final int version;
 
54
 
 
55
/**
 
56
     * Creates a new ExpMobile object.
 
57
     */
 
58
    public ExpMobile(  )
 
59
    {
 
60
        version = 2;
 
61
    }
 
62
 
 
63
/**
 
64
     * Creates a new ExpMobile object.
 
65
     *
 
66
     * @param version DOCUMENT ME!
 
67
     */
 
68
    public ExpMobile( final int version )
 
69
    {
 
70
        this.version = version;
 
71
    }
51
72
 
52
73
    /**
53
74
     * Returns export config if need.
82
103
            final File destination = chooser.getSelectedFile(  );
83
104
 
84
105
            clearDir( destination );
85
 
            exportToDir( data, destination );
 
106
            exportToDir( 
 
107
                data, destination, Application.getInstance(  ).getTimeZone(  ) );
86
108
        }
87
109
    }
88
110
 
102
124
                if( !files[i].isDirectory(  ) )
103
125
                {
104
126
                    if( 
105
 
                        "list".equals( files[i].getName(  ) )
 
127
                        FILE_LIST.equals( files[i].getName(  ) )
106
128
                            || DATA_FILE_RE.matcher( files[i].getName(  ) )
107
129
                                               .matches(  ) )
108
130
                    {
118
140
     *
119
141
     * @param data data for export
120
142
     * @param dir output directory
 
143
     * @param tz DOCUMENT ME!
121
144
     *
122
145
     * @throws IOException DOCUMENT ME!
123
146
     */
124
 
    public void exportToDir( final TVData data, final File dir )
 
147
    public void exportToDir( 
 
148
        final TVData data, final File dir, final TimeZone tz )
125
149
        throws IOException
126
150
    {
127
 
        final DivideIterator itdivide = new DivideIterator(  );
 
151
        final DivideIterator itdivide =
 
152
            new DivideIterator( 
 
153
                tz, System.currentTimeMillis(  ) - ( MSEC_PER_DAY * 2 ) );
128
154
        data.iterate( itdivide );
129
155
 
130
 
        for( 
131
 
            final Iterator it = itdivide.filesData.entrySet(  ).iterator(  );
132
 
                it.hasNext(  ); )
 
156
        for( final Map.Entry<String, TVData> entry : itdivide.filesData
 
157
            .entrySet(  ) )
133
158
        {
134
 
            final Map.Entry entry = (Map.Entry)it.next(  );
135
 
            final String fileName = (String)entry.getKey(  );
 
159
            final String fileName = entry.getKey(  );
136
160
            final TVData dayData = (TVData)entry.getValue(  );
137
 
            exportOneDay( new File( dir, fileName ), dayData );
 
161
            exportOneDay( new File( dir, fileName ), dayData, tz );
138
162
        }
139
163
 
140
164
        final DataOutputStream dout =
141
165
            new DataOutputStream( 
142
166
                new BufferedOutputStream( 
143
 
                    new FileOutputStream( new File( dir, "list" ) ) ) );
 
167
                    new FileOutputStream( new File( dir, FILE_LIST ) ) ) );
144
168
 
145
169
        try
146
170
        {
147
 
            dout.writeShort( 1 ); // version
 
171
            dout.writeShort( version );
148
172
 
149
173
            final String[] channelIDs = data.getChannelIDs(  );
150
174
            dout.writeShort( channelIDs.length );
179
203
     *
180
204
     * @param file output file
181
205
     * @param dayData day data
 
206
     * @param tz DOCUMENT ME!
182
207
     *
183
208
     * @throws IOException
184
209
     */
185
 
    protected void exportOneDay( final File file, final TVData dayData )
 
210
    protected void exportOneDay( 
 
211
        final File file, final TVData dayData, final TimeZone tz )
186
212
        throws IOException
187
213
    {
188
 
        final OutIterator itDay = new OutIterator(  );
 
214
        final OutIterator itDay = new OutIterator( tz, version );
189
215
        // pack data
190
216
        dayData.iterate( itDay );
191
217
 
192
 
        final Map dataOffsets = new TreeMap(  );
 
218
        final Map<String, Integer> dataOffsets =
 
219
            new TreeMap<String, Integer>(  );
193
220
        final int headerLength =
194
221
            exportHeader( itDay.channelsData, dataOffsets, 0 ).length;
195
222
 
203
230
        {
204
231
            out.write( header );
205
232
 
206
 
            for( 
207
 
                final Iterator itCh =
208
 
                    itDay.channelsData.entrySet(  ).iterator(  );
209
 
                    itCh.hasNext(  ); )
 
233
            for( final byte[] channelData : itDay.channelsData.values(  ) )
210
234
            {
211
 
                final Map.Entry entryCh = (Map.Entry)itCh.next(  );
212
 
                final byte[] channelData = (byte[])entryCh.getValue(  );
213
235
                out.write( channelData );
214
236
            }
215
237
 
233
255
     * @throws IOException
234
256
     */
235
257
    protected byte[] exportHeader( 
236
 
        final Map channelsData, final Map offsets, final int headerOffset )
 
258
        final Map<String, byte[]> channelsData,
 
259
        final Map<String, Integer> offsets, final int headerOffset )
237
260
        throws IOException
238
261
    {
239
262
        final ByteArrayOutputStream array = new ByteArrayOutputStream(  );
242
265
 
243
266
        int currentOffset = 0;
244
267
 
245
 
        for( 
246
 
            final Iterator itCh = channelsData.entrySet(  ).iterator(  );
247
 
                itCh.hasNext(  ); )
 
268
        for( final Map.Entry<String, byte[]> entry : channelsData.entrySet(  ) )
248
269
        {
249
 
            final Map.Entry entry = (Map.Entry)itCh.next(  );
250
 
            final String channelID = (String)entry.getKey(  );
251
 
            final byte[] channelData = (byte[])entry.getValue(  );
 
270
            final String channelID = entry.getKey(  );
 
271
            final byte[] channelData = entry.getValue(  );
252
272
            dout.writeUTF( channelID );
253
273
 
254
 
            final Integer offset = (Integer)offsets.get( channelID );
 
274
            final Integer offset = offsets.get( channelID );
255
275
 
256
276
            if( offset != null )
257
277
            {
278
298
     */
279
299
    protected static class DivideIterator extends TVIteratorProgrammes
280
300
    {
281
 
        protected static final long MSEC_PER_DAY = 24L * 60L * 60L * 1000L;
282
301
        protected final SimpleDateFormat dateFormat;
283
 
        protected final Calendar calendar = GregorianCalendar.getInstance(  );
 
302
        protected final Calendar calendar;
 
303
        protected final long minimumDate;
284
304
 
285
305
        /**
286
306
         * Map for store TVData by day. Key is day, value is
287
307
         * TVData for this day.
288
308
         */
289
 
        protected Map filesData = new TreeMap(  );
 
309
        protected Map<String, TVData> filesData =
 
310
            new TreeMap<String, TVData>(  );
290
311
 
291
312
/**
292
313
         * Creates a new DivideIterator object.
293
314
         */
294
 
        public DivideIterator(  )
 
315
        public DivideIterator( final TimeZone tz, final long minimumDate )
295
316
        {
296
 
            dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
 
317
            this.calendar = Calendar.getInstance( tz );
 
318
            this.dateFormat = new SimpleDateFormat( DATEFORMAT_MASK );
 
319
            this.dateFormat.setTimeZone( tz );
 
320
            this.minimumDate = minimumDate;
297
321
        }
298
322
 
299
323
        protected void onChannel( TVChannel channel )
303
327
        protected void onProgramme( TVProgramme programme )
304
328
        {
305
329
            final String fileName = getFileName( programme.getStart(  ) );
 
330
 
 
331
            if( fileName == null )
 
332
            {
 
333
                // too old programme for export
 
334
                return;
 
335
            }
 
336
 
306
337
            TVData data = (TVData)filesData.get( fileName );
307
338
 
308
339
            if( data == null )
318
349
 
319
350
        protected String getFileName( long date )
320
351
        {
321
 
            calendar.setTime( new Date( date ) );
 
352
            calendar.setTimeInMillis( date );
322
353
 
323
 
            Time pTime = new Time( calendar );
 
354
            final Time pTime = new Time( calendar );
324
355
 
325
356
            if( pTime.compareTo( DAY_BEGIN ) < 0 )
326
357
            {
327
 
                date -= MSEC_PER_DAY;
 
358
                calendar.add( Calendar.DAY_OF_YEAR, -1 );
328
359
            }
329
360
 
330
 
            return dateFormat.format( new Date( date ) );
 
361
            final String result = dateFormat.format( calendar.getTime(  ) );
 
362
 
 
363
            calendar.set( Calendar.HOUR_OF_DAY, 0 );
 
364
            calendar.set( Calendar.MINUTE, 0 );
 
365
            calendar.set( Calendar.SECOND, 0 );
 
366
            calendar.set( Calendar.MILLISECOND, 0 );
 
367
 
 
368
            return ( calendar.getTimeInMillis(  ) >= minimumDate ) ? result
 
369
                                                                   : null;
331
370
        }
332
371
    }
333
372
 
339
378
        protected static final int BUFFER_SIZE = 65536;
340
379
 
341
380
        /** Key is channel ID, value is gzipped data. */
342
 
        final Map channelsData = new TreeMap(  );
 
381
        final Map<String, byte[]> channelsData =
 
382
            new TreeMap<String, byte[]>(  );
 
383
        protected final TimeZone tz;
 
384
        protected final int version;
 
385
        protected IOException ex;
 
386
 
 
387
/**
 
388
         * Creates a new OutIterator object.
 
389
         *
 
390
         * @param tz DOCUMENT ME!
 
391
         * @param version DOCUMENT ME!
 
392
         */
 
393
        public OutIterator( final TimeZone tz, final int version )
 
394
        {
 
395
            this.tz = tz;
 
396
            this.version = version;
 
397
        }
343
398
 
344
399
        protected void onChannel( final TVChannel channel )
345
400
        {
346
 
            final Set progs = channel.getProgrammes(  );
 
401
            final Set<TVProgramme> progs = channel.getProgrammes(  );
347
402
            final byte[] data =
348
 
                saveChannel( 
349
 
                    (TVProgramme[])progs.toArray( 
350
 
                        new TVProgramme[progs.size(  )] ) );
 
403
                saveChannel( progs.toArray( new TVProgramme[progs.size(  )] ) );
351
404
 
352
405
            if( data != null )
353
406
            {
362
415
                return null;
363
416
            }
364
417
 
365
 
            final List strings = new ArrayList(  );
 
418
            final List<String> strings = new ArrayList<String>(  );
366
419
            final int[] progNames = new int[programmes.length];
367
420
            final int[] progDescs = new int[programmes.length];
368
421
 
370
423
            {
371
424
                progNames[i] = putToList( strings, programmes[i].getTitle(  ) );
372
425
                progDescs[i] = putToList( 
373
 
                        strings, programmes[i].getDescription(  ) );
 
426
                        strings,
 
427
                        fixDescription( programmes[i].getDescription(  ) ) );
374
428
            }
375
429
 
376
430
            final ByteArrayOutputStream array = new ByteArrayOutputStream(  );
387
441
                {
388
442
                    dout.writeInt( 
389
443
                        (int)( programmes[i].getStart(  ) / 1000 / 60 ) );
 
444
 
 
445
                    switch( version )
 
446
                    {
 
447
                    case 1:
 
448
                        break;
 
449
 
 
450
                    case 2:
 
451
                        dout.writeShort( 
 
452
                            tz.getOffset( programmes[i].getStart(  ) ) / 1000 / 60 );
 
453
 
 
454
                        break;
 
455
 
 
456
                    default:
 
457
                        throw new IOException( "Unknown version" );
 
458
                    }
 
459
 
390
460
                    dout.writeShort( 
391
461
                        (int)( ( programmes[i].getEnd(  )
392
462
                        - programmes[i].getStart(  ) ) / 1000 / 60 ) );
396
466
 
397
467
                for( int i = 0; i < strings.size(  ); i++ )
398
468
                {
399
 
                    final String str = (String)strings.get( i );
 
469
                    final String str = strings.get( i );
400
470
                    dout.writeUTF( str );
401
471
                }
402
472
 
405
475
            }
406
476
            catch( IOException ex )
407
477
            {
408
 
            }
409
 
 
410
 
            return gzipArray( array.toByteArray(  ) );
411
 
        }
412
 
 
413
 
        protected int putToList( final List list, final String str )
 
478
                this.ex = ex;
 
479
            }
 
480
 
 
481
            return ( ex == null ) ? gzipArray( array.toByteArray(  ) ) : null;
 
482
        }
 
483
 
 
484
        protected String fixDescription( String desc )
 
485
        {
 
486
            if( desc == null )
 
487
            {
 
488
                return null;
 
489
            }
 
490
 
 
491
            return desc.replaceAll( "<br>", "\n" ).replaceAll( "<br/>", "\n" )
 
492
                       .replaceAll( "\\n+", "\n" );
 
493
        }
 
494
 
 
495
        protected int putToList( final List<String> list, final String str )
414
496
        {
415
497
            if( ( str == null ) || ( str.length(  ) == 0 ) )
416
498
            {