Google
Web alhem.net

BaseXMLFile.cpp

Go to the documentation of this file.
00001 //#include <stdio.h>
00002 
00003 #include "BaseXMLFile.h"
00004 
00005 
00006 
00007 
00008 BaseXMLFile::BaseXMLFile(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00009 : m_doc(NULL)
00010 ,m_current(NULL)
00011 ,m_ok(false)
00012 {
00013         xmlNodePtr cur;
00014         xmlNsPtr ns;
00015 
00016         if (!(m_doc = xmlParseFile(filename.c_str() )))
00017         {
00018                 fprintf(stderr,"xmlParseFile('%s') failed\n",filename.c_str());
00019                 return;
00020         }
00021         if (!(cur = xmlDocGetRootElement(m_doc)))
00022         {
00023                 fprintf(stderr, "Empty document\n");
00024                 xmlFreeDoc(m_doc);
00025                 m_doc = NULL;
00026                 return;
00027         }
00028         if (verify_ns.size())
00029         {
00030                 if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00031                 {
00032                         fprintf(stderr, "Document namespace != '%s'\n", verify_ns.c_str());
00033                         xmlFreeDoc(m_doc);
00034                         m_doc = NULL;
00035                         return;
00036                 }
00037         }
00038         if (verify_root.size())
00039         {
00040                 if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00041                 {
00042                         fprintf(stderr, "Document root != '%s'\n",verify_root.c_str());
00043                         xmlFreeDoc(m_doc);
00044                         m_doc = NULL;
00045                         return;
00046                 }
00047         }
00048         m_ok = true;
00049 }
00050 
00051 
00052 BaseXMLFile::~BaseXMLFile()
00053 {
00054         if (m_doc)
00055         {
00056                 xmlFreeDoc(m_doc);
00057         }
00058 }
00059 
00060 
00061 xmlNodePtr BaseXMLFile::GetRootElement()
00062 {
00063         m_current = m_doc ? xmlDocGetRootElement(m_doc) : NULL;
00064         return m_current;
00065 }
00066 
00067 
00068 std::string BaseXMLFile::GetProperty(const std::string& name)
00069 {
00070         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00071         if (!p)
00072         {
00073                 return "";
00074         }
00075         std::string str = (char *)p;
00076         xmlFree(p);
00077         return str;
00078 }
00079 
00080 
00081 xmlNodePtr BaseXMLFile::GetChildrenNode()
00082 {
00083         m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00084         return m_current;
00085 }
00086 
00087 
00088 xmlNodePtr BaseXMLFile::GetNextNode()
00089 {
00090         do
00091         {
00092                 m_current = m_current ? m_current -> next : NULL;
00093         } while (m_current && xmlIsBlankNode( m_current ));
00094         return m_current;
00095 }
00096 
00097 
00098 const std::string& BaseXMLFile::GetNodeName()
00099 {
00100         if (m_current)
00101         {
00102                 m_current_name = (char *)m_current -> name;
00103         }
00104         else
00105         {
00106                 m_current_name = "";
00107         }
00108         return m_current_name;
00109 }
00110 
00111 
00112 xmlNsPtr BaseXMLFile::GetNodeNs()
00113 {
00114         if (m_current)
00115                 return m_current -> ns;
00116         return NULL;
00117 }
00118 
00119 
00120 const std::string& BaseXMLFile::GetNodeNsPrefix()
00121 {
00122         if (m_current && m_current -> ns && m_current -> ns -> prefix)
00123         {
00124                 m_ns_prefix = (char *)m_current -> ns -> prefix;
00125         }
00126         else
00127         {
00128                 m_ns_prefix = "";
00129         }
00130         return m_ns_prefix;
00131 }
00132 
00133 
00134 const std::string& BaseXMLFile::GetNodeNsHref()
00135 {
00136         if (m_current && m_current -> ns && m_current -> ns -> href)
00137         {
00138                 m_ns_href = (char *)m_current -> ns -> href;
00139         }
00140         else
00141         {
00142                 m_ns_href = "";
00143         }
00144         return m_ns_href;
00145 }
00146 
00147 
00148 xmlNodePtr BaseXMLFile::GetFirstElement(const std::string& name)
00149 {
00150         GetRootElement();
00151         xmlNodePtr p = GetChildrenNode();
00152         while (p)
00153         {
00154                 if (name == GetNodeName())
00155                 {
00156                         return p;
00157                 }
00158                 p = GetNextNode();
00159         }
00160         return NULL;
00161 }
00162 
00163 
00164 xmlNodePtr BaseXMLFile::GetFirstElement(xmlNodePtr base,const std::string& name)
00165 {
00166         SetCurrent(base);
00167         xmlNodePtr p = GetChildrenNode();
00168         while (p)
00169         {
00170                 if (name == GetNodeName())
00171                 {
00172                         return p;
00173                 }
00174                 p = GetNextNode();
00175         }
00176         return NULL;
00177 }
00178 
00179 
00180 xmlNodePtr BaseXMLFile::GetNextElement(xmlNodePtr p,const std::string& name)
00181 {
00182         SetCurrent(p);
00183         p = GetNextNode();
00184         while (p)
00185         {
00186                 if (name == GetNodeName())
00187                 {
00188                         return p;
00189                 }
00190                 p = GetNextNode();
00191         }
00192         return NULL;
00193 }
00194 
00195 

Generated for Robot World by doxygen 1.3.6

Page, code, and content Copyright (C) 2004 by Anders Hedström