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 "SkTDArray.h"
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);
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     SkXMLStreamWriter(SkWStream*);
67     virtual ~SkXMLStreamWriter();
68     virtual void    writeHeader();
69     SkDEBUGCODE(static void UnitTest();)
70 
71 protected:
72     void onStartElementLen(const char elem[], size_t length) override;
73     void onEndElement() override;
74     void onAddAttributeLen(const char name[], const char value[], size_t length) override;
75     void onAddText(const char text[], size_t length) override;
76 
77 private:
78     SkWStream&      fStream;
79 };
80 
81 class SkXMLParserWriter : public SkXMLWriter {
82 public:
83     SkXMLParserWriter(SkXMLParser*);
84     virtual ~SkXMLParserWriter();
85 protected:
86     virtual void onStartElementLen(const char elem[], size_t length);
87     virtual void onEndElement();
88     virtual void onAddAttributeLen(const char name[], const char value[], size_t length);
89     virtual void onAddText(const char text[], size_t length) override;
90 private:
91     SkXMLParser&        fParser;
92 };
93 
94 
95 #endif
96