source: plugins/captions/com/jeroenwijering/parsers/TTParser.as @ 173

Revision 173, 1.3 KB checked in by jeroen, 4 years ago (diff)

tagged update of all plugins

  • Property svn:executable set to *
Line 
1/**
2* Parse a TimedText XML and return an array of captions.
3**/
4package com.jeroenwijering.parsers {
5
6
7import com.jeroenwijering.utils.Strings;
8
9
10public class TTParser {
11
12
13        /**
14        * Parse the captions XML.
15        *
16        * @param dat    The loaded XML, which must be in W3C TimedText format.
17        * @return               An array with captions.
18        *                               Each caption is an object with 'begin' and 'text' parameters.
19        **/
20        public static function parseCaptions(dat:XML):Array {
21                var arr:Array = new Array({begin:0,text:''});
22                for each (var i:XML in dat.children()) {
23                        if(i.localName() == "body") {
24                                for each (var j:XML in i.children()) {
25                                        for each (var k:XML in j.children()) {
26                                                if(k.localName() == 'p') {
27                                                        var obj:Object = TTParser.parseCaption(k);
28                                                        arr.push(obj);
29                                                        if(obj['end']) {
30                                                                arr.push({begin:obj['end'],text:''});
31                                                                delete obj['end'];
32                                                        } else if (obj['dur']) {
33                                                                arr.push({begin:obj['begin']+obj['dur'],text:''});
34                                                                delete obj['dur'];
35                                                        }
36                                                }
37                                        }
38                                }
39                        }
40                }
41                return arr;
42        };
43
44
45        /** Parse a single captions entry. **/
46        private static function parseCaption(dat:XML):Object {
47                var obj:Object = {
48                        begin:Strings.seconds(dat.@begin),
49                        dur:Strings.seconds(dat.@dur),
50                        end:Strings.seconds(dat.@end),
51                        text:dat.children().toString().replace(new RegExp('\n?<br.*>\n?'),'\n')
52                };
53                return obj;
54        };
55
56
57}
58
59
60}
Note: See TracBrowser for help on using the repository browser.