reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkXMLWriter_DEFINED |
| 9 | #define SkXMLWriter_DEFINED |
| 10 | |
bungeman | a7e9f05 | 2016-02-18 08:53:33 -0800 | [diff] [blame] | 11 | #include "../private/SkTDArray.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | #include "SkDOM.h" |
| 14 | |
| 15 | class SkWStream; |
| 16 | class SkXMLParser; |
| 17 | |
| 18 | class SkXMLWriter { |
| 19 | public: |
| 20 | SkXMLWriter(bool doEscapeMarkup = true); |
| 21 | virtual ~SkXMLWriter(); |
| 22 | |
| 23 | void addS32Attribute(const char name[], int32_t value); |
| 24 | void addAttribute(const char name[], const char value[]); |
| 25 | void addAttributeLen(const char name[], const char value[], size_t length); |
| 26 | void addHexAttribute(const char name[], uint32_t value, int minDigits = 0); |
| 27 | void addScalarAttribute(const char name[], SkScalar value); |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 28 | void addText(const char text[], size_t length); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | void endElement() { this->onEndElement(); } |
| 30 | void startElement(const char elem[]); |
| 31 | void startElementLen(const char elem[], size_t length); |
| 32 | void writeDOM(const SkDOM&, const SkDOM::Node*, bool skipRoot); |
| 33 | void flush(); |
| 34 | virtual void writeHeader(); |
| 35 | |
| 36 | protected: |
| 37 | virtual void onStartElementLen(const char elem[], size_t length) = 0; |
| 38 | virtual void onAddAttributeLen(const char name[], const char value[], size_t length) = 0; |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 39 | virtual void onAddText(const char text[], size_t length) = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | virtual void onEndElement() = 0; |
| 41 | |
| 42 | struct Elem { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 43 | Elem(const char name[], size_t len) |
| 44 | : fName(name, len) |
| 45 | , fHasChildren(false) |
| 46 | , fHasText(false) {} |
| 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkString fName; |
| 49 | bool fHasChildren; |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 50 | bool fHasText; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | }; |
| 52 | void doEnd(Elem* elem); |
| 53 | bool doStart(const char name[], size_t length); |
| 54 | Elem* getEnd(); |
| 55 | const char* getHeader(); |
| 56 | SkTDArray<Elem*> fElems; |
| 57 | |
| 58 | private: |
| 59 | bool fDoEscapeMarkup; |
| 60 | // illegal |
| 61 | SkXMLWriter& operator=(const SkXMLWriter&); |
| 62 | }; |
| 63 | |
| 64 | class SkXMLStreamWriter : public SkXMLWriter { |
| 65 | public: |
| 66 | SkXMLStreamWriter(SkWStream*); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 67 | ~SkXMLStreamWriter() override; |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 68 | void writeHeader() override; |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onStartElementLen(const char elem[], size_t length) override; |
| 72 | void onEndElement() override; |
| 73 | void onAddAttributeLen(const char name[], const char value[], size_t length) override; |
| 74 | void onAddText(const char text[], size_t length) override; |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | private: |
| 77 | SkWStream& fStream; |
| 78 | }; |
| 79 | |
| 80 | class SkXMLParserWriter : public SkXMLWriter { |
| 81 | public: |
| 82 | SkXMLParserWriter(SkXMLParser*); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 83 | ~SkXMLParserWriter() override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 85 | void onStartElementLen(const char elem[], size_t length) override; |
| 86 | void onEndElement() override; |
| 87 | void onAddAttributeLen(const char name[], const char value[], size_t length) override; |
| 88 | void onAddText(const char text[], size_t length) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | private: |
| 90 | SkXMLParser& fParser; |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | #endif |