Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_XMLObject.cpp
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#include "Teuchos_XMLObject.hpp"
43#include "Teuchos_StrUtils.hpp"
44
45
46namespace Teuchos {
47
48
49XMLObject::XMLObject(const std::string& tag)
50 : ptr_(rcp(new XMLObjectImplem(tag)))
51{}
52
53
55 : ptr_(rcp(ptr))
56{}
57
58
60{
61 if (is_null(ptr_))
62 {
63 return XMLObject();
64 }
65 return XMLObject(ptr_->deepCopy());
66}
67
68
69const std::string& XMLObject::getTag() const
70{
72 "XMLObject::getTag: XMLObject is empty");
73 return ptr_->getTag();
74}
75
76
77bool XMLObject::hasAttribute(const std::string& name) const
78{
80 "XMLObject::hasAttribute: XMLObject is empty");
81 return ptr_->hasAttribute(name);
82}
83
84
85const std::string& XMLObject::getAttribute(const std::string& name) const
86{
88 "XMLObject::getAttribute: XMLObject is empty");
89 return ptr_->getAttribute(name);
90}
91
92
93const std::string& XMLObject::getRequired(const std::string& name) const
94{
95 TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
96 "XMLObject::getRequired: key "
97 << name << " not found");
98 return getAttribute(name);
99}
100
101
102template<>
103bool XMLObject::getRequired<bool>(const std::string& name) const
104{
105 return getRequiredBool(name);
106}
107
108
109template<>
110int XMLObject::getRequired<int>(const std::string& name) const
111{
112 return getRequiredInt(name);
113}
114
115
116template<>
117double XMLObject::getRequired<double>(const std::string& name) const
118{
119 return getRequiredDouble(name);
120}
121
122
123template<>
124std::string XMLObject::getRequired<std::string>(const std::string& name) const
125{
126 return getRequired(name);
127}
128
129
130bool XMLObject::getRequiredBool(const std::string& name) const
131{
132 TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
133 "XMLObject::getRequired: key "
134 << name << " not found");
135 std::string val = StrUtils::allCaps(getRequired(name));
136
137 TEUCHOS_TEST_FOR_EXCEPTION( val!="TRUE" && val!="YES" && val!="1"
138 && val!="FALSE" && val!="NO" && val!="0",
139 std::runtime_error,
140 "XMLObject::getRequiredBool value [" << val
141 << "] should have been {TRUE|FALSE|YES|NO|0|1}");
142
143 if (val=="TRUE" || val=="YES" || val=="1")
144 {
145 return true;
146 }
147 else
148 {
149 return false;
150 }
151}
152
153
154template<>
155void XMLObject::addAttribute<const std::string&>(
156 const std::string& name, const std::string& value)
157{
159 "XMLObject::addAttribute: XMLObject is empty");
160 ptr_->addAttribute(name, value);
161}
162
163
165{
167 "XMLObject::numChildren: XMLObject is empty");
168 return ptr_->numChildren();
169}
170
171
173{
175 "XMLObject::getChild: XMLObject is empty");
176 return ptr_->getChild(i);
177}
178
179int XMLObject::findFirstChild(std::string name) const{
181 "XMLObject::getChild: XMLObject is empty");
182 for(int i = 0; i<numChildren(); ++i){
183 if(getChild(i).getTag() == name){
184 return i;
185 }
186 }
187 return -1;
188}
189
191{
193 "XMLObject::numContentLines: XMLObject is empty");
194 return ptr_->numContentLines();
195}
196
197
198const std::string& XMLObject::getContentLine(int i) const
199{
201 "XMLObject::getContentLine: XMLObject is empty");
202 return ptr_->getContentLine(i);
203}
204
205
206std::string XMLObject::toString() const
207{
209 "XMLObject::toString: XMLObject is empty");
210 return ptr_->toString();
211}
212
213
214void XMLObject::print(std::ostream& os, int indent) const
215{
217 "XMLObject::print: XMLObject is empty");
218 ptr_->print(os, indent);
219}
220
221
222std::string XMLObject::header() const
223{
225 "XMLObject::header: XMLObject is empty");
226 return ptr_->header();
227}
228
229
231{
233 "XMLObject::terminatedHeader: XMLObject is empty");
234 return ptr_->terminatedHeader();
235}
236
237
238std::string XMLObject::footer() const
239{
241 "XMLObject::footer: XMLObject is empty");
242 return ptr_->footer();
243}
244
245
246void XMLObject::checkTag(const std::string& expected) const
247{
248 TEUCHOS_TEST_FOR_EXCEPTION(getTag() != expected, std::runtime_error,
249 "XMLObject::checkTag error: expected <"
250 << expected << ">, found <"
251 << getTag() << ">");
252}
253
254
256{
258 "XMLObject::addChild: XMLObject is empty");
259 ptr_->addChild(child);
260}
261
262
263void XMLObject::addContent(const std::string& contentLine)
264{
266 "XMLObject::addContent: XMLObject is empty");
267 ptr_->addContent(contentLine);
268}
269
270
271} // 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.
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.