nxmlnode.cpp

00001 // LICENSE: (Please see the file COPYING for details)
00002 //
00003 // NUS - Nemesis Utilities System: A C++ application development framework 
00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske
00005 //
00006 //  This file is part of NUS
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  modify it under the terms of the GNU Lesser General Public
00010 //  License as published by the Free Software Foundation version 2.1
00011 //  of the License.
00012 //
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Lesser General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU Lesser General Public
00019 //  License along with this library; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 //
00022 #include "nxmlnode.h"
00023 
00028 NXmlNode::NXmlNode(void)
00029       : NObject(),
00030       m_node(NULL)
00031 { 
00032 
00033 }
00034 
00035 NXmlNode::NXmlNode(const NString &name)
00036       : NObject(),
00037       m_node(NULL)
00038 {
00039       init(name);
00040 }
00041 
00042 
00043 NXmlNode::NXmlNode(const NString &name, const NString &data)
00044       : NObject(),
00045       m_node(NULL)
00046 {
00047       init(name);
00048 }
00049 
00050 NXmlNode::NXmlNode(const NXmlNode &other)
00051       : NObject(),
00052       m_node(NULL)
00053 {
00054       m_node = other.getNode();
00055 }
00056 
00057 
00058 NXmlNode::NXmlNode(xmlNodePtr node)
00059       : NObject(),
00060       m_node(NULL)
00061 { 
00062       m_node = node;
00063 }
00064 
00065 NXmlNode::~NXmlNode(void) { 
00066       
00067       // Only the root node can release the memory used by the child nodes
00068       if (m_node && !m_node->parent) { 
00069             xmlFreeNode(m_node);
00070       }
00071 }
00072 
00073 void NXmlNode::init(const NString &name) { 
00074       m_node = xmlNewNode(NULL, 
00075                       reinterpret_cast<const xmlChar *>(name.toChar()));
00076 }
00077 
00078 const xmlNodePtr NXmlNode::getNode(void) const { 
00079       return m_node;
00080 }
00081 
00082 void NXmlNode::setName(const NString &name) { 
00083       if (!m_node) {
00084             init(name);
00085       }
00086       else {
00087             xmlNodeSetName(m_node, reinterpret_cast<const xmlChar *>(name.toChar()));
00088       }
00089 }
00090 
00091 NString NXmlNode::getName(void) const { 
00092       NString ret;
00093 
00094       if (m_node) {
00095             ret = reinterpret_cast<const char *>(m_node->name);
00096       }
00097       
00098       return ret;
00099 }
00100 
00101 
00102 void NXmlNode::setData(const NString &data) { 
00103       if (m_node) {
00104             xmlNodeSetContent(m_node, reinterpret_cast<const xmlChar *>(data.toChar()));
00105       }
00106 }
00107 
00108 
00109 NString NXmlNode::getData(void) const {
00110       NString ret;
00111       xmlChar *content = NULL;
00112       
00113       if (!m_node) {
00114             return ret;
00115       }
00116       
00117       content = xmlNodeGetContent(m_node);
00118       if (!content || isBlank()) { 
00119             return ret;
00120       }
00121       
00122       ret = reinterpret_cast<const char *>(content);
00123       xmlFree(content);
00124       
00125       return ret;
00126 }
00127 
00128 bool NXmlNode::isBlank(void) const { 
00129       if (xmlIsBlankNode(m_node) == 1) { 
00130             return true;
00131       }
00132       
00133       return false;
00134 }
00135 
00136 
00137 bool NXmlNode::isText(void) const {
00138       if (xmlNodeIsText(m_node) == 1) { 
00139             return true;
00140       }
00141       
00142       return false;
00143 }
00144 
00145 
00146 bool NXmlNode::isNull(void) const {
00147       if (!m_node) {
00148             return true;
00149       }
00150       
00151       return false;
00152 }
00153 
00154 xmlNodePtr NXmlNode::createNode(const NString &name) { 
00155       xmlNodePtr node;
00156       
00157       node = xmlNewNode(NULL, reinterpret_cast<const xmlChar *>(name.toChar()));
00158       if (!node) {
00159             throw NXmlError::getLastError(
00160                         "Unable to create a new node to append");
00161       }
00162       
00163       return node;
00164 }
00165 
00166 
00167 xmlNodePtr NXmlNode::createNode(const NString &name, const NString &data) { 
00168       xmlNodePtr node;
00169       
00170       node = xmlNewNode(NULL, reinterpret_cast<const xmlChar *>(name.toChar()));
00171       if (!node) { 
00172             throw NXmlError::getLastError(
00173                         "Unable to create a new node to append");
00174       }
00175       
00176       xmlNodeSetContent(node, reinterpret_cast<const xmlChar *>(data.toChar()));
00177       
00178       return node;
00179 }
00180 
00181 
00182 NXmlNode NXmlNode::addChild(const NString &name) {
00183       xmlNodePtr node;
00184       
00185       if (!m_node) {
00186             throw NXmlError::getLastError("There's no current node set");
00187       }
00188       
00189       node = createNode(name);
00190       if (!xmlAddChild(m_node, node)) {
00191             xmlFreeNode(node); 
00192             throw NXmlError::getLastError("Unable to add child node");
00193       }
00194       
00195       return NXmlNode(node);
00196 }
00197 
00198 
00199 NXmlNode NXmlNode::addChild(const NString &name, const NString &data) {
00200       xmlNodePtr node;
00201       
00202       if (!m_node) {
00203             throw NXmlError::getLastError("There's no current node set");
00204       }
00205       
00206       node = createNode(name, data);
00207       if (!xmlAddChild(m_node, node)) { 
00208             xmlFreeNode(node);
00209             throw NXmlError::getLastError("Unable to add child node");
00210       }
00211       
00212       return NXmlNode(node);
00213 }
00214 
00215 
00216 NXmlNode NXmlNode::addNextSibling(const NString &name) { 
00217       xmlNodePtr node;
00218       
00219       if (!m_node) {
00220             throw NXmlError::getLastError("There's no current node set");
00221       }
00222       
00223       node = createNode(name);
00224       if (!xmlAddNextSibling(m_node, node)) {
00225             xmlFreeNode(node); 
00226             throw NXmlError::getLastError("Unable to add child node");
00227       }
00228       
00229       return NXmlNode(node);
00230 }
00231 
00232 
00233 NXmlNode NXmlNode::addNextSibling(const NString &name, const NString &data) { 
00234       xmlNodePtr node;
00235       
00236       if (!m_node) {
00237             throw NXmlError::getLastError("There's no current node set");
00238       }
00239       
00240       node = createNode(name, data);
00241       if (!xmlAddNextSibling(m_node, node)) {
00242             xmlFreeNode(node); 
00243             throw NXmlError::getLastError("Unable to add child node");
00244       }
00245       
00246       return NXmlNode(node);
00247 }
00248 
00249 
00250 NXmlNode NXmlNode::addPreviousSibling(const NString &name) { 
00251       xmlNodePtr node;
00252       
00253       if (!m_node) {
00254             throw NXmlError::getLastError("There's no current node set");
00255       }
00256       
00257       node = createNode(name);
00258       if (!xmlAddPrevSibling(m_node, node)) { 
00259             xmlFreeNode(node);
00260             throw NXmlError::getLastError("Unable to add child node");
00261       }
00262       
00263       return NXmlNode(node);
00264 }
00265 
00266 
00267 NXmlNode NXmlNode::addPreviousSibling(const NString &name, const NString &data) { 
00268       xmlNodePtr node;
00269       
00270       if (!m_node) {
00271             throw NXmlError::getLastError("There's no current node set");
00272       }
00273       
00274       node = createNode(name, data);
00275       if (!xmlAddPrevSibling(m_node, node)) {
00276             xmlFreeNode(node); 
00277             throw NXmlError::getLastError("Unable to add child node");
00278       }
00279       
00280       return NXmlNode(node);
00281 }
00282 
00283 
00284 bool NXmlNode::hasChild(void) const { 
00285       return m_node && m_node->children ? true : false;
00286 }
00287 
00288 bool NXmlNode::hasLast(void) const { 
00289       return m_node && m_node->last ? true : false;
00290 }
00291 
00292 bool NXmlNode::hasParent(void) const { 
00293       return m_node && m_node->parent ? true : false;
00294 }
00295 
00296 bool NXmlNode::hasNext(void) const { 
00297       return m_node && m_node->next ? true : false;
00298 }
00299 
00300 bool NXmlNode::hasPrevious(void) const { 
00301       return m_node && m_node->prev ? true : false;
00302 }
00303 
00304 NXmlNode NXmlNode::getChild(void) { 
00305       return NXmlNode(m_node->children);
00306 }
00307 
00308 
00309 NXmlNode NXmlNode::getLast(void) { 
00310       return NXmlNode(m_node->last);
00311 }
00312 
00313 NXmlNode NXmlNode::getParent(void) { 
00314       return NXmlNode(m_node->parent);
00315 }
00316 
00317 
00318 NXmlNode NXmlNode::getNext(void) { 
00319       return NXmlNode(m_node->next);
00320 }
00321 
00322 
00323 NXmlNode NXmlNode::getPrevious(void) { 
00324       return NXmlNode(m_node->prev);
00325 }
00326 
00327 
00328 const NXmlNode NXmlNode::getChild(void) const { 
00329       return NXmlNode(m_node->children);
00330 }
00331 
00332 
00333 const NXmlNode NXmlNode::getLast(void) const { 
00334       return NXmlNode(m_node->last);
00335 }
00336 
00337 
00338 const NXmlNode NXmlNode::getParent(void) const { 
00339       return NXmlNode(m_node->parent);
00340 }
00341 
00342 
00343 const NXmlNode NXmlNode::getNext(void) const { 
00344       return NXmlNode(m_node->next);
00345 }
00346 
00347 
00348 const NXmlNode NXmlNode::getPrevious(void) const { 
00349       return NXmlNode(m_node->prev);
00350 }
00351 
00352 
00353 NXmlNode &NXmlNode::addProperty(const NString &name, const NString &data) {
00354       xmlAttrPtr prop;
00355       
00356       if (!m_node) {
00357             throw NXmlError::getLastError("There's no current node set");
00358       }
00359       
00360       prop = xmlNewProp(m_node, 
00361                    reinterpret_cast<const xmlChar *>(name.toChar()),
00362                    reinterpret_cast<const xmlChar *>(data.toChar()));
00363       if (!prop) { 
00364             throw NXmlError::getLastError("Unable to add property");
00365       }
00366       
00367       return *this;
00368 }
00369 
00370 
00371 bool NXmlNode::removeProperty(const NString &name) { 
00372       NString propName;
00373       xmlAttrPtr prop = NULL;
00374       
00375       
00376       if (!m_node) {
00377             return true;
00378       }
00379       
00380       prop = m_node->properties;
00381       while (prop) { 
00382             propName = reinterpret_cast<const char *>(prop->name);
00383             if (name == propName) { 
00384                   xmlRemoveProp(prop);
00385                   return true;
00386             }
00387             
00388             prop = prop->next;
00389       }
00390       
00391       return false;
00392 }
00393 
00394 
00395 NString NXmlNode::getProperty(const NString &name) const { 
00396       NString ret;
00397       const xmlChar *nameX = reinterpret_cast<const xmlChar *>(name.toChar());
00398       
00399       if (m_node) {
00400             ret = reinterpret_cast<const char *>(xmlGetProp(m_node, nameX));
00401       }
00402       
00403       return ret;
00404 }
00405 
00406 
00407 bool NXmlNode::hasProperty(const NString &name) const { 
00408       if (m_node && xmlHasProp(m_node, 
00409           reinterpret_cast<const xmlChar *>(name.toChar()))) 
00410       { 
00411             return true;
00412       }
00413       else { 
00414             return false;
00415       }
00416 }
00417 
00418 bool NXmlNode::removeChild(void) { 
00419       if (m_node && m_node->children) {
00420             xmlFreeNode(m_node->children);
00421             return true;
00422       }
00423       
00424       return false;
00425 }
00426 
00427 
00428 bool NXmlNode::removeChild(const NString &name) { 
00429       NString nodeName;
00430       xmlNodePtr node = NULL;
00431 
00432       if (!m_node) {
00433             return false;
00434       }
00435 
00436       node = m_node->children;
00437       while (node) { 
00438             nodeName = reinterpret_cast<const char *>(node->name);
00439             if (name == nodeName) {
00440                   xmlUnlinkNode(node);
00441                   xmlFreeNode(node);
00442                   
00443                   return true;
00444             }
00445             
00446             node = node->children;
00447       }
00448 
00449       return false;
00450 }
00451 
00452 
00453 bool NXmlNode::removeNextSibling(void) { 
00454       if (m_node && m_node->next) { 
00455             xmlFreeNode(m_node->next);
00456             return true;
00457       }
00458       
00459       return false;
00460 }
00461 
00462 
00463 bool NXmlNode::removeNextSibling(const NString &name) { 
00464       NString nodeName;
00465       xmlNodePtr node = NULL;
00466       
00467       if (!m_node) {
00468             return false;
00469       }
00470       
00471       node = m_node->next;
00472       while (node) { 
00473             nodeName = reinterpret_cast<const char *>(node->name);
00474             if (name == nodeName) {
00475                   xmlUnlinkNode(node);
00476                   xmlFreeNode(node);
00477                   
00478                   return true;
00479             }
00480             
00481             node = node->next;
00482       }
00483 
00484       return false;
00485 }
00486 
00487 
00488 bool NXmlNode::removePreviousSibling(void) {
00489       if (m_node && m_node->prev) { 
00490             xmlFreeNode(m_node->prev);
00491             return true;
00492       }
00493       
00494       return false;
00495 }
00496 
00497 
00498 bool NXmlNode::removePreviousSibling(const NString &name) { 
00499       NString nodeName;
00500       xmlNodePtr node = NULL;
00501       
00502       if (!m_node) {
00503             return false;
00504       }
00505 
00506       node = m_node->prev;
00507       while (node) { 
00508             nodeName = reinterpret_cast<const char *>(node->name);
00509             if (name == nodeName) {
00510                   xmlUnlinkNode(node);
00511                   xmlFreeNode(node);
00512                   
00513                   return true;
00514             }
00515             
00516             node = node->prev;
00517       }
00518 
00519       return false;
00520 }
00521 
00522 
00523 NXmlNode NXmlNode::find(const NString &name) { 
00524       NString nodeName;
00525       xmlNodePtr node = NULL;
00526       
00527       if (!m_node) {
00528             NXmlNode();
00529       }
00530       
00531       node = m_node->children;
00532       while (node) { 
00533             nodeName = reinterpret_cast<const char *>(node->name);
00534             if (name == nodeName) {
00535                   return NXmlNode(node);
00536             }
00537             
00538             node = node->children;
00539       }
00540 
00541       return NXmlNode();
00542 }
00543 
00544 
00545 const NXmlNode NXmlNode::find(const NString &name) const { 
00546       return find(name);
00547 }
00548 
00549 NXmlNode NXmlNode::copy(const NXmlNode &other) const { 
00550       xmlNodePtr node = NULL;
00551       const int recursive = 1; // Do a recursive copy
00552       
00553       if (m_node) {
00554             node = xmlCopyNode(m_node, recursive);
00555       }
00556       
00557       return NXmlNode(node);
00558 }
00559 
00560 
00561 NXmlNode &NXmlNode::operator=(const NXmlNode &rhs) { 
00562       if (m_node && !m_node->parent) { 
00563             xmlFreeNode(m_node);
00564       }
00565       
00566       m_node = rhs.getNode();
00567       return *this;
00568 }

Generated on Wed Mar 5 23:10:36 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4