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