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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : xmlutils.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it 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 "xmlutils.h"
// wxXmlNode compatibilty macros
#include "wx_xml_compatibility.h"
wxXmlNode* XmlUtils::FindNodeByName(const wxXmlNode* parent, const wxString& tagName, const wxString& name)
{
if(!parent) {
return NULL;
}
wxXmlNode* child = parent->GetChildren();
while(child) {
if(child->GetName() == tagName) {
if((child->GetAttribute(wxT("Name"), wxEmptyString) == name) ||
(child->GetAttribute(wxT("name"), wxEmptyString) == name)) {
return child;
}
}
child = child->GetNext();
}
return NULL;
}
wxXmlNode* XmlUtils::FindFirstByTagName(const wxXmlNode* parent, const wxString& tagName)
{
if(!parent) {
return NULL;
}
wxXmlNode* child = parent->GetChildren();
while(child) {
if(child->GetName() == tagName) {
return child;
}
child = child->GetNext();
}
return NULL;
}
wxXmlNode* XmlUtils::FindLastByTagName(const wxXmlNode* parent, const wxString& tagName)
{
wxXmlNode* last_node = NULL;
wxXmlNode* child = parent->GetChildren();
while(child) {
if(child->GetName() == tagName) {
last_node = child;
}
child = child->GetNext();
}
return last_node;
}
void XmlUtils::UpdateProperty(wxXmlNode* node, const wxString& name, const wxString& value)
{
auto prop = node->GetAttributes();
while(prop) {
if(prop->GetName() == name) {
prop->SetValue(value);
return;
}
prop = prop->GetNext();
}
// No such property, create new one and add it
node->AddAttribute(name, value);
}
wxString XmlUtils::ReadString(const wxXmlNode* node, const wxString& propName, const wxString& defaultValue)
{
return node->GetAttribute(propName, defaultValue);
}
bool XmlUtils::ReadStringIfExists(const wxXmlNode* node, const wxString& propName, wxString& value)
{
return node->GetAttribute(propName, &value);
}
long XmlUtils::ReadLong(const wxXmlNode* node, const wxString& propName, long defaultValue)
{
wxString val = node->GetAttribute(propName, wxEmptyString);
if(val.IsEmpty()) {
return defaultValue;
}
if(val.StartsWith(wxT("\""))) {
val = val.AfterFirst(wxT('"'));
}
if(val.EndsWith(wxT("\""))) {
val = val.BeforeLast(wxT('"'));
}
long retVal = defaultValue;
val.ToLong(&retVal);
return retVal;
}
bool XmlUtils::ReadLongIfExists(const wxXmlNode* node, const wxString& propName, long& answer)
{
wxString value;
if(!node->GetAttribute(propName, &value)) {
return false;
}
if(value.StartsWith(wxT("\""))) {
value = value.AfterFirst(wxT('"'));
}
if(value.EndsWith(wxT("\""))) {
value = value.BeforeLast(wxT('"'));
}
bool retVal = value.ToLong(&answer);
return retVal;
}
bool XmlUtils::ReadBool(const wxXmlNode* node, const wxString& propName, bool defaultValue)
{
wxString val = node->GetAttribute(propName, wxEmptyString);
if(val.IsEmpty()) {
return defaultValue;
}
bool retVal = defaultValue;
if(val.CmpNoCase(wxT("yes")) == 0) {
retVal = true;
} else {
retVal = false;
}
return retVal;
}
bool XmlUtils::ReadBoolIfExists(const wxXmlNode* node, const wxString& propName, bool& answer)
{
wxString value;
if(!node->GetAttribute(propName, &value)) {
return false;
}
if(value.CmpNoCase(wxT("yes")) == 0) {
answer = true;
} else {
answer = false;
}
return true;
}
wxArrayString XmlUtils::ChildNodesContentToArray(const wxXmlNode* node, const wxString& tagName /* = wxT("")*/)
{
wxArrayString arr;
if(!node) {
return arr;
}
wxXmlNode* child = node->GetChildren();
while(child) {
if(tagName.empty() || child->GetName() == tagName) {
arr.Add(child->GetNodeContent());
}
child = child->GetNext();
}
return arr;
}
wxString XmlUtils::ChildNodesContentToString(const wxXmlNode* node, const wxString& tagName /* = wxT("")*/,
const wxString& separator /* = wxT(";")*/)
{
wxString str;
if(!node) {
return str;
}
wxXmlNode* child = node->GetChildren();
while(child) {
if(tagName.empty() || child->GetName() == tagName) {
str << child->GetNodeContent() << separator;
}
child = child->GetNext();
}
if(!str.empty()) {
str.RemoveLast(separator.Len());
}
return str;
}
void XmlUtils::SetNodeContent(wxXmlNode* node, const wxString& text)
{
wxXmlNode* n = node->GetChildren();
wxXmlNode* contentNode = NULL;
while(n) {
if(n->GetType() == wxXML_TEXT_NODE || n->GetType() == wxXML_CDATA_SECTION_NODE) {
contentNode = n;
break;
}
n = n->GetNext();
}
if(contentNode) {
// remove old node
node->RemoveChild(contentNode);
delete contentNode;
}
if(!text.IsEmpty()) {
contentNode = new wxXmlNode(wxXML_TEXT_NODE, wxEmptyString, text);
node->AddChild(contentNode);
}
}
void XmlUtils::RemoveChildren(wxXmlNode* node)
{
wxXmlNode* child = node->GetChildren();
while(child) {
wxXmlNode* nextChild = child->GetNext();
node->RemoveChild(child);
delete child;
child = nextChild;
}
}
void XmlUtils::SetCDATANodeContent(wxXmlNode* node, const wxString& text)
{
wxXmlNode* n = node->GetChildren();
wxXmlNode* contentNode = NULL;
while(n) {
if(n->GetType() == wxXML_TEXT_NODE || n->GetType() == wxXML_CDATA_SECTION_NODE) {
contentNode = n;
break;
}
n = n->GetNext();
}
if(contentNode) {
// remove old node
node->RemoveChild(contentNode);
delete contentNode;
}
if(!text.IsEmpty()) {
contentNode = new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxEmptyString, text);
node->AddChild(contentNode);
}
}
bool XmlUtils::StaticReadObject(wxXmlNode* root, const wxString& name, SerializedObject* obj)
{
// find the object node in the xml file
wxXmlNode* node = XmlUtils::FindNodeByName(root, wxT("ArchiveObject"), name);
if(node) {
// Check to see if we need a version check
wxString objectVersion = obj->GetVersion();
if(objectVersion.IsEmpty() == false) {
if(node->GetAttribute(wxT("Version"), wxT("")) != objectVersion) {
return false;
}
}
Archive arch;
arch.SetXmlNode(node);
obj->DeSerialize(arch);
return true;
}
return false;
}
bool XmlUtils::StaticWriteObject(wxXmlNode* root, const wxString& name, SerializedObject* obj)
{
if(!root)
return false;
Archive arch;
wxXmlNode* child = XmlUtils::FindNodeByName(root, wxT("ArchiveObject"), name);
if(child) {
wxXmlNode* n = root;
n->RemoveChild(child);
delete child;
}
// create new xml node for this object
child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("ArchiveObject"));
root->AddChild(child);
wxString objectVersion = obj->GetVersion();
if(objectVersion.IsEmpty() == false)
child->AddAttribute(wxT("Version"), objectVersion);
child->AddAttribute(wxT("Name"), name);
arch.SetXmlNode(child);
// serialize the object into the archive
obj->Serialize(arch);
return true;
}
|