Xmlw::XmlNode Class Reference

#include <XmlNode.h>

List of all members.

Public Member Functions

 XmlNode ()
 XmlNode (XmlDocument &)
 XmlNode (XmlDocument &, const std::string &nodepath)
 XmlNode (const XmlNode &, const std::string &nodepath)
 XmlNode (xmlDocPtr, xmlNodePtr ptr)
 XmlNode (XmlDocument &, xmlNodePtr ptr)
 ~XmlNode ()
 operator xmlNodePtr () const
XmlNode operator[] (const std::string &name) const
 operator std::string () const
void operator++ () const
xmlDocPtr GetDocument () const
bool Valid () const
xmlNodePtr GetRootElement () const
 Assign document root element to 'current node'.
std::string GetProperty (const std::string &propname) const
 Return property 'propname' for 'current node'.
bool PropertyExists (const std::string &propname) const
xmlNodePtr GetChildrenNode () const
 Assign first child node of 'current node' to 'current node'.
xmlNodePtr GetNextNode () const
 Assign next non-blank (xmlIsBlankNode) node of 'current node' to 'current node'.
const std::string & GetNodeName () const
 Return nodename of 'current node'.
const std::string & GetContent () const
 Return content of 'current node'.
void SetCurrent (xmlNodePtr p) const
 Sets 'current node' that the following methods act on: GetProperty GetNodeName GetContent GetNodeNs GetNodeNsPrefix GetNodeNsHref.
xmlNsPtr GetNodeNs () const
 
Returns:
Namespace of 'current node'.

const std::string & GetNodeNsPrefix () const
 
Returns:
Namespace prefix of 'current node'.

const std::string & GetNodeNsHref () const
 
Returns:
Namespace href of 'current node'.

xmlNodePtr GetFirstElement (const std::string &name) const
 Search element named 'name' in Root element child nodes.
xmlNodePtr GetFirstElement (xmlNodePtr parent, const std::string &name) const
 Search element named 'name' in 'parent' element child nodes.
xmlNodePtr GetNextElement (xmlNodePtr node, const std::string &name) const
 Find next element named 'name' on same level as 'node'.
std::string FromUtf8 (const std::string &) const
 Utf8 decrypt.
bool Exists (const std::string &name) const
 Check that child element with name 'name' exists.
std::map< std::string, std::string > GetNsMap () const
 [prefix] = href
std::map< std::string, std::string > GetNsMapRe () const
 [href] = prefix

Private Attributes

xmlDocPtr m_doc
xmlNodePtr m_current
std::string m_current_name
std::string m_ns_prefix
std::string m_ns_href
std::string m_content
std::string m_lookup_name


Detailed Description

Definition at line 35 of file XmlNode.h.


Constructor & Destructor Documentation

Xmlw::XmlNode::XmlNode (  )  [inline]

Definition at line 38 of file XmlNode.h.

Referenced by operator[]().

00038 {}

Xmlw::XmlNode::XmlNode ( XmlDocument  ) 

Definition at line 31 of file XmlNode.cpp.

00032 : m_doc(doc)
00033 , m_current( GetRootElement() )
00034 {
00035 }

Xmlw::XmlNode::XmlNode ( XmlDocument ,
const std::string &  nodepath 
)

Definition at line 38 of file XmlNode.cpp.

References GetFirstElement(), and SetCurrent().

00039 : m_doc(doc)
00040 , m_current( GetRootElement() )
00041 {
00042         xmlNodePtr p = GetFirstElement( nodepath );
00043         SetCurrent( p );
00044 }

Xmlw::XmlNode::XmlNode ( const XmlNode ,
const std::string &  nodepath 
)

Definition at line 47 of file XmlNode.cpp.

References GetFirstElement(), and SetCurrent().

00048 : m_doc( node.GetDocument() )
00049 , m_current( node )
00050 {
00051         xmlNodePtr p = GetFirstElement( node, nodepath );
00052         SetCurrent( p );
00053 }

Xmlw::XmlNode::XmlNode ( xmlDocPtr  ,
xmlNodePtr  ptr 
)

Definition at line 63 of file XmlNode.cpp.

00064 : m_doc(doc)
00065 , m_current( ptr )
00066 {
00067 }

Xmlw::XmlNode::XmlNode ( XmlDocument ,
xmlNodePtr  ptr 
)

Definition at line 56 of file XmlNode.cpp.

00057 : m_doc(doc)
00058 , m_current( ptr )
00059 {
00060 }

Xmlw::XmlNode::~XmlNode (  ) 

