Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_XMLObjectImplem.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 using namespace Teuchos;
46 
47 
48 XMLObjectImplem::XMLObjectImplem(const std::string& tag)
49  : tag_(tag), attributes_(), children_(0), content_(0)
50 {;}
51 
53 {
54  XMLObjectImplem* rtn = new XMLObjectImplem(tag_);
55  TEUCHOS_TEST_FOR_EXCEPTION(rtn==0, std::runtime_error, "XMLObjectImplem::deepCopy()");
56  rtn->attributes_ = attributes_;
57  rtn->content_ = content_;
58 
59  for (int i=0; i<children_.length(); i++)
60  {
61  rtn->addChild(children_[i].deepCopy());
62  }
63 
64  return rtn;
65 }
66 
68 {
69  return children_.length();
70 }
71 
72 void XMLObjectImplem::addAttribute(const std::string& name,
73  const std::string& value)
74 {
75  attributes_[name] = value;
76 }
77 
79 {
80  children_.append(child);
81 }
82 
83 void XMLObjectImplem::addContent(const std::string& contentLine)
84 {
85  content_.append(contentLine);
86 }
87 
89 {
90  return children_[i];
91 }
92 
93 std::string XMLObjectImplem::header(bool strictXML) const
94 {
95  std::string rtn = "<" + tag_;
96  for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
97  {
98  if (strictXML)
99  {
100  rtn += " "
101  + (*i).first
102  + "="
103  + XMLifyAttVal((*i).second);
104  }
105  else
106  {
107  rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
108  }
109  }
110 
111  rtn += ">";
112  return rtn;
113 }
114 
115 std::string XMLObjectImplem::XMLifyAttVal(const std::string &attval) {
116  std::string ret;
117  bool hasQuot, hasApos;
118  char delim;
119 
120  if (attval.find("\"") == std::string::npos)
121  {
122  hasQuot = false;
123  }
124  else
125  {
126  hasQuot = true;
127  }
128 
129  if (attval.find("\'") == std::string::npos)
130  {
131  hasApos = false;
132  }
133  else
134  {
135  hasApos = true;
136  }
137 
138  if (!hasQuot || hasApos)
139  {
140  delim = '\"'; // wrap the attribute value in "
141  }
142  else
143  {
144  delim = '\''; // wrap the attribute value in '
145  }
146 
147  // Rules:
148  // "-wrapped std::string cannot contain a literal "
149  // '-wrapped std::string cannot contain a literal '
150  // attribute value cannot contain a literal <
151  // attribute value cannot contain a literal &
152  ret.push_back(delim);
153  for (std::string::const_iterator i=attval.begin(); i != attval.end(); i++)
154  {
155  if (*i == delim)
156  {
157  if (delim == '\'') ret.append("&apos;");
158  else if (delim == '\"') ret.append("&quot;");
159  }
160  else if (*i == '&')
161  {
162  ret.append("&amp;");
163  }
164  else if (*i == '<')
165  {
166  ret.append("&lt;");
167  }
168  else
169  {
170  ret.push_back(*i);
171  }
172  }
173  ret.push_back(delim);
174 
175  return ret;
176 }
177 
178 std::string XMLObjectImplem::terminatedHeader(bool strictXML) const
179 {
180  std::string rtn = "<" + tag_;
181  for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
182  {
183  if (strictXML)
184  {
185  rtn += " "
186  + (*i).first
187  + "="
188  + XMLifyAttVal((*i).second);
189  }
190  else
191  {
192  rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
193  }
194  }
195 
196  rtn += "/>";
197  return rtn;
198 }
199 
200 std::string XMLObjectImplem::toString() const
201 {
202  std::string rtn;
203  if (content_.length()==0 && children_.length()==0)
204  {
205  rtn = terminatedHeader() + "\n";
206  }
207  else
208  {
209  rtn = header() + "\n";
210  bool allBlankContent = true;
211  for (int i=0; i<content_.length(); i++)
212  {
213  if (!StrUtils::isWhite(content_[i]))
214  {
215  allBlankContent=false;
216  break;
217  }
218  }
219  if (!allBlankContent)
220  {
221  for (int i=0; i<content_.length(); i++)
222  {
223  rtn += content_[i];
224  }
225  rtn += "\n";
226  }
227  for (int i=0; i<children_.length(); i++)
228  {
229  rtn += children_[i].toString();
230  }
231  rtn += "</" + tag_ + ">\n";
232  }
233  return rtn;
234 }
235 
236 void XMLObjectImplem::print(std::ostream& os, int indent) const
237 {
238  for (int i=0; i<indent; i++) os << " ";
239  if (content_.length()==0 && children_.length()==0)
240  {
241  os << terminatedHeader(true) << std::endl;
242  return;
243  }
244  else
245  {
246  os << header(true) << std::endl;
247  printContent(os, indent+2);
248 
249  for (int i=0; i<children_.length(); i++)
250  {
251  children_[i].print(os, indent+2);
252  }
253  for (int i=0; i<indent; i++) os << " ";
254  os << "</" << tag_ << ">\n";
255  }
256 }
257 
258 void XMLObjectImplem::printContent(std::ostream& os, int indent) const
259 {
260  std::string space = "";
261  for (int i=0; i<indent; i++) space += " ";
262 
263  bool allBlankContent = true;
264  for (int i=0; i<content_.length(); i++)
265  {
266  if (!StrUtils::isWhite(content_[i]))
267  {
268  allBlankContent=false;
269  break;
270  }
271  }
272 
273  if (!allBlankContent)
274  {
275  os << space;
276  for (int i=0; i<content_.length(); i++)
277  {
278  os << content_[i];
279  }
280  os << '\n';
281  }
282 }
283 
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.
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 &#39;tag&#39;.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.