20
20
import java.text.ParseException;
21
21
import java.text.SimpleDateFormat;
23
import java.util.Calendar;
24
import java.util.GregorianCalendar;
23
import java.util.Date;
25
24
import java.util.logging.Level;
26
25
import java.util.regex.Matcher;
27
26
import java.util.regex.Pattern;
32
31
* @author Alex Buloichik (alex73 at zaval.org)
34
class XMLTVImportHandler extends DefaultHandler
33
public class XMLTVImportHandler extends DefaultHandler
36
35
final protected IStoragePipe storage;
37
36
final protected XMLTVImport.ProgrammesCountCallback countCallback;
43
42
protected String currentChannelID;
44
43
protected boolean isStarRating;
45
44
protected StringBuffer charData = new StringBuffer( );
46
private Calendar ans = GregorianCalendar.getInstance( );
47
private Pattern p1 = Pattern.compile( "\\A\\d{12}\\z" );
48
private Pattern p2 = Pattern.compile( "\\A\\d{14}\\z" );
49
private SimpleDateFormat f1 = new SimpleDateFormat( "yyyyMMddHHmm" );
50
private SimpleDateFormat f2 = new SimpleDateFormat( "yyyyMMddHHmmss" );
51
private SimpleDateFormat f3 = new SimpleDateFormat( "yyyyMMddHHmmss z" );
52
private SimpleDateFormat f4 = new SimpleDateFormat( "yyyyMMddHHmmss Z" );
53
protected int programmesCount;
45
private Pattern[] dateFormatPatterns =
47
Pattern.compile( "^\\d{12}$" ),
48
Pattern.compile( "^\\d{12} [-+]\\d{4}$" ),
49
Pattern.compile( "^\\d{14}$" ),
50
Pattern.compile( "^\\d{14} [-+]\\d{4}$" ),
51
Pattern.compile( "^\\d{14} .*$" ),
52
Pattern.compile( "^\\d{12} .*$" )
54
private SimpleDateFormat[] dateFormats =
56
new SimpleDateFormat( "yyyyMMddHHmm" ),
57
new SimpleDateFormat( "yyyyMMddHHmm z" ),
58
new SimpleDateFormat( "yyyyMMddHHmmss" ),
59
new SimpleDateFormat( "yyyyMMddHHmmss z" ),
60
new SimpleDateFormat( "yyyyMMddHHmmss Z" ),
61
new SimpleDateFormat( "yyyyMMddHHmm Z" )
63
public int programmesCount;
56
66
* Creates a new Handler object.
144
159
countCallback.onProgramme( programmesCount );
149
String start = attributes.getValue( "start" );
150
String stop = attributes.getValue( "stop" );
154
throw new SAXException(
155
"Time of program start not defined !" );
158
currentProgramme.setStart(
159
parseDate( start ).getTimeInMillis( ) );
163
currentProgramme.setEnd(
164
parseDate( stop ).getTimeInMillis( ) );
167
if( !filter.checkProgrammeStart( currentProgramme ) )
169
currentProgramme = null;
173
currentChannelID = channelPrefix
174
+ attributes.getValue( "channel" );
177
catch( ParseException ex )
179
currentProgramme = null;
180
Application.getInstance( ).getLogger( )
181
.log( Level.FINE, "Error parse XMLTV data", ex );
162
String start = attributes.getValue( "start" );
163
String stop = attributes.getValue( "stop" );
167
throw new SAXException( "Time of program start not defined !" );
170
long startTime = parseDate( start );
174
currentProgramme.setStart( startTime );
178
currentProgramme = null;
185
long stopTime = parseDate( stop );
189
currentProgramme.setEnd( stopTime );
193
if( !filter.checkProgrammeStart( currentProgramme ) )
195
currentProgramme = null;
199
currentChannelID = channelPrefix
200
+ attributes.getValue( "channel" );
405
424
* Parse data string.
411
* @throws ParseException
426
* @param strDate a time represented in string form.
428
* @return the time in milliseconds since 1970 represented by the supplied
413
private Calendar parseDate( String strDate ) throws ParseException
431
private long parseDate( String strDate )
417
m = p1.matcher( strDate );
421
ans.setTime( f1.parse( strDate ) );
426
m = p2.matcher( strDate );
430
ans.setTime( f2.parse( strDate ) );
437
ans.setTime( f3.parse( strDate ) );
439
catch( ParseException ex )
441
ans.setTime( f4.parse( strDate ) );
435
for( int i = 0; i < dateFormatPatterns.length; ++i )
437
Matcher m = dateFormatPatterns[i].matcher( strDate );
443
dtAns = dateFormats[i].parse( strDate );
445
catch( ParseException ex )
447
ex.printStackTrace( );
457
Application.getInstance( ).getLogger( )
460
"Unable to parse date '" + strDate
461
+ "' when parsing XMLTV data." );
465
ans = dtAns.getTime( );