CYCLUS
relax_ng_validator.cc
Go to the documentation of this file.
1 /* relaxngvalidator.cpp
2  * this class is agented off of the schemavalidator in libxml++
3  * here is their license statement:
4  *
5  * libxml++ and this file are copyright (C) 2000 by Ari Johnson,
6  * (C) 2002-2004 by the libxml dev team and
7  * are covered by the GNU Lesser General Public License, which should be
8  * included with libxml++ as the file COPYING.
9  */
10 
11 #include "relax_ng_validator.h"
12 
13 #include <libxml/xmlerror.h>
14 #include <libxml/relaxng.h>
15 #include <libxml++/document.h>
16 
17 #include "error.h"
18 
19 namespace cyclus {
20 
21 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22 RelaxNGValidator::RelaxNGValidator() : schema_(0), valid_context_(0) {}
23 
24 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27 }
28 
29 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30 void RelaxNGValidator::parse_context(xmlRelaxNGParserCtxtPtr context) {
31  release_underlying(); // Free any existing info
32  schema_ = xmlRelaxNGParse(context);
33  if (!schema_) {
34  throw ValidationError("Schema could not be parsed");
35  }
36 }
37 
38 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 void RelaxNGValidator::parse_memory(const Glib::ustring& contents) {
40  xmlRelaxNGParserCtxtPtr context =
41  xmlRelaxNGNewMemParserCtxt(contents.c_str(), contents.bytes());
42  parse_context(context);
43 }
44 
45 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47  if (valid_context_) {
48  xmlRelaxNGFreeValidCtxt(valid_context_);
49  valid_context_ = 0;
50  }
51 
52  if (schema_) {
53  xmlRelaxNGFree(schema_);
54  schema_ = 0;
55  }
56 }
57 
58 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59 bool RelaxNGValidator::Validate(const xmlpp::Document* doc) {
60  if (!doc) {
61  throw ValidationError("Document pointer cannot be 0");
62  }
63 
64  if (!schema_) {
65  throw ValidationError("Must have a schema to validate document");
66  }
67 
68  // A context is required at this stage only
69  if (!valid_context_) {
70  valid_context_ = xmlRelaxNGNewValidCtxt(schema_);
71  }
72 
73  if (!valid_context_) {
74  throw ValidationError("Couldn't create validating context");
75  }
76 
77  int res;
78  try {
79  res = xmlRelaxNGValidateDoc(valid_context_, (xmlDocPtr)doc->cobj());
80  } catch (xmlError e) {
81  throw ValidationError(e.message);
82  }
83 
84  if (res != 0) {
85  throw ValidationError("Document failed schema validation");
86  }
87 
88  return res;
89 }
90 
91 } // namespace cyclus
void parse_memory(const Glib::ustring &contents)
parse a relaxng schema xml file
xmlRelaxNG * schema_
the schema
void release_underlying()
free xml-related memory
For validating files received via IO.
Definition: error.h:71
void parse_context(xmlRelaxNGParserCtxt *context)
parse a relaxng schema context
xmlRelaxNGValidCtxt * valid_context_
the validated context
std::string doc(int x)
Definition: pyne.cc:6557
bool Validate(const xmlpp::Document *doc)
validate an xml file agaisnt the given schema
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14