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>
54 const std::string& rFileName,
55 const ::xsd::cxx::tree::properties<char>& rProps,
58 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument> p_doc;
62 xercesc::XMLPlatformUtils::Initialize();
64 ::xsd::cxx::tree::error_handler<char> error_handler;
68 error_handler.throw_if_failed< ::xsd::cxx::tree::parsing<char> >();
70 catch (const ::xsd::cxx::tree::parsing<char>& e)
74 #if (XSD_INT_VERSION >= 3000000L)
75 const ::xsd::cxx::tree::diagnostics<char>& diags = e.diagnostics();
76 const ::xsd::cxx::tree::error<char>& first_error = diags[0];
78 const ::xsd::cxx::tree::errors<char>& errors = e.errors();
79 const ::xsd::cxx::tree::error<char>& first_error = errors[0];
81 if (first_error.line() == 0u)
83 std::cerr << first_error << std::endl;
84 EXCEPTION(
"Missing file parsing configuration file: " + rFileName);
88 std::cerr << e << std::endl;
89 EXCEPTION(
"XML parsing error in configuration file: " + rFileName);
105 xercesc::XMLPlatformUtils::Terminate();
115 xercesc::XMLPlatformUtils::Initialize();
126 const std::string& rFileName,
127 ::xsd::cxx::xml::error_handler<char>& rErrorHandler,
128 const ::xsd::cxx::tree::properties<char>& rProps,
131 using namespace xercesc;
132 namespace xml = xsd::cxx::xml;
135 const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
136 DOMImplementation* p_impl(DOMImplementationRegistry::getDOMImplementation(ls_id));
138 #if _XERCES_VERSION >= 30000
140 xml::dom::auto_ptr<DOMLSParser> p_parser(p_impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
141 DOMConfiguration* p_conf(p_parser->getDomConfig());
144 p_conf->setParameter(XMLUni::fgDOMComments,
false);
147 p_conf->setParameter(XMLUni::fgDOMDatatypeNormalization,
true);
153 p_conf->setParameter(XMLUni::fgDOMEntities,
false);
156 p_conf->setParameter(XMLUni::fgDOMNamespaces,
true);
159 p_conf->setParameter(XMLUni::fgDOMElementContentWhitespace,
false);
164 p_conf->setParameter(XMLUni::fgDOMValidate,
true);
165 p_conf->setParameter(XMLUni::fgXercesSchema,
true);
166 p_conf->setParameter(XMLUni::fgXercesSchemaFullChecking,
false);
168 if (!rProps.schema_location().empty())
170 xml::string locn(rProps.schema_location());
171 const void* p_locn(locn.c_str());
172 p_conf->setParameter(XMLUni::fgXercesSchemaExternalSchemaLocation,
173 const_cast<void*>(p_locn));
175 if (!rProps.no_namespace_schema_location().empty())
177 xml::string locn(rProps.no_namespace_schema_location());
178 const void* p_locn(locn.c_str());
180 p_conf->setParameter(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
181 const_cast<void*>(p_locn));
188 p_conf->setParameter(XMLUni::fgDOMValidate,
false);
189 p_conf->setParameter(XMLUni::fgXercesSchema,
false);
190 p_conf->setParameter(XMLUni::fgXercesSchemaFullChecking,
false);
195 p_conf->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument,
true);
198 xml::dom::bits::error_handler_proxy<char> ehp(rErrorHandler);
199 p_conf->setParameter(XMLUni::fgDOMErrorHandler, &ehp);
201 #else // _XERCES_VERSION >= 30000
203 xml::dom::auto_ptr<DOMBuilder> p_parser(p_impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
205 p_parser->setFeature(XMLUni::fgDOMComments,
false);
206 p_parser->setFeature(XMLUni::fgDOMDatatypeNormalization,
true);
207 p_parser->setFeature(XMLUni::fgDOMEntities,
false);
208 p_parser->setFeature(XMLUni::fgDOMNamespaces,
true);
209 p_parser->setFeature(XMLUni::fgDOMWhitespaceInElementContent,
false);
210 p_parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,
true);
215 p_parser->setFeature(XMLUni::fgDOMValidation,
true);
216 p_parser->setFeature(XMLUni::fgXercesSchema,
true);
217 p_parser->setFeature(XMLUni::fgXercesSchemaFullChecking,
false);
218 if (!rProps.schema_location().empty())
220 xml::string locn(rProps.schema_location());
221 const void* p_locn(locn.c_str());
222 p_parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,
223 const_cast<void*>(p_locn));
226 if (!rProps.no_namespace_schema_location().empty())
228 xml::string locn(rProps.no_namespace_schema_location());
229 const void* p_locn(locn.c_str());
231 p_parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
232 const_cast<void*>(p_locn));
239 p_parser->setFeature(XMLUni::fgDOMValidation,
false);
240 p_parser->setFeature(XMLUni::fgXercesSchema,
false);
241 p_parser->setFeature(XMLUni::fgXercesSchemaFullChecking,
false);
245 xml::dom::bits::error_handler_proxy<char> ehp(rErrorHandler);
246 p_parser->setErrorHandler(&ehp);
248 #endif // _XERCES_VERSION >= 30000
251 xml::dom::auto_ptr<DOMDocument> p_doc(p_parser->parseURI(rFileName.c_str()));
264 std::string prefix = X2C(pNode->getPrefix());
265 std::string name = X2C(pNode->getLocalName());
266 std::string nsuri = X2C(pNode->getNamespaceURI());
267 std::cout << rMsg <<
" " << pNode <<
" " << prefix <<
":" << name <<
" in " << nsuri << std::endl;
270 for (xercesc::DOMNode* p_node = pNode->getFirstChild();
272 p_node = p_node->getNextSibling())
274 std::cout <<
" child type " << p_node->getNodeType();
277 xercesc::DOMNamedNodeMap* p_attrs = pNode->getAttributes();
280 for (XMLSize_t i=0; i<p_attrs->getLength(); i++)
282 xercesc::DOMNode* p_attr = p_attrs->item(i);
283 std::string value = X2C(p_attr->getNodeValue());
284 PrintNode(
" attr (" + value +
")", p_attr,
false);
292 xercesc::DOMElement* pElement,
293 const XMLCh* pNamespace)
295 using namespace xercesc;
298 DOMNamedNodeMap* p_orig_attrs = pElement->getAttributes();
299 std::vector<std::string> attr_values;
302 for (XMLSize_t i=0; i<p_orig_attrs->getLength(); i++)
304 DOMNode* p_attr = p_orig_attrs->item(i);
305 attr_values.push_back(X2C(p_attr->getNodeValue()));
308 DOMElement* p_new_elt =
static_cast<DOMElement*
>(
309 pDocument->renameNode(pElement, pNamespace, pElement->getLocalName()));
314 DOMNamedNodeMap* p_new_attrs = p_new_elt->getAttributes();
316 assert(p_new_attrs == p_orig_attrs);
317 assert(p_new_attrs->getLength() == attr_values.size());
318 for (XMLSize_t i=0; i<p_new_attrs->getLength(); i++)
320 DOMNode* p_attr = p_new_attrs->item(i);
321 p_attr->setNodeValue(X(attr_values[i]));
327 for (std::vector<DOMElement*>::iterator it = children.begin(); it != children.end(); ++it)
336 xercesc::DOMElement* pElement,
337 const std::string& rNamespace)
339 return SetNamespace(pDocument, pElement, X(rNamespace));
345 std::vector<xercesc::DOMElement*> children;
346 for (xercesc::DOMNode* p_node = pElement->getFirstChild();
348 p_node = p_node->getNextSibling())
350 if (p_node->getNodeType() == xercesc::DOMNode::ELEMENT_NODE)
352 children.push_back(static_cast<xercesc::DOMElement*>(p_node));
360 const std::vector<std::string>& rNames,
361 std::vector<xercesc::DOMElement*>& rResults,
364 for (xercesc::DOMNode* p_node = pContextElement->getFirstChild();
366 p_node = p_node->getNextSibling())
368 if (p_node->getNodeType() == xercesc::DOMNode::ELEMENT_NODE &&
369 X2C(p_node->getLocalName()) == rNames[depth])
371 xercesc::DOMElement* p_child_elt =
static_cast<xercesc::DOMElement*
>(p_node);
372 if (depth == rNames.size() - 1)
374 rResults.push_back(p_child_elt);
385 const std::string& rPath)
387 std::vector<xercesc::DOMElement*> results;
388 std::vector<std::string> path;
389 size_t start_pos = 0;
390 size_t slash_pos = 0;
391 while (slash_pos != std::string::npos)
393 slash_pos = rPath.find(
'/', start_pos);
394 if (slash_pos == std::string::npos)
396 path.push_back(rPath.substr(start_pos));
400 path.push_back(rPath.substr(start_pos, slash_pos-start_pos));
402 start_pos = slash_pos + 1;
410 xercesc::DOMElement* pElement,
411 const XMLCh* pNewElementLocalName)
413 const XMLCh* p_namespace_uri = pElement->getNamespaceURI();
414 const XMLCh* p_prefix = pElement->getPrefix();
415 const XMLCh* p_qualified_name;
421 xercesc::QName qname(p_prefix, pNewElementLocalName, 0);
422 p_qualified_name = qname.getRawName();
427 p_qualified_name = pNewElementLocalName;
429 xercesc::DOMElement* p_wrapper_elt = pDocument->createElementNS(p_namespace_uri, p_qualified_name);
431 xercesc::DOMNodeList* p_children = pElement->getChildNodes();
432 for (
unsigned i=0; i<p_children->getLength(); i++)
434 xercesc::DOMNode* p_child = pElement->removeChild(p_children->item(i));
435 p_wrapper_elt->appendChild(p_child);
438 pElement->appendChild(p_wrapper_elt);
444 std::string escaped_path;
445 for (std::string::const_iterator it = rPath.begin(); it != rPath.end(); ++it)
449 escaped_path +=
"%20";
#define EXCEPTION(message)