00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#ifndef GM_XMLNODE_H
00026
#define GM_XMLNODE_H
00027
00028
#include <iostream>
00029
#include <string>
00030
#include <map>
00031
#include <vector>
00032
#include "gm/exception.h"
00033
00034
namespace GNUMessenger
00035 {
00036
00037
using namespace std;
00038
class Contact;
00039
00042
00067 class XMLNode
00068 {
00069
public:
00070
00075 class InvalidChild:
public Exception
00076 {
00077
public:
00078
InvalidChild(
const string& error):
Exception(error) {}
00079 };
00080
00085 class ParseFailure:
public Exception
00086 {
00087
public:
00088
ParseFailure(
const string& error):
Exception(error) {}
00089 };
00090
00096 class NodeDestroyed:
public Exception
00097 {
00098
public:
00099
NodeDestroyed(
const string& error):
Exception(error) {}
00100 };
00101
00103
XMLNode();
00104
00107
XMLNode(
const XMLNode &other);
00108
00111
XMLNode(
const Contact &other);
00112
00113
00114
virtual ~
XMLNode();
00115
00117
00118
00119 virtual const string&
name()
const {
return m_xmlData->
m_name; }
00120
00121
virtual const string type()
const {
return "xml"; }
00122
00123
const string &data()
const {
return m_xmlData->
m_data; }
00124
00126 map<string,string> &
properties() {
return m_xmlData->
m_properties; }
00127
00129 const map<string,string> &
const_properties()
const {
return m_xmlData->
m_properties; }
00130
00132
const string
property(
const string &name)
const;
00133
00135
int intProperty(
const string &name)
const;
00136
00140
bool hasChild(
const string &name,
int n=1)
const;
00141
00143
unsigned int numChildren(
const string &name)
const;
00144
00146 vector<XMLNode> &
children() {
return m_xmlData->
m_children; }
00147
00148
00150 vector<XMLNode> &
const_children()
const {
return m_xmlData->
m_children; }
00151
00156
XMLNode&
child(
const string &name,
int n=1);
00157
00164
XMLNode child(
const string& name,
int n = 1)
const;
00165
00167 inline XMLNode &
setName(
const string &name) { m_xmlData->
m_name = name;
return *
this; }
00168
00170 inline XMLNode &
setData(
const string &data) { m_xmlData->
m_data = data;
return *
this; }
00171
00173 XMLNode &
setCData(
const string &data) { m_xmlData->
data_is_cdata=
true; m_xmlData->
m_data = data;
return *
this; }
00174
00176
XMLNode &
setProperty(
const string &name,
const string &value);
00178
XMLNode &
setProperty(
const string &name,
int value);
00179
00181 XMLNode &
addChild(
XMLNode &node) { m_xmlData->
m_children.push_back(node);
return node;}
00182
00184
XMLNode &
addChild(
const string &name,
const string &data=
"");
00185
00191
XMLNode &
delChild(
const string &name,
int n=1);
00192
00194
XMLNode&
moveChild(
unsigned int index,
unsigned int newIndex);
00195
00201
friend ostream&
operator<<(ostream &o,
XMLNode &n);
00202
00208
XMLNode &
operator<<(
const string &str)
throw(ParseFailure);
00209
00216
XMLNode &
operator=(
const XMLNode &other);
00217
00219
operator string() const;
00220
00222 string& operator[](const string &name);
00223
00229 string operator[](const string &name) const;
00230
00231 friend class
XMLParser;
00232
00233
00234 protected:
00235
00239 class
XMLNodeData
00240 {
00241
public:
00242 XMLNodeData()
00243 { refCount=1; }
00244
00245 ~XMLNodeData() { }
00246
00249 inline void ref() { refCount++; }
00250
00253 inline bool deref()
00254 {
if (--refCount == 0)
return true;
return false; }
00255
00256 vector<XMLNode> m_children;
00257 map <string,string> m_properties;
00258
00259 string m_name;
00260 string m_data;
00261
00262
private:
00263
int refCount;
00264
00265
public:
00266
bool data_is_cdata;
00267
bool oneline;
00268 };
00269
00270
static bool m_isValid;
00271
00272 XMLNodeData *m_xmlData;
00273 };
00274
00275
00276 }
00277
00278
#endif // !GM_XMLNODE_H