TXMLNode.cxx

Go to the documentation of this file.
00001 // @(#)root/xmlparser:$Id: TXMLNode.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Jose Lo   12/4/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TXMLNode                                                             //
00015 //                                                                      //
00016 // TXMLNode contains a pointer to xmlNode, which is a node under the    //
00017 // DOM tree. A node can be an Element, an Attribute, a Text Node        //
00018 // or a Comment Node.                                                   //
00019 // One can navigate the DOM tree by accessing the siblings and          //
00020 // parent or child nodes. Also retriving the Attribute or the Text in   //
00021 // an Element node.                                                     //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #include "TXMLNode.h"
00026 #include "TXMLAttr.h"
00027 #include "TList.h"
00028 #include <libxml/tree.h>
00029 
00030 
00031 ClassImp(TXMLNode);
00032 
00033 //______________________________________________________________________________
00034 TXMLNode::TXMLNode(xmlNode *node, TXMLNode *parent, TXMLNode *previous) :
00035    fXMLNode(node), fParent(parent), fChildren(0), fNextNode(0),
00036    fPreviousNode(previous), fAttrList(0)
00037 {
00038    // TXMLNode constructor.
00039 }
00040 
00041 //______________________________________________________________________________
00042 TXMLNode::~TXMLNode()
00043 {
00044    // Destructor. It deletes the node's child, next sibling and the
00045    // attribute list.
00046 
00047    delete fChildren;
00048    delete fNextNode;
00049    if (fAttrList)
00050       fAttrList->Delete();
00051    delete fAttrList;
00052 
00053 }
00054 
00055 //______________________________________________________________________________
00056 TXMLNode::EXMLElementType TXMLNode::GetNodeType() const
00057 {
00058    // Returns the node's type.
00059 
00060    return (TXMLNode::EXMLElementType) fXMLNode->type;
00061 }
00062 
00063 //______________________________________________________________________________
00064 const char *TXMLNode::GetNodeName() const
00065 {
00066    // Returns the node's name.
00067 
00068    return (const char *) fXMLNode->name;
00069 }
00070 
00071 //______________________________________________________________________________
00072 TXMLNode *TXMLNode::GetChildren()
00073 {
00074    // Returns the node's child if any, returns 0 if no child.
00075 
00076    if (fChildren)
00077       return fChildren;
00078 
00079    if (fXMLNode->children){
00080       fChildren = new TXMLNode(fXMLNode->children, this);
00081       return fChildren;
00082    }
00083    return 0;
00084 }
00085 
00086 //______________________________________________________________________________
00087 TXMLNode *TXMLNode::GetParent() const
00088 {
00089    // Returns the node's parent if any, returns 0 if no parent.
00090 
00091    return fParent;
00092 }
00093 
00094 //______________________________________________________________________________
00095 const char *TXMLNode::GetContent() const
00096 {
00097    // Returns the content if any, or 0.
00098 
00099    if (fXMLNode->content)
00100       return (const char *) fXMLNode->content;
00101    return 0;
00102 }
00103 
00104 //______________________________________________________________________________
00105 TList *TXMLNode::GetAttributes()
00106 {
00107    // Returns a list of node's attribute if any,
00108    // returns 0 if no attribute.
00109 
00110    if (fAttrList)
00111       return fAttrList;
00112 
00113    if (!HasAttributes())
00114       return 0;
00115 
00116    fAttrList = new TList();
00117    xmlAttr *attr_node = fXMLNode->properties;
00118    for (; attr_node; attr_node = attr_node->next) {
00119       fAttrList->Add(new TXMLAttr((const char *) attr_node->name,
00120                                   (const char *) attr_node->children->content));
00121    }
00122 
00123    return fAttrList;
00124 }
00125 
00126 //______________________________________________________________________________
00127 TXMLNode *TXMLNode::GetNextNode()
00128 {
00129    // Returns the next sibling XMLNode in the DOM tree, if any
00130    // return 0 if no next node.
00131 
00132    if (fNextNode)
00133       return fNextNode;
00134 
00135    if (fXMLNode->next) {
00136       fNextNode = new TXMLNode(fXMLNode->next, fParent, this);
00137       return fNextNode;
00138    }
00139    return 0;
00140 }
00141 
00142 //______________________________________________________________________________
00143 TXMLNode *TXMLNode::GetPreviousNode() const
00144 {
00145    // Returns the previous sibling XMLNode in the DOM tree, if any
00146    // return 0 if no previous node
00147 
00148    return fPreviousNode;
00149 }
00150 
00151 //______________________________________________________________________________
00152 const char *TXMLNode::GetText() const
00153 {
00154    // Returns the content of a Text node if node is a TextNode, 0 otherwise.
00155 
00156    if (GetNodeType() == kXMLElementNode && HasChildren()) {
00157       if (fXMLNode->children->type == XML_TEXT_NODE)
00158          return (const char *) fXMLNode->children->content;
00159    }
00160    return 0;
00161 }
00162 
00163 //______________________________________________________________________________
00164 Bool_t TXMLNode::HasChildren() const
00165 {
00166    // Returns true if node has children.
00167 
00168    return fXMLNode->children ? kTRUE : kFALSE;
00169 }
00170 
00171 //______________________________________________________________________________
00172 Bool_t TXMLNode::HasNextNode() const
00173 {
00174    // Returns true if has next node.
00175 
00176    return fXMLNode->next ? kTRUE : kFALSE;
00177 }
00178 
00179 //______________________________________________________________________________
00180 Bool_t TXMLNode::HasParent() const
00181 {
00182    // Returns true if node has parent.
00183 
00184    return fXMLNode->parent ? kTRUE : kFALSE;
00185 }
00186 
00187 //______________________________________________________________________________
00188 Bool_t TXMLNode::HasPreviousNode() const
00189 {
00190    // Returns true if has previous node.
00191 
00192    return fXMLNode->prev ? kTRUE : kFALSE;
00193 }
00194 
00195 //______________________________________________________________________________
00196 Bool_t TXMLNode::HasAttributes() const
00197 {
00198    // Returns true if Element node has attribute.
00199 
00200    return fXMLNode->properties ? kTRUE : kFALSE;
00201 }
00202 
00203 //______________________________________________________________________________
00204 const char *TXMLNode::GetNamespaceHref() const
00205 {
00206    // Returns the URL for the namespace, or 0 if no namespace.
00207 
00208    if (fXMLNode->ns) {
00209       return (const char *) fXMLNode->ns->href;
00210    }
00211    return 0;
00212 }
00213 
00214 //______________________________________________________________________________
00215 const char *TXMLNode::GetNamespacePrefix() const
00216 {
00217    // Returns prefix for the namespace, or 0 if no namespace.
00218 
00219    if (fXMLNode->ns) {
00220       return (const char *) fXMLNode->ns->prefix;
00221    }
00222    return 0;
00223 }

Generated on Tue Jul 5 14:30:42 2011 for ROOT_528-00b_version by  doxygen 1.5.1