You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/xmllite/xmlparser.h"
  28. #include <string>
  29. #include <vector>
  30. #include "talk/base/common.h"
  31. #include "talk/xmllite/xmlconstants.h"
  32. #include "talk/xmllite/xmlelement.h"
  33. #include "talk/xmllite/xmlnsstack.h"
  34. #include "talk/xmllite/xmlnsstack.h"
  35. namespace buzz {
  36. static void
  37. StartElementCallback(void * userData, const char *name, const char **atts) {
  38. (static_cast<XmlParser *>(userData))->ExpatStartElement(name, atts);
  39. }
  40. static void
  41. EndElementCallback(void * userData, const char *name) {
  42. (static_cast<XmlParser *>(userData))->ExpatEndElement(name);
  43. }
  44. static void
  45. CharacterDataCallback(void * userData, const char *text, int len) {
  46. (static_cast<XmlParser *>(userData))->ExpatCharacterData(text, len);
  47. }
  48. static void
  49. XmlDeclCallback(void * userData, const char * ver, const char * enc, int st) {
  50. (static_cast<XmlParser *>(userData))->ExpatXmlDecl(ver, enc, st);
  51. }
  52. XmlParser::XmlParser(XmlParseHandler *pxph) :
  53. context_(this), pxph_(pxph), sentError_(false) {
  54. expat_ = XML_ParserCreate(NULL);
  55. XML_SetUserData(expat_, this);
  56. XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
  57. XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
  58. XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
  59. }
  60. void
  61. XmlParser::Reset() {
  62. if (!XML_ParserReset(expat_, NULL)) {
  63. XML_ParserFree(expat_);
  64. expat_ = XML_ParserCreate(NULL);
  65. }
  66. XML_SetUserData(expat_, this);
  67. XML_SetElementHandler(expat_, StartElementCallback, EndElementCallback);
  68. XML_SetCharacterDataHandler(expat_, CharacterDataCallback);
  69. XML_SetXmlDeclHandler(expat_, XmlDeclCallback);
  70. context_.Reset();
  71. sentError_ = false;
  72. }
  73. static bool
  74. XmlParser_StartsWithXmlns(const char *name) {
  75. return name[0] == 'x' &&
  76. name[1] == 'm' &&
  77. name[2] == 'l' &&
  78. name[3] == 'n' &&
  79. name[4] == 's';
  80. }
  81. void
  82. XmlParser::ExpatStartElement(const char *name, const char **atts) {
  83. if (context_.RaisedError() != XML_ERROR_NONE)
  84. return;
  85. const char **att;
  86. context_.StartElement();
  87. for (att = atts; *att; att += 2) {
  88. if (XmlParser_StartsWithXmlns(*att)) {
  89. if ((*att)[5] == '\0') {
  90. context_.StartNamespace("", *(att + 1));
  91. }
  92. else if ((*att)[5] == ':') {
  93. if (**(att + 1) == '\0') {
  94. // In XML 1.0 empty namespace illegal with prefix (not in 1.1)
  95. context_.RaiseError(XML_ERROR_SYNTAX);
  96. return;
  97. }
  98. context_.StartNamespace((*att) + 6, *(att + 1));
  99. }
  100. }
  101. }
  102. context_.SetPosition(XML_GetCurrentLineNumber(expat_),
  103. XML_GetCurrentColumnNumber(expat_),
  104. XML_GetCurrentByteIndex(expat_));
  105. pxph_->StartElement(&context_, name, atts);
  106. }
  107. void
  108. XmlParser::ExpatEndElement(const char *name) {
  109. if (context_.RaisedError() != XML_ERROR_NONE)
  110. return;
  111. context_.EndElement();
  112. context_.SetPosition(XML_GetCurrentLineNumber(expat_),
  113. XML_GetCurrentColumnNumber(expat_),
  114. XML_GetCurrentByteIndex(expat_));
  115. pxph_->EndElement(&context_, name);
  116. }
  117. void
  118. XmlParser::ExpatCharacterData(const char *text, int len) {
  119. if (context_.RaisedError() != XML_ERROR_NONE)
  120. return;
  121. context_.SetPosition(XML_GetCurrentLineNumber(expat_),
  122. XML_GetCurrentColumnNumber(expat_),
  123. XML_GetCurrentByteIndex(expat_));
  124. pxph_->CharacterData(&context_, text, len);
  125. }
  126. void
  127. XmlParser::ExpatXmlDecl(const char * ver, const char * enc, int standalone) {
  128. if (context_.RaisedError() != XML_ERROR_NONE)
  129. return;
  130. if (ver && std::string("1.0") != ver) {
  131. context_.RaiseError(XML_ERROR_SYNTAX);
  132. return;
  133. }
  134. if (standalone == 0) {
  135. context_.RaiseError(XML_ERROR_SYNTAX);
  136. return;
  137. }
  138. if (enc && !((enc[0] == 'U' || enc[0] == 'u') &&
  139. (enc[1] == 'T' || enc[1] == 't') &&
  140. (enc[2] == 'F' || enc[2] == 'f') &&
  141. enc[3] == '-' && enc[4] =='8')) {
  142. context_.RaiseError(XML_ERROR_INCORRECT_ENCODING);
  143. return;
  144. }
  145. }
  146. bool
  147. XmlParser::Parse(const char *data, size_t len, bool isFinal) {
  148. if (sentError_)
  149. return false;
  150. if (XML_Parse(expat_, data, static_cast<int>(len), isFinal) !=
  151. XML_STATUS_OK) {
  152. context_.SetPosition(XML_GetCurrentLineNumber(expat_),
  153. XML_GetCurrentColumnNumber(expat_),
  154. XML_GetCurrentByteIndex(expat_));
  155. context_.RaiseError(XML_GetErrorCode(expat_));
  156. }
  157. if (context_.RaisedError() != XML_ERROR_NONE) {
  158. sentError_ = true;
  159. pxph_->Error(&context_, context_.RaisedError());
  160. return false;
  161. }
  162. return true;
  163. }
  164. XmlParser::~XmlParser() {
  165. XML_ParserFree(expat_);
  166. }
  167. void
  168. XmlParser::ParseXml(XmlParseHandler *pxph, std::string text) {
  169. XmlParser parser(pxph);
  170. parser.Parse(text.c_str(), text.length(), true);
  171. }
  172. XmlParser::ParseContext::ParseContext(XmlParser *parser) :
  173. parser_(parser),
  174. xmlnsstack_(),
  175. raised_(XML_ERROR_NONE),
  176. line_number_(0),
  177. column_number_(0),
  178. byte_index_(0) {
  179. }
  180. void
  181. XmlParser::ParseContext::StartNamespace(const char *prefix, const char *ns) {
  182. xmlnsstack_.AddXmlns(*prefix ? prefix : STR_EMPTY, ns);
  183. }
  184. void
  185. XmlParser::ParseContext::StartElement() {
  186. xmlnsstack_.PushFrame();
  187. }
  188. void
  189. XmlParser::ParseContext::EndElement() {
  190. xmlnsstack_.PopFrame();
  191. }
  192. QName
  193. XmlParser::ParseContext::ResolveQName(const char* qname, bool isAttr) {
  194. const char *c;
  195. for (c = qname; *c; ++c) {
  196. if (*c == ':') {
  197. const std::pair<std::string, bool> result =
  198. xmlnsstack_.NsForPrefix(std::string(qname, c - qname));
  199. if (!result.second)
  200. return QName();
  201. return QName(result.first, c + 1);
  202. }
  203. }
  204. if (isAttr)
  205. return QName(STR_EMPTY, qname);
  206. std::pair<std::string, bool> result = xmlnsstack_.NsForPrefix(STR_EMPTY);
  207. if (!result.second)
  208. return QName();
  209. return QName(result.first, qname);
  210. }
  211. void
  212. XmlParser::ParseContext::Reset() {
  213. xmlnsstack_.Reset();
  214. raised_ = XML_ERROR_NONE;
  215. }
  216. void
  217. XmlParser::ParseContext::SetPosition(int line, int column,
  218. long byte_index) {
  219. line_number_ = line;
  220. column_number_ = column;
  221. byte_index_ = byte_index;
  222. }
  223. void
  224. XmlParser::ParseContext::GetPosition(unsigned long * line,
  225. unsigned long * column,
  226. unsigned long * byte_index) {
  227. if (line != NULL) {
  228. *line = static_cast<unsigned long>(line_number_);
  229. }
  230. if (column != NULL) {
  231. *column = static_cast<unsigned long>(column_number_);
  232. }
  233. if (byte_index != NULL) {
  234. *byte_index = static_cast<unsigned long>(byte_index_);
  235. }
  236. }
  237. XmlParser::ParseContext::~ParseContext() {
  238. }
  239. } // namespace buzz