~ubuntu-branches/ubuntu/saucy/freeguide/saucy

« back to all changes in this revision

Viewing changes to src/freeguide/common/lib/importexport/XMLTVImportHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Watkins
  • Date: 2008-09-07 15:49:32 UTC
  • mfrom: (1.2.6 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080907154932-2jvgv76btq068fe0
Tags: 0.10.9-1
* New upstream release. (Closes: #492789)
* Moved package from contrib to main. (Closes: #492544)
* Added lintian override for 'build-depends-without-arch-dep ant', as ant is
  used in the clean target.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import java.text.ParseException;
21
21
import java.text.SimpleDateFormat;
22
22
 
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;
31
30
 *
32
31
 * @author Alex Buloichik (alex73 at zaval.org)
33
32
 */
34
 
class XMLTVImportHandler extends DefaultHandler
 
33
public class XMLTVImportHandler extends DefaultHandler
35
34
{
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 =
 
46
        {
 
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} .*$" )
 
53
        };
 
54
    private SimpleDateFormat[] dateFormats =
 
55
        {
 
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" )
 
62
        };
 
63
    public int programmesCount;
54
64
 
55
65
/**
56
66
     * Creates a new Handler object.
119
129
                currentSite = attributes.getValue( "generator-url" );
120
130
            }
121
131
 
 
132
            if( currentSite == null )
 
133
            {
 
134
                currentSite = "unknown generator";
 
135
            }
 
136
 
122
137
            currentChannel = null;
123
138
            currentProgramme = null;
124
139
        }
144
159
                countCallback.onProgramme( programmesCount );
145
160
            }
146
161
 
147
 
            try
148
 
            {
149
 
                String start = attributes.getValue( "start" );
150
 
                String stop = attributes.getValue( "stop" );
151
 
 
152
 
                if( start == null )
153
 
                {
154
 
                    throw new SAXException( 
155
 
                        "Time of program start not defined !" );
156
 
                }
157
 
 
158
 
                currentProgramme.setStart( 
159
 
                    parseDate( start ).getTimeInMillis(  ) );
160
 
 
161
 
                if( stop != null )
162
 
                {
163
 
                    currentProgramme.setEnd( 
164
 
                        parseDate( stop ).getTimeInMillis(  ) );
165
 
                }
166
 
 
167
 
                if( !filter.checkProgrammeStart( currentProgramme ) )
168
 
                {
169
 
                    currentProgramme = null;
170
 
                }
171
 
                else
172
 
                {
173
 
                    currentChannelID = channelPrefix
174
 
                        + attributes.getValue( "channel" );
175
 
                }
176
 
            }
177
 
            catch( ParseException ex )
178
 
            {
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" );
 
164
 
 
165
            if( start == null )
 
166
            {
 
167
                throw new SAXException( "Time of program start not defined !" );
 
168
            }
 
169
 
 
170
            long startTime = parseDate( start );
 
171
 
 
172
            if( startTime != 0 )
 
173
            {
 
174
                currentProgramme.setStart( startTime );
 
175
            }
 
176
            else
 
177
            {
 
178
                currentProgramme = null;
 
179
 
 
180
                return;
 
181
            }
 
182
 
 
183
            if( stop != null )
 
184
            {
 
185
                long stopTime = parseDate( stop );
 
186
 
 
187
                if( stopTime != 0 )
 
188
                {
 
189
                    currentProgramme.setEnd( stopTime );
 
190
                }
 
191
            }
 
192
 
 
193
            if( !filter.checkProgrammeStart( currentProgramme ) )
 
194
            {
 
195
                currentProgramme = null;
 
196
            }
 
197
            else
 
198
            {
 
199
                currentChannelID = channelPrefix
 
200
                    + attributes.getValue( "channel" );
182
201
            }
183
202
        }
184
203
    }
404
423
    /**
405
424
     * Parse data string.
406
425
     *
407
 
     * @param strDate
408
 
     *
409
 
     * @return
410
 
     *
411
 
     * @throws ParseException
 
426
     * @param strDate a time represented in string form.
 
427
     *
 
428
     * @return the time in milliseconds since 1970 represented by the supplied
 
429
     *         string.
412
430
     */
413
 
    private Calendar parseDate( String strDate ) throws ParseException
 
431
    private long parseDate( String strDate )
414
432
    {
415
 
        Matcher m;
416
 
 
417
 
        m = p1.matcher( strDate );
418
 
 
419
 
        if( m.matches(  ) )
420
 
        {
421
 
            ans.setTime( f1.parse( strDate ) );
422
 
 
423
 
            return ans;
424
 
        }
425
 
 
426
 
        m = p2.matcher( strDate );
427
 
 
428
 
        if( m.matches(  ) )
429
 
        {
430
 
            ans.setTime( f2.parse( strDate ) );
431
 
 
432
 
            return ans;
433
 
        }
434
 
 
435
 
        try
436
 
        {
437
 
            ans.setTime( f3.parse( strDate ) );
438
 
        }
439
 
        catch( ParseException ex )
440
 
        {
441
 
            ans.setTime( f4.parse( strDate ) );
 
433
        Date dtAns = null;
 
434
 
 
435
        for( int i = 0; i < dateFormatPatterns.length; ++i )
 
436
        {
 
437
            Matcher m = dateFormatPatterns[i].matcher( strDate );
 
438
 
 
439
            if( m.matches(  ) )
 
440
            {
 
441
                try
 
442
                {
 
443
                    dtAns = dateFormats[i].parse( strDate );
 
444
                }
 
445
                catch( ParseException ex )
 
446
                {
 
447
                    ex.printStackTrace(  );
 
448
                    dtAns = null;
 
449
                }
 
450
            }
 
451
        }
 
452
 
 
453
        long ans = 0;
 
454
 
 
455
        if( dtAns == null )
 
456
        {
 
457
            Application.getInstance(  ).getLogger(  )
 
458
                       .log( 
 
459
                Level.WARNING,
 
460
                "Unable to parse date '" + strDate
 
461
                + "' when parsing XMLTV data." );
 
462
        }
 
463
        else
 
464
        {
 
465
            ans = dtAns.getTime(  );
442
466
        }
443
467
 
444
468
        return ans;