TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::XML Class Reference

Easy loading and saving of XML and HTML files. More...

#include <XML.h>

Public Types

enum  Type { VALUE, TAG }
 
typedef Table< std::string, XMLAttributeTable
 

Public Member Functions

 XML ()
 
 XML (const std::string &v)
 
 XML (const double &v)
 
 XML (float v)
 
 XML (int v)
 
 XML (Type tagType, const std::string &name, const AttributeTable &at, const Array< XML > &ch=Array< XML >())
 
 XML (Type tagType, const std::string &name, const Array< XML > &ch=Array< XML >())
 
 XML (TextInput &t)
 
void serialize (TextOutput &t) const
 
void deserialize (TextInput &t)
 
void load (const std::string &filename)
 
void save (const std::string &filename) const
 
void parse (const std::string &s)
 
void unparse (std::string &s) const
 
const AttributeTableattributeTable () const
 
const Array< XML > & childArray () const
 
int numChildren () const
 
size_t numAttributes () const
 
const XMLoperator[] (int i) const
 
const XMLoperator[] (const std::string &k) const
 
bool containsAttribute (const std::string &k) const
 
XML get (const std::string &k, const XML &defaultVal) const
 
Type type () const
 
const std::string name () const
 
const std::string & string () const
 
double number () const
 
bool boolean () const
 
 operator std::string () const
 
 operator bool () const
 
 operator double () const
 
 operator float () const
 
 operator int () const
 

Private Attributes

Type m_type
 
std::string m_name
 
std::string m_value
 
AttributeTable m_attribute
 
Array< XMLm_child
 

Detailed Description

Easy loading and saving of XML and HTML files.

The XML class is intended primarily for interchange with other programs. We recommend using G3D::Any to make your own human-readable formats because it is a more general syntax, the implementation is more efficient, and contains better error handling.

Every XML is either a VALUE, or a TAG that contains both a table of its XML attributes and an array of its children. Children are nested tags and the strings between the nested tags.

No validation is performed, and the XML must be completely legal. XML Entity references (e.g., the ampersand codes for greater than and less than) are not automatically converted.

Tags with names that begin with "!" or "?" are ignored. Comment tags must end with "-->" e.g.,

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!DOCTYPE note SYSTEM "Note.dtd">
See also
G3D::Any, http://www.grinninglizard.com/tinyxml/


  child0 ...
  ...
  child2 ...

Member Typedef Documentation

typedef Table<std::string, XML> G3D::XML::AttributeTable

Member Enumeration Documentation

Enumerator
VALUE 
TAG 
68 {VALUE, TAG};
Definition: XML.h:68
Definition: XML.h:68

Constructor & Destructor Documentation

