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::stringstream& 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.str();
31 
32  parser_->parse_memory(xml_input_snippet.str());
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 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42 void XMLParser::Validate(const std::stringstream& xml_schema_snippet) {
43  RelaxNGValidator validator;
44  validator.parse_memory(xml_schema_snippet.str());
45  validator.Validate(this->Document());
46 }
47 
48 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 xmlpp::Document* XMLParser::Document() {
50  xmlpp::Document* doc = parser_->get_document();
51  // This adds the capability to have nice include semantics
52  doc->process_xinclude();
53  // This removes the stupid xml:base attribute that including adds,
54  // but which is unvalidatable. The web is truly cobbled together
55  // by a race of evil gnomes.
56  xmlpp::Element* root = doc->get_root_node();
57  xmlpp::NodeSet have_base = root->find("//*[@xml:base]");
58  xmlpp::NodeSet::iterator it = have_base.begin();
59  for (; it != have_base.end(); ++it) {
60  reinterpret_cast<xmlpp::Element*>(*it)->remove_attribute("base", "xml");
61  }
62  return doc;
63 }
64 
65 } // 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:24
void Validate(const std::stringstream &schema)
validates the file agaisnt a schema
Definition: xml_parser.cc:42
xmlpp::Document * Document()
Definition: xml_parser.cc:49