2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-07-13 23:12:36 +02:00
|
|
|
#ifndef OTMLNODE_H
|
|
|
|
#define OTMLNODE_H
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include "declarations.h"
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2012-07-29 05:34:40 +02:00
|
|
|
class OTMLNode : public stdext::shared_object
|
2011-07-13 23:12:36 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-08-14 04:09:11 +02:00
|
|
|
virtual ~OTMLNode() { }
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-12-01 20:38:46 +01:00
|
|
|
static OTMLNodePtr create(std::string tag = "", bool unique = false);
|
2011-08-19 20:53:23 +02:00
|
|
|
static OTMLNodePtr create(std::string tag, std::string value);
|
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
std::string tag() { return m_tag; }
|
|
|
|
int size() { return m_children.size(); }
|
|
|
|
std::string source() { return m_source; }
|
2012-07-23 06:33:08 +02:00
|
|
|
std::string rawValue() { return m_value; }
|
2011-08-19 20:53:23 +02:00
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
bool isUnique() { return m_unique; }
|
|
|
|
bool isNull() { return m_null; }
|
2011-08-19 20:53:23 +02:00
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
bool hasTag() { return !m_tag.empty(); }
|
|
|
|
bool hasValue() { return !m_value.empty(); }
|
|
|
|
bool hasChildren();
|
2011-08-19 20:53:23 +02:00
|
|
|
bool hasChildAt(const std::string& childTag) { return !!get(childTag); }
|
|
|
|
bool hasChildAtIndex(int childIndex) { return !!getIndex(childIndex); }
|
|
|
|
|
|
|
|
void setTag(std::string tag) { m_tag = tag; }
|
|
|
|
void setValue(const std::string& value) { m_value = value; }
|
|
|
|
void setNull(bool null) { m_null = null; }
|
|
|
|
void setUnique(bool unique) { m_unique = unique; }
|
|
|
|
void setSource(const std::string& source) { m_source = source; }
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
OTMLNodePtr get(const std::string& childTag);
|
|
|
|
OTMLNodePtr getIndex(int childIndex);
|
2011-08-19 20:53:23 +02:00
|
|
|
|
|
|
|
OTMLNodePtr at(const std::string& childTag);
|
|
|
|
OTMLNodePtr atIndex(int childIndex);
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void addChild(const OTMLNodePtr& newChild);
|
|
|
|
bool removeChild(const OTMLNodePtr& oldChild);
|
|
|
|
bool replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newChild);
|
2012-01-07 18:36:58 +01:00
|
|
|
void copy(const OTMLNodePtr& node);
|
2011-08-14 04:09:11 +02:00
|
|
|
void merge(const OTMLNodePtr& node);
|
2011-08-19 20:53:23 +02:00
|
|
|
void clear();
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
OTMLNodeList children();
|
|
|
|
OTMLNodePtr clone();
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
template<typename T = std::string>
|
|
|
|
T value();
|
|
|
|
template<typename T = std::string>
|
|
|
|
T valueAt(const std::string& childTag);
|
|
|
|
template<typename T = std::string>
|
|
|
|
T valueAtIndex(int childIndex);
|
|
|
|
template<typename T = std::string>
|
|
|
|
T valueAt(const std::string& childTag, const T& def);
|
|
|
|
template<typename T = std::string>
|
|
|
|
T valueAtIndex(int childIndex, const T& def);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void write(const T& v);
|
|
|
|
template<typename T>
|
|
|
|
void writeAt(const std::string& childTag, const T& v);
|
|
|
|
template<typename T>
|
|
|
|
void writeIn(const T& v);
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
virtual std::string emit();
|
|
|
|
|
2012-08-01 09:49:09 +02:00
|
|
|
OTMLNodePtr asOTMLNode() { return static_self_cast<OTMLNode>(); }
|
2012-07-29 05:34:40 +02:00
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
protected:
|
|
|
|
OTMLNode() : m_unique(false), m_null(false) { }
|
|
|
|
|
|
|
|
OTMLNodeList m_children;
|
2011-07-13 23:12:36 +02:00
|
|
|
std::string m_tag;
|
|
|
|
std::string m_value;
|
2011-08-14 04:09:11 +02:00
|
|
|
std::string m_source;
|
|
|
|
bool m_unique;
|
2011-08-19 20:53:23 +02:00
|
|
|
bool m_null;
|
2011-07-13 23:12:36 +02:00
|
|
|
};
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
#include "otmlexception.h"
|
|
|
|
|
2012-07-23 06:33:08 +02:00
|
|
|
template<>
|
|
|
|
inline std::string OTMLNode::value<std::string>() {
|
|
|
|
std::string value = m_value;
|
2012-08-01 09:49:09 +02:00
|
|
|
if(stdext::starts_with(value, "\"") && stdext::ends_with(value, "\"")) {
|
2012-07-23 06:33:08 +02:00
|
|
|
value = value.substr(1, value.length()-2);
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::replace_all(value, "\\\\", "\\");
|
|
|
|
stdext::replace_all(value, "\\\"", "\"");
|
|
|
|
stdext::replace_all(value, "\\t", "\t");
|
|
|
|
stdext::replace_all(value, "\\n", "\n");
|
|
|
|
stdext::replace_all(value, "\\'", "\'");
|
2012-07-23 06:33:08 +02:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<typename T>
|
2011-08-19 20:53:23 +02:00
|
|
|
T OTMLNode::value() {
|
|
|
|
T ret;
|
2012-05-28 15:06:26 +02:00
|
|
|
if(!stdext::cast(m_value, ret))
|
2012-07-29 05:34:40 +02:00
|
|
|
throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
|
2011-08-19 20:53:23 +02:00
|
|
|
return ret;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<typename T>
|
2011-08-19 20:53:23 +02:00
|
|
|
T OTMLNode::valueAt(const std::string& childTag) {
|
|
|
|
OTMLNodePtr node = at(childTag);
|
|
|
|
return node->value<T>();
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
template<typename T>
|
|
|
|
T OTMLNode::valueAtIndex(int childIndex) {
|
|
|
|
OTMLNodePtr node = atIndex(childIndex);
|
|
|
|
return node->value<T>();
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
template<typename T>
|
|
|
|
T OTMLNode::valueAt(const std::string& childTag, const T& def) {
|
|
|
|
if(OTMLNodePtr node = get(childTag))
|
|
|
|
if(!node->isNull())
|
|
|
|
return node->value<T>();
|
|
|
|
return def;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 20:53:23 +02:00
|
|
|
template<typename T>
|
|
|
|
T OTMLNode::valueAtIndex(int childIndex, const T& def) {
|
|
|
|
if(OTMLNodePtr node = getIndex(childIndex))
|
|
|
|
return node->value<T>();
|
|
|
|
return def;
|
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void OTMLNode::write(const T& v) {
|
2012-05-28 15:06:26 +02:00
|
|
|
m_value = stdext::safe_cast<std::string>(v);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void OTMLNode::writeAt(const std::string& childTag, const T& v) {
|
2011-08-19 20:53:23 +02:00
|
|
|
OTMLNodePtr child = OTMLNode::create(childTag);
|
2012-02-02 01:10:55 +01:00
|
|
|
child->setUnique(true);
|
2011-08-14 04:09:11 +02:00
|
|
|
child->write<T>(v);
|
2011-08-19 20:53:23 +02:00
|
|
|
addChild(child);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void OTMLNode::writeIn(const T& v) {
|
2011-08-19 20:53:23 +02:00
|
|
|
OTMLNodePtr child = OTMLNode::create();
|
2011-08-14 04:09:11 +02:00
|
|
|
child->write<T>(v);
|
|
|
|
addChild(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-08-19 20:53:23 +02:00
|
|
|
|