Definition at line 70 of file XmlNode.cpp.

00071 {
00072 }


Member Function Documentation

Xmlw::XmlNode::operator xmlNodePtr (  )  const

Definition at line 266 of file XmlNode.cpp.

References m_current.

00267 {
00268         return m_current;
00269 }

XmlNode Xmlw::XmlNode::operator[] ( const std::string &  name  )  const

Definition at line 272 of file XmlNode.cpp.

References GetFirstElement(), m_current, m_doc, SetCurrent(), and XmlNode().

00273 {
00274         xmlNodePtr p0 = m_current;
00275         xmlNodePtr p = GetFirstElement( m_current, name );
00276         SetCurrent( p0 );
00277         if (p)
00278                 return XmlNode( m_doc, p );
00279         throw XmlException("Didn't find node: " + name);
00280 }

Xmlw::XmlNode::operator std::string (  )  const [inline]

Definition at line 48 of file XmlNode.h.

References GetContent().

00048 { return GetContent(); }

void Xmlw::XmlNode::operator++ (  )  const

Definition at line 294 of file XmlNode.cpp.

References GetNextNode(), GetNodeName(), m_current, and m_lookup_name.

00295 {
00296         GetNextNode();
00297         while (m_current)
00298         {
00299                 if (m_lookup_name == GetNodeName())
00300                 {
00301                         return;
00302                 }
00303                 GetNextNode();
00304         }
00305 }

xmlDocPtr Xmlw::XmlNode::GetDocument (  )  const [inline]

Definition at line 51 of file XmlNode.h.

00051 { return m_doc; }

bool Xmlw::XmlNode::Valid (  )  const [inline]

Definition at line 53 of file XmlNode.h.

00053 { return m_current ? true : false; }

xmlNodePtr Xmlw::XmlNode::GetRootElement (  )  const

Assign document root element to 'current node'.

Returns:
Current Node

Definition at line 75 of file XmlNode.cpp.

References m_current, and m_doc.

Referenced by GetFirstElement().

00076 {
00077         m_current = xmlDocGetRootElement(m_doc);
00078         return m_current;
00079 }

std::string Xmlw::XmlNode::GetProperty ( const std::string &  propname  )  const

Return property 'propname' for 'current node'.

Definition at line 82 of file XmlNode.cpp.

References FromUtf8(), GetNodeName(), and m_current.

00083 {
00084         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00085         if (!p)
00086         {
00087                 throw XmlException( "Property '" + name + "' not found in node: " + GetNodeName() );
00088         }
00089         std::string str = (char *)p;
00090         xmlFree(p);
00091         return FromUtf8(str);
00092 }

bool Xmlw::XmlNode::PropertyExists ( const std::string &  propname  )  const

Definition at line 95 of file XmlNode.cpp.

References m_current.

00096 {
00097         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00098         if (!p)
00099         {
00100                 return false;
00101         }
00102         xmlFree(p);
00103         return true;
00104 }

xmlNodePtr Xmlw::XmlNode::GetChildrenNode (  )  const

Assign first child node of 'current node' to 'current node'.

Returns:
Current Node

Definition at line 107 of file XmlNode.cpp.

References m_current.

Referenced by GetContent(), and GetFirstElement().

00108 {
00109         m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00110         return m_current;
00111 }

xmlNodePtr Xmlw::XmlNode::GetNextNode (  )  const

Assign next non-blank (xmlIsBlankNode) node of 'current node' to 'current node'.

Returns:
Current Node

Definition at line 114 of file XmlNode.cpp.

References m_current.

Referenced by GetFirstElement(), GetNextElement(), and operator++().

00115 {
00116         do
00117         {
00118                 m_current = m_current ? m_current -> next : NULL;
00119         } while (m_current && xmlIsBlankNode( m_current ));
00120         return m_current;
00121 }

const std::string & Xmlw::XmlNode::GetNodeName (  )  const

Return nodename of 'current node'.

Definition at line 124 of file XmlNode.cpp.

References FromUtf8(), m_current, and m_current_name.

Referenced by GetFirstElement(), GetNextElement(), GetProperty(), and operator++().

00125 {
00126         if (m_current)
00127         {
00128                 m_current_name = FromUtf8((char *)m_current -> name);
00129         }
00130         else
00131         {
00132                 m_current_name = "";
00133         }
00134         return m_current_name;
00135 }

const std::string & Xmlw::XmlNode::GetContent (  )  const

Return content of 'current node'.

Definition at line 138 of file XmlNode.cpp.

