1 #ifndef _XEXMLWRITER_HPP
2 #define _XEXMLWRITER_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Test Executor
5 * ------------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief XML Writer.
24 *//*--------------------------------------------------------------------*/
25
26 #include "xeDefs.hpp"
27
28 #include <ostream>
29 #include <vector>
30 #include <string>
31 #include <streambuf>
32
33 namespace xe
34 {
35 namespace xml
36 {
37
38 class EscapeStreambuf : public std::streambuf
39 {
40 public:
EscapeStreambuf(std::ostream & dst)41 EscapeStreambuf(std::ostream &dst) : m_dst(dst)
42 {
43 }
44
45 protected:
46 std::streamsize xsputn(const char *s, std::streamsize count);
47 int overflow(int ch = -1);
48
49 private:
50 std::ostream &m_dst;
51 };
52
53 class Writer
54 {
55 public:
56 struct BeginElement
57 {
58 std::string element;
BeginElementxe::xml::Writer::BeginElement59 BeginElement(const char *element_) : element(element_)
60 {
61 }
62 };
63
64 struct Attribute
65 {
66 std::string name;
67 std::string value;
Attributexe::xml::Writer::Attribute68 Attribute(const char *name_, const char *value_) : name(name_), value(value_)
69 {
70 }
Attributexe::xml::Writer::Attribute71 Attribute(const char *name_, const std::string &value_) : name(name_), value(value_)
72 {
73 }
Attributexe::xml::Writer::Attribute74 Attribute(const std::string &name_, const std::string &value_) : name(name_), value(value_)
75 {
76 }
77 };
78
79 static const struct EndElementType
80 {
81 } EndElement;
82
83 Writer(std::ostream &dst);
84 ~Writer(void);
85
86 Writer &operator<<(const BeginElement &begin);
87 Writer &operator<<(const Attribute &attribute);
88 Writer &operator<<(const EndElementType &end);
89
90 template <typename T>
91 Writer &operator<<(const T &value); //!< Write data.
92
93 private:
94 Writer(const Writer &other);
95 Writer &operator=(const Writer &other);
96
97 enum State
98 {
99 STATE_DATA = 0,
100 STATE_ELEMENT,
101 STATE_ELEMENT_END,
102
103 STATE_LAST
104 };
105
106 std::ostream &m_rawDst;
107 EscapeStreambuf m_dataBuf;
108 std::ostream m_dataStr;
109 State m_state;
110 std::vector<std::string> m_elementStack;
111 };
112
113 template <typename T>
operator <<(const T & value)114 Writer &Writer::operator<<(const T &value)
115 {
116 if (m_state == STATE_ELEMENT)
117 m_rawDst << ">";
118
119 m_dataStr << value;
120 m_state = STATE_DATA;
121
122 return *this;
123 }
124
125 } // namespace xml
126 } // namespace xe
127
128 #endif // _XEXMLWRITER_HPP
129