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 "XmlDocument.h"
00034 #include "XmlException.h"
00035 
00036 #ifdef ENABLE_XML
00037 
00038 #ifdef SOCKETS_NAMESPACE
00039 namespace SOCKETS_NAMESPACE {
00040 #endif
00041 
00042 
00043 XmlDocument::XmlDocument(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00044 : m_doc(NULL)
00045 , m_ok(false)
00046 {
00047         xmlNodePtr cur;
00048         xmlNsPtr ns;
00049 
00050         if (!(m_doc = xmlParseFile(filename.c_str() )))
00051         {
00052                 throw XmlException("Parse of file failed: " + filename);
00053         }
00054         if (!(cur = xmlDocGetRootElement(m_doc)))
00055         {
00056                 xmlFreeDoc(m_doc);
00057                 m_doc = NULL;
00058                 throw XmlException("Document is empty: " + filename);
00059         }
00060         if (verify_ns.size())
00061         {
00062                 if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00063                 {
00064                         xmlFreeDoc(m_doc);
00065                         m_doc = NULL;
00066                         throw XmlException("Document namespace != " + verify_ns);
00067                 }
00068         }
00069         if (verify_root.size())
00070         {
00071                 if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00072                 {
00073                         xmlFreeDoc(m_doc);
00074                         m_doc = NULL;
00075                         throw XmlException("Document root != " + verify_root);
00076                 }
00077         }
00078         m_ok = true;
00079 }
00080 
00081 
00082 XmlDocument::~XmlDocument()
00083 {
00084         if (m_doc)
00085         {
00086                 xmlFreeDoc(m_doc);
00087         }
00088 }
00089 
00090 
00091 XmlDocument::operator xmlDocPtr()
00092 {
00093         return m_doc;
00094 }
00095 
00096 
00097 #ifdef SOCKETS_NAMESPACE
00098 }
00099 #endif
00100 #endif // ENABLE_XML