added iOS source code
[wl-app.git] / iOS / Pods / FolioReaderKit / Source / EPUBCore / FRSmilElement.swift
1 //
2 //  FRSmil.swift
3 //  Pods
4 //
5 //  Created by Kevin Jantzer on 12/30/15.
6 //
7 //
8
9 import UIKit
10
11 // Media Overlay Documentation
12 // http://www.idpf.org/accessibility/guidelines/content/overlays/overview.php#mo005-samp
13
14
15 class FRSmilElement: NSObject {
16     var name: String // the name of the tag: <seq>, <par>, <text>, <audio>
17     var attributes: [String: String]!
18     var children: [FRSmilElement]
19
20     init(name: String, attributes: [String:String]!) {
21         self.name = name
22         self.attributes = attributes
23         self.children = [FRSmilElement]()
24     }
25
26     // MARK: - Element attributes
27
28     func getId() -> String! {
29         return getAttribute("id")
30     }
31
32     func getSrc() -> String! {
33         return getAttribute("src")
34     }
35
36     /**
37      Returns array of Strings if `epub:type` attribute is set. An array is returned as there can be multiple types specified, seperated by a whitespace
38      */
39     func getType() -> [String]! {
40         let type = getAttribute("epub:type", defaultVal: "")
41         return type!.components(separatedBy: " ")
42     }
43
44     /**
45      Use to determine if this element matches a given type
46
47      **Example**
48
49      epub:type="bodymatter chapter"
50      isType("bodymatter") -> true
51      */
52     func isType(_ aType:String) -> Bool {
53         return getType().contains(aType)
54     }
55
56     func getAttribute(_ name: String, defaultVal: String!) -> String! {
57         return attributes[name] != nil ? attributes[name] : defaultVal;
58     }
59
60     func getAttribute(_ name: String ) -> String! {
61         return getAttribute(name, defaultVal: nil)
62     }
63
64     // MARK: - Retrieving children elements
65
66     // if <par> tag, a <text> is required (http://www.idpf.org/epub/301/spec/epub-mediaoverlays.html#sec-smil-par-elem)
67     func textElement() -> FRSmilElement! {
68         return childWithName("text")
69     }
70
71     func audioElement() -> FRSmilElement! {
72         return childWithName("audio")
73     }
74
75     func videoElement() -> FRSmilElement! {
76         return childWithName("video")
77     }
78
79     func childWithName(_ name:String) -> FRSmilElement! {
80         for el in children {
81             if( el.name == name ){
82                 return el
83             }
84         }
85         return nil;
86     }
87
88     func childrenWithNames(_ name:[String]) -> [FRSmilElement]! {
89         var matched = [FRSmilElement]()
90         for el in children {
91             if( name.contains(el.name) ){
92                 matched.append(el)
93             }
94         }
95         return matched;
96     }
97
98     func childrenWithName(_ name:String) -> [FRSmilElement]! {
99         return childrenWithNames([name])
100     }
101
102     // MARK: - Audio clip info
103
104     func clipBegin() -> Double {
105         let val = audioElement().getAttribute("clipBegin", defaultVal: "")
106         return val!.clockTimeToSeconds()
107     }
108
109     func clipEnd() -> Double {
110         let val = audioElement().getAttribute("clipEnd", defaultVal: "")
111         return val!.clockTimeToSeconds()
112     }
113 }