2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/libjingle/xmllite/xmlprinter.h"
17 #include "webrtc/libjingle/xmllite/xmlconstants.h"
18 #include "webrtc/libjingle/xmllite/xmlelement.h"
19 #include "webrtc/libjingle/xmllite/xmlnsstack.h"
23 class XmlPrinterImpl {
25 XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack);
26 void PrintElement(const XmlElement* element);
27 void PrintQuotedValue(const std::string& text);
28 void PrintBodyText(const std::string& text);
29 void PrintCDATAText(const std::string& text);
33 XmlnsStack* ns_stack_;
36 void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element) {
38 PrintXml(pout, element, &ns_stack);
41 void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element,
42 XmlnsStack* ns_stack) {
43 XmlPrinterImpl printer(pout, ns_stack);
44 printer.PrintElement(element);
47 XmlPrinterImpl::XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack)
52 void XmlPrinterImpl::PrintElement(const XmlElement* element) {
53 ns_stack_->PushFrame();
55 // first go through attrs of pel to add xmlns definitions
57 for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
58 if (attr->Name() == QN_XMLNS) {
59 ns_stack_->AddXmlns(STR_EMPTY, attr->Value());
60 } else if (attr->Name().Namespace() == NS_XMLNS) {
61 ns_stack_->AddXmlns(attr->Name().LocalPart(),
66 // then go through qnames to make sure needed xmlns definitons are added
67 std::vector<std::string> new_ns;
68 std::pair<std::string, bool> prefix;
69 prefix = ns_stack_->AddNewPrefix(element->Name().Namespace(), false);
71 new_ns.push_back(prefix.first);
72 new_ns.push_back(element->Name().Namespace());
75 for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
76 prefix = ns_stack_->AddNewPrefix(attr->Name().Namespace(), true);
78 new_ns.push_back(prefix.first);
79 new_ns.push_back(attr->Name().Namespace());
83 // print the element name
84 *pout_ << '<' << ns_stack_->FormatQName(element->Name(), false);
87 for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
88 *pout_ << ' ' << ns_stack_->FormatQName(attr->Name(), true) << "=\"";
89 PrintQuotedValue(attr->Value());
93 // and the extra xmlns declarations
94 std::vector<std::string>::iterator i(new_ns.begin());
95 while (i < new_ns.end()) {
96 if (*i == STR_EMPTY) {
97 *pout_ << " xmlns=\"" << *(i + 1) << '"';
99 *pout_ << " xmlns:" << *i << "=\"" << *(i + 1) << '"';
105 const XmlChild* child = element->FirstChild();
112 if (child->IsText()) {
113 if (element->IsCDATA()) {
114 PrintCDATAText(child->AsText()->Text());
116 PrintBodyText(child->AsText()->Text());
119 PrintElement(child->AsElement());
121 child = child->NextChild();
123 *pout_ << "</" << ns_stack_->FormatQName(element->Name(), false) << '>';
126 ns_stack_->PopFrame();
129 void XmlPrinterImpl::PrintQuotedValue(const std::string& text) {
132 size_t unsafe = text.find_first_of("<>&\"", safe);
133 if (unsafe == std::string::npos)
134 unsafe = text.length();
135 *pout_ << text.substr(safe, unsafe - safe);
136 if (unsafe == text.length())
138 switch (text[unsafe]) {
139 case '<': *pout_ << "<"; break;
140 case '>': *pout_ << ">"; break;
141 case '&': *pout_ << "&"; break;
142 case '"': *pout_ << """; break;
145 if (safe == text.length())
150 void XmlPrinterImpl::PrintBodyText(const std::string& text) {
153 size_t unsafe = text.find_first_of("<>&", safe);
154 if (unsafe == std::string::npos)
155 unsafe = text.length();
156 *pout_ << text.substr(safe, unsafe - safe);
157 if (unsafe == text.length())
159 switch (text[unsafe]) {
160 case '<': *pout_ << "<"; break;
161 case '>': *pout_ << ">"; break;
162 case '&': *pout_ << "&"; break;
165 if (safe == text.length())
170 void XmlPrinterImpl::PrintCDATAText(const std::string& text) {
171 *pout_ << "<![CDATA[" << text << "]]>";