5 // Created by Heberti Almeida on 29/04/15.
6 // Copyright (c) 2015 Folio Reader. All rights reserved.
11 open class FRResources: NSObject {
13 var resources = [String: FRResource]()
16 Adds a resource to the resources.
18 func add(_ resource: FRResource) {
19 self.resources[resource.href] = resource
25 Gets the first resource (random order) with the give mediatype.
27 Useful for looking up the table of contents as it's supposed to be the only resource with NCX mediatype.
29 func findByMediaType(_ mediaType: MediaType) -> FRResource? {
30 for resource in resources.values {
31 if resource.mediaType != nil && resource.mediaType == mediaType {
39 Gets the first resource (random order) with the give extension.
41 Useful for looking up the table of contents as it's supposed to be the only resource with NCX extension.
43 func findByExtension(_ ext: String) -> FRResource? {
44 for resource in resources.values {
45 if resource.mediaType != nil && resource.mediaType.defaultExtension == ext {
53 Gets the first resource (random order) with the give properties.
55 - parameter properties: ePub 3 properties. e.g. `cover-image`, `nav`
56 - returns: The Resource.
58 func findByProperty(_ properties: String) -> FRResource? {
59 for resource in resources.values {
60 if resource.properties == properties {
68 Gets the resource with the given href.
70 func findByHref(_ href: String) -> FRResource? {
71 guard !href.isEmpty else { return nil }
73 // This clean is neede because may the toc.ncx is not located in the root directory
74 let cleanHref = href.replacingOccurrences(of: "../", with: "")
75 return resources[cleanHref]
79 Gets the resource with the given href.
81 func findById(_ id: String?) -> FRResource? {
82 guard let id = id else { return nil }
84 for resource in resources.values {
85 if let resourceID = resource.id, resourceID == id {
93 Whether there exists a resource with the given href.
95 func containsByHref(_ href: String) -> Bool {
96 guard !href.isEmpty else { return false }
98 return resources.keys.contains(href)
102 Whether there exists a resource with the given id.
104 func containsById(_ id: String?) -> Bool {
105 guard let id = id else { return false }
107 for resource in resources.values {
108 if let resourceID = resource.id, resourceID == id {