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