13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _XEXMLWRITER_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _XEXMLWRITER_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Test Executor
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief XML Writer.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <ostream>
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <streambuf>
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace xe
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace xml
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass EscapeStreambuf : public std::streambuf
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						EscapeStreambuf	(std::ostream& dst) : m_dst(dst) {}
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprotected:
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::streamsize		xsputn			(const char* s, std::streamsize count);
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int					overflow		(int ch = -1);
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream&		m_dst;
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Writer
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	struct BeginElement
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		std::string element;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		BeginElement (const char* element_) : element(element_) {}
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	struct Attribute
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		std::string name;
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		std::string value;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Attribute (const char* name_, const char* value_) : name(name_), value(value_) {}
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Attribute (const char* name_, const std::string& value_) : name(name_), value(value_) {}
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Attribute (const std::string& name_, const std::string& value_) : name(name_), value(value_) {}
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static const struct EndElementType {} EndElement;
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								Writer			(std::ostream& dst);
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								~Writer			(void);
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Writer&						operator<<		(const BeginElement& begin);
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Writer&						operator<<		(const Attribute& attribute);
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Writer&						operator<<		(const EndElementType& end);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T>
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Writer&						operator<<		(const T& value);	//!< Write data.
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								Writer			(const Writer& other);
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Writer&						operator=		(const Writer& other);
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	enum State
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		STATE_DATA		= 0,
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		STATE_ELEMENT,
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		STATE_ELEMENT_END,
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		STATE_LAST
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream&				m_rawDst;
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	EscapeStreambuf				m_dataBuf;
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream				m_dataStr;
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	State						m_state;
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::vector<std::string>	m_elementStack;
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
1023c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter& Writer::operator<< (const T& value)
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_state == STATE_ELEMENT)
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_rawDst << ">";
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_dataStr << value;
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_state = STATE_DATA;
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // xml
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // xe
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _XEXMLWRITER_HPP
117