ManaPlus
Public Member Functions | Static Public Member Functions | Private Attributes
XML::Document Class Reference

#include <libxml.h>

Inheritance diagram for XML::Document:
Resource MemoryCounter

Public Member Functions

 Document (const std::string &filename, const UseVirtFs useResman, const SkipError skipError)
 
 Document (const char *const data, const int size)
 
 ~Document ()
 
xmlNodePtr rootNode ()
 
bool isLoaded () const
 
bool isValid () const
 
- Public Member Functions inherited from Resource
 Resource ()
 
 ~Resource ()
 
virtual void incRef ()
 
virtual void decRef ()
 
int calcMemoryLocal () const
 
std::string getCounterName () const
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 
virtual int calcMemoryChilds (const int level) const
 

Static Public Member Functions

static bool validateXml (const std::string &fileName)
 

Private Attributes

xmlDocPtr mDoc
 
bool mIsValid
 

Additional Inherited Members

- Data Fields inherited from Resource
time_t mTimeStamp
 
std::string mIdPath
 
std::string mSource
 
unsigned int mRefCount
 
bool mProtected
 
bool mNotCount
 

Detailed Description

A helper class for parsing an XML document, which also cleans it up again (RAII).

Definition at line 52 of file libxml.h.

Constructor & Destructor Documentation

◆ Document() [1/2]

XML::Document::Document ( const std::string &  filename,
const UseVirtFs  useResman,
const SkipError  skipError 
)

Constructor that attempts to load the given file through the resource manager. Logs errors.

Definition at line 86 of file libxml.cpp.

88  :
89  Resource(),
90  mDoc(nullptr),
91  mIsValid(false)
92  {
93 #ifdef USE_FUZZER
94  if (Fuzzer::conditionTerminate(filename.c_str()))
95  return;
96 #endif // USE_FUZZER
97 
98  BLOCK_START("XML::Document::Document")
99  int size = 0;
100  char *data = nullptr;
101  valid = true;
102  if (useResman == UseVirtFs_true)
103  {
104  data = const_cast<char*>(VirtFs::loadFile(
105  filename,
106  size));
107  }
108  else
109  {
110  std::ifstream file;
111  file.open(filename.c_str(), std::ios::in);
112 
113  if (file.is_open())
114  {
115  // Get length of file
116  file.seekg(0, std::ios::end);
117  size = CAST_S32(file.tellg());
118  if (size < 0)
119  {
120  reportAlways("Error loading XML file %s",
121  filename.c_str())
122  }
123  else
124  {
125  file.seekg(0, std::ios::beg);
126  data = new char[size];
127  file.read(data, size);
128  }
129  file.close();
130  }
131  else if (skipError == SkipError_false)
132  {
133  reportAlways("Error loading XML file %s",
134  filename.c_str())
135  }
136  }
137 
138  if (data != nullptr)
139  {
140  mDoc = xmlParseMemory(data, size);
141  delete [] data;
142 
143  if (mDoc == nullptr)
144  {
145  reportAlways("Error parsing XML file %s", filename.c_str())
146  }
147  }
148  else if (skipError == SkipError_false)
149  {
150  reportAlways("Error loading XML file %s", filename.c_str())
151  }
152  mIsValid = valid;
153  BLOCK_END("XML::Document::Document")
154  }

References BLOCK_END, BLOCK_START, CAST_S32, data, VirtFs::loadFile(), mDoc, mIsValid, reportAlways, EmoteDB::size(), SkipError_false, UseVirtFs_true, and anonymous_namespace{libxml.cpp}::valid.

◆ Document() [2/2]

XML::Document::Document ( const char *const  data,
const int  size 
)

Constructor that attempts to load an XML document from memory. Does not log errors.

Parameters
datathe string to parse as XML
sizethe length of the string in bytes

Definition at line 156 of file libxml.cpp.

156  :
157  mDoc(data != nullptr ? xmlParseMemory(data, size) : nullptr),
158  mIsValid(true)
159  {
160  }

◆ ~Document()

XML::Document::~Document ( )

Destructor. Frees the loaded XML file.

Definition at line 162 of file libxml.cpp.

163  {
164  if (mDoc != nullptr)
165  xmlFreeDoc(mDoc);
166  }

References mDoc.

Member Function Documentation

◆ isLoaded()

bool XML::Document::isLoaded ( ) const
inline

Definition at line 85 of file libxml.h.

86  { return mDoc != nullptr; }

References mDoc.

◆ isValid()

bool XML::Document::isValid ( ) const
inline

