SkXMLParser.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkXMLParser_DEFINED
  8. #define SkXMLParser_DEFINED
  9. #include "include/core/SkString.h"
  10. class SkStream;
  11. class SkDOM;
  12. struct SkDOMNode;
  13. class SkXMLParserError {
  14. public:
  15. enum ErrorCode {
  16. kNoError,
  17. kEmptyFile,
  18. kUnknownElement,
  19. kUnknownAttributeName,
  20. kErrorInAttributeValue,
  21. kDuplicateIDs,
  22. kUnknownError
  23. };
  24. SkXMLParserError();
  25. virtual ~SkXMLParserError();
  26. ErrorCode getErrorCode() const { return fCode; }
  27. virtual void getErrorString(SkString* str) const;
  28. int getLineNumber() const { return fLineNumber; }
  29. int getNativeCode() const { return fNativeCode; }
  30. bool hasError() const { return fCode != kNoError || fNativeCode != -1; }
  31. bool hasNoun() const { return fNoun.size() > 0; }
  32. void reset();
  33. void setCode(ErrorCode code) { fCode = code; }
  34. void setNoun(const SkString& str) { fNoun.set(str); }
  35. void setNoun(const char* ch) { fNoun.set(ch); }
  36. void setNoun(const char* ch, size_t len) { fNoun.set(ch, len); }
  37. protected:
  38. ErrorCode fCode;
  39. private:
  40. int fLineNumber;
  41. int fNativeCode;
  42. SkString fNoun;
  43. friend class SkXMLParser;
  44. };
  45. class SkXMLParser {
  46. public:
  47. SkXMLParser(SkXMLParserError* parserError = nullptr);
  48. virtual ~SkXMLParser();
  49. /** Returns true for success
  50. */
  51. bool parse(const char doc[], size_t len);
  52. bool parse(SkStream& docStream);
  53. bool parse(const SkDOM&, const SkDOMNode*);
  54. static void GetNativeErrorString(int nativeErrorCode, SkString* str);
  55. protected:
  56. // override in subclasses; return true to stop parsing
  57. virtual bool onStartElement(const char elem[]);
  58. virtual bool onAddAttribute(const char name[], const char value[]);
  59. virtual bool onEndElement(const char elem[]);
  60. virtual bool onText(const char text[], int len);
  61. public:
  62. // public for ported implementation, not meant for clients to call
  63. bool startElement(const char elem[]);
  64. bool addAttribute(const char name[], const char value[]);
  65. bool endElement(const char elem[]);
  66. bool text(const char text[], int len);
  67. void* fParser;
  68. protected:
  69. SkXMLParserError* fError;
  70. private:
  71. void reportError(void* parser);
  72. };
  73. #endif