// 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 #include #include // std::size_t #include #include #include #include // xercesc::fg* #include #include #include #include namespace xsd { namespace cxx { namespace xml { namespace dom { // // template std::basic_string 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 (p); if (e.isDefaultNamespace (xns.c_str ())) return std::basic_string (); // '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 (ns) == xml::bits::xml_namespace ()) return xml::bits::xml_prefix (); // No prefix for this namespace. Will need to establish one. // std::basic_string 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 (); break; } case 2: { prefix = xml::bits::second_prefix (); break; } case 3: { prefix = xml::bits::third_prefix (); break; } case 4: { prefix = xml::bits::fourth_prefix (); break; } case 5: { prefix = xml::bits::fifth_prefix (); break; } default: { std::basic_ostringstream ostr; ostr << C ('p') << n; prefix = ostr.str (); break; } } if (e.lookupNamespaceURI (xml::string (prefix).c_str ()) == 0) break; } } std::basic_string name (xml::bits::xmlns_prefix ()); name += C(':'); name += prefix; e.setAttributeNS ( xercesc::XMLUni::fgXMLNSURIName, xml::string (name).c_str (), xns.c_str ()); return prefix; } // // template 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 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 (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::iterator i (atts.begin ()), end (atts.end ()); i != end; ++i) { e.removeAttributeNode (*i); (*i)->release (); } } } } } } }