Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GspnParser.cpp
Go to the documentation of this file.
1#include "GspnParser.h"
2#include "storm-config.h"
4
8
10#include "PnmlParser.h"
11
12namespace storm {
13namespace parser {
14
15storm::gspn::GSPN* GspnParser::parse(std::string const& filename, std::string const& constantDefinitions) {
16#ifdef STORM_HAVE_XERCES
17 // initialize xercesc
18 try {
19 xercesc::XMLPlatformUtils::Initialize();
20 } catch (xercesc::XMLException const& toCatch) {
21 // Error occurred during the initialization process. Abort parsing since it is not possible.
22 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, "Failed to initialize xercesc\n");
23 }
24
25 auto parser = new xercesc::XercesDOMParser();
26 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
27 parser->setDoNamespaces(false);
28 parser->setDoSchema(false);
29 parser->setLoadExternalDTD(false);
30 parser->setIncludeIgnorableWhitespace(false);
31
32 auto errHandler = (xercesc::ErrorHandler*)new xercesc::HandlerBase();
33 parser->setErrorHandler(errHandler);
34
35 // parse file
36 try {
37 parser->parse(filename.c_str());
38 } catch (xercesc::XMLException const& toCatch) {
39 auto message = xercesc::XMLString::transcode(toCatch.getMessage());
40 // Error occurred while parsing the file. Abort constructing the gspn since the input file is not valid
41 // or the parser run into a problem.
42 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, message);
43 xercesc::XMLString::release(&message);
44 } catch (const xercesc::DOMException& toCatch) {
45 auto message = xercesc::XMLString::transcode(toCatch.msg);
46 // Error occurred while parsing the file. Abort constructing the gspn since the input file is not valid
47 // or the parser run into a problem.
48 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, message);
49 xercesc::XMLString::release(&message);
50 } catch (...) {
51 // Error occurred while parsing the file. Abort constructing the gspn since the input file is not valid
52 // or the parser run into a problem.
53 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, "Failed to parse pnml file.\n");
54 }
55
56 // build gspn by traversing the DOM object
57 parser->getDocument()->normalizeDocument();
58 xercesc::DOMElement* elementRoot = parser->getDocument()->getDocumentElement();
59
60 if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "pnml") {
61 STORM_LOG_WARN_COND(constantDefinitions == "", "Constant definitions for pnml files are currently not supported.");
62 PnmlParser p;
63 return p.parse(elementRoot);
64 } else if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "project") {
65 GreatSpnEditorProjectParser p(constantDefinitions);
66 return p.parse(elementRoot);
67 } else {
68 // If the top-level node is not a "pnml" or "" node, then throw an exception.
69 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, "Failed to identify the root element.\n");
70 }
71
72 // clean up
73 delete parser;
74 delete errHandler;
75 xercesc::XMLPlatformUtils::Terminate();
76#else
77 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, "Storm is not compiled with XML support: " << filename << " can not be parsed");
78#endif
79}
80} // namespace parser
81} // namespace storm
static storm::gspn::GSPN * parse(std::string const &filename, std::string const &constantDefinitions="")
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18