summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/xml/dom/serialization-header.txx
blob: 14a6d952f457a3b4af7b1db6565254b6f6d7aabf (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
187
188
189
// file      : xsd/cxx/xml/dom/serialization-header.txx
// copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <vector>
#include <sstream>
#include <cstddef> // std::size_t

#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/dom/DOMAttr.hpp>
#include <xercesc/dom/DOMNamedNodeMap.hpp>

#include <xercesc/util/XMLUni.hpp>    // xercesc::fg*
#include <xercesc/util/XMLString.hpp>
#include <xercesc/validators/schema/SchemaSymbols.hpp>

#include <xsd/cxx/xml/string.hxx>
#include <xsd/cxx/xml/bits/literals.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace xml
    {
      namespace dom
      {
        //
        //
        template <typename C>
        std::basic_string<C>
        prefix (const C* ns, xercesc::DOMElement& e, const C* hint)
        {
          string xns (ns);
          const XMLCh* p (e.lookupPrefix (xns.c_str ()));

          if (p != 0)
            return transcode<C> (p);

          if (e.isDefaultNamespace (xns.c_str ()))
            return std::basic_string<C> ();

          // 'xml' prefix requires special handling and Xerces folks
          // refuse to handle this in DOM so I have to do it myself.
          //
          if (std::basic_string<C> (ns) == xml::bits::xml_namespace<C> ())
            return xml::bits::xml_prefix<C> ();

          // No prefix for this namespace. Will need to establish one.
          //
          std::basic_string<C> prefix;

          if (hint != 0 &&
              e.lookupNamespaceURI (xml::string (hint).c_str ()) == 0)
          {
            prefix = hint;
          }
          else
          {
            for (unsigned long n (1);; ++n)
            {
              // Make finding the first few prefixes fast.
              //
              switch (n)
              {
              case 1:
                {
                  prefix = xml::bits::first_prefix<C> ();
                  break;
                }
              case 2:
                {
                  prefix = xml::bits::second_prefix<C> ();
                  break;
                }
              case 3:
                {
                  prefix = xml::bits::third_prefix<C> ();
                  break;
                }
              case 4:
                {
                  prefix = xml::bits::fourth_prefix<C> ();
                  break;
                }
              case 5:
                {
                  prefix = xml::bits::fifth_prefix<C> ();
                  break;
                }
              default:
                {
                  std::basic_ostringstream<C> ostr;
                  ostr << C ('p') << n;
                  prefix = ostr.str ();
                  break;
                }
              }

              if (e.lookupNamespaceURI (xml::string (prefix).c_str ()) == 0)
                break;
            }
          }

          std::basic_string<C> name (xml::bits::xmlns_prefix<C> ());
          name += C(':');
          name += prefix;

          e.setAttributeNS (
            xercesc::XMLUni::fgXMLNSURIName,
            xml::string (name).c_str (),
            xns.c_str ());

          return prefix;
        }

        //
        //
        template <typename C>
        void
        clear (xercesc::DOMElement& e)
        {
          // Cannot use 'using namespace' because of MSXML conflict.
          //
          using xercesc::XMLUni;
          using xercesc::XMLString;
          using xercesc::SchemaSymbols;

          using xercesc::DOMNode;
          using xercesc::DOMAttr;
          using xercesc::DOMNamedNodeMap;

          // Remove child nodes.
          //
          while (DOMNode* n = e.getFirstChild ())
          {
            e.removeChild (n);
            n->release ();
          }

          // Remove attributes.
          //
          DOMNamedNodeMap* att_map (e.getAttributes ());
          XMLSize_t n (att_map->getLength ());

          if (n != 0)
          {
            std::vector<DOMAttr*> atts;

            // Collect all attributes to be removed while filtering
            // out special cases (xmlns & xsi).
            //
            for (XMLSize_t i (0); i != n; ++i)
            {
              DOMAttr* a (static_cast<DOMAttr*> (att_map->item (i)));
              const XMLCh* ns (a->getNamespaceURI ());

              if (ns != 0)
              {
                if (XMLString::equals (ns, XMLUni::fgXMLNSURIName))
                  continue;

                if (XMLString::equals (ns, SchemaSymbols::fgURI_XSI))
                {
                  const XMLCh* name (a->getLocalName ());

                  if (XMLString::equals (
                        name, SchemaSymbols::fgXSI_SCHEMALOCACTION) ||
                      XMLString::equals (
                        name, SchemaSymbols::fgXSI_NONAMESPACESCHEMALOCACTION))
                    continue;
                }
              }

              atts.push_back (a);
            }

            for (std::vector<DOMAttr*>::iterator i (atts.begin ()),
                   end (atts.end ()); i != end; ++i)
            {
              e.removeAttributeNode (*i);
              (*i)->release ();
            }
          }
        }
      }
    }
  }
}