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 8 #ifndef SkXMLWriter_DEFINED 9 #define SkXMLWriter_DEFINED 10 11 #include "include/core/SkString.h" 12 #include "include/private/SkTDArray.h" 13 #include "src/xml/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); 28 void addText(const char text[], size_t length); endElement()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; 39 virtual void onAddText(const char text[], size_t length) = 0; 40 virtual void onEndElement() = 0; 41 42 struct Elem { ElemElem43 Elem(const char name[], size_t len) 44 : fName(name, len) 45 , fHasChildren(false) 46 , fHasText(false) {} 47 48 SkString fName; 49 bool fHasChildren; 50 bool fHasText; 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 enum : uint32_t { 67 kNoPretty_Flag = 0x01, 68 }; 69 70 SkXMLStreamWriter(SkWStream*, uint32_t flags = 0); 71 ~SkXMLStreamWriter() override; 72 void writeHeader() override; 73 74 protected: 75 void onStartElementLen(const char elem[], size_t length) override; 76 void onEndElement() override; 77 void onAddAttributeLen(const char name[], const char value[], size_t length) override; 78 void onAddText(const char text[], size_t length) override; 79 80 private: 81 void newline(); 82 void tab(int lvl); 83 84 SkWStream& fStream; 85 const uint32_t fFlags; 86 }; 87 88 class SkXMLParserWriter : public SkXMLWriter { 89 public: 90 SkXMLParserWriter(SkXMLParser*); 91 ~SkXMLParserWriter() override; 92 protected: 93 void onStartElementLen(const char elem[], size_t length) override; 94 void onEndElement() override; 95 void onAddAttributeLen(const char name[], const char value[], size_t length) override; 96 void onAddText(const char text[], size_t length) override; 97 private: 98 SkXMLParser& fParser; 99 }; 100 101 102 #endif 103