|
Teuchos - Trilinos Tools Package Version of the Day
|
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 00036 XMLObject::XMLObject(const std::string& tag) 00037 : ptr_(rcp(new XMLObjectImplem(tag))) 00038 {} 00039 00040 XMLObject::XMLObject(XMLObjectImplem* ptr) 00041 : ptr_(rcp(ptr)) 00042 {} 00043 00044 XMLObject XMLObject::deepCopy() const 00045 { 00046 if (is_null(ptr_)) 00047 { 00048 return XMLObject(); 00049 } 00050 return XMLObject(ptr_->deepCopy()); 00051 } 00052 00053 const std::string& XMLObject::getTag() const 00054 { 00055 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00056 "XMLObject::getTag: XMLObject is empty"); 00057 return ptr_->getTag(); 00058 } 00059 00060 bool XMLObject::hasAttribute(const std::string& name) const 00061 { 00062 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00063 "XMLObject::hasAttribute: XMLObject is empty"); 00064 return ptr_->hasAttribute(name); 00065 } 00066 00067 const std::string& XMLObject::getAttribute(const std::string& name) const 00068 { 00069 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00070 "XMLObject::getAttribute: XMLObject is empty"); 00071 return ptr_->getAttribute(name); 00072 } 00073 00074 const std::string& XMLObject::getRequired(const std::string& name) const 00075 { 00076 TEST_FOR_EXCEPTION(!hasAttribute(name), std::runtime_error, 00077 "XMLObject::getRequired: key " 00078 << name << " not found"); 00079 return getAttribute(name); 00080 } 00081 00082 std::string XMLObject::getWithDefault(const std::string& name, 00083 const std::string& defaultValue) const 00084 { 00085 if (hasAttribute(name)) return getRequired(name); 00086 else return defaultValue; 00087 } 00088 00089 bool XMLObject::getRequiredBool(const std::string& name) const 00090 { 00091 if (hasAttribute(name)) 00092 { 00093 std::string val = StrUtils::allCaps(getRequired(name)); 00094 if (val=="TRUE" || val=="YES" || val=="1") 00095 { 00096 return true; 00097 } 00098 else if (val=="FALSE" || val=="NO" || val=="0") 00099 { 00100 return false; 00101 } 00102 else 00103 { 00104 TEST_FOR_EXCEPTION(true, std::runtime_error, 00105 "XMLObject::getRequiredBool value [" << val 00106 << "] should have been {TRUE|FALSE|YES|NO|0|1}"); 00107 } 00108 } 00109 return false; // -Wall 00110 } 00111 00112 int XMLObject::numChildren() const 00113 { 00114 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00115 "XMLObject::numChildren: XMLObject is empty"); 00116 return ptr_->numChildren(); 00117 } 00118 00119 const XMLObject& XMLObject::getChild(int i) const 00120 { 00121 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00122 "XMLObject::getChild: XMLObject is empty"); 00123 return ptr_->getChild(i); 00124 } 00125 00126 int XMLObject::numContentLines() const 00127 { 00128 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00129 "XMLObject::numContentLines: XMLObject is empty"); 00130 return ptr_->numContentLines(); 00131 } 00132 00133 const std::string& XMLObject::getContentLine(int i) const 00134 { 00135 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00136 "XMLObject::getContentLine: XMLObject is empty"); 00137 return ptr_->getContentLine(i); 00138 } 00139 00140 std::string XMLObject::toString() const 00141 { 00142 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00143 "XMLObject::toString: XMLObject is empty"); 00144 return ptr_->toString(); 00145 } 00146 00147 void XMLObject::print(std::ostream& os, int indent) const 00148 { 00149 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00150 "XMLObject::print: XMLObject is empty"); 00151 ptr_->print(os, indent); 00152 } 00153 00154 std::string XMLObject::header() const 00155 { 00156 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00157 "XMLObject::header: XMLObject is empty"); 00158 return ptr_->header(); 00159 } 00160 00161 std::string XMLObject::terminatedHeader() const 00162 { 00163 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00164 "XMLObject::terminatedHeader: XMLObject is empty"); 00165 return ptr_->terminatedHeader(); 00166 } 00167 00168 std::string XMLObject::footer() const 00169 { 00170 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00171 "XMLObject::footer: XMLObject is empty"); 00172 return ptr_->footer(); 00173 } 00174 00175 void XMLObject::checkTag(const std::string& expected) const 00176 { 00177 TEST_FOR_EXCEPTION(getTag() != expected, std::runtime_error, 00178 "XMLObject::checkTag error: expected <" 00179 << expected << ">, found <" 00180 << getTag() << ">"); 00181 } 00182 00183 void XMLObject::addAttribute(const std::string& name, const std::string& value) 00184 { 00185 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00186 "XMLObject::addAttribute: XMLObject is empty"); 00187 ptr_->addAttribute(name, value); 00188 } 00189 00190 void XMLObject::addChild(const XMLObject& child) 00191 { 00192 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00193 "XMLObject::addChild: XMLObject is empty"); 00194 ptr_->addChild(child); 00195 } 00196 00197 void XMLObject::addContent(const std::string& contentLine) 00198 { 00199 TEST_FOR_EXCEPTION(is_null(ptr_), Teuchos::EmptyXMLError, 00200 "XMLObject::addContent: XMLObject is empty"); 00201 ptr_->addContent(contentLine); 00202 }
1.7.4