CYCLUS
Loading...
Searching...
No Matches
infile_tree.cc
Go to the documentation of this file.
1// Implements class for querying XML snippets
2#include <iostream>
3#include <sstream>
4
5#include <libxml++/libxml++.h>
6#include <boost/lexical_cast.hpp>
7
8#include "error.h"
9#include "infile_tree.h"
10
11namespace cyclus {
12
13#if LIBXMLXX_MAJOR_VERSION == 2
14typedef xmlpp::NodeSet NodeSet;
15typedef xmlpp::Node::NodeList const_NodeList;
16#else
17typedef xmlpp::Node::NodeSet NodeSet;
18typedef xmlpp::Node::const_NodeList const_NodeList;
19#endif
20
21// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22InfileTree::InfileTree(XMLParser& parser) : current_node_(0) {
23 current_node_ = parser.Document()->get_root_node();
24}
25
26// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27InfileTree::InfileTree(xmlpp::Node* node) : current_node_(0) {
28 current_node_ = node;
29}
30
32 while (!spawned_children_.empty()) {
33 InfileTree* qe_child = *spawned_children_.begin();
34 spawned_children_.erase(spawned_children_.begin());
35 if (qe_child) {
36 delete qe_child;
37 }
38 }
39}
40
41// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43 using xmlpp::Element;
44 int n = 0;
45 const xmlpp::Node::NodeList nodelist = current_node_->get_children();
46 xmlpp::Node::NodeList::const_iterator it;
47 for (it = nodelist.begin(); it != nodelist.end(); it++) {
48 const Element* element = dynamic_cast<const Element*>(*it);
49 if (element) {
50 n++;
51 }
52 }
53 return n;
54}
55
56// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57int InfileTree::NMatches(std::string query) {
58 return current_node_->find(query).size();
59}
60
61// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62std::string InfileTree::GetString(std::string query, int index) {
63 using xmlpp::Element;
64 using xmlpp::Node;
65 using xmlpp::TextNode;
66 const NodeSet nodeset = current_node_->find(query);
67 if (nodeset.empty()) {
68 throw KeyError("Could not find a node by the name: " + query);
69 }
70
71 if (nodeset.size() < index + 1) {
72 throw ValueError("Index exceeds number of nodes in query: " + query);
73 }
74
75 const Element* element = dynamic_cast<const Element*>(nodeset.at(index));
76
77 if (!element) {
78 throw CastError("Node: " + nodeset.at(index)->get_name() +
79 " is not an Element node.");
80 }
81
82 const const_NodeList nodelist = element->get_children();
83 if (nodelist.size() != 1) {
84 throw ValueError("Element node " + element->get_name() +
85 " has more content than expected.");
86 }
87
88 const TextNode* text =
89 dynamic_cast<const xmlpp::TextNode*>(element->get_children().front());
90
91 if (!text) {
92 throw CastError("Node: " + element->get_name() + " is not a Text node.");
93 }
94
95 return text->get_content();
96}
97
98// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99std::string InfileTree::GetElementName(int index) {
100 using xmlpp::Node;
101
102 std::vector<xmlpp::Element*> elements;
103 const Node::NodeList nodelist = current_node_->get_children();
104 Node::NodeList::const_iterator it;
105 for (it = nodelist.begin(); it != nodelist.end(); it++) {
106 xmlpp::Element* element = dynamic_cast<xmlpp::Element*>(*it);
107 if (element) {
108 elements.push_back(element);
109 }
110 }
111 if (elements.size() < index + 1) {
112 throw ValueError("Index exceeds number of elements in node: " +
113 current_node_->get_name());
114 }
115 return elements.at(index)->get_name();
116}
117
118// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119InfileTree* InfileTree::GetEngineFromQuery(std::string query, int index) {
120 using xmlpp::Node;
121
122 const NodeSet nodeset = current_node_->find(query);
123
124 if (nodeset.size() < index + 1) {
125 throw ValueError("Index exceeds number of nodes in query: " + query);
126 }
127
128 xmlpp::Element* element = dynamic_cast<xmlpp::Element*>(nodeset.at(index));
129
130 if (!element) {
131 throw CastError("Node: " + nodeset.at(index)->get_name() +
132 " is not an Element node.");
133 }
134
135 return new InfileTree(element);
136}
137
138InfileTree* InfileTree::SubTree(std::string query, int index) {
139 InfileTree* qe_child = GetEngineFromQuery(query, index);
140 spawned_children_.insert(qe_child);
141 return qe_child;
142}
143
144} // namespace cyclus
For failed casts that shouldn't.
Definition error.h:61
virtual int NElements()
virtual InfileTree * GetEngineFromQuery(std::string query, int index)
every derived infile must return a new instance initialized by a query.
virtual ~InfileTree()
InfileTree(XMLParser &parser)
constructor given a parser
virtual int NMatches(std::string query)
investigates the current status and returns the number of elements matching a query
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
For failed retrieval/insertion of key-based data into/from data structures.
Definition error.h:43
For values that are too big, too small, etc.
Definition error.h:37
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
xmlpp::Document * Document()
Definition xml_parser.cc:61
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
xmlpp::Node::NodeSet NodeSet
xmlpp::Node::const_NodeList const_NodeList