KMBaseParser.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // KMBaseParser.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2022/12/21.
  6. //
  7. import Cocoa
  8. class KMBaseParser: NSObject {
  9. var fileName: String = "comp-light"
  10. var fileType: String = "json"
  11. var jsonParser: [String: Any] {
  12. let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: fileName, ofType: fileType)!))
  13. let jsonData = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
  14. return jsonData as! [String: Any]
  15. }
  16. var globalDict: [String : Any] = [:]
  17. var aliasLightDict: [String : Any] = [:]
  18. var finalDict: [String : Any] = [:]
  19. // MARK: Parser Action
  20. func valueParser(key : String) -> Any {
  21. var parserKey = key
  22. if key.first == "{" {
  23. parserKey.removeFirst(1)
  24. }
  25. if key.last == "}" {
  26. parserKey.removeLast(1)
  27. }
  28. let keys = parserKey.split(separator: ".")
  29. var parserArr: [[String : Any]] = [aliasLightDict, globalDict]
  30. for subKey in keys {
  31. if subKey == keys.last {
  32. if parserArr.count == 1 {
  33. let dict: [String : Any] = parserArr.last!
  34. let valueDict = dict[String(subKey)] as! [String : Any]
  35. let value = valueDict["value"]
  36. if value is [String : Any] {
  37. return value as Any
  38. } else if value is String {
  39. let valueStr = value as! String
  40. if (valueStr.first == "{") && (valueStr.last == "}") {
  41. return valueParser(key: valueStr)
  42. } else if (valueStr.first == "r" && valueStr.last == ")") {
  43. return rgbaParser(key: valueStr)
  44. } else {
  45. return valueStr
  46. }
  47. }
  48. } else if parserArr.count == 2 {
  49. for model in parserArr {
  50. let allKey = model.keys
  51. if allKey.contains(String(subKey)) {
  52. let valueDict = model[String(subKey)] as! [String : Any]
  53. let allKey = valueDict.keys
  54. if allKey.contains("value") {
  55. let value = valueDict["value"]
  56. if value is [String : Any] {
  57. return value as Any
  58. } else if value is String {
  59. let valueStr = value as! String
  60. if (valueStr.first == "{") && (valueStr.last == "}") {
  61. return valueParser(key: valueStr)
  62. } else if (valueStr.first == "r" && valueStr.last == ")") {
  63. return rgbaParser(key: valueStr)
  64. } else {
  65. return valueStr
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. } else {
  73. parserArr = parserAction(parserArr, String(subKey))
  74. }
  75. }
  76. return ""
  77. }
  78. func rgbaParser(key : String) -> Any {
  79. var parserKey = key
  80. if key.first == "r" {
  81. parserKey.removeFirst(5)
  82. }
  83. if key.last == ")" {
  84. parserKey.removeLast(1)
  85. }
  86. let parserKey1 = parserKey.replacingOccurrences(of: " ", with: "") as String
  87. let keys = parserKey1.split(separator: ",")
  88. return String(format: "%@,%@", valueParser(key: String(keys[0])) as! String, String(keys[1]))
  89. }
  90. func parserAction(_ array: [[String : Any]], _ key: String) -> [[String : Any]] {
  91. var parserArr: [[String : Any]] = []
  92. for model in array {
  93. let allKey = model.keys
  94. if allKey.contains(key) {
  95. parserArr.append(model[key] as! [String : Any])
  96. }
  97. }
  98. return parserArr
  99. }
  100. }