-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathxml_reader.go
206 lines (172 loc) · 6.06 KB
/
xml_reader.go
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
package golibxml
/*
#cgo pkg-config: libxml-2.0
#include <libxml/xmlreader.h>
static inline void free_string(char* s) { free(s); }
static inline xmlChar *to_xmlcharptr(const char *s) { return (xmlChar *)s; }
static inline char *to_charptr(const xmlChar *s) { return (char *)s; }
*/
import "C"
////////////////////////////////////////////////////////////////////////////////
// TYPES/STRUCTS
////////////////////////////////////////////////////////////////////////////////
type TextReader struct {
Ptr C.xmlTextReaderPtr
buffer *C.char
}
// xmlReaderTypes
type ReaderType int
const (
XML_READER_TYPE_NONE ReaderType = C.XML_READER_TYPE_NONE
XML_READER_TYPE_ELEMENT = C.XML_READER_TYPE_ELEMENT
XML_READER_TYPE_ATTRIBUTE = C.XML_READER_TYPE_ATTRIBUTE
XML_READER_TYPE_TEXT = C.XML_READER_TYPE_TEXT
XML_READER_TYPE_CDATA = C.XML_READER_TYPE_CDATA
XML_READER_TYPE_ENTITY_REFERENCE = C.XML_READER_TYPE_ENTITY_REFERENCE
XML_READER_TYPE_ENTITY = C.XML_READER_TYPE_ENTITY
XML_READER_TYPE_PROCESSING_INSTRUCTION = C.XML_READER_TYPE_PROCESSING_INSTRUCTION
XML_READER_TYPE_COMMENT = C.XML_READER_TYPE_COMMENT
XML_READER_TYPE_DOCUMENT = C.XML_READER_TYPE_DOCUMENT
XML_READER_TYPE_DOCUMENT_TYPE = C.XML_READER_TYPE_DOCUMENT_TYPE
XML_READER_TYPE_DOCUMENT_FRAGMENT = C.XML_READER_TYPE_DOCUMENT_FRAGMENT
XML_READER_TYPE_NOTATION = C.XML_READER_TYPE_NOTATION
XML_READER_TYPE_WHITESPACE = C.XML_READER_TYPE_WHITESPACE
XML_READER_TYPE_SIGNIFICANT_WHITESPACE = C.XML_READER_TYPE_SIGNIFICANT_WHITESPACE
XML_READER_TYPE_END_ELEMENT = C.XML_READER_TYPE_END_ELEMENT
XML_READER_TYPE_END_ENTITY = C.XML_READER_TYPE_END_ENTITY
XML_READER_TYPE_XML_DECLARATION = C.XML_READER_TYPE_XML_DECLARATION
)
////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
func makeTextReader(reader C.xmlTextReaderPtr, buffer *C.char) *TextReader {
if reader == nil {
return nil
}
return &TextReader{reader, buffer}
}
////////////////////////////////////////////////////////////////////////////////
// INTERFACE
////////////////////////////////////////////////////////////////////////////////
func (t ReaderType) String() string {
switch t {
case XML_READER_TYPE_NONE:
return "None"
case XML_READER_TYPE_ELEMENT:
return "Element"
case XML_READER_TYPE_ATTRIBUTE:
return "Attribute"
case XML_READER_TYPE_TEXT:
return "Text"
case XML_READER_TYPE_CDATA:
return "CData"
case XML_READER_TYPE_ENTITY_REFERENCE:
return "EntityReference"
case XML_READER_TYPE_ENTITY:
return "Entity"
case XML_READER_TYPE_PROCESSING_INSTRUCTION:
return "ProcessingInstruction"
case XML_READER_TYPE_COMMENT:
return "Comment"
case XML_READER_TYPE_DOCUMENT:
return "Document"
case XML_READER_TYPE_DOCUMENT_TYPE:
return "DocumentType"
case XML_READER_TYPE_DOCUMENT_FRAGMENT:
return "DocumentFragment"
case XML_READER_TYPE_NOTATION:
return "Notation"
case XML_READER_TYPE_WHITESPACE:
return "Whitespace"
case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
return "SignificantWhitespace"
case XML_READER_TYPE_END_ELEMENT:
return "EndElement"
case XML_READER_TYPE_END_ENTITY:
return "EndEntity"
case XML_READER_TYPE_XML_DECLARATION:
return "XMLDeclaration"
default:
return "Unknown"
}
}
// xmlReaderForMemory
func ReaderForMemory(buffer string, url, encoding string, options ParserOption) *TextReader {
cbuffer := C.CString(buffer)
clen := C.int(len(buffer))
curl := C.CString(url)
defer C.free_string(curl)
cencoding := C.CString(encoding)
defer C.free_string(cencoding)
coptions := C.int(options)
ptr := C.xmlReaderForMemory(cbuffer, clen, curl, cencoding, coptions)
return makeTextReader(ptr, cbuffer)
}
// xmlTextReaderRead
func (r *TextReader) Read() int {
return int(C.xmlTextReaderRead(r.Ptr))
}
// xmlTextReaderNext
func (r *TextReader) Next() int {
return int(C.xmlTextReaderNext(r.Ptr))
}
// xmlTextReaderMoveToFirstAttribute
func (r *TextReader) MoveToFirstAttribute() int {
return int(C.xmlTextReaderMoveToFirstAttribute(r.Ptr))
}
// xmlTextReaderMoveToNextAttribute
func (r *TextReader) MoveToNextAttribute() int {
return int(C.xmlTextReaderMoveToNextAttribute(r.Ptr))
}
// xmlTextReaderNodeType
func (r *TextReader) NodeType() ReaderType {
return ReaderType(C.xmlTextReaderNodeType(r.Ptr))
}
// xmlTextReaderConstLocalName
func (r *TextReader) LocalName() string {
return C.GoString(C.to_charptr(C.xmlTextReaderConstLocalName(r.Ptr)))
}
// xmlTextReaderConstName
func (r *TextReader) Name() string {
return C.GoString(C.to_charptr(C.xmlTextReaderConstName(r.Ptr)))
}
// xmlTextReaderValue
func (r *TextReader) Value() string {
cvalue := C.to_charptr(C.xmlTextReaderValue(r.Ptr))
defer C.free_string(cvalue)
return C.GoString(cvalue)
}
// xmlTextReaderConstNamespaceUri
func (r *TextReader) NamespaceUri() string {
return C.GoString(C.to_charptr(C.xmlTextReaderConstNamespaceUri(r.Ptr)))
}
// xmlTextReaderConstPrefix
func (r *TextReader) Prefix() string {
return C.GoString(C.to_charptr(C.xmlTextReaderConstPrefix(r.Ptr)))
}
// xmlTextReaderReadString
func (r *TextReader) ReadString() string {
cstr := C.to_charptr(C.xmlTextReaderReadString(r.Ptr))
defer C.free_string(cstr)
return C.GoString(cstr)
}
// xmlTextReaderDepth
func (r *TextReader) Depth() int {
return int(C.xmlTextReaderDepth(r.Ptr))
}
// xmlTextReaderGetAttribute
func (r *TextReader) GetAttribute(name string) *string {
cname := C.CString(name)
defer C.free_string(cname)
cvalue := C.xmlTextReaderGetAttribute(r.Ptr, C.to_xmlcharptr(cname))
if cvalue != nil {
value := C.GoString(C.to_charptr(cvalue))
return &value
}
return nil
}
// xmlFreeTextReader
func (r *TextReader) Free() {
C.xmlFreeTextReader(r.Ptr)
C.free_string(r.buffer)
}