Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(331)

Side by Side Diff: third_party/xmllite/xmlparser.cc

Issue 2443903004: Add xmllite and xmpp sources to third_party/ (Closed)
Patch Set: Restored includes in jingle/ as well Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2004 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/xmllite/xmlparser.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "third_party/xmllite/xmlconstants.h"
11 #include "third_party/xmllite/xmlelement.h"
12 #include "third_party/xmllite/xmlnsstack.h"
13 #include "third_party/xmllite/xmlnsstack.h"
14 #include "webrtc/base/common.h"
15
16 namespace buzz {
17
18
19 static void
20 StartElementCallback(void * userData, const char *name, const char **atts) {
21 (static_cast<XmlParser *>(userData))->ExpatStartElement(name, atts);
22 }
23
24 static void
25 EndElementCallback(void * userData, const char *name) {
26 (static_cast<XmlParser *>(userData))->ExpatEndElement(name);
27 }
28
29 static void
30 CharacterDataCallback(void * userData, const char *text, int len) {
31 (static_cast<XmlParser *>(userData))->ExpatCharacterData(text, len);
32 }
33
34 static void
35 XmlDeclCallback(void * userData, const char * ver, const char * enc, int st) {
36 (static_cast<XmlParser *>(userData))->ExpatXmlDecl(ver, enc, st);
37 }
38
39 XmlParser::XmlParser(XmlParseHandler *pxph) :
40 pxph_(pxph), sentError_(false) {
41 expat_ = XML_ParserCreate(NULL);
42 XML_SetUserData(expat_, this);
43 XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
44 XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
45 XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
46 }
47
48 void
49 XmlParser::Reset() {
50 if (!XML_ParserReset(expat_, NULL)) {
51 XML_ParserFree(expat_);
52 expat_ = XML_ParserCreate(NULL);
53 }
54 XML_SetUserData(expat_, this);
55 XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
56 XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
57 XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
58 context_.Reset();
59 sentError_ = false;
60 }
61
62 static bool
63 XmlParser_StartsWithXmlns(const char *name) {
64 return name[0] == 'x' &&
65 name[1] == 'm' &&
66 name[2] == 'l' &&
67 name[3] == 'n' &&
68 name[4] == 's';
69 }
70
71 void
72 XmlParser::ExpatStartElement(const char *name, const char **atts) {
73 if (context_.RaisedError() != XML_ERROR_NONE)
74 return;
75 const char **att;
76 context_.StartElement();
77 for (att = atts; *att; att += 2) {
78 if (XmlParser_StartsWithXmlns(*att)) {
79 if ((*att)[5] == '\0') {
80 context_.StartNamespace("", *(att + 1));
81 }
82 else if ((*att)[5] == ':') {
83 if (**(att + 1) == '\0') {
84 // In XML 1.0 empty namespace illegal with prefix (not in 1.1)
85 context_.RaiseError(XML_ERROR_SYNTAX);
86 return;
87 }
88 context_.StartNamespace((*att) + 6, *(att + 1));
89 }
90 }
91 }
92 context_.SetPosition(XML_GetCurrentLineNumber(expat_),
93 XML_GetCurrentColumnNumber(expat_),
94 XML_GetCurrentByteIndex(expat_));
95 pxph_->StartElement(&context_, name, atts);
96 }
97
98 void
99 XmlParser::ExpatEndElement(const char *name) {
100 if (context_.RaisedError() != XML_ERROR_NONE)
101 return;
102 context_.EndElement();
103 context_.SetPosition(XML_GetCurrentLineNumber(expat_),
104 XML_GetCurrentColumnNumber(expat_),
105 XML_GetCurrentByteIndex(expat_));
106 pxph_->EndElement(&context_, name);
107 }
108
109 void
110 XmlParser::ExpatCharacterData(const char *text, int len) {
111 if (context_.RaisedError() != XML_ERROR_NONE)
112 return;
113 context_.SetPosition(XML_GetCurrentLineNumber(expat_),
114 XML_GetCurrentColumnNumber(expat_),
115 XML_GetCurrentByteIndex(expat_));
116 pxph_->CharacterData(&context_, text, len);
117 }
118
119 void
120 XmlParser::ExpatXmlDecl(const char * ver, const char * enc, int standalone) {
121 if (context_.RaisedError() != XML_ERROR_NONE)
122 return;
123
124 if (ver && std::string("1.0") != ver) {
125 context_.RaiseError(XML_ERROR_SYNTAX);
126 return;
127 }
128
129 if (standalone == 0) {
130 context_.RaiseError(XML_ERROR_SYNTAX);
131 return;
132 }
133
134 if (enc && !((enc[0] == 'U' || enc[0] == 'u') &&
135 (enc[1] == 'T' || enc[1] == 't') &&
136 (enc[2] == 'F' || enc[2] == 'f') &&
137 enc[3] == '-' && enc[4] =='8')) {
138 context_.RaiseError(XML_ERROR_INCORRECT_ENCODING);
139 return;
140 }
141
142 }
143
144 bool
145 XmlParser::Parse(const char *data, size_t len, bool isFinal) {
146 if (sentError_)
147 return false;
148
149 if (XML_Parse(expat_, data, static_cast<int>(len), isFinal) !=
150 XML_STATUS_OK) {
151 context_.SetPosition(XML_GetCurrentLineNumber(expat_),
152 XML_GetCurrentColumnNumber(expat_),
153 XML_GetCurrentByteIndex(expat_));
154 context_.RaiseError(XML_GetErrorCode(expat_));
155 }
156
157 if (context_.RaisedError() != XML_ERROR_NONE) {
158 sentError_ = true;
159 pxph_->Error(&context_, context_.RaisedError());
160 return false;
161 }
162
163 return true;
164 }
165
166 XmlParser::~XmlParser() {
167 XML_ParserFree(expat_);
168 }
169
170 void
171 XmlParser::ParseXml(XmlParseHandler *pxph, std::string text) {
172 XmlParser parser(pxph);
173 parser.Parse(text.c_str(), text.length(), true);
174 }
175
176 XmlParser::ParseContext::ParseContext() :
177 xmlnsstack_(),
178 raised_(XML_ERROR_NONE),
179 line_number_(0),
180 column_number_(0),
181 byte_index_(0) {
182 }
183
184 void
185 XmlParser::ParseContext::StartNamespace(const char *prefix, const char *ns) {
186 xmlnsstack_.AddXmlns(*prefix ? prefix : STR_EMPTY, ns);
187 }
188
189 void
190 XmlParser::ParseContext::StartElement() {
191 xmlnsstack_.PushFrame();
192 }
193
194 void
195 XmlParser::ParseContext::EndElement() {
196 xmlnsstack_.PopFrame();
197 }
198
199 QName
200 XmlParser::ParseContext::ResolveQName(const char* qname, bool isAttr) {
201 const char *c;
202 for (c = qname; *c; ++c) {
203 if (*c == ':') {
204 const std::pair<std::string, bool> result =
205 xmlnsstack_.NsForPrefix(std::string(qname, c - qname));
206 if (!result.second)
207 return QName();
208 return QName(result.first, c + 1);
209 }
210 }
211 if (isAttr)
212 return QName(STR_EMPTY, qname);
213
214 std::pair<std::string, bool> result = xmlnsstack_.NsForPrefix(STR_EMPTY);
215 if (!result.second)
216 return QName();
217
218 return QName(result.first, qname);
219 }
220
221 void
222 XmlParser::ParseContext::Reset() {
223 xmlnsstack_.Reset();
224 raised_ = XML_ERROR_NONE;
225 }
226
227 void
228 XmlParser::ParseContext::SetPosition(int line, int column,
229 long byte_index) {
230 line_number_ = line;
231 column_number_ = column;
232 byte_index_ = byte_index;
233 }
234
235 void
236 XmlParser::ParseContext::GetPosition(unsigned long * line,
237 unsigned long * column,
238 unsigned long * byte_index) {
239 if (line != NULL) {
240 *line = static_cast<unsigned long>(line_number_);
241 }
242
243 if (column != NULL) {
244 *column = static_cast<unsigned long>(column_number_);
245 }
246
247 if (byte_index != NULL) {
248 *byte_index = static_cast<unsigned long>(byte_index_);
249 }
250 }
251
252 XmlParser::ParseContext::~ParseContext() {
253 }
254
255 } // namespace buzz
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698