Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_XMLObject.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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_XMLObject.hpp"
00043 #include "Teuchos_StrUtils.hpp"
00044 
00045 
00046 namespace Teuchos {
00047 
00048 
00049 XMLObject::XMLObject(const std::string& tag)
00050   : ptr_(rcp(new XMLObjectImplem(tag)))
00051 {}
00052 
00053 
00054 XMLObject::XMLObject(XMLObjectImplem* ptr)
00055   : ptr_(rcp(ptr))
00056 {}
00057 
00058 
00059 XMLObject XMLObject::deepCopy() const
00060 {
00061   if (is_null(ptr_))
00062   {
00063     return XMLObject();
00064   }
00065   return XMLObject(ptr_->deepCopy());
00066 }
00067 
00068 
00069 const std::string& XMLObject::getTag() const
00070 {
00071   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00072          "XMLObject::getTag: XMLObject is empty");
00073   return ptr_->getTag();
00074 }
00075 
00076 
00077 bool XMLObject::hasAttribute(const std::string& name) const 
00078 {
00079   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00080          "XMLObject::hasAttribute: XMLObject is empty");
00081   return ptr_->hasAttribute(name);
00082 }
00083 
00084 
00085 const std::string& XMLObject::getAttribute(const std::string& name) const 
00086 {
00087   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00088          "XMLObject::getAttribute: XMLObject is empty");
00089   return ptr_->getAttribute(name);
00090 }
00091 
00092 
00093 const std::string& XMLObject::getRequired(const std::string& name) const 
00094 {
00095   TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
00096                      "XMLObject::getRequired: key " 
00097                      << name << " not found");
00098   return getAttribute(name);
00099 }
00100 
00101 
00102 template<>
00103 bool XMLObject::getRequired<bool>(const std::string& name) const
00104 {
00105   return getRequiredBool(name);
00106 }
00107 
00108 
00109 template<>
00110 int XMLObject::getRequired<int>(const std::string& name) const
00111 {
00112   return getRequiredInt(name);
00113 }
00114 
00115 
00116 template<>
00117 double XMLObject::getRequired<double>(const std::string& name) const
00118 {
00119   return getRequiredDouble(name);
00120 }
00121 
00122 
00123 template<>
00124 std::string XMLObject::getRequired<std::string>(const std::string& name) const
00125 {
00126   return getRequired(name);
00127 }
00128 
00129 
00130 bool XMLObject::getRequiredBool(const std::string& name) const
00131 {
00132   TEUCHOS_TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error,
00133                      "XMLObject::getRequired: key " 
00134                      << name << " not found");
00135   std::string val = StrUtils::allCaps(getRequired(name));
00136 
00137   TEUCHOS_TEST_FOR_EXCEPTION( val!="TRUE" && val!="YES" && val!="1"
00138     && val!="FALSE" && val!="NO" && val!="0",
00139     std::runtime_error,
00140     "XMLObject::getRequiredBool value [" << val 
00141     << "] should have been {TRUE|FALSE|YES|NO|0|1}");
00142     
00143   if (val=="TRUE" || val=="YES" || val=="1")
00144   {
00145     return true;
00146   }
00147   else 
00148   {
00149     return false;
00150   }
00151 }
00152 
00153 
00154 template<>
00155 void XMLObject::addAttribute<const std::string&>(
00156   const std::string& name, const std::string& value)
00157 {
00158   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00159          "XMLObject::addAttribute: XMLObject is empty");
00160   ptr_->addAttribute(name, value);
00161 }
00162 
00163 
00164 int XMLObject::numChildren() const
00165 {
00166   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00167          "XMLObject::numChildren: XMLObject is empty");
00168   return ptr_->numChildren();
00169 }
00170 
00171 
00172 const XMLObject& XMLObject::getChild(int i) const
00173 {
00174   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00175          "XMLObject::getChild: XMLObject is empty");
00176   return ptr_->getChild(i);
00177 }
00178 
00179 int XMLObject::findFirstChild(std::string name) const{
00180   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00181          "XMLObject::getChild: XMLObject is empty");
00182   for(int i = 0; i<numChildren(); ++i){
00183     if(getChild(i).getTag() == name){
00184       return i;
00185     }
00186   }
00187   return -1;
00188 }
00189 
00190 int XMLObject::numContentLines() const
00191 {
00192   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00193          "XMLObject::numContentLines: XMLObject is empty");
00194   return ptr_->numContentLines();
00195 }
00196 
00197 
00198 const std::string& XMLObject::getContentLine(int i) const
00199 {
00200   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00201          "XMLObject::getContentLine: XMLObject is empty");
00202   return ptr_->getContentLine(i);
00203 }
00204 
00205 
00206 std::string XMLObject::toString() const
00207 {
00208   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00209          "XMLObject::toString: XMLObject is empty");
00210   return ptr_->toString();
00211 }
00212 
00213 
00214 void XMLObject::print(std::ostream& os, int indent) const
00215 {
00216   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00217          "XMLObject::print: XMLObject is empty");
00218   ptr_->print(os, indent);
00219 }
00220 
00221 
00222 std::string XMLObject::header() const
00223 {
00224   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00225          "XMLObject::header: XMLObject is empty");
00226   return ptr_->header();
00227 }
00228 
00229 
00230 std::string XMLObject::terminatedHeader() const
00231 {
00232   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00233          "XMLObject::terminatedHeader: XMLObject is empty");
00234   return ptr_->terminatedHeader();
00235 }
00236 
00237 
00238 std::string XMLObject::footer() const
00239 {
00240   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00241          "XMLObject::footer: XMLObject is empty");
00242   return ptr_->footer();
00243 }
00244 
00245 
00246 void XMLObject::checkTag(const std::string& expected) const 
00247 {
00248   TEUCHOS_TEST_FOR_EXCEPTION(getTag() != expected, std::runtime_error,
00249                      "XMLObject::checkTag error: expected <"
00250                      << expected << ">, found <" 
00251                      << getTag() << ">");
00252 }
00253 
00254 
00255 void XMLObject::addChild(const XMLObject& child)
00256 {
00257   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00258          "XMLObject::addChild: XMLObject is empty");
00259   ptr_->addChild(child);
00260 }
00261 
00262 
00263 void XMLObject::addContent(const std::string& contentLine)
00264 {
00265   TEUCHOS_TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError,
00266          "XMLObject::addContent: XMLObject is empty");
00267   ptr_->addContent(contentLine);
00268 }
00269 
00270 
00271 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines