13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Test Executor
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief XML Writer.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeXMLWriter.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstring>
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace xe
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace xml
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst Writer::EndElementType Writer::EndElement = Writer::EndElementType();
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline const char* getEscapeEntity (char ch)
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	switch (ch)
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case '<':	return "&lt;";
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case '>':	return "&gt;";
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case '&':	return "&amp;";
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case '\'':	return "&apos;";
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case '"':	return "&quot;";
44368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry
45368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		// Non-printable characters.
46368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 0:		return "&lt;NUL&gt;";
47368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 1:		return "&lt;SOH&gt;";
48368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 2:		return "&lt;STX&gt;";
49368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 3:		return "&lt;ETX&gt;";
50368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 4:		return "&lt;EOT&gt;";
51368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 5:		return "&lt;ENQ&gt;";
52368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 6:		return "&lt;ACK&gt;";
53368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 7:		return "&lt;BEL&gt;";
54368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 8:		return "&lt;BS&gt;";
55368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 11:	return "&lt;VT&gt;";
56368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 12:	return "&lt;FF&gt;";
57368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 14:	return "&lt;SO&gt;";
58368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 15:	return "&lt;SI&gt;";
59368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 16:	return "&lt;DLE&gt;";
60368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 17:	return "&lt;DC1&gt;";
61368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 18:	return "&lt;DC2&gt;";
62368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 19:	return "&lt;DC3&gt;";
63368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 20:	return "&lt;DC4&gt;";
64368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 21:	return "&lt;NAK&gt;";
65368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 22:	return "&lt;SYN&gt;";
66368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 23:	return "&lt;ETB&gt;";
67368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 24:	return "&lt;CAN&gt;";
68368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 25:	return "&lt;EM&gt;";
69368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 26:	return "&lt;SUB&gt;";
70368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 27:	return "&lt;ESC&gt;";
71368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 28:	return "&lt;FS&gt;";
72368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 29:	return "&lt;GS&gt;";
73368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 30:	return "&lt;RS&gt;";
74368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry		case 31:	return "&lt;US&gt;";
75368115f54422a1007cfe1367896c181fb89833dfJarkko Pöyry
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		default:	return DE_NULL;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::streamsize EscapeStreambuf::xsputn (const char* s, std::streamsize count)
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::streamsize	numWritten = 0;
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (std::streamsize inPos = 0; inPos < count; inPos++)
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char* entity = getEscapeEntity(s[inPos]);
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (entity)
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Flush data prior to entity.
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (inPos > numWritten)
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				m_dst.write(s + numWritten, inPos-numWritten);
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (m_dst.fail())
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					return numWritten;
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Flush entity value
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_dst.write(entity, (std::streamsize)strlen(entity));
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			numWritten = inPos+1;
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (numWritten < count)
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_dst.write(s + numWritten, count-numWritten);
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_dst.fail())
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return numWritten;
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return count;
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint EscapeStreambuf::overflow (int ch)
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (ch == -1)
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return -1;
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT((ch & 0xff) == ch);
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char chVal = (char)(deUint8)(ch & 0xff);
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return xsputn(&chVal, 1) == 1 ? ch : -1;
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1273c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter::Writer (std::ostream& dst)
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_rawDst	(dst)
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_dataBuf	(dst)
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_dataStr	(&m_dataBuf)
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_state	(STATE_DATA)
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1353c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter::~Writer (void)
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter& Writer::operator<< (const BeginElement& begin)
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_state == STATE_ELEMENT)
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_rawDst << ">";
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_state == STATE_ELEMENT || m_state == STATE_ELEMENT_END)
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_rawDst << "\n";
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int i = 0; i < (int)m_elementStack.size(); i++)
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_rawDst << "  ";
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_rawDst << "<" << begin.element;
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_elementStack.push_back(begin.element);
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_state = STATE_ELEMENT;
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter& Writer::operator<< (const Attribute& attribute)
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(m_state == STATE_ELEMENT);
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2012-09-05 pyry] Escape?
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_rawDst << " " << attribute.name << "=\"" << attribute.value << "\"";
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1693c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWriter& Writer::operator<< (const EndElementType&)
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_state == STATE_ELEMENT)
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_rawDst << "/>";
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_state == STATE_ELEMENT_END)
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_rawDst << "\n";
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int i = 0; i < (int)m_elementStack.size()-1; i++)
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				m_rawDst << "  ";
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_rawDst << "</" << m_elementStack.back() << ">";
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_elementStack.pop_back();
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_state = STATE_ELEMENT_END;
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // xml
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // xe
193