References FromUtf8(), GetChildrenNode(), m_content, m_current, and SetCurrent().

Referenced by operator std::string().

00139 {
00140         m_content = "";
00141         if (m_current)
00142         {
00143                 xmlNodePtr p = m_current;
00144                 xmlNodePtr p2 = GetChildrenNode();
00145                 if (p2 && p2 -> content)
00146                 {
00147                         m_content = FromUtf8((char *)p2 -> content);
00148                 }
00149                 SetCurrent(p);
00150         }
00151         return m_content;
00152 }

void Xmlw::XmlNode::SetCurrent ( xmlNodePtr  p  )  const [inline]

Sets 'current node' that the following methods act on: GetProperty GetNodeName GetContent GetNodeNs GetNodeNsPrefix GetNodeNsHref.

Definition at line 84 of file XmlNode.h.

Referenced by Exists(), GetContent(), GetFirstElement(), GetNextElement(), operator[](), and XmlNode().

00084 { m_current = p; }

xmlNsPtr Xmlw::XmlNode::GetNodeNs (  )  const

Returns:
Namespace of 'current node'.

Definition at line 155 of file XmlNode.cpp.

References m_current.

00156 {
00157         if (m_current)
00158                 return m_current -> ns;
00159         return NULL;
00160 }

const std::string & Xmlw::XmlNode::GetNodeNsPrefix (  )  const

Returns:
Namespace prefix of 'current node'.

Definition at line 163 of file XmlNode.cpp.

References FromUtf8(), m_current, and m_ns_prefix.

00164 {
00165         if (m_current && m_current -> ns && m_current -> ns -> prefix)
00166         {
00167                 m_ns_prefix = FromUtf8((char *)m_current -> ns -> prefix);
00168         }
00169         else
00170         {
00171                 m_ns_prefix = "";
00172         }
00173         return m_ns_prefix;
00174 }

const std::string & Xmlw::XmlNode::GetNodeNsHref (  )  const

Returns:
Namespace href of 'current node'.

Definition at line 177 of file XmlNode.cpp.

References FromUtf8(), m_current, and m_ns_href.

00178 {
00179         if (m_current && m_current -> ns && m_current -> ns -> href)
00180         {
00181                 m_ns_href = FromUtf8((char *)m_current -> ns -> href);
00182         }
00183         else
00184         {
00185                 m_ns_href = "";
00186         }
00187         return m_ns_href;
00188 }

xmlNodePtr Xmlw::XmlNode::GetFirstElement ( const std::string &  name  )  const

Search element named 'name' in Root element child nodes.

Set 'current node' to Root element.

Definition at line 191 of file XmlNode.cpp.

References GetChildrenNode(), GetNextNode(), GetNodeName(), GetRootElement(), and m_lookup_name.

Referenced by Exists(), operator[](), and XmlNode().

00192 {
00193         if (m_lookup_name.empty())
00194                 m_lookup_name = name;
00195         GetRootElement();
00196         xmlNodePtr p = GetChildrenNode();
00197         while (p)
00198         {
00199                 if (name == GetNodeName())
00200                 {
00201                         return p;
00202                 }
00203                 p = GetNextNode();
00204         }
00205         return NULL;
00206 }

xmlNodePtr Xmlw::XmlNode::GetFirstElement ( xmlNodePtr  parent,
const std::string &  name 
) const

Search element named 'name' in 'parent' element child nodes.

Set 'current node' to 'parent'.

Definition at line 209 of file XmlNode.cpp.

References GetChildrenNode(), GetNextNode(), GetNodeName(), m_lookup_name, and SetCurrent().

00210 {
00211         if (m_lookup_name.empty())
00212                 m_lookup_name = name;
00213         SetCurrent(base);
00214         xmlNodePtr p = GetChildrenNode();
00215         while (p)
00216         {
00217                 if (name == GetNodeName())
00218                 {
00219                         return p;
00220                 }
00221                 p = GetNextNode();
00222         }
00223         return NULL;
00224 }

xmlNodePtr Xmlw::XmlNode::GetNextElement ( xmlNodePtr  node,
const std::string &  name 
) const

Find next element named 'name' on same level as 'node'.

Set 'current node' to 'p'.

Definition at line 227 of file XmlNode.cpp.

References GetNextNode(), GetNodeName(), and SetCurrent().

00228 {
00229         SetCurrent(p);
00230         p = GetNextNode();
00231         while (p)
00232         {
00233                 if (name == GetNodeName())
00234                 {
00235                         return p;
00236                 }
00237                 p = GetNextNode();
00238         }
00239         return NULL;
00240 }

