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 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_.put(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   Array<string> names;
00081   Array<string> values;
00082   attributes_.arrayify(names, values);
00083 
00084   for (int i=0; i<names.length(); i++)
00085     {
00086       rtn += " " + names[i] + "=\"" + values[i] + "\"";
00087     }
00088   rtn += ">";
00089   return rtn;
00090 }
00091 
00092 string XMLObjectImplem::toString() const
00093 {
00094   string rtn = "<" + tag_;
00095       
00096   Array<string> names;
00097   Array<string> values;
00098   attributes_.arrayify(names, values);
00099   int i = 0;
00100   for (i=0; i<names.length(); i++)
00101     {
00102       rtn += " " + names[i] + "=\"" + values[i] + "\"";
00103     }
00104   if (content_.length()==0 && children_.length()==0) 
00105     {
00106       rtn += "/>\n" ;
00107     }
00108   else
00109     {
00110       rtn += ">\n";
00111       bool allBlankContent = true;
00112       for (i=0; i<content_.length(); i++)
00113         {
00114           if (!StrUtils::isWhite(content_[i])) 
00115             {
00116               allBlankContent=false;
00117               break;
00118             }
00119         }
00120       if (allBlankContent)
00121         {
00122           for (i=0; i<content_.length(); i++)
00123             {
00124               rtn += content_[i] + "\n";
00125             }
00126         }
00127       for (i=0; i<children_.length(); i++)
00128         {
00129           rtn += children_[i].toString();
00130         }
00131       rtn += "</" + tag_ + ">\n";
00132     }
00133   return rtn;
00134 }
00135 

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