Definition at line 88 of file libxml.h.

89  { return mIsValid; }

References mIsValid.

◆ rootNode()

xmlNodePtr XML::Document::rootNode ( )

Returns the root node of the document (or NULL if there was a load error).

Definition at line 168 of file libxml.cpp.

169  {
170  return mDoc != nullptr ? xmlDocGetRootElement(mDoc) : nullptr;
171  }

References mDoc.

Referenced by Particle::addEffect(), ParticleEngine::addEffect(), MapReader::addLayerToList(), EquipmentWindow::fillBoxes(), SpriteDef::includeSprite(), Configuration::init(), CharDB::load(), SpriteDef::load(), ColorDB::loadColorLists(), Theme::loadColors(), ColorDB::loadHair(), MapDB::loadInfo(), Theme::loadInfo(), MapDB::loadRemapXmlFile(), ServerDialog::loadServers(), EmoteDB::loadSpecialXmlFile(), SoundDB::loadXmlFile(), TextDb::loadXmlFile(), LanguageDb::loadXmlFile(), ItemFieldDb::loadXmlFile(), StatDb::loadXmlFile(), DeadDB::loadXmlFile(), PETDB::loadXmlFile(), SkillUnitDb::loadXmlFile(), ModDB::loadXmlFile(), UnitsDb::loadXmlFile(), QuestDb::loadXmlFile(), GroupDb::loadXmlFile(), ItemOptionDb::loadXmlFile(), ClanDb::loadXmlFile(), NpcDialogDB::loadXmlFile(), HorseDB::loadXmlFile(), AvatarDB::loadXmlFile(), MonsterDB::loadXmlFile(), MercenaryDB::loadXmlFile(), CommandsDB::loadXmlFile(), HomunculusDB::loadXmlFile(), ElementalDb::loadXmlFile(), EmoteDB::loadXmlFile(), ItemDB::loadXmlFile(), NPCDB::loadXmlFile(), EffectManager::loadXmlFile(), StatusEffectDB::loadXmlFile(), loadXMLFile(), SkillDialog::loadXmlFile(), EquipmentWindow::prepareSlotNames(), Theme::readSkin(), MapReader::readTileset(), readXmlIntMap(), readXmlIntVector(), readXmlStringMap(), and Configuration::reInit().

◆ validateXml()

bool XML::Document::validateXml ( const std::string &  fileName)
static

Definition at line 319 of file libxml.cpp.

320  {
321  const xmlDocPtr doc = xmlReadFile(fileName.c_str(),
322  nullptr, XML_PARSE_PEDANTIC);
323  const bool valid1(doc != nullptr);
324  xmlFreeDoc(doc);
325  if (!valid1)
326  return false;
327 
328  std::ifstream file;
329  file.open(fileName.c_str(), std::ios::in);
330  if (!file.is_open())
331  {
332  file.close();
333  return false;
334  }
335  char line[101];
336  if (!file.getline(line, 100))
337  return false;
338  file.close();
339 
340  const std::string str = line;
341  if (!strStartWith(str, "<?xml "))
342  return false;
343 
344  return true;
345  }

References fileName, and strStartWith().

Referenced by Net::Download::downloadThread().

Field Documentation

◆ mDoc

xmlDocPtr XML::Document::mDoc
private

Definition at line 94 of file libxml.h.

Referenced by Document(), isLoaded(), rootNode(), and ~Document().

◆ mIsValid

bool XML::Document::mIsValid
private

Definition at line 95 of file libxml.h.

Referenced by Document(), and isValid().


The documentation for this class was generated from the following files:
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
anonymous_namespace{libxml.cpp}::valid
bool valid
Definition: libxml.cpp:42
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
if
if(!vert) return
XML::Document::mIsValid
bool mIsValid
Definition: libxml.h:95
fileName
std::string fileName
Definition: testmain.cpp:38
nullptr
#define nullptr
Definition: localconsts.h:44
data
uint32_t data
Definition: maptypeproperty2.h:3
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
XML::Document::mDoc
xmlDocPtr mDoc
Definition: libxml.h:94
EmoteDB::size
int size()
Definition: emotedb.cpp:305
Resource::Resource
Resource()
Definition: resource.h:39
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
CAST_S32
#define CAST_S32
Definition: cast.h:29
strStartWith
bool strStartWith(const std::string &str1, const std::string &str2)
Definition: stringutils.cpp:723
VirtFs::loadFile
const char * loadFile(std::string filename, int &fileSize)
Definition: fs.cpp:858