From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/xml/dom/serialization-header.txx | 192 ++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 libxsd/xsd/cxx/xml/dom/serialization-header.txx (limited to 'libxsd/xsd/cxx/xml/dom/serialization-header.txx') diff --git a/libxsd/xsd/cxx/xml/dom/serialization-header.txx b/libxsd/xsd/cxx/xml/dom/serialization-header.txx new file mode 100644 index 0000000..76d3d43 --- /dev/null +++ b/libxsd/xsd/cxx/xml/dom/serialization-header.txx @@ -0,0 +1,192 @@ +// file : xsd/cxx/xml/dom/serialization-header.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 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); + +#if _XERCES_VERSION >= 30000 + const XMLCh* p (e.lookupPrefix (xns.c_str ())); +#else + const XMLCh* p (e.lookupNamespacePrefix (xns.c_str (), false)); +#endif + 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) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMNode; + using xercesc::DOMAttr; + using xercesc::DOMNamedNodeMap; + using xercesc::XMLString; + using xercesc::SchemaSymbols; + + // Remove child nodes. + // + while (xercesc::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, xercesc::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 (); + } + } + } + } + } + } +} -- cgit v1.1