summaryrefslogtreecommitdiff
path: root/libjingle/xmllite/xmlprinter.cc
blob: 27d7cc0bfe514af73cab822b73609bf89470c896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/libjingle/xmllite/xmlprinter.h"

#include <sstream>
#include <string>
#include <vector>

#include "webrtc/libjingle/xmllite/xmlconstants.h"
#include "webrtc/libjingle/xmllite/xmlelement.h"
#include "webrtc/libjingle/xmllite/xmlnsstack.h"

namespace buzz {

class XmlPrinterImpl {
public:
  XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack);
  void PrintElement(const XmlElement* element);
  void PrintQuotedValue(const std::string& text);
  void PrintBodyText(const std::string& text);
  void PrintCDATAText(const std::string& text);

private:
  std::ostream *pout_;
  XmlnsStack* ns_stack_;
};

void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element) {
  XmlnsStack ns_stack;
  PrintXml(pout, element, &ns_stack);
}

void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element,
                          XmlnsStack* ns_stack) {
  XmlPrinterImpl printer(pout, ns_stack);
  printer.PrintElement(element);
}

XmlPrinterImpl::XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack)
    : pout_(pout),
      ns_stack_(ns_stack) {
}

void XmlPrinterImpl::PrintElement(const XmlElement* element) {
  ns_stack_->PushFrame();

  // first go through attrs of pel to add xmlns definitions
  const XmlAttr* attr;
  for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
    if (attr->Name() == QN_XMLNS) {
      ns_stack_->AddXmlns(STR_EMPTY, attr->Value());
    } else if (attr->Name().Namespace() == NS_XMLNS) {
      ns_stack_->AddXmlns(attr->Name().LocalPart(),
                          attr->Value());
    }
  }

  // then go through qnames to make sure needed xmlns definitons are added
  std::vector<std::string> new_ns;
  std::pair<std::string, bool> prefix;
  prefix = ns_stack_->AddNewPrefix(element->Name().Namespace(), false);
  if (prefix.second) {
    new_ns.push_back(prefix.first);
    new_ns.push_back(element->Name().Namespace());
  }

  for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
    prefix = ns_stack_->AddNewPrefix(attr->Name().Namespace(), true);
    if (prefix.second) {
      new_ns.push_back(prefix.first);
      new_ns.push_back(attr->Name().Namespace());
    }
  }

  // print the element name
  *pout_ << '<' << ns_stack_->FormatQName(element->Name(), false);

  // and the attributes
  for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
    *pout_ << ' ' << ns_stack_->FormatQName(attr->Name(), true) << "=\"";
    PrintQuotedValue(attr->Value());
    *pout_ << '"';
  }

  // and the extra xmlns declarations
  std::vector<std::string>::iterator i(new_ns.begin());
  while (i < new_ns.end()) {
    if (*i == STR_EMPTY) {
      *pout_ << " xmlns=\"" << *(i + 1) << '"';
    } else {
      *pout_ << " xmlns:" << *i << "=\"" << *(i + 1) << '"';
    }
    i += 2;
  }

  // now the children
  const XmlChild* child = element->FirstChild();

  if (child == NULL)
    *pout_ << "/>";
  else {
    *pout_ << '>';
    while (child) {
      if (child->IsText()) {
        if (element->IsCDATA()) {
          PrintCDATAText(child->AsText()->Text());
        } else {
          PrintBodyText(child->AsText()->Text());
        }
      } else {
        PrintElement(child->AsElement());
      }
      child = child->NextChild();
    }
    *pout_ << "</" << ns_stack_->FormatQName(element->Name(), false) << '>';
  }

  ns_stack_->PopFrame();
}

void XmlPrinterImpl::PrintQuotedValue(const std::string& text) {
  size_t safe = 0;
  for (;;) {
    size_t unsafe = text.find_first_of("<>&\"", safe);
    if (unsafe == std::string::npos)
      unsafe = text.length();
    *pout_ << text.substr(safe, unsafe - safe);
    if (unsafe == text.length())
      return;
    switch (text[unsafe]) {
      case '<': *pout_ << "&lt;"; break;
      case '>': *pout_ << "&gt;"; break;
      case '&': *pout_ << "&amp;"; break;
      case '"': *pout_ << "&quot;"; break;
    }
    safe = unsafe + 1;
    if (safe == text.length())
      return;
  }
}

void XmlPrinterImpl::PrintBodyText(const std::string& text) {
  size_t safe = 0;
  for (;;) {
    size_t unsafe = text.find_first_of("<>&", safe);
    if (unsafe == std::string::npos)
      unsafe = text.length();
    *pout_ << text.substr(safe, unsafe - safe);
    if (unsafe == text.length())
      return;
    switch (text[unsafe]) {
      case '<': *pout_ << "&lt;"; break;
      case '>': *pout_ << "&gt;"; break;
      case '&': *pout_ << "&amp;"; break;
    }
    safe = unsafe + 1;
    if (safe == text.length())
      return;
  }
}

void XmlPrinterImpl::PrintCDATAText(const std::string& text) {
  *pout_ << "<![CDATA[" << text << "]]>";
}

}  // namespace buzz