Teuchos_XMLObjectImplem.cpp

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 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 

Generated on Thu Sep 18 12:39:10 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1