xmlparser.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_
  11. #define THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_
  12. #include <string>
  13. #include "third_party/libjingle_xmpp/xmllite/xmlnsstack.h"
  14. #ifdef EXPAT_RELATIVE_PATH
  15. #include "expat.h"
  16. #else
  17. #include "third_party/expat/v2_0_1/Source/lib/expat.h"
  18. #endif // EXPAT_RELATIVE_PATH
  19. struct XML_ParserStruct;
  20. typedef struct XML_ParserStruct* XML_Parser;
  21. namespace jingle_xmpp {
  22. class XmlParseHandler;
  23. class XmlParseContext;
  24. class XmlParser;
  25. class XmlParseContext {
  26. public:
  27. virtual ~XmlParseContext() {}
  28. virtual QName ResolveQName(const char * qname, bool isAttr) = 0;
  29. virtual void RaiseError(XML_Error err) = 0;
  30. virtual void GetPosition(unsigned long * line, unsigned long * column,
  31. unsigned long * byte_index) = 0;
  32. };
  33. class XmlParseHandler {
  34. public:
  35. virtual ~XmlParseHandler() {}
  36. virtual void StartElement(XmlParseContext * pctx,
  37. const char * name, const char ** atts) = 0;
  38. virtual void EndElement(XmlParseContext * pctx,
  39. const char * name) = 0;
  40. virtual void CharacterData(XmlParseContext * pctx,
  41. const char * text, int len) = 0;
  42. virtual void Error(XmlParseContext * pctx,
  43. XML_Error errorCode) = 0;
  44. };
  45. class XmlParser {
  46. public:
  47. static void ParseXml(XmlParseHandler * pxph, std::string text);
  48. explicit XmlParser(XmlParseHandler * pxph);
  49. bool Parse(const char * data, size_t len, bool isFinal);
  50. void Reset();
  51. virtual ~XmlParser();
  52. // expat callbacks
  53. void ExpatStartElement(const char * name, const char ** atts);
  54. void ExpatEndElement(const char * name);
  55. void ExpatCharacterData(const char * text, int len);
  56. void ExpatXmlDecl(const char * ver, const char * enc, int standalone);
  57. private:
  58. class ParseContext : public XmlParseContext {
  59. public:
  60. ParseContext();
  61. virtual ~ParseContext();
  62. virtual QName ResolveQName(const char * qname, bool isAttr);
  63. virtual void RaiseError(XML_Error err) { if (!raised_) raised_ = err; }
  64. virtual void GetPosition(unsigned long * line, unsigned long * column,
  65. unsigned long * byte_index);
  66. XML_Error RaisedError() { return raised_; }
  67. void Reset();
  68. void StartElement();
  69. void EndElement();
  70. void StartNamespace(const char * prefix, const char * ns);
  71. void SetPosition(int line, int column, long byte_index);
  72. private:
  73. XmlnsStack xmlnsstack_;
  74. XML_Error raised_;
  75. XML_Size line_number_;
  76. XML_Size column_number_;
  77. XML_Index byte_index_;
  78. };
  79. ParseContext context_;
  80. XML_Parser expat_;
  81. XmlParseHandler * pxph_;
  82. bool sentError_;
  83. };
  84. } // namespace jingle_xmpp
  85. #endif // THIRD_PARTY_LIBJINGLE_XMPP_XMLLITE_XMLPARSER_H_