ATLAS Offline Software
Loading...
Searching...
No Matches
XercesParser.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
10#include "GaudiKernel/MsgStream.h"
11
12#include "xercesc/util/PlatformUtils.hpp"
13#include "xercesc/util/XMLException.hpp"
14#include <xercesc/dom/DOM.hpp>
15#include <xercesc/framework/StdOutFormatTarget.hpp>
16#include <xercesc/framework/LocalFileFormatTarget.hpp>
17#include <xercesc/framework/MemBufInputSource.hpp>
18#include <xercesc/parsers/XercesDOMParser.hpp>
19#include <xercesc/util/XMLUni.hpp>
20#include <xercesc/util/OutOfMemoryException.hpp>
21
22#include <iostream>
23
24using namespace xercesc;
25
30
36
38 : IAGDDParser(s),m_doc(0),m_initialized(false),
39 m_xs(xs)
40{
41}
42
43bool XercesParser::ParseFile(const std::string& s_in)
44{
45 bool errorsOccured = false;
46 m_fileName=s_in;
47 std::string s=PathResolver::find_file(s_in,"XMLPATH");
48 if (s.empty()) {
49 MsgStream log(Athena::getMessageSvc(), "XercesParser");
50 log<<MSG::WARNING<<"ParseFile() - something wrong, could not find XML file "<<s<<endmsg;
51 errorsOccured = true;
52 }
53 else
54 {
56 m_parser = std::make_unique<XercesDOMParser>();
57 try
58 {
59 m_parser->parse(s.c_str());
60 }
61 catch (const OutOfMemoryException&)
62 {
63 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
64 errorsOccured = true;
65 }
66 catch (const XMLException& e)
67 {
68 XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n Message: "
69 << XMLString::transcode(e.getMessage()) << XERCES_STD_QUALIFIER endl;
70 errorsOccured = true;
71 }
72 catch (const DOMException& e)
73 {
74 const unsigned int maxChars = 2047;
75 XMLCh errText[maxChars + 1];
76
77 XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << s << "'\n"
78 << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
79
80 if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
81 XERCES_STD_QUALIFIER cerr << "Message is: " << XMLString::transcode(errText) << XERCES_STD_QUALIFIER endl;
82
83 errorsOccured = true;
84 }
85 catch (...)
86 {
87 XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n " << XERCES_STD_QUALIFIER endl;
88 errorsOccured = true;
89 }
90 m_doc=m_parser->getDocument();
91 return errorsOccured;
92 }
93 return errorsOccured;
94}
96 const std::string& s)
97{
98 bool errorOccured = ParseFile(s);
99 if (errorOccured) return false;
100 navigateTree(c);
101 return true;
102}
103bool XercesParser::ParseString(const std::string& s)
104{
105 const char* str=s.c_str();
106 MemBufInputSource memBuf (reinterpret_cast<const XMLByte*>(str),strlen(str),"prodInfo",false);
107 m_parser = std::make_unique<XercesDOMParser>();
108 bool errorsOccured = false;
110 try
111 {
112 m_parser->parse(memBuf);
113 }
114 catch (const OutOfMemoryException&)
115 {
116 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
117 errorsOccured = true;
118 }
119 catch (const XMLException& e)
120 {
121 XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n Message: "
122 << XMLString::transcode(e.getMessage()) << XERCES_STD_QUALIFIER endl;
123 errorsOccured = true;
124 }
125 catch (const DOMException& e)
126 {
127 const unsigned int maxChars = 2047;
128 XMLCh errText[maxChars + 1];
129
130 XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << s << "'\n"
131 << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
132
133 if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
134 XERCES_STD_QUALIFIER cerr << "Message is: " << XMLString::transcode(errText) << XERCES_STD_QUALIFIER endl;
135
136 errorsOccured = true;
137 }
138 catch (...)
139 {
140 XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n " << XERCES_STD_QUALIFIER endl;
141 errorsOccured = true;
142 }
143 m_doc=m_parser->getDocument();
144 return errorsOccured;
145}
147 const std::string& s)
148{
149 bool errorOccured = ParseString(s);
150 if (!errorOccured) navigateTree(c);
151 return true;
152}
153
154bool XercesParser::WriteToFile(const std::string& s)
155{
156 XMLCh tempStr[100];
157 XMLString::transcode("LS 3.0 Core 2.0", tempStr, 99);
158 DOMImplementation* implementation = DOMImplementationRegistry::getDOMImplementation(tempStr);
159 DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(implementation)->createLSSerializer();
160 // if one wants a nicely indented file -- not in this case as it goes to the DB and be compressed
161 // DOMConfiguration* domconfig = serializer->getDomConfig();
162 // domconfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
163 XMLFormatTarget* target = new LocalFileFormatTarget(s.c_str());
164 DOMLSOutput* domoutput = static_cast<DOMImplementationLS*>(implementation)->createLSOutput();
165 // remove all comments
166 if( m_doc->getDOMConfig()->canSetParameter(XMLUni::fgDOMComments, true) ) m_doc->getDOMConfig()->setParameter(XMLUni::fgDOMComments, false);
167 m_doc->normalizeDocument();
168
169 domoutput->setByteStream(target);
170 serializer->write(m_doc, domoutput);
171 domoutput->release();
172 serializer->release();
173 delete target;
174
175 return true;
176}
177
179{
180 if (!m_doc) {
181 MsgStream log(Athena::getMessageSvc(), "XercesParser");
182 log<<MSG::WARNING<<"navigateTree() - something is wrong! no document set! doing nothing!"<<endmsg;
183 return;
184 }
185 DOMNode* node = 0;
186 node = dynamic_cast<DOMNode*>(m_doc->getDocumentElement());
187 if( !node ) std::abort();
188 elementLoop(c, node);
189}
190
192
194 DOMNode *e)
195{
196 if (!e) {
197 MsgStream log(Athena::getMessageSvc(), "XercesParser");
198 log<<MSG::WARNING<<"Calling elementLoop() with NULL pointer!!!"<<endmsg;
199 return;
200 }
201 if (!(e->getNodeType()==DOMNode::ELEMENT_NODE)) return;
202 XMLHandler *h = m_xs.GetHandler(e);
203 bool stopLoop=false;
204 if (h)
205 {
206 h->Handle(c, e);
207 stopLoop=h->IsLoopToBeStopped();
208 }
209 DOMNode *child;
210 std::string sName;
211 if (!stopLoop && e)
212 {
213 char* name=XMLString::transcode(e->getNodeName());
214 sName=name;
215 XMLString::release(&name);
216 for (child=e->getFirstChild();child!=0;child=child->getNextSibling())
217 {
218 if (child->getNodeType()==DOMNode::ELEMENT_NODE) {
219 elementLoop(c, child);
220 }
221 }
222 }
223}
224
226{
227 try {
228 XMLPlatformUtils::Initialize();
229 }
230
231 catch(const XMLException &toCatch) {
232 XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
233 << " Exception message:"
234 << XERCES_STD_QUALIFIER endl;
235 return 1;
236 }
237 m_initialized=true;
238 return 0;
239}
240
242{
243 XMLPlatformUtils::Terminate();
244 m_initialized=false;
245 return 0;
246}
#define endmsg
std::string m_fileName
Definition IAGDDParser.h:31
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
virtual bool ParseString(const std::string &) override
virtual void elementLoop() override
virtual bool WriteToFile(const std::string &) override
virtual bool ParseFile(const std::string &) override
virtual ~XercesParser()
virtual bool ParseFileAndNavigate(AGDDController &c, const std::string &) override
xercesc::DOMDocument * m_doc
XMLHandlerStore & m_xs
std::unique_ptr< xercesc::XercesDOMParser > m_parser
virtual void navigateTree(AGDDController &c) override
XercesParser(XMLHandlerStore &xs)
virtual bool ParseStringAndNavigate(AGDDController &c, const std::string &) override
Definition node.h:24
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)