CYCLUS
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"
9 #include "relax_ng_validator.h"
10 
11 namespace cyclus {
12 
13 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14 XMLParser::XMLParser() : parser_(NULL) {
15  parser_ = new xmlpp::DomParser();
16 }
17 
18 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20  delete parser_;
21 }
22 
23 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24 void XMLParser::Init(const std::string& xml_input_snippet) {
25  if (parser_ != NULL) {
26  delete parser_;
27  }
28  parser_ = new xmlpp::DomParser();
29  try {
30  CLOG(LEV_DEBUG5) << "Parsing the snippet: " << xml_input_snippet;
31 
32  parser_->parse_memory(xml_input_snippet);
33  if (!parser_) {
34  throw ValidationError("Could not parse xml file.");
35  }
36  } catch (const std::exception& ex) {
37  throw ValidationError("Error loading xml file: " + std::string(ex.what()));
38  }
39 }
40 
41 void XMLParser::Init(const std::stringstream& xml_input_snippet) {
42  Init(xml_input_snippet.str());
43 }
44 
45 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 void XMLParser::Validate(const std::stringstream& xml_schema_snippet) {
47  RelaxNGValidator validator;
48  validator.parse_memory(xml_schema_snippet.str());
49  validator.Validate(this->Document());
50 }
51 
52 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53 xmlpp::Document* XMLParser::Document() {
54  xmlpp::Document* doc = parser_->get_document();
55  // This adds the capability to have nice include semantics
56  doc->process_xinclude();
57  // This removes the stupid xml:base attribute that including adds,
58  // but which is unvalidatable. The web is truly cobbled together
59  // by a race of evil gnomes.
60  xmlpp::Element* root = doc->get_root_node();
61  xmlpp::NodeSet have_base = root->find("//*[@xml:base]");
62  xmlpp::NodeSet::iterator it = have_base.begin();
63  for (; it != have_base.end(); ++it) {
64  reinterpret_cast<xmlpp::Element*>(*it)->remove_attribute("base", "xml");
65  }
66  return doc;
67 }
68 
69 } // 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
std::string doc(int x)
Definition: pyne.cc:6557
#define CLOG(level)
Definition: logger.h:39
bool Validate(const xmlpp::Document *doc)
validate an xml file agaisnt the given schema
XMLParser()
constructor
Definition: xml_parser.cc:14
debugging information - most verbose
Definition: logger.h:62
virtual ~XMLParser()
destructor
Definition: xml_parser.cc:19
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition: xml_parser.cc:41
void Validate(const std::stringstream &schema)
validates the file agaisnt a schema
Definition: xml_parser.cc:46
xmlpp::Document * Document()
Definition: xml_parser.cc:53