Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_XMLObject.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_XMLObject.hpp"
11 #include "Teuchos_StrUtils.hpp"
12 
13 
14 namespace Teuchos {
15 
16 
17 XMLObject::XMLObject(const std::string& tag)
18  : ptr_(rcp(new XMLObjectImplem(tag)))
19 {}
20 
21 
23  : ptr_(rcp(ptr))
24 {}
25 
26 
28 {
29  if (is_null(ptr_))
30  {
31  return XMLObject();
32  }
33  return XMLObject(ptr_->deepCopy());
34 }
35 
36 
37 const std::string& XMLObject::getTag() const
38 {
40  "XMLObject::getTag: XMLObject is empty");
41  return ptr_->getTag();
42 }
43 
44 
45 bool XMLObject::hasAttribute(const std::string& name) const
46 {
48  "XMLObject::hasAttribute: XMLObject is empty");
49  return ptr_->hasAttribute(name);
50 }
51 
52 
53 const std::string& XMLObject::getAttribute(const std::string& name) const
54 {
56  "XMLObject::getAttribute: XMLObject is empty");
57  return ptr_->getAttribute(name);
58 }
59 
60 
61 const std::string& XMLObject::getRequired(const std::string& name) const
62 {
63  TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
64  "XMLObject::getRequired: key "
65  << name << " not found");
66  return getAttribute(name);
67 }
68 
69 
70 template<>
71 bool XMLObject::getRequired<bool>(const std::string& name) const
72 {
73  return getRequiredBool(name);
74 }
75 
76 
77 template<>
78 int XMLObject::getRequired<int>(const std::string& name) const
79 {
80  return getRequiredInt(name);
81 }
82 
83 
84 template<>
85 double XMLObject::getRequired<double>(const std::string& name) const
86 {
87  return getRequiredDouble(name);
88 }
89 
90 
91 template<>
92 std::string XMLObject::getRequired<std::string>(const std::string& name) const
93 {
94  return getRequired(name);
95 }
96 
97 
98 bool XMLObject::getRequiredBool(const std::string& name) const
99 {
100  TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
101  "XMLObject::getRequired: key "
102  << name << " not found");
103  std::string val = StrUtils::allCaps(getRequired(name));
104 
105  TEUCHOS_TEST_FOR_EXCEPTION( val!="TRUE" && val!="YES" && val!="1"
106  && val!="FALSE" && val!="NO" && val!="0",
107  std::runtime_error,
108  "XMLObject::getRequiredBool value [" << val
109  << "] should have been {TRUE|FALSE|YES|NO|0|1}");
110 
111  if (val=="TRUE" || val=="YES" || val=="1")
112  {
113  return true;
114  }
115  else
116  {
117  return false;
118  }
119 }
120 
121 
122 template<>
123 void XMLObject::addAttribute<const std::string&>(
124  const std::string& name, const std::string& value)
125 {
127  "XMLObject::addAttribute: XMLObject is empty");
128  ptr_->addAttribute(name, value);
129 }
130 
131 
133 {
135  "XMLObject::numChildren: XMLObject is empty");
136  return ptr_->numChildren();
137 }
138 
139 
140 const XMLObject& XMLObject::getChild(int i) const
141 {
143  "XMLObject::getChild: XMLObject is empty");
144  return ptr_->getChild(i);
145 }
146 
147 int XMLObject::findFirstChild(std::string name) const{
149  "XMLObject::getChild: XMLObject is empty");
150  for(int i = 0; i<numChildren(); ++i){
151  if(getChild(i).getTag() == name){
152  return i;
153  }
154  }
155  return -1;
156 }
157 
159 {
161  "XMLObject::numContentLines: XMLObject is empty");
162  return ptr_->numContentLines();
163 }
164 
165 
166 const std::string& XMLObject::getContentLine(int i) const
167 {
169  "XMLObject::getContentLine: XMLObject is empty");
170  return ptr_->getContentLine(i);
171 }
172 
173 
174 std::string XMLObject::toString() const
175 {
177  "XMLObject::toString: XMLObject is empty");
178  return ptr_->toString();
179 }
180 
181 
182 void XMLObject::print(std::ostream& os, int indent) const
183 {
185  "XMLObject::print: XMLObject is empty");
186  ptr_->print(os, indent);
187 }
188 
189 
190 std::string XMLObject::header() const
191 {
193  "XMLObject::header: XMLObject is empty");
194  return ptr_->header();
195 }
196 
197 
198 std::string XMLObject::terminatedHeader() const
199 {
201  "XMLObject::terminatedHeader: XMLObject is empty");
202  return ptr_->terminatedHeader();
203 }
204 
205 
206 std::string XMLObject::footer() const
207 {
209  "XMLObject::footer: XMLObject is empty");
210  return ptr_->footer();
211 }
212 
213 
214 void XMLObject::checkTag(const std::string& expected) const
215 {
216  TEUCHOS_TEST_FOR_EXCEPTION(getTag() != expected, std::runtime_error,
217  "XMLObject::checkTag error: expected <"
218  << expected << ">, found <"
219  << getTag() << ">");
220 }
221 
222 
223 void XMLObject::addChild(const XMLObject& child)
224 {
226  "XMLObject::addChild: XMLObject is empty");
227  ptr_->addChild(child);
228 }
229 
230 
231 void XMLObject::addContent(const std::string& contentLine)
232 {
234  "XMLObject::addContent: XMLObject is empty");
235  ptr_->addContent(contentLine);
236 }
237 
238 
239 } // namespace Teuchos
const std::string & getTag() const
Return the tag of the current node.
std::string footer() const
Write the footer for this object to a std::string.
static std::string allCaps(const std::string &str)
Converts a std::string to all upper case.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void addChild(const XMLObject &child)
Add a child node to the node.
XMLObject deepCopy() const
Make a deep copy of this object.
XMLObject()
Empty constructor.
A std::string utilities class for Teuchos.
Thrown when attempting to parse an empty XML std::string.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::string toString() const
Represent this node and its children as a std::string.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
std::string terminatedHeader() const
Write the header for this object to a std::string.
std::string header() const
Write the header for this object to a std::string.
bool getRequiredBool(const std::string &name) const
Get a required attribute, returning it as a bool.
void print(std::ostream &os, int indent) const
Print this node and its children to stream with the given indentation.
int numChildren() const
Return the number of child nodes owned by this node.
void addContent(const std::string &contentLine)
Add a line of character content.
const std::string & getAttribute(const std::string &name) const
Return the value of the attribute with the specified name.
int numContentLines() const
Return the number of lines of character content stored in this node.
void checkTag(const std::string &expected) const
Check that a tag is equal to an expected std::string.
The XMLObjectImplem class takes care of the low-level implementation details of XMLObject.
int findFirstChild(std::string tagName) const
Returns the index of the first child found with the given tag name. Returns -1 if no child is found...
const XMLObject & getChild(int i) const
Return the i-th child node.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
const std::string & getContentLine(int i) const
Return the i-th line of character content stored in this node.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
An object representation of a subset of XML data.