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 53 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 87 of file libxml.cpp.

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

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 157 of file libxml.cpp.

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

◆ ~Document()

XML::Document::~Document ( )

Destructor. Frees the loaded XML file.

Definition at line 163 of file libxml.cpp.

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

References mDoc.

Member Function Documentation

◆ isLoaded()

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

Definition at line 86 of file libxml.h.

87  { return mDoc != nullptr; }

References mDoc.

◆ isValid()

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

Definition at line 89 of file libxml.h.

90  { 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 169 of file libxml.cpp.

170  {
171  return mDoc != nullptr ? xmlDocGetRootElement(mDoc) : nullptr;
172  }
#define nullptr
Definition: localconsts.h:45

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(), loadXMLFile(), EffectManager::loadXmlFile(), SkillDialog::loadXmlFile(), AvatarDB::loadXmlFile(), ClanDb::loadXmlFile(), CommandsDB::loadXmlFile(), DeadDB::loadXmlFile(), ElementalDb::loadXmlFile(), EmoteDB::loadXmlFile(), GroupDb::loadXmlFile(), HomunculusDB::loadXmlFile(), HorseDB::loadXmlFile(), ItemFieldDb::loadXmlFile(), ItemOptionDb::loadXmlFile(), LanguageDb::loadXmlFile(), MercenaryDB::loadXmlFile(), ModDB::loadXmlFile(), MonsterDB::loadXmlFile(), NPCDB::loadXmlFile(), NpcDialogDB::loadXmlFile(), PETDB::loadXmlFile(), QuestDb::loadXmlFile(), SkillUnitDb::loadXmlFile(), SoundDB::loadXmlFile(), StatDb::loadXmlFile(), StatusEffectDB::loadXmlFile(), TextDb::loadXmlFile(), UnitsDb::loadXmlFile(), ItemDB::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 320 of file libxml.cpp.

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

References fileName, and strStartWith().

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

Field Documentation

◆ mDoc

xmlDocPtr XML::Document::mDoc
private

Definition at line 95 of file libxml.h.

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

◆ mIsValid

bool XML::Document::mIsValid
private

Definition at line 96 of file libxml.h.

Referenced by Document(), and isValid().


The documentation for this class was generated from the following files: