FEDM_XmlParser_TinyReader.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 |       FEDM_XmlParser_TinyReader.cpp                                           |
00004 |                                                       |
00005 ---------------------------------------------------------
00006 
00007 Copyright � 2008-2011 FEIG ELECTRONIC GmbH, All Rights Reserved.
00008                                                 Lange Strasse 4
00009                                                 D-35781 Weilburg
00010                                                 Federal Republic of Germany
00011                                                 phone    : +49 6471 31090
00012                                                 fax      : +49 6471 310999
00013                                                 e-mail   : obid-support@feig.de
00014                                                 Internet : http://www.feig.de
00015                                         
00016 Author                  :       Markus Hultsch
00017 Begin                   :       23.01.2008
00018 Version                         :       04.00.02 / 22.08.2011 / M. Hultsch
00019 Operation Systems       :       independent
00020 Function                        :       xml reader class based on tinyxml parser API
00021 
00022 OBID� and OBID i-scan� are registered Trademarks of FEIG ELECTRONIC GmbH.
00023 Linux� is a registered trademark of Linus Torvalds.
00024 Microsoft� and Windows� are registered trademarks of Microsoft Corporation.
00025 */
00026 
00027 
00028 
00029 //#############################
00030 //      I N C L U D E S
00031 //#############################
00032 #include "FEDM_XmlParser_TinyReader.h"                          // public include file of library
00033 #include "FEDM_XmlParser_Base.h"
00034 
00035 
00036 //#######################################################################################
00037 //      class FEDM_XmlParser_TinyReader
00038 //#######################################################################################
00039 FEDM_XmlParser_TinyReader::FEDM_XmlParser_TinyReader(void)
00040 {
00041         m_uiLastTagLevel        = 0;
00042         m_pDoc                          = NULL;
00043 
00044         // parent node pointer set by last FindTag()
00045         m_pLastParent           = NULL;
00046 
00047         // child node pointer for each tag level set by last FindTag()
00048         memset(m_pLastChild, NULL, 20*sizeof(TiXmlNode*));
00049 }
00050 
00051 
00052 FEDM_XmlParser_TinyReader::~FEDM_XmlParser_TinyReader(void)
00053 {
00054         if(m_pDoc != NULL)
00055                 delete m_pDoc;
00056 }
00057 
00058 // open a xml document
00059 int FEDM_XmlParser_TinyReader::OpenDoc(char* sFileName)
00060 {
00061         CloseDoc();
00062         m_pLastParent           = NULL;
00063 
00064         m_pDoc = new TiXmlDocument(sFileName);
00065         if(m_pDoc == NULL)
00066         {
00067                 m_pXmlParser->SetLastError(FEDM_ERROR_OPEN_FILE);
00068                 return FEDM_ERROR_OPEN_FILE;
00069         }
00070 
00071         return FEDM_OK;
00072 }
00073 
00074 // close xml document
00075 int FEDM_XmlParser_TinyReader::CloseDoc()
00076 {
00077         m_pXmlParser->SetLastError(FEDM_OK);
00078 
00079         if(m_pDoc != NULL)
00080         {
00081                 delete m_pDoc;
00082                 m_pDoc = NULL;
00083         }
00084 
00085         return FEDM_OK;
00086 }
00087 
00088 // read the xml text from file
00089 int FEDM_XmlParser_TinyReader::LoadDoc()
00090 {
00091         TiXmlNode* node = NULL;
00092 
00093         m_pXmlParser->SetLastError(FEDM_OK);
00094 
00095         if(m_pDoc == NULL)
00096         {
00097                 m_pXmlParser->SetLastError(FEDM_ERROR_NULL_POINTER);
00098                 return FEDM_ERROR_NULL_POINTER;
00099         }
00100 
00101         bool bBack = m_pDoc->LoadFile();
00102         if(! bBack)
00103         {
00104                 CloseDoc();
00105                 m_pXmlParser->SetLastError(FEDM_ERROR_READ_FILE);
00106                 return FEDM_ERROR_READ_FILE;
00107         }
00108 
00109         node = m_pDoc->RootElement();
00110         if(node == NULL)
00111         {
00112                 CloseDoc();
00113                 m_pXmlParser->SetLastError(FEDM_ERROR_READ_FILE);
00114                 return FEDM_ERROR_READ_FILE;
00115         }
00116 
00117         if(strcmp(node->Value(), "OBID") != 0)
00118         {
00119                 CloseDoc();
00120                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_NO_OBID_TAG);
00121                 return FEDM_XML_ERROR_NO_OBID_TAG;
00122         }
00123 
00124         memset(m_pLastChild, NULL, 20*sizeof(TiXmlNode*));
00125         return FEDM_OK;
00126 }
00127 
00128 // read the xml text from file
00129 int FEDM_XmlParser_TinyReader::LoadDoc(const char* szOemRootTag)
00130 {
00131         TiXmlNode* node = NULL;
00132 
00133         m_pXmlParser->SetLastError(FEDM_OK);
00134 
00135         if(m_pDoc == NULL)
00136         {
00137                 m_pXmlParser->SetLastError(FEDM_ERROR_NULL_POINTER);
00138                 return FEDM_ERROR_NULL_POINTER;
00139         }
00140 
00141         bool bBack = m_pDoc->LoadFile();
00142         if(! bBack)
00143         {
00144                 CloseDoc();
00145                 m_pXmlParser->SetLastError(FEDM_ERROR_READ_FILE);
00146                 return FEDM_ERROR_READ_FILE;
00147         }
00148 
00149         node = m_pDoc->RootElement();
00150         if(node == NULL)
00151         {
00152                 CloseDoc();
00153                 m_pXmlParser->SetLastError(FEDM_ERROR_READ_FILE);
00154                 return FEDM_ERROR_READ_FILE;
00155         }
00156 
00157         if(strlen(szOemRootTag) > 0)
00158         {
00159                 if(strcmp(node->Value(), szOemRootTag) != 0)
00160                 {
00161                         CloseDoc();
00162                         m_pXmlParser->SetLastError(FEDM_XML_ERROR_NO_OBID_TAG);
00163                         return FEDM_XML_ERROR_NO_OBID_TAG;
00164                 }
00165         }
00166         else
00167         {
00168                 if(strcmp(node->Value(), "OBID") != 0)
00169                 {
00170                         CloseDoc();
00171                         m_pXmlParser->SetLastError(FEDM_XML_ERROR_NO_OBID_TAG);
00172                         return FEDM_XML_ERROR_NO_OBID_TAG;
00173                 }
00174         }
00175 
00176         memset(m_pLastChild, NULL, 20*sizeof(TiXmlNode*));
00177         return FEDM_OK;
00178 }
00179 
00180 int FEDM_XmlParser_TinyReader::LoadStream(wstring sXmlStream)
00181 {
00182         unsigned int i=0;
00183 
00184         CloseDoc();
00185 
00186         // parent node pointer set by last FindTag()
00187         m_pLastParent           = NULL;
00188 
00189         // child node pointer for each tag level set by last FindTag()
00190         memset(m_pLastChild, NULL, 20*sizeof(TiXmlNode*));
00191 
00192         m_pDoc = new TiXmlDocument;
00193         if(m_pDoc == NULL)
00194         {
00195                 m_pXmlParser->SetLastError(FEDM_ERROR_OPEN_FILE);
00196                 return FEDM_ERROR_OPEN_FILE;
00197         }
00198 
00199         unsigned int len = (unsigned int)sXmlStream.size();
00200         char* str = new char[(len+1)*sizeof(char)];
00201         for(i=0; i<sXmlStream.size(); ++i)
00202                 str[i] = (char)sXmlStream[i];
00203         str[i] = 0;
00204 
00205         m_pDoc->Parse(str);
00206         TiXmlNode* node = m_pDoc->RootElement();
00207         if(node == NULL)
00208         {
00209                 delete [] str;
00210                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00211                 return FEDM_XML_ERROR_TAG_NOT_FOUND;
00212         }
00213 
00214         delete [] str;
00215         m_pXmlParser->SetLastError(FEDM_OK);
00216         return FEDM_OK;
00217 }
00218 
00219 void FEDM_XmlParser_TinyReader::DeleteXmlTree()
00220 {
00221         return; // nothing to do
00222 }
00223 
00224 int FEDM_XmlParser_TinyReader::AddTagItem(FEDM_XML_TREEITEM* pParent, FEDM_XML_TREEITEM* pChild)
00225 {
00226         FEDM_XML_ATTRIBUTE_LIST_ITOR AttrItor;
00227 
00228         m_pXmlParser->SetLastError(FEDM_OK);
00229 
00230         // we misuse this function to delete the child
00231         for(AttrItor = pChild->aAttribList.begin(); AttrItor != pChild->aAttribList.end(); AttrItor++)
00232         {
00233                 (*AttrItor)->sAttribute.erase();
00234                 (*AttrItor)->sValue.erase();
00235 
00236                 delete *AttrItor;
00237         }
00238 
00239         delete pChild;
00240 
00241         return FEDM_OK;
00242 }
00243 
00244 FEDM_XML_TREEITEM* FEDM_XmlParser_TinyReader::FindTag(  wstring sTag,
00245                                                                                                                 unsigned int uiTagLevel,
00246                                                                                                                 bool bNext )
00247 {
00248         char cTag[256];
00249         wchar_t wcValue[256];
00250         TiXmlNode* node                         = NULL;
00251         TiXmlNode* parent                       = NULL;
00252         TiXmlNode* child                        = NULL;
00253         TiXmlElement* element           = NULL;
00254         TiXmlAttribute* attr            = NULL;
00255         FEDM_XML_TREEITEM* item         = NULL;
00256         FEDM_XML_ATTRIBUTE* attrib      = NULL;
00257 
00258         swscanf(sTag.c_str(), L"%S", cTag);
00259         
00260         // determine the parent node
00261         if(m_pLastParent == NULL)
00262         {
00263                 if(uiTagLevel == 0) // root tag is (mostly) "OBID"
00264                 {
00265                         node = m_pDoc->RootElement();
00266                         if(node == NULL)
00267                         {
00268                                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00269                                 return NULL;
00270                         }
00271 
00272                         m_uiLastTagLevel = 0;
00273                         m_pLastChild[0] = node;
00274 
00275                         element = node->ToElement();
00276                         if(element == NULL)
00277                         {
00278                                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00279                                 return NULL;
00280                         }
00281 
00282                         const char* cTagFound = element->Value();
00283                         if(cTagFound != NULL)
00284                         {
00285                                 if(strcmp(cTag, cTagFound) != 0)
00286                                 {
00287                                         m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00288                                         return NULL;
00289                                 }
00290                         }
00291 
00292                         // create item structure
00293                         item = new FEDM_XML_TREEITEM;
00294                         item->sTag = sTag;
00295 
00296                         // fill with value and attributes
00297                         if(element->GetText() != NULL)
00298                         {
00299                                 if(strcmp(element->GetText(), "(null)")!=0)
00300                                 {
00301 #if defined(__BORLANDC__) || defined(_MSC_VER)
00302         #if _MSC_VER <= 1200
00303                                         swprintf(wcValue, L"%S", element->GetText());
00304         #else
00305                 #if defined(_WIN32_WCE)
00306                                         swprintf(wcValue, L"%S", element->GetText());
00307                 #else
00308                                         swprintf(wcValue, 256, L"%S", element->GetText());
00309                 #endif
00310         #endif
00311 #else
00312                                         swprintf(wcValue, 256, L"%s", element->GetText());
00313 #endif
00314                                         item->sValue = wcValue;
00315                                 }
00316                         }
00317 
00318                         attr = element->FirstAttribute();
00319                         while(attr)
00320                         {
00321                                 // create attribute pair
00322                                 attrib = new FEDM_XML_ATTRIBUTE;
00323                                 if(attrib == NULL)
00324                                 {
00325                                         m_pXmlParser->SetLastError(FEDM_ERROR_NO_MORE_MEM);
00326                                         break;
00327                                 }
00328 
00329 #if defined(__BORLANDC__) || defined(_MSC_VER)
00330         #if _MSC_VER <= 1200
00331                                 swprintf(wcValue, L"%S", attr->Name());
00332         #else
00333                 #if defined(_WIN32_WCE)
00334                                 swprintf(wcValue, L"%S", attr->Name());
00335                 #else
00336                                 swprintf(wcValue, 256, L"%S", attr->Name());
00337                 #endif
00338         #endif
00339 #else
00340                                 swprintf(wcValue, 256, L"%s", attr->Name());
00341 #endif
00342                                 attrib->sAttribute = wcValue;
00343 #if defined(__BORLANDC__) || defined(_MSC_VER)
00344         #if _MSC_VER <= 1200
00345                                 swprintf(wcValue, L"%S", attr->Value());
00346         #else
00347                 #if defined(_WIN32_WCE)
00348                                 swprintf(wcValue, L"%S", attr->Value());
00349                 #else
00350                                 swprintf(wcValue, 256, L"%S", attr->Value());
00351                 #endif
00352         #endif
00353 #else
00354                                 swprintf(wcValue, 256, L"%s", attr->Value());
00355 #endif
00356                                 attrib->sValue = wcValue;
00357                                 item->aAttribList.push_back(attrib);
00358 
00359                                 attr = attr->Next();
00360                         }
00361 
00362                         return item;
00363                 }
00364                 else if(uiTagLevel == 1)
00365                 {
00366                         node = m_pDoc->RootElement();
00367                         if(node == NULL)
00368                         {
00369                                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00370                                 return NULL;
00371                         }
00372 
00373                         parent = node;
00374                 }
00375                 else
00376                 {
00377                         m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00378                         return NULL;
00379                 }
00380         }
00381         else
00382         {
00383                 parent = m_pLastParent;
00384 
00385                 // traverse upwards
00386                 if(uiTagLevel < m_uiLastTagLevel)
00387                 {
00388                         for(unsigned int uiCnt=m_uiLastTagLevel; uiCnt>uiTagLevel; uiCnt--)
00389                         {
00390                                 node = parent->Parent();
00391                                 if(node == NULL)
00392                                 {
00393                                         m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00394                                         return NULL;
00395                                 }
00396 
00397                                 parent = node;
00398                                 m_pLastChild[uiCnt] = NULL;
00399                         }
00400                 }
00401                 else if(uiTagLevel > m_uiLastTagLevel) // traverse downwards
00402                 {
00403                         parent = m_pLastChild[m_uiLastTagLevel];
00404                 }
00405         }
00406 
00407         if(bNext)
00408         {
00409                 if(m_pLastChild[uiTagLevel] == NULL)
00410                 {
00411                         node = parent->FirstChild(cTag);
00412                 }
00413                 else
00414                 {
00415                         node = m_pLastChild[uiTagLevel]->NextSibling(cTag);
00416                 }
00417         }
00418         else
00419         {
00420                 node = parent->FirstChild(cTag);
00421         }
00422 
00423         if(node == NULL)
00424         {
00425                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00426                 return NULL;
00427         }
00428 
00429         child = node;
00430         element = child->ToElement();
00431         if(element == NULL)
00432         {
00433                 m_pXmlParser->SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
00434                 return NULL;
00435         }
00436 
00437         // create item structure
00438         item = new FEDM_XML_TREEITEM;
00439         if(item == NULL)
00440         {
00441                 m_pXmlParser->SetLastError(FEDM_ERROR_NO_MORE_MEM);
00442                 return NULL;
00443         }
00444         item->sTag = sTag;
00445 
00446         if(element->GetText() != NULL)
00447         {
00448                 if(strcmp(element->GetText(), "(null)")!=0)
00449                 {
00450 #if defined(__BORLANDC__) || defined(_MSC_VER)
00451         #if _MSC_VER <= 1200
00452                         swprintf(wcValue, L"%S", element->GetText());
00453         #else
00454                 #if defined(_WIN32_WCE)
00455                         swprintf(wcValue, L"%S", element->GetText());
00456                 #else
00457                         swprintf(wcValue, 256, L"%S", element->GetText());
00458                 #endif
00459         #endif
00460 #else
00461                         swprintf(wcValue, 256, L"%s", element->GetText());
00462 #endif
00463                         item->sValue = wcValue;
00464                 }
00465         }
00466 
00467         attr = element->FirstAttribute();
00468         while(attr)
00469         {
00470                 // create attribute pair
00471                 attrib = new FEDM_XML_ATTRIBUTE;
00472                 if(attrib == NULL)
00473                 {
00474                         m_pXmlParser->SetLastError(FEDM_ERROR_NO_MORE_MEM);
00475                         break;
00476                 }
00477 
00478 #if defined(__BORLANDC__) || defined(_MSC_VER)
00479         #if _MSC_VER <= 1200
00480                 swprintf(wcValue, L"%S", attr->Name());
00481         #else
00482                 #if defined(_WIN32_WCE)
00483                 swprintf(wcValue, L"%S", attr->Name());
00484                 #else
00485                 swprintf(wcValue, 256, L"%S", attr->Name());
00486                 #endif
00487         #endif
00488 #else
00489                 swprintf(wcValue, 256, L"%s", attr->Name());
00490 #endif
00491                 attrib->sAttribute = wcValue;
00492 #if defined(__BORLANDC__) || defined(_MSC_VER)
00493         #if _MSC_VER <= 1200
00494                 swprintf(wcValue, L"%S", attr->Value());
00495         #else
00496                 #if defined(_WIN32_WCE)
00497                 swprintf(wcValue, L"%S", attr->Value());
00498                 #else
00499                 swprintf(wcValue, 256, L"%S", attr->Value());
00500                 #endif
00501         #endif
00502 #else
00503                 swprintf(wcValue, 256, L"%s", attr->Value());
00504 #endif
00505                 attrib->sValue = wcValue;
00506                 item->aAttribList.push_back(attrib);
00507 
00508                 attr = attr->Next();
00509         }
00510 
00511 
00512         // save search status for next FindTag()
00513         m_uiLastTagLevel                        = uiTagLevel;
00514         m_pLastParent                           = parent;
00515         m_pLastChild[uiTagLevel]        = child;
00516 
00517         return item;
00518 }
00519 
00520 FEDM_XML_TREEITEM* FEDM_XmlParser_TinyReader::FindTag(  wstring sTag,
00521                                                                                                                 wstring sAttribute,
00522                                                                                                                 unsigned int uiTagLevel,
00523                                                                                                                 bool bNext )
00524 {
00525         FEDM_XML_TREEITEM* item = FindTag(sTag, uiTagLevel, bNext);
00526         if(item == NULL)
00527                 return NULL;
00528 
00529         for(int i=0; i<(int)item->aAttribList.size(); ++i)
00530         {
00531                 if(item->aAttribList[i]->sAttribute == sAttribute)
00532                 {
00533                         m_pXmlParser->SetLastError(FEDM_OK);
00534                         return item;
00535                 }
00536         }
00537 
00538         m_pXmlParser->SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
00539         return NULL;
00540 }


maggie_rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:05:29