-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJi.swift
executable file
·300 lines (226 loc) · 9.5 KB
/
Ji.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// Ji.swift
// Ji
//
// Created by Honghao Zhang on 2015-07-21.
// Copyright (c) 2015 Honghao Zhang (张宏昊)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
public typealias 戟 = Ji
/// Ji document
public class Ji {
/// A flag specifies whether the data is XML or not.
internal var isXML: Bool = true
/// The XML/HTML data.
private(set) public var data: NSData?
/// The encoding used by in this document.
private(set) public var encoding: NSStringEncoding = NSUTF8StringEncoding
public typealias htmlDocPtr = xmlDocPtr
/// The xmlDocPtr for this document
private(set) public var xmlDoc: xmlDocPtr = nil
/// Alias for xmlDoc
private(set) public var htmlDoc: htmlDocPtr {
get { return xmlDoc }
set { xmlDoc = newValue }
}
// MARK: - Init
/**
Initializes a Ji document object with the supplied data, encoding and boolean flag.
- parameter data: The XML/HTML data.
- parameter encoding: The encoding used by data.
- parameter isXML: Whether this is a XML data, true for XML, false for HTML.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public required init?(data: NSData?, encoding: NSStringEncoding, isXML: Bool) {
if let data = data where data.length > 0 {
self.isXML = isXML
self.data = data
self.encoding = encoding
let cBuffer = UnsafePointer<CChar>(data.bytes)
let cSize = CInt(data.length)
let cfEncoding = CFStringConvertNSStringEncodingToEncoding(encoding)
let cfEncodingAsString: CFStringRef = CFStringConvertEncodingToIANACharSetName(cfEncoding)
let cEncoding: UnsafePointer<CChar> = CFStringGetCStringPtr(cfEncodingAsString, 0)
if isXML {
let options = CInt(XML_PARSE_RECOVER.rawValue)
xmlDoc = xmlReadMemory(cBuffer, cSize, nil, cEncoding, options)
} else {
let options = CInt(HTML_PARSE_RECOVER.rawValue | HTML_PARSE_NOWARNING.rawValue | HTML_PARSE_NOERROR.rawValue)
htmlDoc = htmlReadMemory(cBuffer, cSize, nil, cEncoding, options)
}
if xmlDoc == nil { return nil }
} else {
return nil
}
}
/**
Initializes a Ji document object with the supplied data and boolean flag, using NSUTF8StringEncoding.
- parameter data: The XML/HTML data.
- parameter isXML: Whether this is a XML data, true for XML, false for HTML.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(data: NSData?, isXML: Bool) {
self.init(data: data, encoding: NSUTF8StringEncoding, isXML: isXML)
}
// MARK: - Data Init
/**
Initializes a Ji document object with the supplied XML data and encoding.
- parameter xmlData: The XML data.
- parameter encoding: The encoding used by data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(xmlData: NSData, encoding: NSStringEncoding) {
self.init(data: xmlData, encoding: encoding, isXML: true)
}
/**
Initializes a Ji document object with the supplied XML data, using NSUTF8StringEncoding.
- parameter xmlData: The XML data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(xmlData: NSData) {
self.init(data: xmlData, isXML: true)
}
/**
Initializes a Ji document object with the supplied HTML data and encoding.
- parameter htmlData: The HTML data.
- parameter encoding: The encoding used by data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(htmlData: NSData, encoding: NSStringEncoding) {
self.init(data: htmlData, encoding: encoding, isXML: false)
}
/**
Initializes a Ji document object with the supplied HTML data, using NSUTF8StringEncoding.
- parameter htmlData: The HTML data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(htmlData: NSData) {
self.init(data: htmlData, isXML: false)
}
// MARK: - URL Init
/**
Initializes a Ji document object with the contents of supplied URL, encoding and boolean flag.
- parameter url: The URL from which to read data.
- parameter encoding: The encoding used by data.
- parameter isXML: Whether this is a XML data URL, true for XML, false for HTML.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(contentsOfURL url: NSURL, encoding: NSStringEncoding, isXML: Bool) {
let data = NSData(contentsOfURL: url)
self.init(data: data, encoding: encoding, isXML: isXML)
}
/**
Initializes a Ji document object with the contents of supplied URL, and boolean flag, using NSUTF8StringEncoding.
- parameter url: The URL from which to read data.
- parameter isXML: Whether this is a XML data URL, true for XML, false for HTML.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(contentsOfURL url: NSURL, isXML: Bool) {
self.init(contentsOfURL: url, encoding: NSUTF8StringEncoding, isXML: isXML)
}
/**
Initializes a Ji document object with the contents of supplied XML URL, using NSUTF8StringEncoding.
- parameter xmlURL: The XML URL from which to read data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(xmlURL: NSURL) {
self.init(contentsOfURL: xmlURL, isXML: true)
}
/**
Initializes a Ji document object with the contents of supplied HTML URL, using NSUTF8StringEncoding.
- parameter htmlURL: The HTML URL from which to read data.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(htmlURL: NSURL) {
self.init(contentsOfURL: htmlURL, isXML: false)
}
// MARK: - String Init
/**
Initializes a Ji document object with a XML string and it's encoding.
- parameter xmlString: XML string.
- parameter encoding: The encoding used by xmlString.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(xmlString: String, encoding: NSStringEncoding) {
let data = xmlString.dataUsingEncoding(encoding, allowLossyConversion: false)
self.init(data: data, encoding: encoding, isXML: true)
}
/**
Initializes a Ji document object with a HTML string and it's encoding.
- parameter htmlString: HTML string.
- parameter encoding: The encoding used by htmlString.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(htmlString: String, encoding: NSStringEncoding) {
let data = htmlString.dataUsingEncoding(encoding, allowLossyConversion: false)
self.init(data: data, encoding: encoding, isXML: false)
}
/**
Initializes a Ji document object with a XML string, using NSUTF8StringEncoding.
- parameter xmlString: XML string.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(xmlString: String) {
let data = xmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
self.init(data: data, isXML: true)
}
/**
Initializes a Ji document object with a HTML string, using NSUTF8StringEncoding.
- parameter htmlString: HTML string.
- returns: The initialized Ji document object or nil if the object could not be initialized.
*/
public convenience init?(htmlString: String) {
let data = htmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
self.init(data: data, isXML: false)
}
// MARK: - Deinit
deinit {
xmlFreeDoc(xmlDoc)
}
// MARK: - Public methods
/// Root node of this Ji document object.
public lazy var rootNode: JiNode? = {
let rootNodePointer = xmlDocGetRootElement(self.xmlDoc)
if rootNodePointer == nil {
return nil
} else {
return JiNode(xmlNode: rootNodePointer, jiDocument: self)
}
}()
/**
Perform XPath query on this document.
- parameter xPath: XPath query string.
- returns: An array of JiNode or nil if rootNode is nil. An empty array will be returned if XPath matches no nodes.
*/
public func xPath(xPath: String) -> [JiNode]? {
return self.rootNode?.xPath(xPath)
}
}
// MARK: - Equatable
extension Ji: Equatable { }
public func ==(lhs: Ji, rhs: Ji) -> Bool {
return lhs.xmlDoc == rhs.xmlDoc
}
// MARK: - CustomStringConvertible
extension Ji: CustomStringConvertible {
public var description: String {
return rootNode?.rawContent ?? "nil"
}
}