00001
#ifndef XERCESDOMUTIL_H
00002
#define XERCESDOMUTIL_H
00003
00004
#include <exception>
00005
#include <string>
00006
#include <vector>
00007
#include <xercesc/dom/DOM.hpp>
00008
#include <xercesc/util/XMLString.hpp>
00009
#include <sys/time.h>
00010
00011
#include "Exceptions.h"
00012
00013 BOREALIS_NAMESPACE_BEGIN;
00014
00015
#ifdef XERCES_CPP_NAMESPACE
00016
using namespace xercesc;
00017
#endif
00018
00019
00020
00021
00022
00023
void writeDomDocToFile(
const DOMDocument & doc, string filename)
00024
throw (exception);
00025
00026
00027
00028 DOMDocument *
readDomDocFromFile(DOMImplementationLS & impl, string filename)
00029
throw (exception);
00030
00031
00032
00033 DOMImplementationLS *
getDomImplementationLs()
00034 throw (exception);
00035
00036
00037 DOMImplementation * getDomImplementation()
00038 throw (exception);
00039
00040 DOMElement * createDomElement(DOMDocument & docOwner, string elementName)
00041 throw (exception);
00042
00043
00044
00045 DOMElement * getDomChildByKeyTag(const DOMElement & parent, string childTagName)
00046 throw (exception);
00047
00048
00049
00050
00051
00052
bool parseXmlStringAsBool(const XMLCh * pXml)
00053 throw (exception);
00054
00055
00056
00057
00058
int parseXmlStringAsInt(const XMLCh * pXml)
00059 throw (exception);
00060
00061
unsigned int parseXmlStringAsUInt(const XMLCh * pXml)
00062 throw (exception);
00063
00064
unsigned long parseXmlStringAsLong(const XMLCh * pXml)
00065 throw (exception);
00066
00067
unsigned long parseXmlStringAsULong(const XMLCh * pXml)
00068 throw (exception);
00069
00070
00071
00072
00073
unsigned long long parseXmlStringAsULongLong(const XMLCh * pXml)
00074 throw (exception);
00075
00076
double parseXmlStringAsDouble(const XMLCh * pXml)
00077 throw (exception);
00078
00079
00080
00081
00082
00083
void saveIntVectorToXml(const vector<
int> & v, DOMDocument & doc, DOMElement & elem)
00084 throw (exception);
00085
00086
00087
00088
void loadIntVectorFromXml(vector<
int> & v, DOMElement & elem)
00089 throw (exception);
00090
00091
00092
00093
00094
00095
void saveTimevalToXml(const timeval & tv, DOMDocument & doc, DOMElement & elem)
00096 throw (exception);
00097
00098
00099
00100
void loadTimevalFromXml(timeval & tv, DOMElement & elem)
00101 throw (exception);
00102
00103
00104
00105
00106
00107
00108
00109
void setDomAttribute(DOMElement & elem, string attributeName,
bool newValue)
00110 throw (exception);
00111
00112
void setDomAttribute(DOMElement & elem, string attributeName,
int newValue)
00113 throw (exception);
00114
00115
void setDomAttribute(DOMElement & elem, string attributeName,
unsigned int newValue)
00116 throw (exception);
00117
00118
void setDomAttribute(DOMElement & elem, string attributeName,
long newValue)
00119 throw (exception);
00120
00121
void setDomAttribute(DOMElement & elem, string attributeName,
unsigned long newValue)
00122 throw (exception);
00123
00124
void setDomAttribute(DOMElement & elem, string attributeName,
unsigned long long newValue)
00125 throw (exception);
00126
00127
00128
00129
00130
00131
void setDomAttribute(DOMElement & elem, string attributeName, string newValue)
00132 throw (exception);
00133
00134
void setDomAttribute(DOMElement & elem, string attributeName,
double newValue)
00135 throw (exception);
00136
00137
00138
00139
void getDomAttribute(const DOMElement & elem, string attributeName,
bool & value)
00140 throw (exception);
00141
00142
void getDomAttribute(const DOMElement & elem, string attributeName,
int & value)
00143 throw (exception);
00144
00145
void getDomAttribute(const DOMElement & elem, string attributeName,
unsigned int & value)
00146 throw (exception);
00147
00148
void getDomAttribute(const DOMElement & elem, string attributeName,
long & value)
00149 throw (exception);
00150
00151
void getDomAttribute(const DOMElement & elem, string attributeName,
unsigned long & value)
00152 throw (exception);
00153
00154
void getDomAttribute(const DOMElement & elem, string attributeName,
unsigned long long & value)
00155 throw (exception);
00156
00157
void getDomAttribute(const DOMElement & elem, string attributeName, string & newValue)
00158 throw (exception);
00159
00160
void getDomAttribute(const DOMElement & elem, string attributeName,
double & newValue)
00161 throw (exception);
00162
00163
00164
00165
00166
bool isDomAttributePresent(const DOMElement & elem, string attributeName)
00167 throw (exception);
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 enum XmlAttrReq {
00181
ATTR_NOT_REQUIRED = 0,
00182
ATTR_REQUIRED = 1,
00183
ATTR_NON_EMPTY = 2
00184 };
00185
00186
00187
00188 ptr<DOMDocument>
parseXmlFile(string filename)
throw (AuroraBadXmlException);
00189
00190
00191 ptr<DOMDocument>
parseXmlString(string data)
throw (AuroraBadXmlException);
00192
00193
00194 ptr<DOMDocument>
parseXmlString(
const void *data,
unsigned int length)
throw (AuroraBadXmlException);
00195
00196
00197
00198
00199 string
xmlAttribute(
const DOMElement *element, string name,
00200 XmlAttrReq req = ATTR_NOT_REQUIRED)
throw(AuroraBadEntityException);
00201
00202
00203
00204 string
xmlAttribute(
const DOMElement *element, string name, string def)
throw (AuroraBadEntityException);
00205
00206
00207
00208
void xmlExpectTag(
const DOMElement *element, string name)
throw (AuroraBadEntityException);
00209
00210
00211
00212
00213
00214
00215
00216
template <
class T>
00217 inline bool xmlTypedAttribute(
const DOMElement *element, string name, T& value,
00218 XmlAttrReq req = ATTR_NOT_REQUIRED)
throw (AuroraBadEntityException)
00219 {
00220 XMLCh xname[64];
00221 XMLString::transcode(name.c_str(), xname,
sizeof xname - 1);
00222
00223
const DOMAttr *attr = element->getAttributeNode(xname);
00224
if (!attr) {
00225
if (req !=
ATTR_NOT_REQUIRED) {
00226
Throw(AuroraBadEntityException,
00227
"<" +
to_string(element->getTagName()) +
"> lacks "
00228
"required attribute \"" + name +
"\"");
00229 }
00230
return false;
00231 }
00232
00233
00234 string val =
to_string(attr->getValue());
00235
00236 istringstream i(val);
00237
if (i >> value)
00238
return true;
00239
00240
Throw(AuroraBadEntityException,
"Invalid format for <" +
00241
to_string(element->getTagName()) +
" " + name +
"=\"" + val +
"\">");
00242 }
00243
00244
00245 string
xmlEscape(string in);
00246
00247
00248
00249 DOMElement*
xmlOnlyTag(
const DOMElement* parent,
bool required =
true)
00250
throw (
AuroraException);
00251
00252
00253
00254
00255 DOMElement*
xmlOnlyTag(
const DOMElement* parent, string tag,
bool required =
true)
00256
throw (
AuroraException);
00257
00258
00259
void xmlChildElements(vector<DOMElement*>& children,
const DOMElement* parent, string tag = string());
00260
00261
00262 string
xmlSerialize(
const DOMNode *node);
00263
00264 BOREALIS_NAMESPACE_END;
00265
00266
#endif