blob: c500e16c3388331f6ea849002f5254f6dc65193d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkXMLWriter_DEFINED
9#define SkXMLWriter_DEFINED
10
bungemana7e9f052016-02-18 08:53:33 -080011#include "../private/SkTDArray.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkString.h"
13#include "SkDOM.h"
14
15class SkWStream;
16class SkXMLParser;
17
18class SkXMLWriter {
19public:
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);
reede73da402015-02-04 18:29:27 -080028 void addText(const char text[], size_t length);
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 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
36protected:
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;
reede73da402015-02-04 18:29:27 -080039 virtual void onAddText(const char text[], size_t length) = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 virtual void onEndElement() = 0;
41
42 struct Elem {
fmalitafe3f2602015-02-03 17:47:12 -080043 Elem(const char name[], size_t len)
44 : fName(name, len)
45 , fHasChildren(false)
46 , fHasText(false) {}
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkString fName;
49 bool fHasChildren;
fmalitafe3f2602015-02-03 17:47:12 -080050 bool fHasText;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 };
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
58private:
59 bool fDoEscapeMarkup;
60 // illegal
61 SkXMLWriter& operator=(const SkXMLWriter&);
62};
63
64class SkXMLStreamWriter : public SkXMLWriter {
65public:
66 SkXMLStreamWriter(SkWStream*);
Brian Salomond3b65972017-03-22 12:05:03 -040067 ~SkXMLStreamWriter() override;
mtkleinf0599002015-07-13 06:18:39 -070068 void writeHeader() override;
reede73da402015-02-04 18:29:27 -080069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070protected:
mtklein36352bf2015-03-25 18:17:31 -070071 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;
fmalitafe3f2602015-02-03 17:47:12 -080075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076private:
77 SkWStream& fStream;
78};
79
80class SkXMLParserWriter : public SkXMLWriter {
81public:
82 SkXMLParserWriter(SkXMLParser*);
Brian Salomond3b65972017-03-22 12:05:03 -040083 ~SkXMLParserWriter() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +000084protected:
mtkleinf0599002015-07-13 06:18:39 -070085 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.com8a1c16f2008-12-17 15:59:43 +000089private:
90 SkXMLParser& fParser;
91};
92
93
94#endif