1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 *//*--------------------------------------------------------------------*/
24 #include "xeXMLWriter.hpp"
33 const Writer::EndElementType Writer::EndElement = Writer::EndElementType();
35 inline const char* getEscapeEntity (char ch)
39 case '<': return "<";
40 case '>': return ">";
41 case '&': return "&";
42 case '\'': return "'";
43 case '"': return """;
44 default: return DE_NULL;
48 std::streamsize EscapeStreambuf::xsputn (const char* s, std::streamsize count)
50 std::streamsize numWritten = 0;
52 for (std::streamsize inPos = 0; inPos < count; inPos++)
54 const char* entity = getEscapeEntity(s[inPos]);
58 // Flush data prior to entity.
59 if (inPos > numWritten)
61 m_dst.write(s + numWritten, inPos-numWritten);
67 m_dst.write(entity, (std::streamsize)strlen(entity));
73 if (numWritten < count)
75 m_dst.write(s + numWritten, count-numWritten);
83 int EscapeStreambuf::overflow (int ch)
89 DE_ASSERT((ch & 0xff) == ch);
90 const char chVal = (char)(deUint8)(ch & 0xff);
91 return xsputn(&chVal, 1) == 1 ? ch : -1;
95 Writer::Writer (std::ostream& dst)
98 , m_dataStr (&m_dataBuf)
99 , m_state (STATE_DATA)
103 Writer::~Writer (void)
107 Writer& Writer::operator<< (const BeginElement& begin)
109 if (m_state == STATE_ELEMENT)
112 if (m_state == STATE_ELEMENT || m_state == STATE_ELEMENT_END)
115 for (int i = 0; i < (int)m_elementStack.size(); i++)
119 m_rawDst << "<" << begin.element;
121 m_elementStack.push_back(begin.element);
122 m_state = STATE_ELEMENT;
127 Writer& Writer::operator<< (const Attribute& attribute)
129 DE_ASSERT(m_state == STATE_ELEMENT);
131 // \todo [2012-09-05 pyry] Escape?
132 m_rawDst << " " << attribute.name << "=\"" << attribute.value << "\"";
137 Writer& Writer::operator<< (const EndElementType&)
139 if (m_state == STATE_ELEMENT)
143 if (m_state == STATE_ELEMENT_END)
146 for (int i = 0; i < (int)m_elementStack.size()-1; i++)
150 m_rawDst << "</" << m_elementStack.back() << ">";
153 m_elementStack.pop_back();
154 m_state = STATE_ELEMENT_END;