DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
DocumentHandler.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <XML/Printout.h>
16 #include <XML/UriReader.h>
17 #include <XML/DocumentHandler.h>
18 
19 // C/C++ include files
20 #include <memory>
21 #include <iostream>
22 #include <stdexcept>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifndef _WIN32
26 #include <libgen.h>
27 #endif
28 #include <TSystem.h>
29 
30 using namespace dd4hep::xml;
31 
32 namespace {
33  std::string undressed_file_name(const std::string& fn) {
34  if ( !fn.empty() ) {
35  TString tfn(fn);
36  gSystem->ExpandPathName(tfn);
37  return std::string(tfn.Data());
38  }
39  return fn;
40  }
41  int s_minPrintLevel = dd4hep::INFO;
42 
43  std::string _clean_fname(const std::string& filepath) {
44  // This function seems to resolve environment variables inside the filepath string and return resolved string
45  std::string const& temp = getEnviron(filepath);
46  std::string temp2 = undressed_file_name( temp.empty() ? filepath : temp );
47  if ( strncmp(temp2.c_str(),"file:",5)==0 ) return temp2.substr(5);
48  return temp2;
49  }
50 
51 }
52 
53 #ifndef __TIXML__
54 #include <xercesc/framework/LocalFileFormatTarget.hpp>
55 #include <xercesc/framework/StdOutFormatTarget.hpp>
56 #include <xercesc/framework/MemBufFormatTarget.hpp>
57 #include <xercesc/framework/MemBufInputSource.hpp>
58 #include <xercesc/sax/SAXParseException.hpp>
59 #include <xercesc/sax/EntityResolver.hpp>
60 #include <xercesc/sax/InputSource.hpp>
61 #include <xercesc/parsers/XercesDOMParser.hpp>
62 #include <xercesc/util/XMLEntityResolver.hpp>
63 #include <xercesc/util/PlatformUtils.hpp>
64 #include <xercesc/util/XercesDefs.hpp>
65 #include <xercesc/util/XMLUni.hpp>
66 #include <xercesc/util/XMLURL.hpp>
67 #include <xercesc/util/XMLString.hpp>
68 #include <xercesc/dom/DOM.hpp>
69 #include <xercesc/sax/ErrorHandler.hpp>
70 
71 using namespace xercesc;
72 
74 namespace dd4hep {
75 
77  namespace xml {
78 
81  public:
84  }
87  printout(DEBUG,"DocumentErrorHandler","+++ Destructing the XercesC DOM-XML document error handler....");
88  }
90  void resetErrors() {
91  }
93  void warning(const SAXParseException& /* e */) {
94  }
96  void error(const SAXParseException& e);
98  void fatalError(const SAXParseException& e);
100  virtual bool handleError(const DOMError& domError);
101  };
102 
104  bool DocumentErrorHandler::handleError(const DOMError& domError) {
105  std::string err = "DOM UNKNOWN: ";
106  switch (domError.getSeverity()) {
107  case DOMError::DOM_SEVERITY_WARNING:
108  err = "DOM WARNING: ";
109  break;
110  case DOMError::DOM_SEVERITY_ERROR:
111  err = "DOM ERROR: ";
112  break;
113  case DOMError::DOM_SEVERITY_FATAL_ERROR:
114  err = "DOM FATAL: ";
115  break;
116  default:
117  return false;
118  }
119  printout(FATAL,"DocumentErrorHandler", "+++ %s %s: %s", err.c_str(),
120  _toString(domError.getType()).c_str(),_toString(domError.getMessage()).c_str());
121  DOMLocator* loc = domError.getLocation();
122  if (loc) {
123  printout(FATAL,"DocumentErrorHandler","+++ Location: Line:%d Column: %d",
124  int(loc->getLineNumber()),int(loc->getColumnNumber()));
125  }
126  return false;
127  }
129  void DocumentErrorHandler::error(const SAXParseException& e) {
130  std::string m(_toString(e.getMessage()));
131  if (m.find("The values for attribute 'name' must be names or name tokens") != std::string::npos
132  || m.find("The values for attribute 'ID' must be names or name tokens") != std::string::npos
133  || m.find("for attribute 'name' must be Name or Nmtoken") != std::string::npos
134  || m.find("for attribute 'ID' must be Name or Nmtoken") != std::string::npos
135  || m.find("for attribute 'name' is invalid Name or NMTOKEN value") != std::string::npos
136  || m.find("for attribute 'ID' is invalid Name or NMTOKEN value") != std::string::npos)
137  return;
138  std::string sys(_toString(e.getSystemId()));
139  printout(ERROR,"XercesC","+++ Error at file \"%s\", Line %d Column: %d Message:%s",
140  sys.c_str(), int(e.getLineNumber()), int(e.getColumnNumber()), m.c_str());
141  }
143  void DocumentErrorHandler::fatalError(const SAXParseException& e) {
144  std::string m(_toString(e.getMessage()));
145  std::string sys(_toString(e.getSystemId()));
146  printout(FATAL,"XercesC","+++ FATAL Error at file \"%s\", Line %d Column: %d Message:%s",
147  sys.c_str(), int(e.getLineNumber()), int(e.getColumnNumber()), m.c_str());
148  }
149 
150  namespace {
151 
153  class dd4hepDOMParser : public XercesDOMParser {
158  class Resolver : public XMLEntityResolver {
159  dd4hepDOMParser* parser;
160  public:
161  Resolver(dd4hepDOMParser* p) : parser(p) {}
162  virtual ~Resolver() {}
163  virtual InputSource *resolveEntity(XMLResourceIdentifier *id)
164  { return parser->read_uri(id); }
165  };
166  Resolver m_resolver;
167  public:
169  dd4hepDOMParser(UriReader* rdr) : XercesDOMParser(), m_reader(rdr), m_resolver(this) {
170  //printout(FATAL,"XercesC","+++ Creating new parser");
171  setErrorHandler(&m_errHandle_tr);
172  setXMLEntityResolver(&m_resolver);
173  }
175  virtual ~dd4hepDOMParser() {
176  //printout(FATAL,"XercesC","+++ Deleting new parser");
177  }
179  InputSource *read_uri(XMLResourceIdentifier *id) {
180  if ( m_reader ) {
181  std::string buf, systemID(_toString(id->getSystemId()));
182  if ( m_reader->load(systemID, buf) ) {
183  const XMLByte* input = (const XMLByte*)XMLString::replicate(buf.c_str());
184 #if 0
185  std::string baseURI(_toString(id->getBaseURI()));
186  std::string schema(_toString(id->getSchemaLocation()));
187  std::string ns(_toString(id->getNameSpace()));
188  if ( s_minPrintLevel <= INFO ) {
189  printout(INFO,"XercesC","+++ Resolved URI: sysID:%s uri:%s ns:%s schema:%s",
190  systemID.c_str(), baseURI.c_str(), ns.c_str(), schema.c_str());
191  }
192 #endif
193  return new MemBufInputSource(input,buf.length(),systemID.c_str(),true);
194  }
195  }
196  return 0;
197  }
198  };
199 
201  XercesDOMParser* make_parser(UriReader* reader=0) {
202  XercesDOMParser* parser = new dd4hepDOMParser(reader);
203  parser->setValidationScheme(XercesDOMParser::Val_Auto);
204  parser->setValidationSchemaFullChecking(true);
205  parser->setCreateEntityReferenceNodes(false);
206  parser->setDoNamespaces(false);
207  parser->setDoSchema(true);
208 
210  //domParser->setDoSchema( true );
212  //domParser->setValidationSchemaFullChecking( true );
213  return parser;
214  }
215  }
216 
218  void dumpTree(DOMNode* doc, std::ostream& os) {
219  if ( doc ) {
220  DOMImplementation *imp = DOMImplementationRegistry::getDOMImplementation(Strng_t("LS"));
221  MemBufFormatTarget *tar = new MemBufFormatTarget();
222  DOMLSOutput *out = imp->createLSOutput();
223  DOMLSSerializer *wrt = imp->createLSSerializer();
224  out->setByteStream(tar);
225  wrt->getDomConfig()->setParameter(Strng_t("format-pretty-print"), true);
226  wrt->write(doc, out);
227  os << tar->getRawBuffer() << std::endl << std::flush;
228  out->release();
229  wrt->release();
230  return;
231  }
232  printout(ERROR,"dumpTree","+++ Cannot dump invalid document.");
233  }
234 
236  void dump_doc(DOMDocument* doc, std::ostream& os) {
237  dumpTree(doc,os);
238  }
240  void dump_tree(Handle_t elt, std::ostream& os) {
241  dumpTree((DOMNode*)elt.ptr(),os);
242  }
244  void dump_tree(Document doc, std::ostream& os) {
245  dump_doc((DOMDocument*)doc.ptr(),os);
246  }
247  }
248 }
249 
250 #ifdef DD4HEP_NONE
251 std::string DocumentHandler::system_path(Handle_t base, const std::string& fn) {
253  std::string path = system_path(base);
254  std::string dir = ::dirname((char*)path.c_str());
255  return dir+fn;
256 }
257 #else
258 
259 #include <TUri.h>
260 #include <TUrl.h>
261 #endif
262 
264 std::string DocumentHandler::system_path(Handle_t base, const std::string& fn) {
265  std::string path, dir = system_path(base);
266  TUri uri_base(dir.c_str()), uri_rel(fn.c_str());
267  TUrl url_base(dir.c_str());
268  path = TUri::MergePaths(uri_rel,uri_base);
269  TUri final(path.c_str());
270  final.Normalise();
271  path = url_base.GetProtocol()+std::string("://")+final.GetUri().Data();
272  if ( path[path.length()-1]=='/' ) path = path.substr(0,path.length()-1);
273  return path;
274 }
275 
278  DOMElement* elt = (DOMElement*)base.ptr();
279  std::string path = _toString(elt->getBaseURI());
280  if ( path[0] == '/' ) {
281  std::string tmp = "file:"+path;
282  return tmp;
283  }
284  return path;
285 }
286 
288 Document DocumentHandler::load(Handle_t base, const XMLCh* fname, UriReader* reader) const {
289  std::string path;
290  DOMElement* elt = (DOMElement*)base.ptr();
291  try {
292  Document doc;
293  Strng_t p = _toString(fname);
294  path = _toString(fname);
296  try {
297  XMLURL ref_url(elt->getBaseURI(), p);
298  path = _toString(ref_url.getURLText());
299  }
300  catch(...) {
301  }
302  return load(path, reader);
303  }
304  catch(const std::exception& exc) {
305  std::string b = _toString(elt->getBaseURI());
306  std::string e = _toString(fname);
307  printout(DEBUG,"DocumentHandler","+++ URI exception: %s -> %s [%s]",b.c_str(),e.c_str(),exc.what());
308  }
309  catch(...) {
310  std::string b = _toString(elt->getBaseURI());
311  std::string e = _toString(fname);
312  printout(DEBUG,"DocumentHandler","+++ URI exception: %s -> %s",b.c_str(),e.c_str());
313  }
314  if ( reader ) {
315  std::string buf, sys = system_path(base,fname);
316 #if 0
317  std::string buf, sys, dir = _toString(elt->getBaseURI());
318  std::string fn = _toString(fname);
319  dir = ::dirname((char*)dir.c_str());
320  while( fn.substr(0,3) == "../" ) {
321  dir = ::dirname((char*)dir.c_str());
322  fn = fn.substr(3);
323  }
324  sys = dir + "/" + fn;
325 #endif
326  if ( reader->load(sys, buf) ) {
327 #if 0
328  Document doc = parse(buf.c_str(), buf.length(), sys.c_str(), reader);
329  dumpTree(doc);
330  return doc;
331 #endif
332  return parse(buf.c_str(), buf.length(), sys.c_str(), reader);
333  }
334  }
335  return Document(0);
336 }
337 
339 Document DocumentHandler::load(const std::string& fname, UriReader* reader) const {
340  auto fname_clean = _clean_fname(fname);
341  std::string path;
342  printout(DEBUG,"DocumentHandler","+++ Loading document URI: %s",fname_clean.c_str());
343  try {
344  size_t idx = fname_clean.find(':');
345  size_t idq = fname_clean.find('/');
346  if ( idq == std::string::npos ) idq = 0;
347  XMLURL xerurl = (const XMLCh*) Strng_t(idx==std::string::npos || idx>idq ? "file:"+fname_clean : fname_clean);
348  std::string proto = _toString(xerurl.getProtocolName());
349  path = _toString(xerurl.getPath());
350  printout(DEBUG,"DocumentHandler","+++ protocol:%s path:%s",proto.c_str(), path.c_str());
351  }
352  catch(...) {
353  }
354  std::unique_ptr < XercesDOMParser > parser(make_parser(reader));
355  try {
356  if ( !path.empty() ) {
357  parser->parse(path.c_str());
358  if ( reader ) reader->parserLoaded(path);
359  }
360  else {
361  if ( reader && reader->load(fname_clean, path) ) {
362  MemBufInputSource src((const XMLByte*)path.c_str(), path.length(), fname.c_str(), false);
363  parser->parse(src);
364  return (XmlDocument*)parser->adoptDocument();
365  }
366  return (XmlDocument*)0;
367  }
368  }
369  catch (const std::exception& e) {
370  printout(ERROR,"DocumentHandler","+++ Exception(XercesC): parse(path):%s",e.what());
371  try {
372  parser->parse(fname.c_str());
373  if ( reader ) reader->parserLoaded(path);
374  }
375  catch (const std::exception& ex) {
376  printout(FATAL,"DocumentHandler","+++ Exception(XercesC): parse(URI):%s",ex.what());
377  throw;
378  }
379  }
380  printout(DEBUG,"DocumentHandler","+++ Document %s succesfully parsed with XercesC .....",path.c_str());
381  return (XmlDocument*)parser->adoptDocument();
382 }
383 
385 Document DocumentHandler::parse(const char* bytes, size_t length, const char* sys_id, UriReader* rdr) const {
386  std::unique_ptr < XercesDOMParser > parser(make_parser(rdr));
387  MemBufInputSource src((const XMLByte*)bytes, length, sys_id, false);
388  parser->parse(src);
389  DOMDocument* doc = parser->adoptDocument();
390  doc->setXmlStandalone(true);
391  doc->setStrictErrorChecking(true);
392  return (XmlDocument*) doc;
393 }
394 
396 int DocumentHandler::output(Document doc, const std::string& fname) const {
397  XMLFormatTarget *tar = 0;
398  DOMImplementation *imp = DOMImplementationRegistry::getDOMImplementation(Strng_t("LS"));
399  DOMLSOutput *out = imp->createLSOutput();
400  DOMLSSerializer *wrt = imp->createLSSerializer();
401 
402  if (fname.empty())
403  tar = new StdOutFormatTarget();
404  else {
405  std::string fn = undressed_file_name(fname);
406  tar = new LocalFileFormatTarget(Strng_t(fn));
407  }
408  out->setByteStream(tar);
409  wrt->getDomConfig()->setParameter(Strng_t("format-pretty-print"), true);
410  wrt->write((xercesc::DOMDocument*) doc.ptr(), out);
411  out->release();
412  wrt->release();
413  delete tar;
414  return 1;
415 }
416 
417 #else
418 
419 #include <XML/tinyxml.h>
420 
422 namespace dd4hep {
424  namespace xml {
425 
427  class DocumentErrorHandler {};
428 
429  union Xml {
430  Xml(void* ptr) : p(ptr) {}
431  Xml(const void* ptr) : cp(ptr) {}
432  void* p;
433  const void* cp;
434  TiXmlElement* e;
435  XmlElement* xe;
436  TiXmlAttribute* a;
437  Attribute attr;
438  TiXmlNode* n;
439  TiXmlDocument* d;
440  XmlDocument* xd;
441  };
442  }}
443 
445 std::string DocumentHandler::system_path(Handle_t base, const std::string& fname) {
446  std::string fn, clean = _clean_fname(fname);
447  struct stat st;
448  Element elt(base);
449  // Poor man's URI handling. Xerces is much much better here
450  if ( elt ) {
451  std::string bn = Xml(elt.document()).d->Value();
452 #ifdef _WIN32
453  char drive[_MAX_DRIVE], dir[_MAX_DIR], file[_MAX_FNAME], ext[_MAX_EXT];
454  ::_splitpath(bn.c_str(),drive,dir,file,ext);
455  fn = drive;
456  fn += ":";
457  fn += dir;
458  fn += "/";
459  fn += clean;
460 #else
461  fn = ::dirname((char*)bn.c_str());
462 #endif
463  fn += "/";
464  fn += _clean_fname(fname);
465  }
466  if ( ::stat(fn.c_str(),&st)==0 )
467  return fn;
468  else if ( ::stat(clean.c_str(),&st)==0 )
469  return clean;
470  return fname;
471 }
472 
474 std::string DocumentHandler::system_path(Handle_t base) {
475  std::string fn;
476  Element elt(base);
477  // Poor man's URI handling. Xerces is much much better here
478  if ( elt ) {
479  fn = Xml(elt.document()).d->Value();
480  }
481  return undressed_file_name(fn);
482 }
483 
485 Document DocumentHandler::load(const std::string& fname, UriReader* reader) const {
486  std::string clean = _clean_fname(fname);
487  if ( reader ) {
488  printout(WARNING,"DocumentHandler","+++ Loading document URI: %s %s",
489  fname.c_str(),"[URI Resolution is not supported by TiXML]");
490  }
491  else if ( s_minPrintLevel <= INFO ) {
492  printout(INFO,"DocumentHandler","+++ Loading document URI: %s [Resolved:'%s']",
493  fname.c_str(),clean.c_str());
494  }
495  TiXmlDocument* doc = new TiXmlDocument(clean.c_str());
496  bool result = false;
497  try {
498  result = doc->LoadFile();
499  if ( !result ) {
500  if ( doc->Error() ) {
501  printout(FATAL,"DocumentHandler","+++ Error (TinyXML) parsing XML document:%s [%s]",
502  fname.c_str(), clean.c_str());
503  printout(FATAL,"DocumentHandler","+++ Error (TinyXML) XML parsing error:%s",
504  doc->ErrorDesc());
505  printout(FATAL,"DocumentHandler","+++ Document:%s Location Line:%d Column:%d",
506  doc->Value(), doc->ErrorRow(), doc->ErrorCol());
507  except("dd4hep:XML","++ file:%s error:%s",clean.c_str(),doc->ErrorDesc());
508  }
509  except("dd4hep:XML","++ Unknown error (TinyXML) while parsing:%s",fname.c_str());
510  }
511  }
512  catch(std::exception& e) {
513  printout(ERROR,"DocumentHandler","+++ Exception (TinyXML): parse(path):%s",e.what());
514  }
515  if ( result ) {
516  if ( s_minPrintLevel <= INFO ) {
517  printout(INFO,"DocumentHandler","+++ Document %s succesfully parsed with TinyXML .....",
518  fname.c_str());
519  }
520  return (XmlDocument*)doc;
521  }
522  delete doc;
523  return 0;
524 }
525 
527 Document DocumentHandler::load(Handle_t base, const XmlChar* fname, UriReader* reader) const {
528  std::string path = system_path(base, fname);
529  return load(path,reader);
530 }
531 
533 Document DocumentHandler::parse(const char* bytes, size_t length, const char* /* sys_id */, UriReader* reader) const {
534  if ( reader ) {
535  printout(WARNING,"DocumentHandler","+++ Parsing memory document %s",
536  "[URI Resolution is not supported by TiXML]");
537  }
538  TiXmlDocument* doc = new TiXmlDocument();
539  try {
540  if ( bytes ) {
541  size_t str_len = ::strlen(bytes);
542  size_t len = length;
543  // TiXml does not support white spaces at the end. Check and remove.
544  if ( str_len+1 != len || bytes[str_len] != 0 || ::isspace(bytes[str_len-1]) ) {
545  std::unique_ptr<char[]> data(new char[len+1]);
546  char* buff = data.get();
547  try {
548  ::memcpy(buff, bytes, len+1);
549  buff[len] = 0;
550  for(size_t i=len-1; i>0 && (buff[i]==0 || ::isspace(buff[i])); --i)
551  buff[i] = 0;
552  if ( 0 == doc->Parse(buff) ) {
553  return (XmlDocument*)doc;
554  }
555  }
556  catch(...) {
557  }
558  }
559  if ( 0 == doc->Parse(bytes) ) {
560  return (XmlDocument*)doc;
561  }
562  if ( doc->Error() ) {
563  printout(FATAL,"DocumentHandler",
564  "+++ Error (TinyXML) while parsing XML string [%s]",
565  doc->ErrorDesc());
566  printout(FATAL,"DocumentHandler",
567  "+++ XML Document error: %s Location Line:%d Column:%d",
568  doc->Value(), doc->ErrorRow(), doc->ErrorCol());
569  throw std::runtime_error(std::string("dd4hep: ")+doc->ErrorDesc());
570  }
571  throw std::runtime_error("dd4hep: Unknown error while parsing XML document string with TiXml.");
572  }
573  throw std::runtime_error("dd4hep: FAILED to parse invalid document string [NULL] with TiXml.");
574  }
575  catch(std::exception& e) {
576  printout(ERROR,"DocumentHandler","+++ Exception (TinyXML): parse(string):%s",e.what());
577  }
578  delete doc;
579  return 0;
580 }
581 
583 int DocumentHandler::output(Document doc, const std::string& fname) const {
584  std::string fn = undressed_file_name(fname);
585  FILE* file = fn.empty() ? stdout : ::fopen(fn.c_str(),"w");
586  if ( !file ) {
587  printout(ERROR,"DocumentHandler","+++ Failed to open output file: %s",fname.c_str());
588  return 0;
589  }
590  TiXmlDocument* d = (TiXmlDocument*)doc.ptr();
591  d->Print(file);
592  if ( !fn.empty() ) ::fclose(file);
593  return 1;
594 }
595 
597 void dd4hep::xml::dump_tree(Handle_t elt, std::ostream& os) {
598  TiXmlNode* node = (TiXmlNode*)elt.ptr();
599  TiXmlPrinter printer;
600  printer.SetStreamPrinting();
601  node->Accept( &printer );
602  os << printer.Str();
603 }
604 
606 void dd4hep::xml::dump_tree(Document doc, std::ostream& os) {
607  TiXmlDocument* node = (TiXmlDocument*)doc.ptr();
608  TiXmlPrinter printer;
609  printer.SetStreamPrinting();
610  node->Accept( &printer );
611  os << printer.Str();
612 }
613 #endif
614 
615 
618 
621 
624  int tmp = s_minPrintLevel;
625  s_minPrintLevel = level;
626  return tmp;
627 }
628 
631  const char comment[] = "\n"
632  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
633  " ++++ dd4hep generated alignment file using the ++++\n"
634  " ++++ dd4hep Detector description XML generator. ++++\n"
635  " ++++ ++++\n"
636  " ++++ Parser:"
638  " ++++\n"
639  " ++++ ++++\n"
640  " ++++ M.Frank CERN/LHCb ++++\n"
641  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n ";
642  return comment;
643 }
644 
646 Document DocumentHandler::load(const std::string& fname) const {
647  return load(fname, 0);
648 }
649 
651 Document DocumentHandler::load(Handle_t base, const XmlChar* fname) const {
652  return load(base, fname, 0);
653 }
654 
656 Document DocumentHandler::parse(const char* bytes, size_t length) const {
657  return parse(bytes, length, "xml-memory-buffer", 0);
658 }
659 
661 std::string DocumentHandler::system_path(Handle_t base, const XmlChar* fname) {
662  std::string fn = _toString(fname);
663  return system_path(base, fn);
664 }
665 
667 std::string DocumentHandler::system_directory(Handle_t base, const XmlChar* fname) {
668  std::string path = system_path(base,fname);
669  std::string dir = ::dirname((char*)path.c_str());
670  return dir;
671 }
672 
675  std::string path = system_path(base);
676  std::string dir = ::dirname((char*)path.c_str());
677  return dir;
678 }
679 
681 Document DocumentHandler::create(const char* tag, const char* comment) const {
682  std::string top(tag);
683  std::string empty = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
684  empty += "<" + top + "/>\0\0";
685  Document doc = parse(empty.c_str(), empty.length() + 1);
686  if (comment) {
687  Element top_elt = doc.root();
688  top_elt.addComment(comment);
689  }
690  return doc;
691 }
692 
693 // Create new XML document by parsing empty xml buffer
694 Document DocumentHandler::create(const std::string& tag, const std::string& comment) const {
695  return create(tag.c_str(), comment.c_str());
696 }
697 
700  dump_tree(elt,std::cout);
701 }
702 
705  dump_tree(doc,std::cout);
706 }
TiXmlElement
TinyXML class. See http://www.grinninglizard.com/tinyxml.
Definition: tinyxml.h:1069
dd4hep::xml::DocumentHandler::create
Document create(const char *tag, const char *comment=0) const
Create new XML document by parsing empty xml buffer.
Definition: DocumentHandler.cpp:681
TiXmlDocument::Print
void Print() const
Definition: tinyxml.h:1696
UriReader.h
TiXmlDocument
TinyXML class. See http://www.grinninglizard.com/tinyxml.
Definition: tinyxml.h:1547
dd4hep::xml::DocumentErrorHandler::~DocumentErrorHandler
virtual ~DocumentErrorHandler()
Destructor.
Definition: DocumentHandler.cpp:86
TiXmlDocument::ErrorCol
int ErrorCol() const
The column where the error occured. See ErrorRow()
Definition: tinyxml.h:1648
dd4hep::xml::UriReader::load
virtual bool load(const std::string &system_id, std::string &data)
Resolve a given URI to a string containing the data.
Definition: UriReader.cpp:22
TiXmlPrinter::Str
const std::string & Str()
Return the result.
Definition: tinyxml.h:2003
dd4hep::error
std::size_t error(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:67
ns
Helper structure to shortcut type definitions for the factories.
Definition: Factories.h:204
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::xml::Attribute
const XmlAttr * Attribute
Definition: XMLElements.h:38
dd4hep::xml::DocumentErrorHandler::fatalError
void fatalError(const SAXParseException &e)
Fatal error handler.
Definition: DocumentHandler.cpp:143
dd4hep::xml::DocumentHandler::load
virtual Document load(const std::string &fname) const
Load XML file and parse it.
Definition: DocumentHandler.cpp:646
tinyxml.h
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
TiXmlDocument::Error
bool Error() const
Definition: tinyxml.h:1622
dd4hep::xml::UriReader::parserLoaded
virtual void parserLoaded(const std::string &system_id)
Inform reader about a locally (e.g. by XercesC) handled source load.
Definition: UriReader.cpp:27
m_reader
UriReader * m_reader
Pointer to URI reader.
Definition: DocumentHandler.cpp:155
TiXmlDocument::ErrorRow
int ErrorRow() const
Definition: tinyxml.h:1645
TiXmlDocument::Parse
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING) override
Definition: tinyxmlparser_inl.h:733
parser
dd4hepDOMParser * parser
Definition: DocumentHandler.cpp:159
dd4hep::xml::parse
void parse(Handle_t e, RotationZYX &rot)
Convert rotation XML objects to dd4hep::RotationZYX.
Definition: XMLParsers.cpp:48
DocumentHandler.h
TiXmlDocument::Accept
virtual bool Accept(TiXmlVisitor *content) const override
Definition: tinyxml_inl.h:1185
dd4hep::xml::DocumentErrorHandler::resetErrors
void resetErrors()
Reset errors (Noop)
Definition: DocumentHandler.cpp:90
TiXmlAttribute
TinyXML class. See http://www.grinninglizard.com/tinyxml.
Definition: tinyxml.h:880
dd4hep::xml
Namespace for the AIDA detector description toolkit supporting XML utilities.
Definition: ConditionsTags.h:27
dd4hep::xml::Document::ptr
DOC ptr() const
Accessot to DOM document behaviour.
Definition: XMLElements.h:717
dd4hep::xml::dump_doc
void dump_doc(DOMDocument *doc, std::ostream &os)
Dump DOM tree using XercesC handles.
Definition: DocumentHandler.cpp:236
dd4hep::xml::_toString
std::string _toString(const Attribute attr)
Convert xml attribute to STL string.
Definition: XMLElements.cpp:234
TiXmlNode
TinyXML class. See http://www.grinninglizard.com/tinyxml.
Definition: tinyxml.h:434
dd4hep::xml::DocumentErrorHandler::warning
void warning(const SAXParseException &)
Warnings callback. Ignore them.
Definition: DocumentHandler.cpp:93
dd4hep::xml::Strng_t
Helper class to encapsulate a unicode string.
Definition: XMLElements.h:170
m_resolver
Resolver m_resolver
Definition: DocumentHandler.cpp:166
Xml::d
xercesc::DOMDocument * d
Definition: XMLElements.cpp:134
dd4hep::xml::DocumentHandler::defaultComment
static std::string defaultComment()
Default comment string.
Definition: DocumentHandler.cpp:630
TiXmlDocument::ErrorDesc
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1627
XML_IMPLEMENTATION_TYPE
#define XML_IMPLEMENTATION_TYPE
Definition: config.h:64
dd4hep::xml::UriReader
Class supporting to read data given a URI.
Definition: UriReader.h:35
dd4hep::xml::DocumentHandler::parse
virtual Document parse(const char *doc_string, size_t length) const
Parse a standalong XML string into a document.
Definition: DocumentHandler.cpp:656
dd4hep::xml::Document
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:697
DOMErrorHandler
XercesC internal class. Not described here.
Definition: DD4hepGroups.h:67
dd4hep::xml::DocumentHandler::setMinimumPrintLevel
static int setMinimumPrintLevel(int level)
Set minimum print level.
Definition: DocumentHandler.cpp:623
dd4hep::xml::DocumentHandler::output
virtual int output(Document doc, const std::string &fname) const
Write xml document to output file (stdout if file name empty)
Definition: DocumentHandler.cpp:396
dd4hep::xml::DocumentErrorHandler
XML-DOM ERror handler class for the XercesC document parser.
Definition: DocumentHandler.cpp:80
TiXmlNode::Value
const char * Value() const
Definition: tinyxml.h:490
TiXmlPrinter::SetStreamPrinting
void SetStreamPrinting()
Definition: tinyxml.h:1988
dd4hep::xml::XmlChar
XERCES_XMLCH_T XmlChar
Use the definition from the autoconf header of Xerces:
Definition: config.h:53
Xml
Union to ease castless object access when using XercesC.
Definition: XMLElements.cpp:126
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep::xml::dump_tree
void dump_tree(Handle_t elt)
Dump partial or full XML trees to stdout.
Definition: DocumentHandler.cpp:699
TiXmlDocument::LoadFile
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxml_inl.h:953
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::xml::DocumentErrorHandler::DocumentErrorHandler
DocumentErrorHandler()
Constructor.
Definition: DocumentHandler.cpp:83
dd4hep::xml::getEnviron
std::string getEnviron(const std::string &env)
Helper function to lookup environment from the expression evaluator.
Definition: XMLElements.cpp:403
dd4hep::xml::DocumentErrorHandler::error
void error(const SAXParseException &e)
Error handler.
Definition: DocumentHandler.cpp:129
dd4hep::xml::DocumentHandler::system_directory
static std::string system_directory(Handle_t base)
System directory of a given XML entity.
Definition: DocumentHandler.cpp:674
dd4hep::xml::DocumentHandler::~DocumentHandler
virtual ~DocumentHandler()
Default destructor.
Definition: DocumentHandler.cpp:620
m_errHandle_tr
DocumentErrorHandler m_errHandle_tr
Xerces Error handler.
Definition: DocumentHandler.cpp:157
dd4hep::xml::dumpTree
void dumpTree(XmlDocument *doc)
Dump DOM tree of a document.
TiXmlNode::Accept
virtual bool Accept(TiXmlVisitor *visitor) const =0
dd4hep::xml::DocumentHandler::DocumentHandler
DocumentHandler()
Default constructor.
Definition: DocumentHandler.cpp:617
dd4hep::xml::DocumentHandler::system_path
static std::string system_path(Handle_t base)
System ID of a given XML entity.
Definition: DocumentHandler.cpp:277
TiXmlPrinter
TinyXML class. See http://www.grinninglizard.com/tinyxml.
Definition: tinyxml.h:1946
dd4hep::xml::DocumentErrorHandler::handleError
virtual bool handleError(const DOMError &domError)
Dom Error handler callback.
Definition: DocumentHandler.cpp:104
dd4hep::xml::Handle_t::ptr
Elt_t ptr() const
Direct access to the XmlElement by function.
Definition: XMLElements.h:401
ErrorHandler
XercesC internal class. Not described here.
Definition: DD4hepGroups.h:64
dd4hep::xml::Element::addComment
void addComment(const XmlChar *text) const
Add comment node to the element.
Definition: XMLElements.cpp:1135
dd4hep::xml::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: XMLElements.cpp:1040
Printout.h