Teuchos_XMLObjectImplem.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_XMLObject.hpp"
00030 #include "Teuchos_StrUtils.hpp"
00031 
00032 using namespace Teuchos;
00033 
00034 
00035 XMLObjectImplem::XMLObjectImplem(const std::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, std::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
00055 {
00056   return children_.length();
00057 }
00058 
00059 void XMLObjectImplem::addAttribute(const std::string& name,
00060            const std::string& value)
00061 {
00062   attributes_[name] = value;
00063 }
00064 
00065 void XMLObjectImplem::addChild(const XMLObject& child)
00066 {
00067   children_.append(child);
00068 }
00069 
00070 void XMLObjectImplem::addContent(const std::string& contentLine)
00071 {
00072   content_.append(contentLine);
00073 }
00074 
00075 const XMLObject& XMLObjectImplem::getChild(int i) const 
00076 {
00077   return children_[i];
00078 }
00079 
00080 std::string XMLObjectImplem::header(bool strictXML) const
00081 {
00082   std::string rtn = "<" + tag_;
00083   for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
00084   {
00085     if (strictXML)
00086     {
00087       rtn += " " 
00088   + (*i).first 
00089   + "="
00090   + XMLifyAttVal((*i).second);
00091     }
00092     else
00093     {
00094       rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
00095     }
00096   }
00097   
00098   rtn += ">";
00099   return rtn;
00100 }
00101 
00102 std::string XMLObjectImplem::XMLifyAttVal(const std::string &attval) {
00103   std::string ret;
00104   bool hasQuot, hasApos;
00105   char delim;
00106 
00107   if (attval.find("\"") == std::string::npos)
00108   {
00109     hasQuot = false;
00110   }
00111   else
00112   {
00113     hasQuot = true;
00114   }
00115 
00116   if (attval.find("\'") == std::string::npos)
00117   {
00118     hasApos = false;
00119   }
00120   else
00121   {
00122     hasApos = true;
00123   }
00124 
00125   if (!hasQuot || hasApos)
00126   {
00127     delim = '\"'; // wrap the attribute value in "
00128   }
00129   else
00130   {
00131     delim = '\''; // wrap the attribute value in '
00132   }
00133 
00134   // Rules:
00135   // "-wrapped std::string cannot contain a literal "
00136   // '-wrapped std::string cannot contain a literal '
00137   // attribute value cannot contain a literal <
00138   // attribute value cannot contain a literal &
00139   ret.push_back(delim);
00140   for (std::string::const_iterator i=attval.begin(); i != attval.end(); i++)
00141   {
00142     if (*i == delim)
00143     {
00144       if (delim == '\'') ret.append("&apos;");
00145       else if (delim == '\"') ret.append("&quot;");
00146     }
00147     else if (*i == '&')
00148     {
00149       ret.append("&amp;");
00150     }
00151     else if (*i == '<')
00152     {
00153       ret.append("&lt;");
00154     }
00155     else
00156     {
00157       ret.push_back(*i);  
00158     }
00159   }
00160   ret.push_back(delim);
00161 
00162   return ret;
00163 }
00164 
00165 std::string XMLObjectImplem::terminatedHeader(bool strictXML) const
00166 {
00167   std::string rtn = "<" + tag_;
00168   for (Map::const_iterator i=attributes_.begin(); i!=attributes_.end(); ++i)
00169   {
00170     if (strictXML)
00171     {
00172       rtn += " " 
00173   + (*i).first 
00174   + "="
00175   + XMLifyAttVal((*i).second);
00176     }
00177     else
00178     {
00179       rtn += " " + (*i).first + "=\"" + (*i).second + "\"";
00180     }
00181   }
00182 
00183   rtn += "/>";
00184   return rtn;
00185 }
00186 
00187 std::string XMLObjectImplem::toString() const
00188 {
00189   std::string rtn;
00190   if (content_.length()==0 && children_.length()==0) 
00191   {
00192     rtn = terminatedHeader() + "\n";
00193   }
00194   else
00195   {
00196     rtn = header() + "\n";
00197     bool allBlankContent = true;
00198     for (int i=0; i<content_.length(); i++)
00199     {
00200       if (!StrUtils::isWhite(content_[i])) 
00201       {
00202   allBlankContent=false;
00203   break;
00204       }
00205     }
00206     if (!allBlankContent)
00207     {
00208       for (int i=0; i<content_.length(); i++)
00209       {
00210   rtn += content_[i];
00211       }
00212       rtn += "\n";
00213     }
00214     for (int i=0; i<children_.length(); i++)
00215     {
00216       rtn += children_[i].toString();
00217     }
00218     rtn += "</" + tag_ + ">\n";
00219   }
00220   return rtn;
00221 }
00222 
00223 void XMLObjectImplem::print(std::ostream& os, int indent) const
00224 {
00225   for (int i=0; i<indent; i++) os << " ";
00226   if (content_.length()==0 && children_.length()==0) 
00227   {
00228     os << terminatedHeader(true) << std::endl;
00229     return;
00230   }
00231   else
00232   {
00233     os << header(true) << std::endl;
00234     printContent(os, indent+2);
00235     
00236     for (int i=0; i<children_.length(); i++)
00237     {
00238       children_[i].print(os, indent+2);
00239     }
00240     for (int i=0; i<indent; i++) os << " ";
00241     os << "</" << tag_ << ">\n";
00242   }
00243 }
00244 
00245 void XMLObjectImplem::printContent(std::ostream& os, int indent) const 
00246 {
00247   std::string space = "";
00248   for (int i=0; i<indent; i++) space += " ";
00249 
00250   bool allBlankContent = true;
00251   for (int i=0; i<content_.length(); i++)
00252   {
00253     if (!StrUtils::isWhite(content_[i])) 
00254     {
00255       allBlankContent=false;
00256       break;
00257     }
00258   }
00259   
00260   if (!allBlankContent) 
00261   {
00262     os << space;
00263     for (int i=0; i<content_.length(); i++)
00264     {
00265       os << content_[i];
00266     }
00267     os << '\n';
00268   }
00269 }
00270 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:14:01 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1