00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "nxmldocument.h"
00023
00028 NXmlDocument::NXmlDocument(const NString &rootElement)
00029 : NObject(),
00030 m_doc(NULL),
00031 m_root(rootElement),
00032 m_xpath_ctxt(NULL),
00033 m_xpath_obj(NULL),
00034 m_pos(-1)
00035 {
00036 init();
00037 }
00038
00039
00040 NXmlDocument::NXmlDocument(void)
00041 : NObject(),
00042 m_doc(NULL),
00043 m_root(NULL),
00044 m_xpath_ctxt(NULL),
00045 m_xpath_obj(NULL),
00046 m_pos(-1)
00047 {
00048
00049 }
00050
00051
00052 NXmlDocument::~NXmlDocument(void) {
00053 freeSearchResources();
00054 if (m_doc) {
00055 xmlFreeDoc(m_doc);
00056 }
00057
00058
00059 }
00060
00061
00062 void NXmlDocument::init(void) {
00063
00064 m_doc = xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"));
00065 if (!m_doc) {
00066 throw NXmlError::getLastError(
00067 "Unable to create basic document structure",
00068 NXmlException::XML_INIT_ERROR);
00069 }
00070 xmlDocSetRootElement(m_doc, m_root.getNode());
00071 }
00072
00073
00074 void NXmlDocument::setRootNode(const NXmlNode &root) {
00075 m_root = root;
00076 }
00077
00078
00079 NXmlNode NXmlDocument::getRootNode(void) {
00080 return m_root;
00081 }
00082
00083
00084 const NXmlNode NXmlDocument::constGetRootNode(void) const {
00085 return m_root;
00086 }
00087
00088
00089 void NXmlDocument::freeSearchResources(void) const {
00090 if (m_xpath_ctxt) {
00091 xmlXPathFreeContext(m_xpath_ctxt);
00092 m_xpath_ctxt = NULL;
00093 }
00094
00095 if (m_xpath_obj) {
00096 xmlXPathFreeObject(m_xpath_obj);
00097 m_xpath_obj = NULL;
00098 }
00099
00100 m_pos = -1;
00101 }
00102
00103
00104 void NXmlDocument::createSearchResources(const NString &expr) const {
00105 freeSearchResources();
00106
00107 m_xpath_ctxt = xmlXPathNewContext(m_doc);
00108 if (!m_xpath_ctxt) {
00109 throw NXmlError::getLastError("Unable to create search context",
00110 NXmlException::XML_INIT_ERROR);
00111 }
00112
00113 m_xpath_obj = xmlXPathEvalExpression(
00114 reinterpret_cast<const xmlChar *>(expr.toChar()),
00115 m_xpath_ctxt);
00116
00117 if (!m_xpath_obj) {
00118 freeSearchResources();
00119 throw NXmlError::getLastError(
00120 "Unable to evaluate search expression", NXmlException::XML_PARSER_ERROR);
00121 }
00122
00123 if (xmlXPathNodeSetIsEmpty(m_xpath_obj->nodesetval)) {
00124 NDebug::print() << "NodeSet is empty";
00125 }
00126 }
00127
00128 NXmlNode NXmlDocument::findNode(const NString &name) {
00129 createSearchResources(name);
00130 if (!m_xpath_obj || m_xpath_obj->nodesetval->nodeNr == 0) {
00131 NDebug::print() << "Node " << name << " not found!";
00132 return NXmlNode();
00133 }
00134
00135 m_pos = 0;
00136 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00137 }
00138
00139
00140 const NXmlNode NXmlDocument::constFindNode(const NString &name) const {
00141 createSearchResources(name);
00142 if (!m_xpath_obj || m_xpath_obj->nodesetval->nodeNr == 0) {
00143 NDebug::print() << "Node " << name << " not found!";
00144 return NXmlNode();
00145 }
00146
00147 m_pos = 0;
00148 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00149 }
00150
00151 NXmlNode NXmlDocument::next(void) {
00152 m_pos++;
00153
00154 if (m_pos <= m_xpath_obj->nodesetval->nodeNr) {
00155 NDebug::print() << "Next node not found!";
00156 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00157 }
00158
00159 return NXmlNode();
00160 }
00161
00162
00163 const NXmlNode NXmlDocument::constNext(void) const {
00164 m_pos++;
00165
00166 if (m_pos <= m_xpath_obj->nodesetval->nodeNr) {
00167 NDebug::print() << "Next node not found!";
00168 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00169 }
00170
00171 return NXmlNode();
00172 }
00173
00174 NXmlNode NXmlDocument::previous(void) {
00175 m_pos--;
00176
00177 if (m_pos >= 0) {
00178 NDebug::print() << "Previous node not found!";
00179 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00180 }
00181
00182 return NXmlNode();
00183 }
00184
00185
00186 const NXmlNode NXmlDocument::constPrevious(void) const {
00187 m_pos--;
00188
00189 if (m_pos >= 0) {
00190 NDebug::print() << "Previous node not found!";
00191 return NXmlNode(m_xpath_obj->nodesetval->nodeTab[m_pos]);
00192 }
00193
00194 return NXmlNode();
00195 }
00196
00197 void NXmlDocument::setXmlDoc(xmlDocPtr doc) {
00198 m_doc = doc;
00199 }
00200
00201
00202
00203 xmlDocPtr NXmlDocument::getXmlDoc(void) {
00204 return m_doc;
00205 }