diff --git a/include/cantera/base/xml.h b/include/cantera/base/xml.h index 68123798bc0..c7c4ab058b7 100644 --- a/include/cantera/base/xml.h +++ b/include/cantera/base/xml.h @@ -109,13 +109,12 @@ class XML_Node XML_Node& operator=(const XML_Node& right); virtual ~XML_Node(); - //! Add a child node to the current node containing a comment + //! Clear the current node and everything under it /*! - * Child node will have the name, comment. - * - * @param comment Content of the comment + * The value, attributes and children are all zeroed. The name and the + * parent information is kept. */ - void addComment(const std::string& comment); + void clear(); //! Merge an existing node as a child node to the current node /*! @@ -190,6 +189,14 @@ class XML_Node * isn't modified in any way. */ void removeChild(const XML_Node* const node); + + //! Add a child node to the current node containing a comment + /*! + * Child node will have the name, comment. + * + * @param comment Content of the comment + */ + void addComment(const std::string& comment); //! Modify the value for the current node /*! @@ -310,13 +317,6 @@ class XML_Node */ std::string attrib(const std::string& attr) const; - //! Clear the current node and everything under it - /*! - * The value, attributes and children are all zeroed. The name and the - * parent information is kept. - */ - void clear(); - private: //! Returns a changeable value of the attributes map for the current node /*! diff --git a/src/base/xml.cpp b/src/base/xml.cpp index d4d6927704e..e965c465d40 100644 --- a/src/base/xml.cpp +++ b/src/base/xml.cpp @@ -379,11 +379,6 @@ void XML_Node::clear() m_linenum = 0; } -void XML_Node::addComment(const std::string& comment) -{ - addChild("comment", comment); -} - XML_Node& XML_Node::mergeAsChild(XML_Node& node) { m_children.push_back(&node); @@ -425,12 +420,9 @@ void XML_Node::removeChild(const XML_Node* const node) m_childindex.erase(node->name()); } -std::string XML_Node::id() const +void XML_Node::addComment(const std::string& comment) { - if (hasAttrib("id")) { - return attrib("id"); - } - return ""; + addChild("comment", comment); } void XML_Node::addValue(const std::string& val) @@ -451,24 +443,24 @@ std::string XML_Node::value() const return m_value; } -doublereal XML_Node::fp_value() const +std::string XML_Node::value(const std::string& cname) const { - return fpValueCheck(m_value); + return child(cname).value(); } -integer XML_Node::int_value() const +std::string XML_Node::operator()(const std::string& cname) const { - return std::atoi(m_value.c_str()); + return value(cname); } -std::string XML_Node::value(const std::string& cname) const +doublereal XML_Node::fp_value() const { - return child(cname).value(); + return fpValueCheck(m_value); } -std::string XML_Node::operator()(const std::string& loc) const +integer XML_Node::int_value() const { - return value(loc); + return std::atoi(m_value.c_str()); } void XML_Node::addAttribute(const std::string& attrib, const std::string& value) @@ -543,6 +535,14 @@ bool XML_Node::hasAttrib(const std::string& a) const return (m_attribs.find(a) != m_attribs.end()); } +std::string XML_Node::id() const +{ + if (hasAttrib("id")) { + return attrib("id"); + } + return ""; +} + XML_Node& XML_Node::child(const size_t n) const { return *m_children[n]; @@ -676,10 +676,10 @@ XML_Node* XML_Node::findByAttr(const std::string& attr, return 0; } -XML_Node* XML_Node::findByName(const std::string& nm, int depth) +const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const { if (name() == nm) { - return this; + return const_cast(this); } if (depth > 0) { for (size_t i = 0; i < nChildren(); i++) { @@ -692,10 +692,10 @@ XML_Node* XML_Node::findByName(const std::string& nm, int depth) return 0; } -const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const +XML_Node* XML_Node::findByName(const std::string& nm, int depth) { if (name() == nm) { - return const_cast(this); + return this; } if (depth > 0) { for (size_t i = 0; i < nChildren(); i++) { @@ -708,9 +708,55 @@ const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const return 0; } -void XML_Node::writeHeader(std::ostream& s) +std::vector XML_Node::getChildren(const std::string& nm) const { - s << "" << endl; + std::vector children_; + for (size_t i = 0; i < nChildren(); i++) { + if (caseInsensitiveEquals(child(i).name(), nm)) { + children_.push_back(&child(i)); + } + } + return children_; +} + +XML_Node& XML_Node::child(const std::string& aloc) const +{ + string loc = aloc; + while (true) { + size_t iloc = loc.find('/'); + if (iloc != string::npos) { + string cname = loc.substr(0,iloc); + loc = loc.substr(iloc+1, loc.size()); + auto i = m_childindex.find(cname); + if (i != m_childindex.end()) { + return i->second->child(loc); + } else { + throw XML_NoChild(this, m_name, cname, root().m_filename, + lineNumber()); + } + } else { + auto i = m_childindex.find(loc); + if (i != m_childindex.end()) { + return *(i->second); + } else { + throw XML_NoChild(this, m_name, loc, root().m_filename, + lineNumber()); + } + } + } +} + +XML_Node& XML_Node::root() const +{ + return *m_root; +} + +void XML_Node::setRoot(const XML_Node& newRoot) +{ + m_root = const_cast(&newRoot); + for (size_t i = 0; i < m_children.size(); i++) { + m_children[i]->setRoot(newRoot); + } } void XML_Node::build(const std::string& filename) @@ -861,42 +907,9 @@ void XML_Node::unlock() } } -std::vector XML_Node::getChildren(const std::string& nm) const -{ - std::vector children_; - for (size_t i = 0; i < nChildren(); i++) { - if (caseInsensitiveEquals(child(i).name(), nm)) { - children_.push_back(&child(i)); - } - } - return children_; -} - -XML_Node& XML_Node::child(const std::string& aloc) const +void XML_Node::writeHeader(std::ostream& s) { - string loc = aloc; - while (true) { - size_t iloc = loc.find('/'); - if (iloc != string::npos) { - string cname = loc.substr(0,iloc); - loc = loc.substr(iloc+1, loc.size()); - auto i = m_childindex.find(cname); - if (i != m_childindex.end()) { - return i->second->child(loc); - } else { - throw XML_NoChild(this, m_name, cname, root().m_filename, - lineNumber()); - } - } else { - auto i = m_childindex.find(loc); - if (i != m_childindex.end()) { - return *(i->second); - } else { - throw XML_NoChild(this, m_name, loc, root().m_filename, - lineNumber()); - } - } - } + s << "" << endl; } void XML_Node::write_int(std::ostream& s, int level, int numRecursivesAllowed) const @@ -1022,19 +1035,6 @@ void XML_Node::write(std::ostream& s, const int level, int numRecursivesAllowed) s << endl; } -XML_Node& XML_Node::root() const -{ - return *m_root; -} - -void XML_Node::setRoot(const XML_Node& newRoot) -{ - m_root = const_cast(&newRoot); - for (size_t i = 0; i < m_children.size(); i++) { - m_children[i]->setRoot(newRoot); - } -} - XML_Node* findXMLPhase(XML_Node* root, const std::string& phaseId) {