EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
XMLParser.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "../eve-core.h"
27 
28 #include "../log/LogNew.h"
29 #include "../memory/SafeMem.h"
30 #include "utils/XMLParser.h"
31 
32 /************************************************************************/
33 /* XMLParser */
34 /************************************************************************/
36 {
37 }
38 
40 {
41  ClearParsers();
42 }
43 
44 bool XMLParser::ParseFile( const char* file )
45 {
46  m_pXML_Document = make_unique<TiXmlDocument>(file);
47  if (!m_pXML_Document->LoadFile()) {
48  sLog.Error( "XMLParser", "Unable to load '%s': %s.", file, m_pXML_Document->ErrorDesc() );
49  return false;
50  }
51 
52  TiXmlElement* root = m_pXML_Document->RootElement();
53  if (root == nullptr) {
54  sLog.Error( "XMLParser", "Unable to find root in '%s'.", file );
55  return false;
56  }
57 
58  return ParseElement( root );
59 }
60 
61 bool XMLParser::ParseElement( const TiXmlElement* element ) const
62 {
63  std::map<std::string, ElementParser*>::const_iterator res = mParsers.find( element->Value() );
64  if (res == mParsers.end()) {
65  sLog.Error( "XMLParser", "Unknown element '%s' at line %d.", element->Value(), element->Row() );
66  return true; // Ignore any unanticipated XML tags and structures and continue parsing the rest of the XML
67  }
68 
69  return res->second->Parse( element );
70 }
71 
72 bool XMLParser::ParseElementChildren( const TiXmlElement* element, size_t max ) const
73 {
74  const TiXmlNode* child = nullptr;
75 
76  size_t count = 0;
77  while ((child = element->IterateChildren(child))) {
78  if (child->Type() == TiXmlNode::TINYXML_ELEMENT) {
79  const TiXmlElement* childElement = child->ToElement();
80  if ((max > 0) && (max <= count)) {
81  sLog.Error( "XMLParser", "Maximal children count %lu exceeded"
82  " in element '%s' at line %d"
83  " by element '%s' at line %d.",
84  max,
85  element->Value(), element->Row(),
86  childElement->Value(), childElement->Row() );
87 
88  return false;
89  }
90 
91  if (!ParseElement(childElement))
92  return false;
93 
94  ++count;
95  }
96  }
97 
98  return true;
99 }
100 
101 void XMLParser::AddParser( const char* name, ElementParser* parser )
102 {
103  mParsers[ name ] = parser;
104 }
105 
106 void XMLParser::RemoveParser( const char* name )
107 {
108  std::map<std::string, ElementParser*>::iterator res = mParsers.find( name );
109  if( mParsers.end() != res ) {
110  SafeDelete(res->second);
111  mParsers.erase(res);
112  }
113 }
114 
116  for (auto cur : mParsers)
117  SafeDelete(cur.second);
118 
119  mParsers.clear();
120 }
bool ParseElement(const TiXmlElement *element) const
Parses element using registered parsers.
Definition: XMLParser.cpp:61
bool ParseFile(const char *file)
Parses file using registered parsers.
Definition: XMLParser.cpp:44
XMLParser()
Primary constructor.
Definition: XMLParser.cpp:35
std::map< std::string, ElementParser * > mParsers
Definition: XMLParser.h:117
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
This virtual interface must be implemented by all parsers.
Definition: XMLParser.h:42
virtual ~XMLParser()
A destructor.
Definition: XMLParser.cpp:39
std::unique_ptr< TiXmlDocument > m_pXML_Document
Definition: XMLParser.h:113
void AddParser(const char *name, ElementParser *parser)
Adds a parser.
Definition: XMLParser.cpp:101
int64 max(int64 x, int64 y=0)
Definition: misc.h:103
void RemoveParser(const char *name)
Removes a parser.
Definition: XMLParser.cpp:106
void ClearParsers()
Clears all parsers.
Definition: XMLParser.cpp:115
bool ParseElementChildren(const TiXmlElement *element, size_t max=0) const
Parses element's children using registered parsers.
Definition: XMLParser.cpp:72