aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/tree/streaming/serializer.cxx
blob: c879533d4cfab86fae423ab6b64d5369d1d483c4 (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
175
176
177
178
179
180
181
182
183
184
185
186
// file      : examples/cxx/tree/streaming/serializer.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <xercesc/util/XMLUni.hpp>

#include <xercesc/dom/DOM.hpp>

#include <xsd/cxx/xml/string.hxx>
#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>
#include <xsd/cxx/xml/dom/serialization-source.hxx>

#include <xsd/cxx/tree/exceptions.hxx>
#include <xsd/cxx/tree/error-handler.hxx>

#include "serializer.hxx"

using namespace std;
using namespace xercesc;

namespace xml = xsd::cxx::xml;
namespace tree = xsd::cxx::tree;

class serializer_impl
{
public:
  typedef serializer::namespace_infomap namespace_infomap;

  serializer_impl ();

  void
  start (ostream& os, const string& encoding);

  DOMElement*
  create (const string& name, const namespace_infomap&);

  DOMElement*
  create (const string& ns, const string& qname, const namespace_infomap&);

  void
  serialize (DOMElement& e);

private:
  void
  add_namespaces (xercesc::DOMElement*, const namespace_infomap&);

private:
  // Serializer.
  //
  xml::dom::auto_ptr<DOMLSOutput> out_;
  xml::dom::auto_ptr<DOMLSSerializer> serializer_;

  auto_ptr<xml::dom::ostream_format_target> oft_;

  tree::error_handler<char> error_handler_;
  xml::dom::bits::error_handler_proxy<char> error_proxy_;

  // DOM document that we use to create the elements.
  //
  DOMImplementation& dom_impl_;
  xml::dom::auto_ptr<DOMDocument> doc_;
};

const XMLCh ls[] = {chLatin_L, chLatin_S, chNull};

serializer_impl::
serializer_impl ()
    : error_proxy_ (error_handler_),
      dom_impl_ (*DOMImplementationRegistry::getDOMImplementation (ls)),
      doc_ (dom_impl_.createDocument ())
{
  serializer_.reset (dom_impl_.createLSSerializer ());
  DOMConfiguration* conf (serializer_->getDomConfig ());

  conf->setParameter (XMLUni::fgDOMErrorHandler, &error_proxy_);
  conf->setParameter (XMLUni::fgDOMWRTDiscardDefaultContent, true);
  conf->setParameter (XMLUni::fgDOMWRTFormatPrettyPrint, true);
  conf->setParameter (XMLUni::fgDOMXMLDeclaration, false);
}

void serializer_impl::
start (ostream& os, const string& encoding)
{
  error_handler_.reset ();
  oft_.reset (new xml::dom::ostream_format_target (os));

  out_.reset (dom_impl_.createLSOutput ());
  out_->setEncoding (xml::string (encoding).c_str ());
  out_->setByteStream (oft_.get ());
}

DOMElement* serializer_impl::
create (const string& name, const namespace_infomap& map)
{
  DOMElement* r (doc_->createElement (xml::string (name).c_str ()));

  if (!map.empty ())
    add_namespaces (r, map);

  return r;
}

DOMElement* serializer_impl::
create (const string& ns, const string& qname, const namespace_infomap& map)
{
  DOMElement* r (
    doc_->createElementNS (
      xml::string (ns).c_str (), xml::string (qname).c_str ()));

  if (!map.empty ())
    add_namespaces (r, map);

  return r;
}

void serializer_impl::
add_namespaces (DOMElement* e, const namespace_infomap& map)
{
  for (namespace_infomap::const_iterator i (map.begin ()), end (map.end ());
       i != end; ++i)
  {
    if (i->first.empty ())
    {
      // Empty prefix.
      //
      if (!i->second.name.empty ())
        e->setAttributeNS (
          XMLUni::fgXMLNSURIName,
          xml::string ("xmlns").c_str (),
          xml::string (i->second.name).c_str ());
    }
    else
    {
      e->setAttributeNS (
        XMLUni::fgXMLNSURIName,
        xml::string ("xmlns:" + i->first).c_str (),
        xml::string (i->second.name).c_str ());
    }
  }
}

void serializer_impl::
serialize (DOMElement& e)
{
  serializer_->write (&e, out_.get ());
  error_handler_.throw_if_failed<tree::serialization<char> > ();
}

//
// serializer
//

serializer::
~serializer ()
{
}

serializer::
serializer ()
    : impl_ (new serializer_impl)
{
}

void serializer::
start (ostream& os, const string& encoding)
{
  impl_->start (os, encoding);
}

DOMElement* serializer::
create (const string& name, const namespace_infomap& map)
{
  return impl_->create (name, map);
}

DOMElement* serializer::
create (const string& ns, const string& qname, const namespace_infomap& map)
{
  return impl_->create (ns, qname, map);
}

void serializer::
serialize (DOMElement& e)
{
  impl_->serialize (e);
}