3
3
import freeguide.common.gui.FileChooserExtension;
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;
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
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";
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;
56
* Creates a new ExpMobile object.
64
* Creates a new ExpMobile object.
66
* @param version DOCUMENT ME!
68
public ExpMobile( final int version )
70
this.version = version;
53
74
* Returns export config if need.
82
103
final File destination = chooser.getSelectedFile( );
84
105
clearDir( destination );
85
exportToDir( data, destination );
107
data, destination, Application.getInstance( ).getTimeZone( ) );
119
141
* @param data data for export
120
142
* @param dir output directory
143
* @param tz DOCUMENT ME!
122
145
* @throws IOException DOCUMENT ME!
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
127
final DivideIterator itdivide = new DivideIterator( );
151
final DivideIterator itdivide =
153
tz, System.currentTimeMillis( ) - ( MSEC_PER_DAY * 2 ) );
128
154
data.iterate( itdivide );
131
final Iterator it = itdivide.filesData.entrySet( ).iterator( );
156
for( final Map.Entry<String, TVData> entry : itdivide.filesData
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 );
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 ) ) ) );
147
dout.writeShort( 1 ); // version
171
dout.writeShort( version );
149
173
final String[] channelIDs = data.getChannelIDs( );
150
174
dout.writeShort( channelIDs.length );
180
204
* @param file output file
181
205
* @param dayData day data
206
* @param tz DOCUMENT ME!
183
208
* @throws IOException
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
188
final OutIterator itDay = new OutIterator( );
214
final OutIterator itDay = new OutIterator( tz, version );
190
216
dayData.iterate( itDay );
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;
204
231
out.write( header );
207
final Iterator itCh =
208
itDay.channelsData.entrySet( ).iterator( );
233
for( final byte[] channelData : itDay.channelsData.values( ) )
211
final Map.Entry entryCh = (Map.Entry)itCh.next( );
212
final byte[] channelData = (byte[])entryCh.getValue( );
213
235
out.write( channelData );
233
255
* @throws IOException
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
239
262
final ByteArrayOutputStream array = new ByteArrayOutputStream( );
243
266
int currentOffset = 0;
246
final Iterator itCh = channelsData.entrySet( ).iterator( );
268
for( final Map.Entry<String, byte[]> entry : channelsData.entrySet( ) )
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 );
254
final Integer offset = (Integer)offsets.get( channelID );
274
final Integer offset = offsets.get( channelID );
256
276
if( offset != null )
279
299
protected static class DivideIterator extends TVIteratorProgrammes
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;
286
306
* Map for store TVData by day. Key is day, value is
287
307
* TVData for this day.
289
protected Map filesData = new TreeMap( );
309
protected Map<String, TVData> filesData =
310
new TreeMap<String, TVData>( );
292
313
* Creates a new DivideIterator object.
294
public DivideIterator( )
315
public DivideIterator( final TimeZone tz, final long minimumDate )
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;
299
323
protected void onChannel( TVChannel channel )
319
350
protected String getFileName( long date )
321
calendar.setTime( new Date( date ) );
352
calendar.setTimeInMillis( date );
323
Time pTime = new Time( calendar );
354
final Time pTime = new Time( calendar );
325
356
if( pTime.compareTo( DAY_BEGIN ) < 0 )
327
date -= MSEC_PER_DAY;
358
calendar.add( Calendar.DAY_OF_YEAR, -1 );
330
return dateFormat.format( new Date( date ) );
361
final String result = dateFormat.format( calendar.getTime( ) );
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 );
368
return ( calendar.getTimeInMillis( ) >= minimumDate ) ? result
339
378
protected static final int BUFFER_SIZE = 65536;
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;
388
* Creates a new OutIterator object.
390
* @param tz DOCUMENT ME!
391
* @param version DOCUMENT ME!
393
public OutIterator( final TimeZone tz, final int version )
396
this.version = version;
344
399
protected void onChannel( final TVChannel channel )
346
final Set progs = channel.getProgrammes( );
401
final Set<TVProgramme> progs = channel.getProgrammes( );
347
402
final byte[] data =
349
(TVProgramme[])progs.toArray(
350
new TVProgramme[progs.size( )] ) );
403
saveChannel( progs.toArray( new TVProgramme[progs.size( )] ) );
352
405
if( data != null )
371
424
progNames[i] = putToList( strings, programmes[i].getTitle( ) );
372
425
progDescs[i] = putToList(
373
strings, programmes[i].getDescription( ) );
427
fixDescription( programmes[i].getDescription( ) ) );
376
430
final ByteArrayOutputStream array = new ByteArrayOutputStream( );
389
443
(int)( programmes[i].getStart( ) / 1000 / 60 ) );
452
tz.getOffset( programmes[i].getStart( ) ) / 1000 / 60 );
457
throw new IOException( "Unknown version" );
391
461
(int)( ( programmes[i].getEnd( )
392
462
- programmes[i].getStart( ) ) / 1000 / 60 ) );
406
476
catch( IOException ex )
410
return gzipArray( array.toByteArray( ) );
413
protected int putToList( final List list, final String str )
481
return ( ex == null ) ? gzipArray( array.toByteArray( ) ) : null;
484
protected String fixDescription( String desc )
491
return desc.replaceAll( "<br>", "\n" ).replaceAll( "<br/>", "\n" )
492
.replaceAll( "\\n+", "\n" );
495
protected int putToList( final List<String> list, final String str )
415
497
if( ( str == null ) || ( str.length( ) == 0 ) )