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.

121 lines
4.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. #ifndef TALK_XMLLITE_XMLPARSER_H_
  28. #define TALK_XMLLITE_XMLPARSER_H_
  29. #include <string>
  30. #include "talk/xmllite/xmlnsstack.h"
  31. #ifdef EXPAT_RELATIVE_PATH
  32. #include "expat.h"
  33. #else
  34. #include "third_party/expat/v2_0_1/Source/lib/expat.h"
  35. #endif // EXPAT_RELATIVE_PATH
  36. struct XML_ParserStruct;
  37. typedef struct XML_ParserStruct* XML_Parser;
  38. namespace buzz {
  39. class XmlParseHandler;
  40. class XmlParseContext;
  41. class XmlParser;
  42. class XmlParseContext {
  43. public:
  44. virtual ~XmlParseContext() {}
  45. virtual QName ResolveQName(const char * qname, bool isAttr) = 0;
  46. virtual void RaiseError(XML_Error err) = 0;
  47. virtual void GetPosition(unsigned long * line, unsigned long * column,
  48. unsigned long * byte_index) = 0;
  49. };
  50. class XmlParseHandler {
  51. public:
  52. virtual ~XmlParseHandler() {}
  53. virtual void StartElement(XmlParseContext * pctx,
  54. const char * name, const char ** atts) = 0;
  55. virtual void EndElement(XmlParseContext * pctx,
  56. const char * name) = 0;
  57. virtual void CharacterData(XmlParseContext * pctx,
  58. const char * text, int len) = 0;
  59. virtual void Error(XmlParseContext * pctx,
  60. XML_Error errorCode) = 0;
  61. };
  62. class XmlParser {
  63. public:
  64. static void ParseXml(XmlParseHandler * pxph, std::string text);
  65. explicit XmlParser(XmlParseHandler * pxph);
  66. bool Parse(const char * data, size_t len, bool isFinal);
  67. void Reset();
  68. virtual ~XmlParser();
  69. // expat callbacks
  70. void ExpatStartElement(const char * name, const char ** atts);
  71. void ExpatEndElement(const char * name);
  72. void ExpatCharacterData(const char * text, int len);
  73. void ExpatXmlDecl(const char * ver, const char * enc, int standalone);
  74. private:
  75. class ParseContext : public XmlParseContext {
  76. public:
  77. ParseContext(XmlParser * parser);
  78. virtual ~ParseContext();
  79. virtual QName ResolveQName(const char * qname, bool isAttr);
  80. virtual void RaiseError(XML_Error err) { if (!raised_) raised_ = err; }
  81. virtual void GetPosition(unsigned long * line, unsigned long * column,
  82. unsigned long * byte_index);
  83. XML_Error RaisedError() { return raised_; }
  84. void Reset();
  85. void StartElement();
  86. void EndElement();
  87. void StartNamespace(const char * prefix, const char * ns);
  88. void SetPosition(int line, int column, long byte_index);
  89. private:
  90. const XmlParser * parser_;
  91. XmlnsStack xmlnsstack_;
  92. XML_Error raised_;
  93. XML_Size line_number_;
  94. XML_Size column_number_;
  95. XML_Index byte_index_;
  96. };
  97. ParseContext context_;
  98. XML_Parser expat_;
  99. XmlParseHandler * pxph_;
  100. bool sentError_;
  101. };
  102. } // namespace buzz
  103. #endif // TALK_XMLLITE_XMLPARSER_H_