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
14 typedef xmlpp::NodeSet NodeSet;
15 typedef xmlpp::Node::NodeList const_NodeList;
16#else
17 typedef xmlpp::Node::NodeSet NodeSet;
18 typedef xmlpp::Node::const_NodeList const_NodeList;
19#endif
20
21// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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::Node;
64 using xmlpp::TextNode;
65 using xmlpp::Element;
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 =
76 dynamic_cast<const Element*>(nodeset.at(index));
77
78 if (!element) {
79 throw CastError("Node: " + nodeset.at(index)->get_name() +
80 " is not an Element node.");
81 }
82
83 const const_NodeList nodelist = element->get_children();
84 if (nodelist.size() != 1) {
85 throw ValueError("Element node " + element->get_name() +
86 " has more content than expected.");
87 }
88
89 const TextNode* text =
90 dynamic_cast<const xmlpp::TextNode*>(element->get_children().front());
91
92 if (!text) {
93 throw CastError("Node: " + element->get_name() + " is not a Text node.");
94 }
95
96 return text->get_content();
97}
98
99// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100std::string InfileTree::GetElementName(int index) {
101 using xmlpp::Node;
102
103 std::vector<xmlpp::Element*> elements;
104 const Node::NodeList nodelist = current_node_->get_children();
105 Node::NodeList::const_iterator it;
106 for (it = nodelist.begin(); it != nodelist.end(); it++) {
107 xmlpp::Element* element = dynamic_cast<xmlpp::Element*>(*it);
108 if (element) {
109 elements.push_back(element);
110 }
111 }
112 if (elements.size() < index + 1) {
113 throw ValueError("Index exceeds number of elements in node: "
114 + current_node_->get_name());
115 }
116 return elements.at(index)->get_name();
117}
118
119// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121 using xmlpp::Node;
122
123 const NodeSet nodeset = current_node_->find(query);
124
125 if (nodeset.size() < index + 1) {
126 throw ValueError("Index exceeds number of nodes in query: " + query);
127 }
128
129 xmlpp::Element* element = dynamic_cast<xmlpp::Element*>(nodeset.at(index));
130
131 if (!element) {
132 throw CastError("Node: " + nodeset.at(index)->get_name() +
133 " is not an Element node.");
134 }
135
136 return new InfileTree(element);
137}
138
139InfileTree* InfileTree::SubTree(std::string query, int index) {
142 spawned_children_.insert(qe_child);
143 return qe_child;
144}
145
146} // namespace cyclus
For failed casts that shouldn't.
Definition error.h:65
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
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:47
For values that are too big, too small, etc.
Definition error.h:41
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
xmlpp::Node::NodeSet NodeSet
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
xmlpp::Node::const_NodeList const_NodeList