• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 protected:
44 	std::streamsize		xsputn			(const char* s, std::streamsize count);
45 	int					overflow		(int ch = -1);
46 
47 private:
48 	std::ostream&		m_dst;
49 };
50 
51 class Writer
52 {
53 public:
54 	struct BeginElement
55 	{
56 		std::string element;
BeginElementxe::xml::Writer::BeginElement57 		BeginElement (const char* element_) : element(element_) {}
58 	};
59 
60 	struct Attribute
61 	{
62 		std::string name;
63 		std::string value;
Attributexe::xml::Writer::Attribute64 		Attribute (const char* name_, const char* value_) : name(name_), value(value_) {}
Attributexe::xml::Writer::Attribute65 		Attribute (const char* name_, const std::string& value_) : name(name_), value(value_) {}
Attributexe::xml::Writer::Attribute66 		Attribute (const std::string& name_, const std::string& value_) : name(name_), value(value_) {}
67 	};
68 
69 	static const struct EndElementType {} EndElement;
70 
71 								Writer			(std::ostream& dst);
72 								~Writer			(void);
73 
74 	Writer&						operator<<		(const BeginElement& begin);
75 	Writer&						operator<<		(const Attribute& attribute);
76 	Writer&						operator<<		(const EndElementType& end);
77 
78 	template <typename T>
79 	Writer&						operator<<		(const T& value);	//!< Write data.
80 
81 private:
82 								Writer			(const Writer& other);
83 	Writer&						operator=		(const Writer& other);
84 
85 	enum State
86 	{
87 		STATE_DATA		= 0,
88 		STATE_ELEMENT,
89 		STATE_ELEMENT_END,
90 
91 		STATE_LAST
92 	};
93 
94 	std::ostream&				m_rawDst;
95 	EscapeStreambuf				m_dataBuf;
96 	std::ostream				m_dataStr;
97 	State						m_state;
98 	std::vector<std::string>	m_elementStack;
99 };
100 
101 template <typename T>
operator <<(const T & value)102 Writer& Writer::operator<< (const T& value)
103 {
104 	if (m_state == STATE_ELEMENT)
105 		m_rawDst << ">";
106 
107 	m_dataStr << value;
108 	m_state = STATE_DATA;
109 
110 	return *this;
111 }
112 
113 } // xml
114 } // xe
115 
116 #endif // _XEXMLWRITER_HPP
117