CYCLUS
Loading...
Searching...
No Matches
xml_parser.cc
Go to the documentation of this file.
1#include "xml_parser.h"
2
3#include <stdlib.h>
4#include <string>
5#include <libxml++/libxml++.h>
6
7#include "error.h"
8#include "logger.h"
10
11namespace cyclus {
12
13#if LIBXMLXX_MAJOR_VERSION == 2
14 typedef xmlpp::NodeSet NodeSet;
15 typedef xmlpp::Node::NodeList const_NodeList;
16#else
17 typedef xmlpp::Node::NodeSet NodeSet;
18 typedef xmlpp::Node::const_NodeList const_NodeList;
19#endif
20
21// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23 parser_ = new xmlpp::DomParser();
24}
25
26// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28 delete parser_;
29}
30
31// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32void XMLParser::Init(const std::string& xml_input_snippet) {
33 if (parser_ != NULL) {
34 delete parser_;
35 }
36 parser_ = new xmlpp::DomParser();
37 try {
38 CLOG(LEV_DEBUG5) << "Parsing the snippet: " << xml_input_snippet;
39
40 parser_->parse_memory(xml_input_snippet);
41 if (!parser_) {
42 throw ValidationError("Could not parse xml file.");
43 }
44 } catch (const std::exception& ex) {
45 throw ValidationError("Error loading xml file: " + std::string(ex.what()));
46 }
47}
48
49void XMLParser::Init(const std::stringstream& xml_input_snippet) {
51}
52
53// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54void XMLParser::Validate(const std::stringstream& xml_schema_snippet) {
57 validator.Validate(this->Document());
58}
59
60// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61xmlpp::Document* XMLParser::Document() {
62 xmlpp::Document* doc = parser_->get_document();
63 // This adds the capability to have nice include semantics
64 bool generate_xinclude_nodes = true;
65 bool fixup_base_uris = false;
66 #if LIBXMLXX_MAJOR_VERSION == 2
67 #if LIBXMLXX_MINOR_VERSION < 42
68 doc->process_xinclude(generate_xinclude_nodes);
69 #else
70 doc->process_xinclude(generate_xinclude_nodes, fixup_base_uris);
71 #endif
72 #else
73 doc->process_xinclude(generate_xinclude_nodes, fixup_base_uris);
74 #endif
75 // This removes the stupid xml:base attribute that including adds,
76 // but which is unvalidatable. The web is truly cobbled together
77 // by a race of evil gnomes. After libxml++ v2.42.0 process_xinclude
78 // supports the fixup_base_uris which does this removal for us
79 xmlpp::Element* root = doc->get_root_node();
80 NodeSet have_base = root->find("//*[@xml:base]");
81 NodeSet::iterator it = have_base.begin();
82 for (; it != have_base.end(); ++it) {
83 reinterpret_cast<xmlpp::Element*>(*it)->remove_attribute("base", "xml");
84 }
85 return doc;
86}
87
88} // namespace cyclus
void parse_memory(const Glib::ustring &contents)
parse a relaxng schema xml file
For validating files received via IO.
Definition error.h:71
void Validate(const std::stringstream &schema)
validates the file agaisnt a schema
Definition xml_parser.cc:54
virtual ~XMLParser()
destructor
Definition xml_parser.cc:27
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition xml_parser.cc:49
XMLParser()
constructor
Definition xml_parser.cc:22
xmlpp::Document * Document()
Definition xml_parser.cc:61
Code providing rudimentary logging capability for the Cyclus core.
#define CLOG(level)
Definition logger.h:39
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
@ LEV_DEBUG5
debugging information - most verbose
Definition logger.h:62
xmlpp::Node::NodeSet NodeSet
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
xmlpp::Node::const_NodeList const_NodeList