00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "Teuchos_XMLObject.hpp"
00030 #include "Teuchos_StrUtils.hpp"
00031
00032 using namespace Teuchos;
00033
00034
00035 XMLObjectImplem::XMLObjectImplem(const string& tag)
00036 : tag_(tag), attributes_(), children_(0), content_(0)
00037 {;}
00038
00039 XMLObjectImplem* XMLObjectImplem::deepCopy() const
00040 {
00041 XMLObjectImplem* rtn = new XMLObjectImplem(tag_);
00042 TEST_FOR_EXCEPTION(rtn==0, runtime_error, "XMLObjectImplem::deepCopy()");
00043 rtn->attributes_ = attributes_;
00044 rtn->content_ = content_;
00045
00046 for (int i=0; i<children_.length(); i++)
00047 {
00048 rtn->addChild(children_[i].deepCopy());
00049 }
00050
00051 return rtn;
00052 }
00053
00054 int XMLObjectImplem::numChildren() const {return children_.length();}
00055
00056 void XMLObjectImplem::addAttribute(const string& name, const string& value)
00057 {
00058 attributes_[name] = value;
00059 }
00060
00061 void XMLObjectImplem::addChild(const XMLObject& child)
00062 {
00063 children_.append(child);
00064 }
00065
00066 void XMLObjectImplem::addContent(const string& contentLine)
00067 {
00068 content_.append(contentLine);
00069 }
00070
00071 const XMLObject& XMLObjectImplem::getChild(int i) const
00072 {
00073 return children_[i];
00074 }
00075
00076 string XMLObjectImplem::header() const
00077 {
00078 string rtn = "<" + tag_;
00079
00080 for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
00081 {
00082 rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
00083 }
00084
00085 rtn += ">";
00086 return rtn;
00087 }
00088
00089 string XMLObjectImplem::terminatedHeader() const
00090 {
00091 string rtn = "<" + tag_;
00092
00093 for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
00094 {
00095 rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
00096 }
00097
00098 rtn += "/>";
00099
00100 return rtn;
00101 }
00102
00103 string XMLObjectImplem::toString() const
00104 {
00105 string rtn;
00106
00107 if (content_.length()==0 && children_.length()==0)
00108 {
00109 rtn= terminatedHeader() + "\n";
00110 }
00111 else
00112 {
00113 rtn = header() + "\n";
00114 bool allBlankContent = true;
00115 for (int i=0; i<content_.length(); i++)
00116 {
00117 if (!StrUtils::isWhite(content_[i]))
00118 {
00119 allBlankContent=false;
00120 break;
00121 }
00122 }
00123 if (allBlankContent)
00124 {
00125 for (int i=0; i<content_.length(); i++)
00126 {
00127 rtn += content_[i] + "\n";
00128 }
00129 }
00130 for (int i=0; i<children_.length(); i++)
00131 {
00132 rtn += children_[i].toString();
00133 }
00134 rtn += "</" + tag_ + ">\n";
00135 }
00136 return rtn;
00137 }
00138
00139 void XMLObjectImplem::print(ostream& os, int indent) const
00140 {
00141 for (int i=0; i<indent; i++) os << " ";
00142
00143
00144
00145 if (content_.length()==0 && children_.length()==0)
00146 {
00147 os << terminatedHeader() << endl;
00148 return;
00149 }
00150 else
00151 {
00152 os << header() << endl;
00153 printContent(os, indent+2);
00154
00155 for (int i=0; i<children_.length(); i++)
00156 {
00157 children_[i].print(os, indent+2);
00158 }
00159 for (int i=0; i<indent; i++) os << " ";
00160 os << "</" << tag_ << ">\n";
00161 }
00162 }
00163
00164 void XMLObjectImplem::printContent(ostream& os, int indent) const
00165 {
00166 string space = "";
00167 for (int i=0; i<indent; i++) space += " ";
00168
00169 bool allBlankContent = true;
00170 for (int i=0; i<content_.length(); i++)
00171 {
00172 if (!StrUtils::isWhite(content_[i]))
00173 {
00174 allBlankContent=false;
00175 break;
00176 }
00177 }
00178
00179 if (allBlankContent)
00180 {
00181 for (int i=0; i<content_.length(); i++)
00182 {
00183 os << space << content_[i];
00184 }
00185 }
00186 }
00187