Skip to content

Commit

Permalink
Add error check, homogenize parse methods (Argonne-National-Laborator…
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Apr 6, 2021
1 parent 4b9a347 commit b9031b0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
23 changes: 17 additions & 6 deletions src/parse.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,23 @@ void recurseSubstitute(rapidxml::xml_node<> * node, std::map<std::string, std::s
}

// iterate over all child nodes
for (rapidxml::xml_node<> * child = node->first_node(); child; child = child->next_sibling())
for (rapidxml::xml_node<> * child = node->first_node(); child; child = child->next_sibling())
recurseSubstitute(child, substitutions);
}

std::shared_ptr<NEMLModel> parse_string(std::string input, std::map<std::string, std::string> substitutions)
std::shared_ptr<NEMLModel> parse_string(std::string input, std::string mname, std::map<std::string, std::string> substitutions)
{
// Parse the string to the rapidxml representation
rapidxml::xml_document<> doc;
doc.parse<0>(&input[0]);

// The model is the root node
rapidxml::xml_node<> * found = doc.first_node();
// Grab the root node
rapidxml::xml_node<> * root = doc.first_node();

// Find the node with the right name
rapidxml::xml_node<> * found = mname ? root->first_node(mname.c_str()) : root;
if (!found)
throw ModelNotFound(mname);

// substitute {variables} in DOM tree
recurseSubstitute(found, substitutions);
Expand Down Expand Up @@ -79,7 +84,9 @@ std::unique_ptr<NEMLModel> parse_string_unique(std::string input, std::string mn
rapidxml::xml_node<> * root = doc.first_node();

// Find the node with the right name
rapidxml::xml_node<> * found = root->first_node(mname.c_str());
rapidxml::xml_node<> * found = mname ? root->first_node(mname.c_str()) : root;
if (!found)
throw ModelNotFound(mname);

// substitute {variables} in DOM tree
recurseSubstitute(found, substitutions);
Expand Down Expand Up @@ -108,7 +115,9 @@ std::shared_ptr<NEMLModel> parse_xml(std::string fname, std::string mname, std::
rapidxml::xml_node<> * root = doc.first_node();

// Find the node with the right name
rapidxml::xml_node<> * found = root->first_node(mname.c_str());
rapidxml::xml_node<> * found = mname ? root->first_node(mname.c_str()) : root;
if (!found)
throw ModelNotFound(mname);

// substitute {variables} in DOM tree
recurseSubstitute(found, substitutions);
Expand Down Expand Up @@ -138,6 +147,8 @@ std::unique_ptr<NEMLModel> parse_xml_unique(std::string fname, std::string mname

// Find the node with the right name
rapidxml::xml_node<> * found = root->first_node(mname.c_str());
if (!found)
throw ModelNotFound(mname);

// substitute {variables} in DOM tree
recurseSubstitute(found, substitutions);
Expand Down
38 changes: 29 additions & 9 deletions src/parse.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
#ifndef PARSE_H
#define PARSE_H

#include "objects.h"
#include "models.h"
#include "damage.h"
#include "models.h"
#include "objects.h"

#include "windows.h"

#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"

#include <algorithm>
#include <exception>
#include <memory>
#include <string>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <exception>

namespace neml {

/// perform {variable} substitution in DOM tree
void recurseSubstitute(rapidxml::xml_node<> * node, std::map<std::string, std::string> substitutions);

/// Parse from a string to a shared_ptr
std::shared_ptr<NEMLModel> parse_string(std::string input, std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());
NEML_EXPORT std::shared_ptr<NEMLModel> parse_string(std::string input, std::string mname = "", std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());

/// Parse from a string to a unique_ptr
std::unique_ptr<NEMLModel> parse_string_unique(std::string input, std::string mname, std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());
NEML_EXPORT std::unique_ptr<NEMLModel> parse_string_unique(std::string input, std::string mname = "", std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());

/// Parse from file to a shared_ptr
NEML_EXPORT std::shared_ptr<NEMLModel> parse_xml(std::string fname, std::string mname, std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());
NEML_EXPORT std::shared_ptr<NEMLModel> parse_xml(std::string fname, std::string mname = "", std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());

/// Parse from file to a unique_ptr
NEML_EXPORT std::unique_ptr<NEMLModel> parse_xml_unique(std::string fname, std::string mname, std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());
NEML_EXPORT std::unique_ptr<NEMLModel> parse_xml_unique(std::string fname, std::string mname = "", std::map<std::string, std::string> substitutions = std::map<std::string, std::string>());

/// Extract a NEMLObject from a xml node as a unique_ptr
NEML_EXPORT std::unique_ptr<NEMLObject> get_object_unique(const rapidxml::xml_node<> * node);
Expand Down Expand Up @@ -113,6 +113,26 @@ class NodeNotFound: public std::exception {
std::string message_;
};

/// If a model is not found
class ModelNotFound: public std::exception {
public:
ModelNotFound(std::string model_name) :
{
std::stringstream ss;
ss << "Model with name " << model_name
<< " was not found in the supplied XML!";
message_ = ss.str();
};

const char * what() const throw ()
{
return message_.c_str();
};

private:
std::string message_;
};

/// If a node is not unique (and it should be)
class DuplicateNode: public std::exception {
public:
Expand Down

0 comments on commit b9031b0

Please sign in to comment.