1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "xml_parser.h"
#include <fstream>
#include <stdexcept>
#include "conversion.h"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include "leak_dumper.h"
XERCES_CPP_NAMESPACE_USE
using namespace std;
namespace Shared{ namespace Xml{
using namespace Util;
// =====================================================
// class ErrorHandler
// =====================================================
class ErrorHandler: public DOMErrorHandler{
public:
virtual bool handleError (const DOMError &domError){
if(domError.getSeverity()== DOMError::DOM_SEVERITY_FATAL_ERROR){
char msgStr[strSize], fileStr[strSize];
XMLString::transcode(domError.getMessage(), msgStr, strSize-1);
XMLString::transcode(domError.getLocation()->getURI(), fileStr, strSize-1);
int lineNumber= domError.getLocation()->getLineNumber();
throw runtime_error("Error parsing XML, file: " + string(fileStr) + ", line: " + intToStr(lineNumber) + ": " + string(msgStr));
}
return true;
}
};
// =====================================================
// class XmlIo
// =====================================================
bool XmlIo::initialized= false;
XmlIo::XmlIo(){
try{
XMLPlatformUtils::Initialize();
}
catch(const XMLException&){
throw runtime_error("Error initializing XML system");
}
try{
XMLCh str[strSize];
XMLString::transcode("LS", str, strSize-1);
implementation = DOMImplementationRegistry::getDOMImplementation(str);
}
catch(const DOMException){
throw runtime_error("Exception while creating XML parser");
}
}
XmlIo &XmlIo::getInstance(){
static XmlIo XmlIo;
return XmlIo;
}
XmlIo::~XmlIo(){
XMLPlatformUtils::Terminate();
}
XmlNode *XmlIo::load(const string &path){
try{
ErrorHandler errorHandler;
DOMBuilder *parser= (static_cast<DOMImplementationLS*>(implementation))->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
parser->setErrorHandler(&errorHandler);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
parser->setFeature(XMLUni::fgDOMValidation, true);
DOMDocument *document= parser->parseURI(path.c_str());
if(document==NULL){
throw runtime_error("Can not parse URL: " + path);
}
XmlNode *rootNode= new XmlNode(document->getDocumentElement());
parser->release();
return rootNode;
}
catch(const DOMException &e){
throw runtime_error("Exception while loading: " + path + ": " + XMLString::transcode(e.msg));
}
}
void XmlIo::save(const string &path, const XmlNode *node){
try{
XMLCh str[strSize];
XMLString::transcode(node->getName().c_str(), str, strSize-1);
DOMDocument *document= implementation->createDocument(0, str, 0);
DOMElement *documentElement= document->getDocumentElement();
for(int i=0; i<node->getChildCount(); ++i){
documentElement->appendChild(node->getChild(i)->buildElement(document));
}
LocalFileFormatTarget file(path.c_str());
DOMWriter* writer = implementation->createDOMWriter();
writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
writer->writeNode(&file, *document);
document->release();
}
catch(const DOMException &e){
throw runtime_error("Exception while saving: " + path + ": " + XMLString::transcode(e.msg));
}
}
// =====================================================
// class XmlTree
// =====================================================
XmlTree::XmlTree(){
rootNode= NULL;
}
void XmlTree::init(const string &name){
this->rootNode= new XmlNode(name);
}
void XmlTree::load(const string &path){
this->rootNode= XmlIo::getInstance().load(path);
}
void XmlTree::save(const string &path){
XmlIo::getInstance().save(path, rootNode);
}
XmlTree::~XmlTree(){
delete rootNode;
}
// =====================================================
// class XmlNode
// =====================================================
XmlNode::XmlNode(DOMNode *node){
//get name
char str[strSize];
XMLString::transcode(node->getNodeName(), str, strSize-1);
name= str;
//check document
if(node->getNodeType()==DOMNode::DOCUMENT_NODE){
name="document";
}
//check children
for(int i=0; i<node->getChildNodes()->getLength(); ++i){
DOMNode *currentNode= node->getChildNodes()->item(i);
if(currentNode->getNodeType()==DOMNode::ELEMENT_NODE){
XmlNode *xmlNode= new XmlNode(currentNode);
children.push_back(xmlNode);
}
}
//check attributes
DOMNamedNodeMap *domAttributes= node->getAttributes();
if(domAttributes!=NULL){
for(int i=0; i<domAttributes->getLength(); ++i){
DOMNode *currentNode= domAttributes->item(i);
if(currentNode->getNodeType()==DOMNode::ATTRIBUTE_NODE){
XmlAttribute *xmlAttribute= new XmlAttribute(domAttributes->item(i));
attributes.push_back(xmlAttribute);
}
}
}
}
XmlNode::XmlNode(const string &name){
this->name= name;
}
XmlNode::~XmlNode(){
for(int i=0; i<children.size(); ++i){
delete children[i];
}
for(int i=0; i<attributes.size(); ++i){
delete attributes[i];
}
}
XmlAttribute *XmlNode::getAttribute(int i) const{
if(i>=attributes.size()){
throw runtime_error(getName()+" node doesn't have "+intToStr(i)+" attributes");
}
return attributes[i];
}
XmlAttribute *XmlNode::getAttribute(const string &name) const{
for(int i=0; i<attributes.size(); ++i){
if(attributes[i]->getName()==name){
return attributes[i];
}
}
throw runtime_error("\"" + getName() + "\" node doesn't have a attribute named \"" + name + "\"");
}
XmlNode *XmlNode::getChild(int i) const {
if(i>=children.size())
throw runtime_error("\"" + getName()+"\" node doesn't have "+intToStr(i+1)+" children");
return children[i];
}
XmlNode *XmlNode::getChild(const string &childName, int i) const{
if(i>=children.size()){
throw runtime_error("\"" + name + "\" node doesn't have "+intToStr(i+1)+" children named \"" + childName + "\"\n\nTree: "+getTreeString());
}
int count= 0;
for(int j=0; j<children.size(); ++j){
if(children[j]->getName()==childName){
if(count==i){
return children[j];
}
count++;
}
}
throw runtime_error("Node \""+getName()+"\" doesn't have "+intToStr(i+1)+" children named \""+childName+"\"\n\nTree: "+getTreeString());
}
XmlNode *XmlNode::addChild(const string &name){
XmlNode *node= new XmlNode(name);
children.push_back(node);
return node;
}
XmlAttribute *XmlNode::addAttribute(const string &name, const string &value){
XmlAttribute *attr= new XmlAttribute(name, value);
attributes.push_back(attr);
return attr;
}
DOMElement *XmlNode::buildElement(DOMDocument *document) const{
XMLCh str[strSize];
XMLString::transcode(name.c_str(), str, strSize-1);
DOMElement *node= document->createElement(str);
for(int i=0; i<attributes.size(); ++i){
XMLString::transcode(attributes[i]->getName().c_str(), str, strSize-1);
DOMAttr *attr= document->createAttribute(str);
XMLString::transcode(attributes[i]->getValue().c_str(), str, strSize-1);
attr->setValue(str);
node->setAttributeNode(attr);
}
for(int i=0; i<children.size(); ++i){
node->appendChild(children[i]->buildElement(document));
}
return node;
}
string XmlNode::getTreeString() const{
string str;
str+= getName();
if(!children.empty()){
str+= " (";
for(int i=0; i<children.size(); ++i){
str+= children[i]->getTreeString();
str+= " ";
}
str+=") ";
}
return str;
}
// =====================================================
// class XmlAttribute
// =====================================================
XmlAttribute::XmlAttribute(DOMNode *attribute){
char str[strSize];
XMLString::transcode(attribute->getNodeValue(), str, strSize-1);
value= str;
XMLString::transcode(attribute->getNodeName(), str, strSize-1);
name= str;
}
XmlAttribute::XmlAttribute(const string &name, const string &value){
this->name= name;
this->value= value;
}
bool XmlAttribute::getBoolValue() const{
if(value=="true"){
return true;
}
else if(value=="false"){
return false;
}
else{
throw runtime_error("Not a valid bool value (true or false): " +getName()+": "+ value);
}
}
int XmlAttribute::getIntValue() const{
return strToInt(value);
}
int XmlAttribute::getIntValue(int min, int max) const{
int i= strToInt(value);
if(i<min || i>max){
throw runtime_error("Xml Attribute int out of range: " + getName() + ": " + value);
}
return i;
}
float XmlAttribute::getFloatValue() const{
return strToFloat(value);
}
float XmlAttribute::getFloatValue(float min, float max) const{
float f= strToFloat(value);
if(f<min || f>max){
throw runtime_error("Xml attribute float out of range: " + getName() + ": " + value);
}
return f;
}
const string &XmlAttribute::getRestrictedValue() const
{
const string allowedCharacters = "abcdefghijklmnopqrstuvwxyz1234567890._-/";
for(int i= 0; i<value.size(); ++i){
if(allowedCharacters.find(value[i])==string::npos){
throw runtime_error(
string("The string \"" + value + "\" contains a character that is not allowed: \"") + value[i] +
"\"\nFor portability reasons the only allowed characters in this field are: " + allowedCharacters);
}
}
return value;
}
}}//end namespace
|