14 using namespace Teuchos;
18 : tag_(tag), attributes_(), children_(0), content_(0)
25 rtn->attributes_ = attributes_;
26 rtn->content_ = content_;
28 for (
int i=0; i<children_.length(); i++)
38 return children_.length();
42 const std::string& value)
44 attributes_[name] = value;
49 children_.append(child);
54 content_.
append(contentLine);
71 std::string rtn =
"<" + tag_;
72 for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
79 + XMLifyAttVal((*i).second);
83 rtn +=
" " + (*i).first +
"=\"" + (*i).second +
"\"";
91 std::string XMLObjectImplem::XMLifyAttVal(
const std::string &attval) {
93 bool hasQuot, hasApos;
96 if (attval.find(
"\"") == std::string::npos)
105 if (attval.find(
"\'") == std::string::npos)
114 if (!hasQuot || hasApos)
128 ret.push_back(delim);
129 for (std::string::const_iterator i=attval.begin(); i != attval.end(); i++)
133 if (delim ==
'\'') ret.append(
"'");
134 else if (delim ==
'\"') ret.append(
""");
149 ret.push_back(delim);
156 std::string rtn =
"<" + tag_;
157 for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
164 + XMLifyAttVal((*i).second);
168 rtn +=
" " + (*i).first +
"=\"" + (*i).second +
"\"";
179 if (content_.
length()==0 && children_.length()==0)
186 bool allBlankContent =
true;
187 for (
int i=0; i<content_.
length(); i++)
191 allBlankContent=
false;
195 if (!allBlankContent)
197 for (
int i=0; i<content_.
length(); i++)
203 for (
int i=0; i<children_.length(); i++)
205 rtn += children_[i].toString();
207 rtn +=
"</" + tag_ +
">\n";
214 for (
int i=0; i<indent; i++) os <<
" ";
215 if (content_.
length()==0 && children_.length()==0)
222 os <<
header(
true) << std::endl;
223 printContent(os, indent+2);
225 for (
int i=0; i<children_.length(); i++)
227 children_[i].print(os, indent+2);
229 for (
int i=0; i<indent; i++) os <<
" ";
230 os <<
"</" << tag_ <<
">\n";
234 void XMLObjectImplem::printContent(std::ostream& os,
int indent)
const
236 std::string space =
"";
237 for (
int i=0; i<indent; i++) space +=
" ";
239 bool allBlankContent =
true;
240 for (
int i=0; i<content_.
length(); i++)
244 allBlankContent=
false;
249 if (!allBlankContent)
252 for (
int i=0; i<content_.
length(); i++)
255 std::string s(content_[i]);
256 s.erase(
size_t(0), s.find_first_not_of(
" \r\t"));
258 os << space << s <<
'\n';
std::string toString() const
Write as a std::string. Output may be ill-formed XML.
Array< T > & append(const T &x)
Add a new entry at the end of the array.
void addChild(const XMLObject &child)
Add a child XMLObject.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
XMLObjectImplem * deepCopy() const
Deep copy.
const XMLObject & getChild(int i) const
Look up a child by its index.
iterator erase(iterator position)
A std::string utilities class for Teuchos.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
void addContent(const std::string &contentLine)
Add a content line.
std::string terminatedHeader(bool strictXML=false) const
Write the header terminated as <Header>
static bool isWhite(const std::string &str)
Returns true if a std::string consists entirely of whitespace.
XMLObjectImplem(const std::string &tag)
Construct with a 'tag'.
void removeContentLine(const size_t &i)
Remove content line by index.
int length() const
Return number of elements in the array.
void print(std::ostream &os, int indent) const
Print to stream with the given indentation level. Output will be well-formed XML. ...
std::string header(bool strictXML=false) const
Write the header.
The XMLObjectImplem class takes care of the low-level implementation details of XMLObject.
void addAttribute(const std::string &name, const std::string &value)
Add a [name, value] attribute.
int numChildren() const
Return the number of children.
An object representation of a subset of XML data.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...