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