forked from tid-kijyun/Kanna
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLParser.swift
More file actions
106 lines (87 loc) · 3.07 KB
/
HTMLParser.swift
File metadata and controls
106 lines (87 loc) · 3.07 KB
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
/**@file
* @brief Swift-HTML-Parser
* @author _tid_
*/
import Foundation
let DUMP_BUFFER_SIZE : Int = 4000
func ConvXmlCharToString(str: UnsafePointer<xmlChar>) -> String! {
if str != nil {
return String.fromCString(UnsafeMutablePointer<CChar>(str))
}
return ""
}
func rawContentsOfNode(node : xmlNode, pointer: xmlNodePtr) -> String! {
var result : String?
var xmlBuffer = xmlBufferCreateSize(DUMP_BUFFER_SIZE)
var outputBuffer : xmlOutputBufferPtr = xmlOutputBufferCreateBuffer(xmlBuffer, nil)
let document = node.doc
let xmlCharContent = document.memory.encoding
let contentAddress = unsafeBitCast(xmlCharContent, UnsafePointer<xmlChar>.self)
let constChar = UnsafePointer<Int8>(contentAddress)
htmlNodeDumpOutput(outputBuffer, document, pointer, constChar)
xmlOutputBufferFlush(outputBuffer)
if xmlBuffer.memory.content != nil {
result = ConvXmlCharToString(xmlBuffer.memory.content)
}
xmlOutputBufferClose(outputBuffer)
xmlBufferFree(xmlBuffer)
return result
}
/**
* HTMLParser
*/
public class HTMLParser {
private var _doc : htmlDocPtr = nil
private var rootNode : HTMLNode?
public var htmlString : String = ""
/**
* HTML tag
*/
public var html : HTMLNode? {
return rootNode?.findChildTag("html")
}
/**
* HEAD tag
*/
public var head : HTMLNode? {
return rootNode?.findChildTag("head")
}
/**
* BODY tag
*/
public var body : HTMLNode? {
return rootNode?.findChildTag("body")
}
/**
* @param[in] html HTML文字列
* @param[in] error エラーがあれば返します
*/
public init(html: String, encoding: UInt, option: CInt, inout error: NSError?) {
if html.lengthOfBytesUsingEncoding(encoding) > 0 {
self.htmlString = html
var cfenc : CFStringEncoding = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)
var cfencstr : CFStringRef = CFStringConvertEncodingToIANACharSetName(cfenc)
var cur : [CChar]? = html.cStringUsingEncoding(NSUTF8StringEncoding)
var url : String = ""
var enc = CFStringGetCStringPtr(cfencstr, 0)
let optionHtml : CInt = option
if var ucur = cur {
_doc = htmlReadDoc(UnsafePointer<CUnsignedChar>(ucur), url, enc, optionHtml)
rootNode = HTMLNode(doc: _doc)
} else {
error = NSError(domain: "HTMLParserdomain", code: 1, userInfo: nil)
}
} else {
error = NSError(domain: "HTMLParserdomain", code: 1, userInfo: nil)
}
}
public convenience init(html: String, inout error: NSError?) {
self.init(html: html, encoding: NSUTF8StringEncoding, option: 1, error: &error)
}
public convenience init(html: String, encoding : UInt, inout error: NSError?) {
self.init(html: html, encoding: encoding, option: 1, error: &error)
}
deinit {
xmlFreeDoc(_doc)
}
}