36 #include "XmlTools.hpp"
40 #include <xercesc/dom/DOM.hpp>
41 #include <xercesc/util/PlatformUtils.hpp>
42 #include <xercesc/util/QName.hpp>
43 #include <xercesc/util/XMLUniDefs.hpp>
44 #include <xercesc/framework/Wrapper4InputSource.hpp>
45 #include <xercesc/validators/common/Grammar.hpp>
47 #include <xsd/cxx/xml/sax/std-input-source.hxx>
48 #include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>
49 #include <xsd/cxx/tree/exceptions.hxx>
56 const std::string& rFileName,
57 const ::xsd::cxx::tree::properties<char>& rProps,
60 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument> p_doc;
64 xercesc::XMLPlatformUtils::Initialize();
66 ::xsd::cxx::tree::error_handler<char> error_handler;
70 error_handler.throw_if_failed< ::xsd::cxx::tree::parsing<char> >();
72 catch (const ::xsd::cxx::tree::parsing<char>& e)
76 #if (XSD_INT_VERSION >= 3000000L)
77 const ::xsd::cxx::tree::diagnostics<char>& diags = e.diagnostics();
78 const ::xsd::cxx::tree::error<char>& first_error = diags[0];
80 const ::xsd::cxx::tree::errors<char>& errors = e.errors();
81 const ::xsd::cxx::tree::error<char>& first_error = errors[0];
83 if (first_error.line() == 0u)
85 std::cerr << first_error << std::endl;
86 EXCEPTION(
"Missing file parsing configuration file: " + rFileName);
90 std::cerr << e << std::endl;
91 EXCEPTION(
"XML parsing error in configuration file: " + rFileName);
94 #define COVERAGE_IGNORE
100 #undef COVERAGE_IGNORE
107 xercesc::XMLPlatformUtils::Terminate();
116 #define COVERAGE_IGNORE
117 xercesc::XMLPlatformUtils::Initialize();
118 #undef COVERAGE_IGNORE
128 const std::string& rFileName,
129 ::xsd::cxx::xml::error_handler<char>& rErrorHandler,
130 const ::xsd::cxx::tree::properties<char>& rProps,
133 using namespace xercesc;
134 namespace xml = xsd::cxx::xml;
137 const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
138 DOMImplementation* p_impl(DOMImplementationRegistry::getDOMImplementation(ls_id));
140 #if _XERCES_VERSION >= 30000
142 xml::dom::auto_ptr<DOMLSParser> p_parser(p_impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
143 DOMConfiguration* p_conf(p_parser->getDomConfig());
146 p_conf->setParameter(XMLUni::fgDOMComments,
false);
149 p_conf->setParameter(XMLUni::fgDOMDatatypeNormalization,
true);
155 p_conf->setParameter(XMLUni::fgDOMEntities,
false);
158 p_conf->setParameter(XMLUni::fgDOMNamespaces,
true);
161 p_conf->setParameter(XMLUni::fgDOMElementContentWhitespace,
false);
166 p_conf->setParameter(XMLUni::fgDOMValidate,
true);
167 p_conf->setParameter(XMLUni::fgXercesSchema,
true);
168 p_conf->setParameter(XMLUni::fgXercesSchemaFullChecking,
false);
170 if (!rProps.schema_location().empty())
172 xml::string locn(rProps.schema_location());
173 const void* p_locn(locn.c_str());
174 p_conf->setParameter(XMLUni::fgXercesSchemaExternalSchemaLocation,
175 const_cast<void*>(p_locn));
177 if (!rProps.no_namespace_schema_location().empty())
179 xml::string locn(rProps.no_namespace_schema_location());
180 const void* p_locn(locn.c_str());
182 p_conf->setParameter(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
183 const_cast<void*>(p_locn));
189 #define COVERAGE_IGNORE
190 p_conf->setParameter(XMLUni::fgDOMValidate,
false);
191 p_conf->setParameter(XMLUni::fgXercesSchema,
false);
192 p_conf->setParameter(XMLUni::fgXercesSchemaFullChecking,
false);
193 #undef COVERAGE_IGNORE
197 p_conf->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument,
true);
200 xml::dom::bits::error_handler_proxy<char> ehp(rErrorHandler);
201 p_conf->setParameter(XMLUni::fgDOMErrorHandler, &ehp);
203 #else // _XERCES_VERSION >= 30000
205 xml::dom::auto_ptr<DOMBuilder> p_parser(p_impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
207 p_parser->setFeature(XMLUni::fgDOMComments,
false);
208 p_parser->setFeature(XMLUni::fgDOMDatatypeNormalization,
true);
209 p_parser->setFeature(XMLUni::fgDOMEntities,
false);
210 p_parser->setFeature(XMLUni::fgDOMNamespaces,
true);
211 p_parser->setFeature(XMLUni::fgDOMWhitespaceInElementContent,
false);
212 p_parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,
true);
217 p_parser->setFeature(XMLUni::fgDOMValidation,
true);
218 p_parser->setFeature(XMLUni::fgXercesSchema,
true);
219 p_parser->setFeature(XMLUni::fgXercesSchemaFullChecking,
false);
220 if (!rProps.schema_location().empty())
222 xml::string locn(rProps.schema_location());
223 const void* p_locn(locn.c_str());
224 p_parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,
225 const_cast<void*>(p_locn));
228 if (!rProps.no_namespace_schema_location().empty())
230 xml::string locn(rProps.no_namespace_schema_location());
231 const void* p_locn(locn.c_str());
233 p_parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
234 const_cast<void*>(p_locn));
240 #define COVERAGE_IGNORE
241 p_parser->setFeature(XMLUni::fgDOMValidation,
false);
242 p_parser->setFeature(XMLUni::fgXercesSchema,
false);
243 p_parser->setFeature(XMLUni::fgXercesSchemaFullChecking,
false);
244 #undef COVERAGE_IGNORE
247 xml::dom::bits::error_handler_proxy<char> ehp(rErrorHandler);
248 p_parser->setErrorHandler(&ehp);
250 #endif // _XERCES_VERSION >= 30000
253 xml::dom::auto_ptr<DOMDocument> p_doc(p_parser->parseURI(rFileName.c_str()));
263 #define COVERAGE_IGNORE
266 std::string prefix = X2C(pNode->getPrefix());
267 std::string name = X2C(pNode->getLocalName());
268 std::string nsuri = X2C(pNode->getNamespaceURI());
269 std::cout << rMsg <<
" " << pNode <<
" " << prefix <<
":" << name <<
" in " << nsuri << std::endl;
272 for (xercesc::DOMNode* p_node = pNode->getFirstChild();
274 p_node = p_node->getNextSibling())
276 std::cout <<
" child type " << p_node->getNodeType();
279 xercesc::DOMNamedNodeMap* p_attrs = pNode->getAttributes();
282 for (XMLSize_t i=0; i<p_attrs->getLength(); i++)
284 xercesc::DOMNode* p_attr = p_attrs->item(i);
285 std::string value = X2C(p_attr->getNodeValue());
286 PrintNode(
" attr (" + value +
")", p_attr,
false);
291 #undef COVERAGE_IGNORE
294 xercesc::DOMElement* pElement,
295 const XMLCh* pNamespace)
297 using namespace xercesc;
300 DOMNamedNodeMap* p_orig_attrs = pElement->getAttributes();
301 std::vector<std::string> attr_values;
304 for (XMLSize_t i=0; i<p_orig_attrs->getLength(); i++)
306 DOMNode* p_attr = p_orig_attrs->item(i);
307 attr_values.push_back(X2C(p_attr->getNodeValue()));
310 DOMElement* p_new_elt =
static_cast<DOMElement*
>(
311 pDocument->renameNode(pElement, pNamespace, pElement->getLocalName()));
316 DOMNamedNodeMap* p_new_attrs = p_new_elt->getAttributes();
318 assert(p_new_attrs == p_orig_attrs);
319 assert(p_new_attrs->getLength() == attr_values.size());
320 for (XMLSize_t i=0; i<p_new_attrs->getLength(); i++)
322 DOMNode* p_attr = p_new_attrs->item(i);
323 p_attr->setNodeValue(X(attr_values[i]));
329 for (std::vector<DOMElement*>::iterator it = children.begin(); it != children.end(); ++it)
338 xercesc::DOMElement* pElement,
339 const std::string& rNamespace)
341 return SetNamespace(pDocument, pElement, X(rNamespace));
347 std::vector<xercesc::DOMElement*> children;
348 for (xercesc::DOMNode* p_node = pElement->getFirstChild();
350 p_node = p_node->getNextSibling())
352 if (p_node->getNodeType() == xercesc::DOMNode::ELEMENT_NODE)
354 children.push_back(static_cast<xercesc::DOMElement*>(p_node));
362 const std::vector<std::string>& rNames,
363 std::vector<xercesc::DOMElement*>& rResults,
366 for (xercesc::DOMNode* p_node = pContextElement->getFirstChild();
368 p_node = p_node->getNextSibling())
370 if (p_node->getNodeType() == xercesc::DOMNode::ELEMENT_NODE &&
371 X2C(p_node->getLocalName()) == rNames[depth])
373 xercesc::DOMElement* p_child_elt =
static_cast<xercesc::DOMElement*
>(p_node);
374 if (depth == rNames.size() - 1)
376 rResults.push_back(p_child_elt);
387 const std::string& rPath)
389 std::vector<xercesc::DOMElement*> results;
390 std::vector<std::string> path;
391 size_t start_pos = 0;
392 size_t slash_pos = 0;
393 while (slash_pos != std::string::npos)
395 slash_pos = rPath.find(
'/', start_pos);
396 if (slash_pos == std::string::npos)
398 path.push_back(rPath.substr(start_pos));
402 path.push_back(rPath.substr(start_pos, slash_pos-start_pos));
404 start_pos = slash_pos + 1;
412 xercesc::DOMElement* pElement,
413 const XMLCh* pNewElementLocalName)
415 const XMLCh* p_namespace_uri = pElement->getNamespaceURI();
416 const XMLCh* p_prefix = pElement->getPrefix();
417 const XMLCh* p_qualified_name;
420 #define COVERAGE_IGNORE
423 xercesc::QName qname(p_prefix, pNewElementLocalName, 0);
424 p_qualified_name = qname.getRawName();
425 #undef COVERAGE_IGNORE
429 p_qualified_name = pNewElementLocalName;
431 xercesc::DOMElement* p_wrapper_elt = pDocument->createElementNS(p_namespace_uri, p_qualified_name);
433 xercesc::DOMNodeList* p_children = pElement->getChildNodes();
434 for (
unsigned i=0; i<p_children->getLength(); i++)
436 xercesc::DOMNode* p_child = pElement->removeChild(p_children->item(i));
437 p_wrapper_elt->appendChild(p_child);
440 pElement->appendChild(p_wrapper_elt);
446 std::string escaped_path;
447 for (std::string::const_iterator it = rPath.begin(); it != rPath.end(); ++it)
451 escaped_path +=
"%20";
#define EXCEPTION(message)