00001 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #include "XmlNode.h"
00034 #include "XmlDocument.h"
00035 #include "XmlException.h"
00036 #include "Utility.h"
00037 
00038 #ifdef ENABLE_XML
00039 
00040 #ifdef SOCKETS_NAMESPACE
00041 namespace SOCKETS_NAMESPACE {
00042 #endif
00043 
00044 
00045 XmlNode::XmlNode(XmlDocument& doc)
00046 : m_doc(doc)
00047 , m_current( GetRootElement() )
00048 {
00049 }
00050 
00051 
00052 XmlNode::XmlNode(XmlDocument& doc, const std::string& nodepath)
00053 : m_doc(doc)
00054 , m_current( GetRootElement() )
00055 {
00056         xmlNodePtr p = GetFirstElement( nodepath );
00057         SetCurrent( p );
00058 }
00059 
00060 
00061 XmlNode::XmlNode(const XmlNode& node, const std::string& nodepath)
00062 : m_doc( node.GetDocument() )
00063 , m_current( node )
00064 {
00065         xmlNodePtr p = GetFirstElement( node, nodepath );
00066         SetCurrent( p );
00067 }
00068 
00069 
00070 XmlNode::XmlNode(XmlDocument& doc, xmlNodePtr ptr)
00071 : m_doc(doc)
00072 , m_current( ptr )
00073 {
00074 }
00075 
00076 
00077 XmlNode::XmlNode(xmlDocPtr doc, xmlNodePtr ptr)
00078 : m_doc(doc)
00079 , m_current( ptr )
00080 {
00081 }
00082 
00083 
00084 XmlNode::~XmlNode()
00085 {
00086 }
00087 
00088 
00089 xmlNodePtr XmlNode::GetRootElement() const
00090 {
00091         m_current = xmlDocGetRootElement(m_doc);
00092         return m_current;
00093 }
00094 
00095 
00096 std::string XmlNode::GetProperty(const std::string& name) const
00097 {
00098         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00099         if (!p)
00100         {
00101                 throw XmlException( "Property '" + name + "' not found in node: " + GetNodeName() );
00102         }
00103         std::string str = (char *)p;
00104         xmlFree(p);
00105         return Utility::FromUtf8(str);
00106 }
00107 
00108 
00109 bool XmlNode::PropertyExists(const std::string& name) const
00110 {
00111         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00112         if (!p)
00113         {
00114                 return false;
00115         }
00116         xmlFree(p);
00117         return true;
00118 }
00119 
00120 
00121 xmlNodePtr XmlNode::GetChildrenNode() const
00122 {
00123         m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00124         return m_current;
00125 }
00126 
00127 
00128 xmlNodePtr XmlNode::GetNextNode() const
00129 {
00130         do
00131         {
00132                 m_current = m_current ? m_current -> next : NULL;
00133         } while (m_current && xmlIsBlankNode( m_current ));
00134         return m_current;
00135 }
00136 
00137 
00138 const std::string& XmlNode::GetNodeName() const
00139 {
00140         if (m_current)
00141         {
00142                 m_current_name = Utility::FromUtf8((char *)m_current -> name);
00143         }
00144         else
00145         {
00146                 m_current_name = "";
00147         }
00148         return m_current_name;
00149 }
00150 
00151 
00152 const std::string& XmlNode::GetContent() const
00153 {
00154         m_content = "";
00155         if (m_current)
00156         {
00157                 xmlNodePtr p = m_current;
00158                 xmlNodePtr p2 = GetChildrenNode();
00159                 if (p2 && p2 -> content)
00160                 {
00161                         m_content = Utility::FromUtf8((char *)p2 -> content);
00162                 }
00163                 SetCurrent(p);
00164         }
00165         return m_content;
00166 }
00167 
00168 
00169 xmlNsPtr XmlNode::GetNodeNs() const
00170 {
00171         if (m_current)
00172                 return m_current -> ns;
00173         return NULL;
00174 }
00175 
00176 
00177 const std::string& XmlNode::GetNodeNsPrefix() const
00178 {
00179         if (m_current && m_current -> ns && m_current -> ns -> prefix)
00180         {
00181                 m_ns_prefix = Utility::FromUtf8((char *)m_current -> ns -> prefix);
00182         }
00183         else
00184         {
00185                 m_ns_prefix = "";
00186         }
00187         return m_ns_prefix;
00188 }
00189 
00190 
00191 const std::string& XmlNode::GetNodeNsHref() const
00192 {
00193         if (m_current && m_current -> ns && m_current -> ns -> href)
00194         {
00195                 m_ns_href = Utility::FromUtf8((char *)m_current -> ns -> href);
00196         }
00197         else
00198         {
00199                 m_ns_href = "";
00200         }
00201         return m_ns_href;
00202 }
00203 
00204 
00205 xmlNodePtr XmlNode::GetFirstElement(const std::string& name) const
00206 {
00207         if (m_lookup_name.empty())
00208                 m_lookup_name = name;
00209         GetRootElement();
00210         xmlNodePtr p = GetChildrenNode();
00211         while (p)
00212         {
00213                 if (name == GetNodeName())
00214                 {
00215                         return p;
00216                 }
00217                 p = GetNextNode();
00218         }
00219         return NULL;
00220 }
00221 
00222 
00223 xmlNodePtr XmlNode::GetFirstElement(xmlNodePtr base,const std::string& name) const
00224 {
00225         if (m_lookup_name.empty())
00226                 m_lookup_name = name;
00227         SetCurrent(base);
00228         xmlNodePtr p = GetChildrenNode();
00229         while (p)
00230         {
00231                 if (name == GetNodeName())
00232                 {
00233                         return p;
00234                 }
00235                 p = GetNextNode();
00236         }
00237         return NULL;
00238 }
00239 
00240 
00241 xmlNodePtr XmlNode::GetNextElement(xmlNodePtr p,const std::string& name) const
00242 {
00243         SetCurrent(p);
00244         p = GetNextNode();
00245         while (p)
00246         {
00247                 if (name == GetNodeName())
00248                 {
00249                         return p;
00250                 }
00251                 p = GetNextNode();
00252         }
00253         return NULL;
00254 }
00255 
00256 
00257 XmlNode::operator xmlNodePtr() const
00258 {
00259         return m_current;
00260 }
00261 
00262 
00263 XmlNode XmlNode::operator[](const std::string& name) const
00264 {
00265         xmlNodePtr p0 = m_current;
00266         xmlNodePtr p = GetFirstElement( m_current, name );
00267         SetCurrent( p0 );
00268         if (p)
00269                 return XmlNode( m_doc, p );
00270         throw XmlException("Didn't find node: " + name);
00271 }
00272 
00273 
00274 bool XmlNode::Exists(const std::string& name) const
00275 {
00276         xmlNodePtr p0 = m_current;
00277         xmlNodePtr p = GetFirstElement( m_current, name );
00278         SetCurrent( p0 );
00279         if (p)
00280                 return true;
00281         return false;
00282 }
00283 
00284 
00285 void XmlNode::operator++() const
00286 {
00287         GetNextNode();
00288         while (m_current)
00289         {
00290                 if (m_lookup_name == GetNodeName())
00291                 {
00292                         return;
00293                 }
00294                 GetNextNode();
00295         }
00296 }
00297 
00298 
00299 std::map<std::string, std::string> XmlNode::GetNsMap() const
00300 {
00301         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00302         std::map<std::string, std::string> vec;
00303         int i = 0;
00304         while (p[i])
00305         {
00306                 std::string href = Utility::FromUtf8((char *)p[i] -> href);
00307                 std::string prefix = p[i] -> prefix ? Utility::FromUtf8((char *)p[i] -> prefix) : "";
00308                 vec[prefix] = href;
00309                 ++i;
00310         }
00311         return vec;
00312 }
00313 
00314 
00315 std::map<std::string, std::string> XmlNode::GetNsMapRe() const
00316 {
00317         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00318         std::map<std::string, std::string> vec;
00319         int i = 0;
00320         while (p[i])
00321         {
00322                 std::string href = Utility::FromUtf8((char *)p[i] -> href);
00323                 std::string prefix = p[i] -> prefix ? Utility::FromUtf8((char *)p[i] -> prefix) : "";
00324                 vec[href] = prefix;
00325                 if (!p[i] -> next)
00326                         break;
00327                 ++i;
00328         }
00329         return vec;
00330 }
00331 
00332 
00333 const std::string XmlNode::FindProperty(const std::string& propname, bool climb) const
00334 {
00335         while (m_current)
00336         {
00337                 if (PropertyExists(propname))
00338                 {
00339                         return GetProperty(propname);
00340                 }
00341                 if (!climb)
00342                 {
00343                         break;
00344                 }
00345                 m_current = m_current -> parent;
00346         }
00347         return "";
00348 }
00349 
00350 
00351 #ifdef SOCKETS_NAMESPACE
00352 }
00353 #endif
00354 
00355 #endif // ENABLE_XML