G3D::XML::XML ( )
inline
82 : m_type(VALUE) {}
Type m_type
Definition: XML.h:74
Definition: XML.h:68
G3D::XML::XML ( const std::string &  v)
inline
84 : m_type(VALUE), m_value(v) {}
Type m_type
Definition: XML.h:74
std::string m_value
Definition: XML.h:76
Definition: XML.h:68
G3D::XML::XML ( const double &  v)
inline
86 : m_type(VALUE), m_value(format("%f", v)) {}
Type m_type
Definition: XML.h:74
std::string m_value
Definition: XML.h:76
Definition: XML.h:68
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
G3D::XML::XML ( float  v)
inline
88 : m_type(VALUE), m_value(format("%f", v)) {}
Type m_type
Definition: XML.h:74
std::string m_value
Definition: XML.h:76
Definition: XML.h:68
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
G3D::XML::XML ( int  v)
inline
90 : m_type(VALUE), m_value(format("%d", v)) {}
Type m_type
Definition: XML.h:74
std::string m_value
Definition: XML.h:76
Definition: XML.h:68
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
G3D::XML::XML ( Type  tagType,
const std::string &  name,
const AttributeTable at,
const Array< XML > &  ch = Array<XML>() 
)
inline
Parameters
tagTypeMust be XML::TAG to dismbiguate from the string constructor
93  : m_type(TAG), m_name(name), m_attribute(at), m_child(ch) {
94  (void)tagType;
95  debugAssert(tagType == TAG);
96  }
AttributeTable m_attribute
Definition: XML.h:77
Type m_type
Definition: XML.h:74
const std::string name() const
Definition: XML.h:167
#define debugAssert(exp)
Definition: debugAssert.h:160
Definition: XML.h:68
Array< XML > m_child
Definition: XML.h:78
std::string m_name
Definition: XML.h:75
G3D::XML::XML ( Type  tagType,
const std::string &  name,
const Array< XML > &  ch = Array<XML>() 
)
inline
Parameters
tagTypeMust be XML::TAG to dismbiguate from the string constructor
99  : m_type(TAG), m_name(name), m_child(ch) {
100  (void)tagType;
101  debugAssert(tagType == TAG);
102  }
Type m_type
Definition: XML.h:74
const std::string name() const
Definition: XML.h:167
#define debugAssert(exp)
Definition: debugAssert.h:160
Definition: XML.h:68
Array< XML > m_child
Definition: XML.h:78
std::string m_name
Definition: XML.h:75
G3D::XML::XML ( TextInput t)
22  : m_type(VALUE) {
23  deserialize(t);
24 }
Type m_type
Definition: XML.h:74
Definition: XML.h:68
void deserialize(TextInput &t)
Definition: XML.cpp:125

+ Here is the call graph for this function:

Member Function Documentation

const AttributeTable& G3D::XML::attributeTable ( ) const
inline
118  {
119  return m_attribute;
120  }
AttributeTable m_attribute
Definition: XML.h:77
bool G3D::XML::boolean ( ) const

Returns false if a TAG.

31  {
33 }
std::string m_value
Definition: XML.h:76
static bool parseBoolean(const std::string &_string)
Definition: TextInput.cpp:42

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const Array<XML>& G3D::XML::childArray ( ) const
inline
122  {
123  return m_child;
124  }
Array< XML > m_child
Definition: XML.h:78
bool G3D::XML::containsAttribute ( const std::string &  k) const
inline
147  {
148  return m_attribute.containsKey(k);
149  }
AttributeTable m_attribute
Definition: XML.h:77
bool containsKey(const Key &key) const
Definition: Table.h:874

+ Here is the call graph for this function:

