3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/xmllite/xmlprinter.h"
34
#include "talk/xmllite/xmlconstants.h"
35
#include "talk/xmllite/xmlelement.h"
36
#include "talk/xmllite/xmlnsstack.h"
40
class XmlPrinterImpl {
42
XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack);
43
void PrintElement(const XmlElement* element);
44
void PrintQuotedValue(const std::string& text);
45
void PrintBodyText(const std::string& text);
46
void PrintCDATAText(const std::string& text);
50
XmlnsStack* ns_stack_;
53
void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element) {
55
PrintXml(pout, element, &ns_stack);
58
void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element,
59
XmlnsStack* ns_stack) {
60
XmlPrinterImpl printer(pout, ns_stack);
61
printer.PrintElement(element);
64
XmlPrinterImpl::XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack)
69
void XmlPrinterImpl::PrintElement(const XmlElement* element) {
70
ns_stack_->PushFrame();
72
// first go through attrs of pel to add xmlns definitions
74
for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
75
if (attr->Name() == QN_XMLNS) {
76
ns_stack_->AddXmlns(STR_EMPTY, attr->Value());
77
} else if (attr->Name().Namespace() == NS_XMLNS) {
78
ns_stack_->AddXmlns(attr->Name().LocalPart(),
83
// then go through qnames to make sure needed xmlns definitons are added
84
std::vector<std::string> new_ns;
85
std::pair<std::string, bool> prefix;
86
prefix = ns_stack_->AddNewPrefix(element->Name().Namespace(), false);
88
new_ns.push_back(prefix.first);
89
new_ns.push_back(element->Name().Namespace());
92
for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
93
prefix = ns_stack_->AddNewPrefix(attr->Name().Namespace(), true);
95
new_ns.push_back(prefix.first);
96
new_ns.push_back(attr->Name().Namespace());
100
// print the element name
101
*pout_ << '<' << ns_stack_->FormatQName(element->Name(), false);
103
// and the attributes
104
for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
105
*pout_ << ' ' << ns_stack_->FormatQName(attr->Name(), true) << "=\"";
106
PrintQuotedValue(attr->Value());
110
// and the extra xmlns declarations
111
std::vector<std::string>::iterator i(new_ns.begin());
112
while (i < new_ns.end()) {
113
if (*i == STR_EMPTY) {
114
*pout_ << " xmlns=\"" << *(i + 1) << '"';
116
*pout_ << " xmlns:" << *i << "=\"" << *(i + 1) << '"';
122
const XmlChild* child = element->FirstChild();
129
if (child->IsText()) {
130
if (element->IsCDATA()) {
131
PrintCDATAText(child->AsText()->Text());
133
PrintBodyText(child->AsText()->Text());
136
PrintElement(child->AsElement());
138
child = child->NextChild();
140
*pout_ << "</" << ns_stack_->FormatQName(element->Name(), false) << '>';
143
ns_stack_->PopFrame();
146
void XmlPrinterImpl::PrintQuotedValue(const std::string& text) {
149
size_t unsafe = text.find_first_of("<>&\"", safe);
150
if (unsafe == std::string::npos)
151
unsafe = text.length();
152
*pout_ << text.substr(safe, unsafe - safe);
153
if (unsafe == text.length())
155
switch (text[unsafe]) {
156
case '<': *pout_ << "<"; break;
157
case '>': *pout_ << ">"; break;
158
case '&': *pout_ << "&"; break;
159
case '"': *pout_ << """; break;
162
if (safe == text.length())
167
void XmlPrinterImpl::PrintBodyText(const std::string& text) {
170
size_t unsafe = text.find_first_of("<>&", safe);
171
if (unsafe == std::string::npos)
172
unsafe = text.length();
173
*pout_ << text.substr(safe, unsafe - safe);
174
if (unsafe == text.length())
176
switch (text[unsafe]) {
177
case '<': *pout_ << "<"; break;
178
case '>': *pout_ << ">"; break;
179
case '&': *pout_ << "&"; break;
182
if (safe == text.length())
187
void XmlPrinterImpl::PrintCDATAText(const std::string& text) {
188
*pout_ << "<![CDATA[" << text << "]]>";