-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionProfile.swift
100 lines (86 loc) · 2.59 KB
/
SessionProfile.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// SessionProfile.swift
// liteterm
//
// Created by yjroot on 2015. 7. 24..
// Copyright (c) 2015년 Liteterm team. All rights reserved.
//
import Foundation
enum ProfileError: ErrorType {
case UnkonwnVersion
}
class SessionProfile {
var parent: BaseProfile = OptionProfile.sharedInstance
var error: Bool = false
var filepath: NSURL? = nil
private func emptyXML() -> XMLIndexer {
let root = XMLElement(name: rootElementName)
root.addElement("liteterm", withAttributes: ["version":"0.01"])
return XMLIndexer(root)
}
private var xml: XMLIndexer? = nil
var root: XMLIndexer {
if self.xml != nil {
return self.xml!["liteterm"]
}
if let data: NSData = NSData(contentsOfURL: self.filepath!) {
// TODO: Because parse mothod will be slow with large file, it needs
// to change to lazy mothod. But, lazy parse does not support
// description yet.
let xml = SWXMLHash.parse(data)
if xml["liteterm"].element?.attributes["version"] == "0.01" {
self.xml = xml
}
}
if self.xml == nil {
self.xml = self.emptyXML()
}
return self.xml!["liteterm"]
}
var name: String {
return self.filepath?.URLByDeletingPathExtension?.lastPathComponent ?? ""
}
init(filepath: NSURL? = nil) {
self.filepath = filepath
if filepath == nil {
self.xml = emptyXML()
}
}
func save() -> Bool {
if let path: NSURL = self.filepath {
do {
try self.xml!.description.writeToURL(path, atomically: true, encoding: NSUTF8StringEncoding)
} catch _ {
}
return true
}
return false
}
}
extension SessionProfile: BaseProfile {
func getValue(keys: [String]) -> String? {
var xml: XMLIndexer = self.root
for key in keys {
xml = xml[key]
}
if let element: XMLElement = xml.element {
return element.text ?? ""
}
return nil
}
func setValue(keys: [String], value: String) {
var xml: XMLIndexer = self.root
for key in keys {
if xml[key].boolValue {
xml = xml[key]
} else {
xml.element?.addElement(key, withAttributes: [:])
xml = xml[key]
}
}
xml.element?.text = value
}
var description: String {
return "description"
}
}