123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // KMBaseParser.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/12/21.
- //
- import Cocoa
- class KMBaseParser: NSObject {
-
- var fileName: String = "comp-light"
- var fileType: String = "json"
- var jsonParser: [String: Any] {
- let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: fileName, ofType: fileType)!))
- let jsonData = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
- return jsonData as! [String: Any]
- }
- var globalDict: [String : Any] = [:]
- var aliasLightDict: [String : Any] = [:]
-
- var finalDict: [String : Any] = [:]
- // MARK: Parser Action
-
- func valueParser(key : String) -> Any {
- var parserKey = key
- if key.first == "{" {
- parserKey.removeFirst(1)
- }
- if key.last == "}" {
- parserKey.removeLast(1)
- }
- let keys = parserKey.split(separator: ".")
- var parserArr: [[String : Any]] = [aliasLightDict, globalDict]
- for subKey in keys {
- if subKey == keys.last {
- if parserArr.count == 1 {
- let dict: [String : Any] = parserArr.last!
- let valueDict = dict[String(subKey)] as! [String : Any]
- let value = valueDict["value"]
- if value is [String : Any] {
- return value as Any
- } else if value is String {
- let valueStr = value as! String
- if (valueStr.first == "{") && (valueStr.last == "}") {
- return valueParser(key: valueStr)
- } else if (valueStr.first == "r" && valueStr.last == ")") {
- return rgbaParser(key: valueStr)
- } else {
- return valueStr
- }
- }
- } else if parserArr.count == 2 {
- for model in parserArr {
- let allKey = model.keys
- if allKey.contains(String(subKey)) {
- let valueDict = model[String(subKey)] as! [String : Any]
- let allKey = valueDict.keys
- if allKey.contains("value") {
- let value = valueDict["value"]
- if value is [String : Any] {
- return value as Any
- } else if value is String {
- let valueStr = value as! String
- if (valueStr.first == "{") && (valueStr.last == "}") {
- return valueParser(key: valueStr)
- } else if (valueStr.first == "r" && valueStr.last == ")") {
- return rgbaParser(key: valueStr)
- } else {
- return valueStr
- }
- }
- }
- }
- }
- }
- } else {
- parserArr = parserAction(parserArr, String(subKey))
- }
- }
- return ""
- }
-
- func rgbaParser(key : String) -> Any {
- var parserKey = key
- if key.first == "r" {
- parserKey.removeFirst(5)
- }
- if key.last == ")" {
- parserKey.removeLast(1)
- }
- let parserKey1 = parserKey.replacingOccurrences(of: " ", with: "") as String
- let keys = parserKey1.split(separator: ",")
- return String(format: "%@,%@", valueParser(key: String(keys[0])) as! String, String(keys[1]))
- }
-
- func parserAction(_ array: [[String : Any]], _ key: String) -> [[String : Any]] {
- var parserArr: [[String : Any]] = []
- for model in array {
- let allKey = model.keys
- if allKey.contains(key) {
- parserArr.append(model[key] as! [String : Any])
- }
- }
- return parserArr
- }
- }
|