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