added iOS source code
[wl-app.git] / iOS / Pods / FolioReaderKit / Source / EPUBCore / FRResources.swift
1 //
2 //  FRResources.swift
3 //  FolioReaderKit
4 //
5 //  Created by Heberti Almeida on 29/04/15.
6 //  Copyright (c) 2015 Folio Reader. All rights reserved.
7 //
8
9 import UIKit
10
11 open class FRResources: NSObject {
12     
13     var resources = [String: FRResource]()
14
15     /**
16      Adds a resource to the resources.
17      */
18     func add(_ resource: FRResource) {
19         self.resources[resource.href] = resource
20     }
21
22     // MARK: Find
23
24     /**
25      Gets the first resource (random order) with the give mediatype.
26
27      Useful for looking up the table of contents as it's supposed to be the only resource with NCX mediatype.
28      */
29     func findByMediaType(_ mediaType: MediaType) -> FRResource? {
30         for resource in resources.values {
31             if resource.mediaType != nil && resource.mediaType == mediaType {
32                 return resource
33             }
34         }
35         return nil
36     }
37
38     /**
39      Gets the first resource (random order) with the give extension.
40
41      Useful for looking up the table of contents as it's supposed to be the only resource with NCX extension.
42      */
43     func findByExtension(_ ext: String) -> FRResource? {
44         for resource in resources.values {
45             if resource.mediaType != nil && resource.mediaType.defaultExtension == ext {
46                 return resource
47             }
48         }
49         return nil
50     }
51
52     /**
53      Gets the first resource (random order) with the give properties.
54
55      - parameter properties: ePub 3 properties. e.g. `cover-image`, `nav`
56      - returns: The Resource.
57      */
58     func findByProperty(_ properties: String) -> FRResource? {
59         for resource in resources.values {
60             if resource.properties == properties {
61                 return resource
62             }
63         }
64         return nil
65     }
66
67     /**
68      Gets the resource with the given href.
69      */
70     func findByHref(_ href: String) -> FRResource? {
71         guard !href.isEmpty else { return nil }
72
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]
76     }
77
78     /**
79      Gets the resource with the given href.
80      */
81     func findById(_ id: String?) -> FRResource? {
82         guard let id = id else { return nil }
83
84         for resource in resources.values {
85             if let resourceID = resource.id, resourceID == id {
86                 return resource
87             }
88         }
89         return nil
90     }
91
92     /**
93      Whether there exists a resource with the given href.
94      */
95     func containsByHref(_ href: String) -> Bool {
96         guard !href.isEmpty else { return false }
97
98         return resources.keys.contains(href)
99     }
100
101     /**
102      Whether there exists a resource with the given id.
103      */
104     func containsById(_ id: String?) -> Bool {
105         guard let id = id else { return false }
106
107         for resource in resources.values {
108             if let resourceID = resource.id, resourceID == id {
109                 return true
110             }
111         }
112         return false
113     }
114 }