Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
14namespace Teuchos {
15
16
17XMLObject::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
37const std::string& XMLObject::getTag() const
38{
40 "XMLObject::getTag: XMLObject is empty");
41 return ptr_->getTag();
42}
43
44
45bool XMLObject::hasAttribute(const std::string& name) const
46{
48 "XMLObject::hasAttribute: XMLObject is empty");
49 return ptr_->hasAttribute(name);
50}
51
52
53const std::string& XMLObject::getAttribute(const std::string& name) const
54{
56 "XMLObject::getAttribute: XMLObject is empty");
57 return ptr_->getAttribute(name);
58}
59
60
61const 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
70template<>
71bool XMLObject::getRequired<bool>(const std::string& name) const
72{
73 return getRequiredBool(name);
74}
75
76
77template<>
78int XMLObject::getRequired<int>(const std::string& name) const
79{
80 return getRequiredInt(name);
81}
82
83
84template<>
85double XMLObject::getRequired<double>(const std::string& name) const
86{
87 return getRequiredDouble(name);
88}
89
90
91template<>
92std::string XMLObject::getRequired<std::string>(const std::string& name) const
93{
94 return getRequired(name);
95}
96
97
98bool 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
122template<>
123void 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
141{
143 "XMLObject::getChild: XMLObject is empty");
144 return ptr_->getChild(i);
145}
146
147int 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
166const std::string& XMLObject::getContentLine(int i) const
167{
169 "XMLObject::getContentLine: XMLObject is empty");
170 return ptr_->getContentLine(i);
171}
172
173
174std::string XMLObject::toString() const
175{
177 "XMLObject::toString: XMLObject is empty");
178 return ptr_->toString();
179}
180
181
182void XMLObject::print(std::ostream& os, int indent) const
183{
185 "XMLObject::print: XMLObject is empty");
186 ptr_->print(os, indent);
187}
188
189
190std::string XMLObject::header() const
191{
193 "XMLObject::header: XMLObject is empty");
194 return ptr_->header();
195}
196
197
199{
201 "XMLObject::terminatedHeader: XMLObject is empty");
202 return ptr_->terminatedHeader();
203}
204
205
206std::string XMLObject::footer() const
207{
209 "XMLObject::footer: XMLObject is empty");
210 return ptr_->footer();
211}
212
213
214void 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
224{
226 "XMLObject::addChild: XMLObject is empty");
227 ptr_->addChild(child);
228}
229
230
231void XMLObject::addContent(const std::string& contentLine)
232{
234 "XMLObject::addContent: XMLObject is empty");
235 ptr_->addContent(contentLine);
236}
237
238
239} // namespace Teuchos
A std::string utilities class for Teuchos.
An object representation of a subset of XML data.
Thrown when attempting to parse an empty XML std::string.
Smart reference counting pointer class for automatic garbage collection.
static std::string allCaps(const std::string &str)
Converts a std::string to all upper case.
The XMLObjectImplem class takes care of the low-level implementation details of XMLObject.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
double getRequiredDouble(const std::string &name) const
Get a required attribute, returning it as a double.
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 addChild(const XMLObject &child)
Add a child node to the node.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
const std::string & getTag() const
Return the tag of the current node.
const std::string & getAttribute(const std::string &name) const
Return the value of the attribute with the specified name.
std::string toString() const
Represent this node and its children as a std::string.
void checkTag(const std::string &expected) const
Check that a tag is equal to an expected std::string.
int numContentLines() const
Return the number of lines of character content stored in this node.
int getRequiredInt(const std::string &name) const
Get a required attribute, returning it as an int.
void addContent(const std::string &contentLine)
Add a line of character content.
std::string footer() const
Write the footer for this object to a std::string.
XMLObject deepCopy() const
Make a deep copy of this object.
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.
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 std::string & getContentLine(int i) const
Return the i-th line of character content stored in this node.
XMLObject()
Empty constructor.
std::string terminatedHeader() const
Write the header for this object to a std::string.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.