void G3D::XML::deserialize ( TextInput t)
125  {
126  begin:
127  Token n = t.read();
128  m_attribute.clear();
129  m_child.clear();
130  m_name = "";
131  m_value = "";
132 
133  if ((n.type() == Token::SYMBOL) && (n.string() == "<")) {
134  // Beginning a tag
135 
136  // Read name
137  n = t.read();
138  debugAssert(n.type() == Token::SYMBOL);
139  bool isComment =
140  (n.string() == "!") &&
141  (t.peek().type() == Token::SYMBOL) &&
142  (t.peek().string() == "--");
143 
144  // ignored tag: <?xml> or <!xml>
145  // comment tag: <!-- ... -->
146 
147  if ((n.string() == "?") || ((n.string() == "!") && ! isComment)) {
148  // Ignore this tag
149  while (t.hasMore() && ! ((n.type() == Token::SYMBOL) && (n.string() == ">"))) {
150  n = t.read();
151  }
152  goto begin;
153  } else if (isComment) {
154  // Ignore until "-->"
155  bool prevWasDash = false;
156  while (t.hasMore() && ! ((n.type() == Token::SYMBOL) && (n.string() == ">") && prevWasDash)) {
157  prevWasDash = (n.type() == Token::SYMBOL) && (n.string() == "--");
158  n = t.read();
159  }
160  goto begin;
161  }
162 
163  // Keep reading until no colon
164  m_name += n.string();
165  n = t.read();
166  while ((n.type() == Token::SYMBOL) && (n.string() == ":")) {
167  // tag with namespace: <x:y>
168  m_name += ":" + t.readSymbol();
169  n = t.read();
170  }
171 
172  // Read end of tag/close
173  bool done = false;
174  while (! done) {
175  debugAssert(n.type() == Token::SYMBOL);
176  if (n.string() == "/") {
177  // empty-element tag: <foo/>
178  // Consume the close tag
179  t.readSymbol(">");
180  done = true;
181 
182  } else if (n.string() == ">") {
183  // End of open tag: read children until close tag
184  while (! atClose(t, m_name)) {
185  m_child.next().deserialize(t);
186  }
187 
188  // Read close tag (we wouldn't be here unless it parses correctly)
189  while (t.hasMore() && ! (t.readSymbol() == ">")) {}
190 
191  done = true;
192  } else {
193  // Attribute pair
194  std::string k = n.string();
195  t.readSymbol("=");
196  std::string v = t.read().string();
197  m_attribute.set(k, v);
198 
199  // Advance to next
200  n = t.read();
201  }
202  }
203  } else {
204  // Beginning embedded content. Read until the end of file or the next tag.
205  m_type = VALUE;
206  m_value += n.string();
207 
208  n = t.peek();
209  while (t.hasMore() && ! ((n.type() == Token::SYMBOL) && (n.string() == "<"))) {
210  m_value += " " + t.read().string();
211  n = t.peek();
212  }
213  }
214 }
AttributeTable m_attribute
Definition: XML.h:77
Definition: TextInput.h:58
Type m_type
Definition: XML.h:74
void clear()
Definition: Table.h:578
std::string m_value
Definition: XML.h:76
void set(const Key &key, const Value &value)
Definition: Table.h:599
Definition: XML.h:68
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< XML > m_child
Definition: XML.h:78
std::string m_name
Definition: XML.h:75
static bool atClose(TextInput &t, const std::string name)
Definition: XML.cpp:98

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

XML G3D::XML::get ( const std::string &  k,
const XML defaultVal 
) const
inline

Note that the result is always copied, making this inefficient for return values that are not VALUEs.

153  {
154  const XML* x = m_attribute.getPointer(k);
155  if (x) {
156  return *x;
157  } else {
158  return defaultVal;
159  }
160  }
AttributeTable m_attribute
Definition: XML.h:77
Value * getPointer(const Key &key) const
Definition: Table.h:731
XML()
Definition: XML.h:82
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the call graph for this function:

void G3D::XML::load ( const std::string &  filename)
36  {
37  TextInput::Settings s;
38  s.cppBlockComments = false;
39  s.cppLineComments = false;
40  s.proofSymbols = false;
41 
42  TextInput t(filename, s);
43  deserialize(t);
44 }
void deserialize(TextInput &t)
Definition: XML.cpp:125

+ Here is the call graph for this function:

const std::string G3D::XML::name ( ) const
inline

The name, if this is a TAG.

167  {
168  return m_name;
169  }
std::string m_name
Definition: XML.h:75
size_t G3D::XML::numAttributes ( ) const
inline

Attribute table size; zero for a TAG

132  {
133  return m_attribute.size();
134  }
AttributeTable m_attribute
Definition: XML.h:77
size_t size() const
Definition: Table.h:589

+ Here is the call graph for this function:

double G3D::XML::number ( ) const

Parse as a number. Returns nan() if a TAG or unparseable as a number.

26  {
28 }
std::string m_value
Definition: XML.h:76
static double parseNumber(const std::string &_string)
Definition: TextInput.cpp:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int G3D::XML::numChildren ( ) const
inline

Array size; zero for a VALUE

127  {
128  return m_child.size();
129  }
Array< XML > m_child
Definition: XML.h:78

+ Here is the call graph for this function:

G3D::XML::operator bool ( ) const
inline
186  {
187  return boolean();
188  }
bool boolean() const
Definition: XML.cpp:31

