1 | // file : xsd/cxx/xml/dom/serialization-header.txx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #include <vector> |
---|
7 | |
---|
8 | #include <xercesc/dom/DOMNode.hpp> |
---|
9 | #include <xercesc/dom/DOMAttr.hpp> |
---|
10 | #include <xercesc/dom/DOMNamedNodeMap.hpp> |
---|
11 | |
---|
12 | #include <xercesc/util/XMLUni.hpp> |
---|
13 | #include <xercesc/util/XMLString.hpp> |
---|
14 | #include <xercesc/validators/schema/SchemaSymbols.hpp> |
---|
15 | |
---|
16 | #include <xsd/cxx/xml/string.hxx> |
---|
17 | #include <xsd/cxx/xml/bits/literals.hxx> |
---|
18 | |
---|
19 | namespace xsd |
---|
20 | { |
---|
21 | namespace cxx |
---|
22 | { |
---|
23 | namespace xml |
---|
24 | { |
---|
25 | namespace dom |
---|
26 | { |
---|
27 | // |
---|
28 | // |
---|
29 | template <typename C> |
---|
30 | std::basic_string<C> |
---|
31 | prefix (const C* ns, const xercesc::DOMElement& e) |
---|
32 | { |
---|
33 | string xns (ns); |
---|
34 | |
---|
35 | #if _XERCES_VERSION >= 30000 |
---|
36 | const XMLCh* p (e.lookupPrefix (xns.c_str ())); |
---|
37 | |
---|
38 | if (p == 0) |
---|
39 | { |
---|
40 | // 'xml' prefix requires special handling and Xerces folks |
---|
41 | // refuse to handle this in DOM so I have to do it myself. |
---|
42 | // |
---|
43 | if (std::basic_string<C> (ns) == xml::bits::xml_namespace<C> ()) |
---|
44 | return xml::bits::xml_prefix<C> (); |
---|
45 | |
---|
46 | throw no_prefix (); |
---|
47 | } |
---|
48 | #else |
---|
49 | const XMLCh* p (e.lookupNamespacePrefix (xns.c_str (), false)); |
---|
50 | |
---|
51 | if (p == 0) |
---|
52 | { |
---|
53 | if (e.isDefaultNamespace (xns.c_str ())) |
---|
54 | { |
---|
55 | return std::basic_string<C> (); |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | // 'xml' prefix requires special handling and Xerces folks |
---|
60 | // refuse to handle this in DOM so I have to do it myself. |
---|
61 | // |
---|
62 | if (std::basic_string<C> (ns) == xml::bits::xml_namespace<C> ()) |
---|
63 | return xml::bits::xml_prefix<C> (); |
---|
64 | |
---|
65 | throw no_prefix (); |
---|
66 | } |
---|
67 | } |
---|
68 | #endif |
---|
69 | return transcode<C> (p); |
---|
70 | } |
---|
71 | |
---|
72 | // |
---|
73 | // |
---|
74 | template <typename C> |
---|
75 | void |
---|
76 | clear (xercesc::DOMElement& e) |
---|
77 | { |
---|
78 | // HP aCC cannot handle using namespace xercesc; |
---|
79 | // |
---|
80 | using xercesc::DOMNode; |
---|
81 | using xercesc::DOMAttr; |
---|
82 | using xercesc::DOMNamedNodeMap; |
---|
83 | using xercesc::XMLString; |
---|
84 | using xercesc::SchemaSymbols; |
---|
85 | |
---|
86 | // Remove child nodes. |
---|
87 | // |
---|
88 | while (xercesc::DOMNode* n = e.getFirstChild ()) |
---|
89 | { |
---|
90 | e.removeChild (n); |
---|
91 | n->release (); |
---|
92 | } |
---|
93 | |
---|
94 | // Remove attributes. |
---|
95 | // |
---|
96 | DOMNamedNodeMap* att_map (e.getAttributes ()); |
---|
97 | XMLSize_t n (att_map->getLength ()); |
---|
98 | |
---|
99 | if (n != 0) |
---|
100 | { |
---|
101 | std::vector<DOMAttr*> atts; |
---|
102 | |
---|
103 | // Collect all attributes to be removed while filtering |
---|
104 | // out special cases (xmlns & xsi). |
---|
105 | // |
---|
106 | for (XMLSize_t i (0); i != n; ++i) |
---|
107 | { |
---|
108 | DOMAttr* a (static_cast<DOMAttr*> (att_map->item (i))); |
---|
109 | const XMLCh* ns (a->getNamespaceURI ()); |
---|
110 | |
---|
111 | if (ns != 0) |
---|
112 | { |
---|
113 | if (XMLString::equals (ns, xercesc::XMLUni::fgXMLNSURIName)) |
---|
114 | continue; |
---|
115 | |
---|
116 | if (XMLString::equals (ns, SchemaSymbols::fgURI_XSI)) |
---|
117 | { |
---|
118 | const XMLCh* name (a->getLocalName ()); |
---|
119 | |
---|
120 | if (XMLString::equals ( |
---|
121 | name, SchemaSymbols::fgXSI_SCHEMALOCACTION) || |
---|
122 | XMLString::equals ( |
---|
123 | name, SchemaSymbols::fgXSI_NONAMESPACESCHEMALOCACTION)) |
---|
124 | continue; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | atts.push_back (a); |
---|
129 | } |
---|
130 | |
---|
131 | for (std::vector<DOMAttr*>::iterator i (atts.begin ()), |
---|
132 | end (atts.end ()); i != end; ++i) |
---|
133 | { |
---|
134 | e.removeAttributeNode (*i); |
---|
135 | (*i)->release (); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|