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