+ Here is the call graph for this function:

G3D::XML::operator double ( ) const
inline
190  {
191  return number();
192  }
double number() const
Definition: XML.cpp:26

+ Here is the call graph for this function:

G3D::XML::operator float ( ) const
inline
194  {
195  return float(number());
196  }
double number() const
Definition: XML.cpp:26

+ Here is the call graph for this function:

G3D::XML::operator int ( ) const
inline
198  {
199  return iRound(number());
200  }
int iRound(double fValue)
Definition: g3dmath.h:226
double number() const
Definition: XML.cpp:26

+ Here is the call graph for this function:

G3D::XML::operator std::string ( ) const
inline
182  {
183  return m_value;
184  }
std::string m_value
Definition: XML.h:76
const XML& G3D::XML::operator[] ( int  i) const
inline

Return child i. Children are nested tags and the unquoted strings of characters between tags.

138  {
139  return m_child[i];
140  }
Array< XML > m_child
Definition: XML.h:78
const XML& G3D::XML::operator[] ( const std::string &  k) const
inline

Return the attribute with this name.

143  {
144  return m_attribute[k];
145  }
AttributeTable m_attribute
Definition: XML.h:77
void G3D::XML::parse ( const std::string &  s)
91  {
92  TextInput t(TextInput::FROM_STRING, s);
93  deserialize(t);
94 }
Definition: TextInput.h:575
void deserialize(TextInput &t)
Definition: XML.cpp:125

+ Here is the call graph for this function:

void G3D::XML::save ( const std::string &  filename) const
47  {
48  std::string s;
49  unparse(s);
50  writeWholeFile(filename, s);
51 }
void unparse(std::string &s) const
Definition: XML.cpp:54
void writeWholeFile(const std::string &filename, const std::string &str, bool flush=true)
Definition: fileutils.cpp:207

+ Here is the call graph for this function:

void G3D::XML::serialize ( TextOutput t) const
65  {
66  if (m_type == VALUE) {
67  // Raw string; no quotes
68  t.writeSymbol(m_value);
69  } else {
70  t.printf("<%s", m_name.c_str());
71  for (AttributeTable::Iterator it = m_attribute.begin(); it.isValid(); ++it) {
72  t.printf(" %s=\"%s\"", it->key.c_str(), it->value.m_value.c_str());
73  }
74  t.printf(">");
75  t.writeNewline();
76  t.pushIndent();
77  for (int i = 0; i < m_child.size(); ++i) {
78  m_child[i].serialize(t);
79  if (m_child[i].m_type == VALUE) {
80  // Only tags know to append a newline
81  t.writeNewline();
82  }
83  }
84  t.popIndent();
85  t.printf("</%s>", m_name.c_str());
86  t.writeNewline();
87  }
88 }
AttributeTable m_attribute
Definition: XML.h:77
Type m_type
Definition: XML.h:74
std::string m_value
Definition: XML.h:76
Iterator begin() const
Definition: Table.h:562
Definition: XML.h:68
Array< XML > m_child
Definition: XML.h:78
std::string m_name
Definition: XML.h:75

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::string& G3D::XML::string ( ) const
inline

Returns "" if a TAG.

172  {
173  return m_value;
174  }
std::string m_value
Definition: XML.h:76
Type G3D::XML::type ( ) const
inline
162  {
163  return m_type;
164  }
Type m_type
Definition: XML.h:74
void G3D::XML::unparse ( std::string &  s) const
54  {
55  TextOutput::Settings set;
57  TextOutput t(set);
58 
59  serialize(t);
60 
61  t.commitString(s);
62 }
void serialize(TextOutput &t) const
Definition: XML.cpp:65

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

AttributeTable G3D::XML::m_attribute
private
Array<XML> G3D::XML::m_child
private
std::string G3D::XML::m_name
private
Type G3D::XML::m_type
private
std::string G3D::XML::m_value
private

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