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
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "third_party/libjingle_xmpp/xmllite/xmlparser.h"
#include <string>
#include <vector>
#include "third_party/libjingle_xmpp/xmllite/xmlconstants.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
#include "third_party/libjingle_xmpp/xmllite/xmlnsstack.h"
#include "third_party/libjingle_xmpp/xmllite/xmlnsstack.h"
#include "third_party/webrtc/base/common.h"
namespace buzz {
static void
StartElementCallback(void * userData, const char *name, const char **atts) {
(static_cast<XmlParser *>(userData))->ExpatStartElement(name, atts);
}
static void
EndElementCallback(void * userData, const char *name) {
(static_cast<XmlParser *>(userData))->ExpatEndElement(name);
}
static void
CharacterDataCallback(void * userData, const char *text, int len) {
(static_cast<XmlParser *>(userData))->ExpatCharacterData(text, len);
}
static void
XmlDeclCallback(void * userData, const char * ver, const char * enc, int st) {
(static_cast<XmlParser *>(userData))->ExpatXmlDecl(ver, enc, st);
}
XmlParser::XmlParser(XmlParseHandler *pxph) :
pxph_(pxph), sentError_(false) {
expat_ = XML_ParserCreate(NULL);
XML_SetUserData(expat_, this);
XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
}
void
XmlParser::Reset() {
if (!XML_ParserReset(expat_, NULL)) {
XML_ParserFree(expat_);
expat_ = XML_ParserCreate(NULL);
}
XML_SetUserData(expat_, this);
XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
context_.Reset();
sentError_ = false;
}
static bool
XmlParser_StartsWithXmlns(const char *name) {
return name[0] == 'x' &&
name[1] == 'm' &&
name[2] == 'l' &&
name[3] == 'n' &&
name[4] == 's';
}
void
XmlParser::ExpatStartElement(const char *name, const char **atts) {
if (context_.RaisedError() != XML_ERROR_NONE)
return;
const char **att;
context_.StartElement();
for (att = atts; *att; att += 2) {
if (XmlParser_StartsWithXmlns(*att)) {
if ((*att)[5] == '\0') {
context_.StartNamespace("", *(att + 1));
}
else if ((*att)[5] == ':') {
if (**(att + 1) == '\0') {
// In XML 1.0 empty namespace illegal with prefix (not in 1.1)
context_.RaiseError(XML_ERROR_SYNTAX);
return;
}
context_.StartNamespace((*att) + 6, *(att + 1));
}
}
}
context_.SetPosition(XML_GetCurrentLineNumber(expat_),
XML_GetCurrentColumnNumber(expat_),
XML_GetCurrentByteIndex(expat_));
pxph_->StartElement(&context_, name, atts);
}
void
XmlParser::ExpatEndElement(const char *name) {
if (context_.RaisedError() != XML_ERROR_NONE)
return;
context_.EndElement();
context_.SetPosition(XML_GetCurrentLineNumber(expat_),
XML_GetCurrentColumnNumber(expat_),
XML_GetCurrentByteIndex(expat_));
pxph_->EndElement(&context_, name);
}
void
XmlParser::ExpatCharacterData(const char *text, int len) {
if (context_.RaisedError() != XML_ERROR_NONE)
return;
context_.SetPosition(XML_GetCurrentLineNumber(expat_),
XML_GetCurrentColumnNumber(expat_),
XML_GetCurrentByteIndex(expat_));
pxph_->CharacterData(&context_, text, len);
}
void
XmlParser::ExpatXmlDecl(const char * ver, const char * enc, int standalone) {
if (context_.RaisedError() != XML_ERROR_NONE)
return;
if (ver && std::string("1.0") != ver) {
context_.RaiseError(XML_ERROR_SYNTAX);
return;
}
if (standalone == 0) {
context_.RaiseError(XML_ERROR_SYNTAX);
return;
}
if (enc && !((enc[0] == 'U' || enc[0] == 'u') &&
(enc[1] == 'T' || enc[1] == 't') &&
(enc[2] == 'F' || enc[2] == 'f') &&
enc[3] == '-' && enc[4] =='8')) {
context_.RaiseError(XML_ERROR_INCORRECT_ENCODING);
return;
}
}
bool
XmlParser::Parse(const char *data, size_t len, bool isFinal) {
if (sentError_)
return false;
if (XML_Parse(expat_, data, static_cast<int>(len), isFinal) !=
XML_STATUS_OK) {
context_.SetPosition(XML_GetCurrentLineNumber(expat_),
XML_GetCurrentColumnNumber(expat_),
XML_GetCurrentByteIndex(expat_));
context_.RaiseError(XML_GetErrorCode(expat_));
}
if (context_.RaisedError() != XML_ERROR_NONE) {
sentError_ = true;
pxph_->Error(&context_, context_.RaisedError());
return false;
}
return true;
}
XmlParser::~XmlParser() {
XML_ParserFree(expat_);
}
void
XmlParser::ParseXml(XmlParseHandler *pxph, std::string text) {
XmlParser parser(pxph);
parser.Parse(text.c_str(), text.length(), true);
}
XmlParser::ParseContext::ParseContext() :
xmlnsstack_(),
raised_(XML_ERROR_NONE),
line_number_(0),
column_number_(0),
byte_index_(0) {
}
void
XmlParser::ParseContext::StartNamespace(const char *prefix, const char *ns) {
xmlnsstack_.AddXmlns(*prefix ? prefix : STR_EMPTY, ns);
}
void
XmlParser::ParseContext::StartElement() {
xmlnsstack_.PushFrame();
}
void
XmlParser::ParseContext::EndElement() {
xmlnsstack_.PopFrame();
}
QName
XmlParser::ParseContext::ResolveQName(const char* qname, bool isAttr) {
const char *c;
for (c = qname; *c; ++c) {
if (*c == ':') {
const std::pair<std::string, bool> result =
xmlnsstack_.NsForPrefix(std::string(qname, c - qname));
if (!result.second)
return QName();
return QName(result.first, c + 1);
}
}
if (isAttr)
return QName(STR_EMPTY, qname);
std::pair<std::string, bool> result = xmlnsstack_.NsForPrefix(STR_EMPTY);
if (!result.second)
return QName();
return QName(result.first, qname);
}
void
XmlParser::ParseContext::Reset() {
xmlnsstack_.Reset();
raised_ = XML_ERROR_NONE;
}
void
XmlParser::ParseContext::SetPosition(int line, int column,
long byte_index) {
line_number_ = line;
column_number_ = column;
byte_index_ = byte_index;
}
void
XmlParser::ParseContext::GetPosition(unsigned long * line,
unsigned long * column,
unsigned long * byte_index) {
if (line != NULL) {
*line = static_cast<unsigned long>(line_number_);
}
if (column != NULL) {
*column = static_cast<unsigned long>(column_number_);
}
if (byte_index != NULL) {
*byte_index = static_cast<unsigned long>(byte_index_);
}
}
XmlParser::ParseContext::~ParseContext() {
}
} // namespace buzz
|