std::string Xmlw::XmlNode::FromUtf8 ( const std::string &   )  const

Utf8 decrypt.

Definition at line 243 of file XmlNode.cpp.

Referenced by GetContent(), GetNodeName(), GetNodeNsHref(), GetNodeNsPrefix(), GetNsMap(), GetNsMapRe(), and GetProperty().

00244 {
00245         if (!str.size())
00246                 return "";
00247         std::string r;
00248         for (size_t i = 0; i < str.size(); i++)
00249         {
00250                 if (i < str.size() - 1 && (str[i] & 0xe0) == 0xc0 && (str[i + 1] & 0xc0) == 0x80)
00251                 {
00252                         int c1 = str[i] & 0x1f;
00253                         int c2 = str[++i] & 0x3f;
00254                         int c = (c1 << 6) + c2;
00255                         r += (char)c;
00256                 }
00257                 else
00258                 {
00259                         r += str[i];
00260                 }
00261         }
00262         return r;
00263 }

bool Xmlw::XmlNode::Exists ( const std::string &  name  )  const

Check that child element with name 'name' exists.

Definition at line 283 of file XmlNode.cpp.

References GetFirstElement(), m_current, and SetCurrent().

00284 {
00285         xmlNodePtr p0 = m_current;
00286         xmlNodePtr p = GetFirstElement( m_current, name );
00287         SetCurrent( p0 );
00288         if (p)
00289                 return true;
00290         return false;
00291 }

std::map< std::string, std::string > Xmlw::XmlNode::GetNsMap (  )  const

[prefix] = href

Definition at line 308 of file XmlNode.cpp.

References FromUtf8(), m_current, and m_doc.

00309 {
00310         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00311         std::map<std::string, std::string> vec;
00312         int i = 0;
00313         while (p[i])
00314         {
00315                 std::string href = FromUtf8((char *)p[i] -> href);
00316                 std::string prefix = p[i] -> prefix ? FromUtf8((char *)p[i] -> prefix) : "";
00317                 vec[prefix] = href;
00318                 ++i;
00319         }
00320         return vec;
00321 }

std::map< std::string, std::string > Xmlw::XmlNode::GetNsMapRe (  )  const

[href] = prefix

Definition at line 324 of file XmlNode.cpp.

References FromUtf8(), m_current, and m_doc.

00325 {
00326         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00327         std::map<std::string, std::string> vec;
00328         int i = 0;
00329         while (p[i])
00330         {
00331                 std::string href = FromUtf8((char *)p[i] -> href);
00332                 std::string prefix = p[i] -> prefix ? FromUtf8((char *)p[i] -> prefix) : "";
00333                 vec[href] = prefix;
00334                 if (!p[i] -> next)
00335                         break;
00336                 ++i;
00337         }
00338         return vec;
00339 }


Member Data Documentation

xmlDocPtr Xmlw::XmlNode::m_doc [private]

Definition at line 118 of file XmlNode.h.

Referenced by GetNsMap(), GetNsMapRe(), GetRootElement(), and operator[]().

xmlNodePtr Xmlw::XmlNode::m_current [mutable, private]

Definition at line 119 of file XmlNode.h.

Referenced by Exists(), GetChildrenNode(), GetContent(), GetNextNode(), GetNodeName(), GetNodeNs(), GetNodeNsHref(), GetNodeNsPrefix(), GetNsMap(), GetNsMapRe(), GetProperty(), GetRootElement(), operator xmlNodePtr(), operator++(), operator[](), and PropertyExists().

std::string Xmlw::XmlNode::m_current_name [mutable, private]

Definition at line 120 of file XmlNode.h.

Referenced by GetNodeName().

std::string Xmlw::XmlNode::m_ns_prefix [mutable, private]

Definition at line 121 of file XmlNode.h.

Referenced by GetNodeNsPrefix().

std::string Xmlw::XmlNode::m_ns_href [mutable, private]

Definition at line 122 of file XmlNode.h.

Referenced by GetNodeNsHref().

std::string Xmlw::XmlNode::m_content [mutable, private]

Definition at line 123 of file XmlNode.h.

Referenced by GetContent().

std::string Xmlw::XmlNode::m_lookup_name [mutable, private]

Definition at line 124 of file XmlNode.h.

Referenced by GetFirstElement(), and operator++().


The documentation for this class was generated from the following files:
Generated on Sun Feb 10 17:01:03 2008 for xmlw - libxml2 C++ wrapper by  doxygen 1.5.2