diff --git a/exml/EXmlAttribute.cpp b/exml/EXmlAttribute.cpp index a3211d8..a306dc5 100644 --- a/exml/EXmlAttribute.cpp +++ b/exml/EXmlAttribute.cpp @@ -9,6 +9,14 @@ #include #include +exml::EXmlAttribute::EXmlAttribute(const etk::UString& _name, const etk::UString& _value) : + exml::EXmlNode(_value), + m_name(_name) +{ + +} + + bool exml::EXmlAttribute::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos) { EXML_DEBUG("start parse : 'attribute'"); diff --git a/exml/EXmlAttribute.h b/exml/EXmlAttribute.h index c6ca688..3446f64 100644 --- a/exml/EXmlAttribute.h +++ b/exml/EXmlAttribute.h @@ -18,10 +18,16 @@ namespace exml { public: EXmlAttribute(void) { }; + EXmlAttribute(const etk::UString& _name, const etk::UString& _value); virtual ~EXmlAttribute(void) { }; virtual nodeType_te GetType(void) { return exml::typeAttribute; }; virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos); virtual bool Generate(etk::UString& _data, int32_t _indent); + protected: + etk::UString m_name; + public: + virtual void SetName(etk::UString _name) { m_name = _name; }; + virtual const etk::UString& GetName(void) { return m_name; }; }; }; diff --git a/exml/EXmlElement.cpp b/exml/EXmlElement.cpp index cf8991d..e7112cf 100644 --- a/exml/EXmlElement.cpp +++ b/exml/EXmlElement.cpp @@ -37,6 +37,21 @@ void exml::EXmlElement::AppendSub(exml::EXmlNode* _node) m_listSub.PushBack(_node); } +exml::EXmlNode* exml::EXmlElement::GetNode(const etk::UString& _name) +{ + if (_name.Size()==0) { + return NULL; + } + for (int32_t iii=0; iiiGetType() == exml::typeElement + && m_listSub[iii]->GetValue() == _name) { + return m_listSub[iii]; + } + } + return NULL; +} + exml::EXmlAttribute* exml::EXmlElement::GetAttribute(int32_t _id) { if (_id <0 || _id>m_listAttribute.Size()) { @@ -59,6 +74,20 @@ void exml::EXmlElement::AppendAttribute(exml::EXmlAttribute* _node) m_listAttribute.PushBack(_node); } +const etk::UString& exml::EXmlElement::GetAttribute(const etk::UString& _name) +{ + static const etk::Ustring errorReturn(""); + if (_name.Size()==0) { + return errorReturn; + } + for (int32_t iii=0; iiiGetName() == _name) { + return m_listAttribute[iii]->GetValue(); + } + } + return errorReturn; +} bool exml::EXmlElement::Generate(etk::UString& _data, int32_t _indent) { @@ -210,7 +239,7 @@ bool exml::EXmlElement::SubParse(const etk::UString& _data, int32_t& _pos, bool } } etk::UString tmpname = _data.Extract(iii+white+2, endPosName+1); - if( tmpname == m_name) { + if( tmpname == m_value) { // find end of node : // find > element ... for (int32_t jjj=endPosName+1; jjj<_data.Size(); jjj++) { @@ -256,7 +285,7 @@ bool exml::EXmlElement::SubParse(const etk::UString& _data, int32_t& _pos, bool EXML_ERROR(_filePos << " Allocation error ..."); return false; } - element->SetName(tmpname); + element->SetValue(tmpname); _pos = endPosName+1; _filePos += ivec2(endPosName,0); if (false==element->Parse(_data, _pos, _caseSensitive, _filePos)) { @@ -269,8 +298,6 @@ bool exml::EXmlElement::SubParse(const etk::UString& _data, int32_t& _pos, bool continue; } - - } else { if (_data[iii] == '>') { EXML_ERROR(_filePos << " find elemement '>' ==> no reason to be here ..."); diff --git a/exml/EXmlElement.h b/exml/EXmlElement.h index aaf571c..6374c27 100644 --- a/exml/EXmlElement.h +++ b/exml/EXmlElement.h @@ -19,6 +19,7 @@ namespace exml { public: EXmlElement(void) { }; + EXmlElement(const etk::UString& _value) : exml::EXmlNode(_value) { }; virtual ~EXmlElement(void) { }; virtual nodeType_te GetType(void) { return typeElement; }; protected: @@ -38,6 +39,8 @@ namespace exml virtual bool Generate(etk::UString& _data, int32_t _indent); protected: bool SubParse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos, bool _mainNode=false); + + const etk::UString& GetAttribute(const etk::UString& _name); }; }; diff --git a/exml/EXmlNode.cpp b/exml/EXmlNode.cpp index c9157fe..ab3e0f2 100644 --- a/exml/EXmlNode.cpp +++ b/exml/EXmlNode.cpp @@ -9,6 +9,13 @@ #include #include + +exml::EXmlNode::EXmlNode(const etk::UString& _value) : + m_value(_value) +{ + +} + void exml::EXmlNode::AddIndent(etk::UString& _data, int32_t _indent) { for (int32_t iii=0; iii<_indent; iii++) { @@ -91,3 +98,63 @@ int32_t exml::EXmlNode::CountWhiteChar(const etk::UString& _data, int32_t _pos) } return white; } + + + bool IsDocument(void) { return GetType()==exml::typeDocument; }; + bool IsAttribute(void) { return GetType()==exml::typeAttribute; }; + bool IsComment(void) { return GetType()==exml::typeComment; }; + bool IsDeclaration(void) { return GetType()==exml::typeDeclaration; }; + bool IsElement(void) { return GetType()==exml::typeElement; }; + bool IsText(void) { return GetType()==exml::typeText; }; + + +exml::EXmlDocument* exml::EXmlNode::ToDocument(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + +exml::EXmlAttribute* exml::EXmlNode::ToAttribute(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + +exml::EXmlComment* exml::EXmlNode::ToComment(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + +exml::EXmlDeclaration* exml::EXmlNode::ToDeclaration(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + +exml::EXmlElement* exml::EXmlNode::ToElement(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + +exml::EXmlText* exml::EXmlNode::ToText(void) +{ + if (GetType()==exml::typeDocument) { + return static_cast(*this); + } + return NULL; +} + + + diff --git a/exml/EXmlNode.h b/exml/EXmlNode.h index 7caae40..b3f318a 100644 --- a/exml/EXmlNode.h +++ b/exml/EXmlNode.h @@ -15,6 +15,13 @@ namespace exml { + class EXmlDocument; + class EXmlAttribute; + class EXmlComment; + class EXmlDeclaration; + class EXmlElement; + class EXmlText; + typedef enum { typeNode, //!< might be an error ... typeDocument, //!< all the file main access @@ -29,6 +36,7 @@ namespace exml { public: EXmlNode(void) { }; + EXmlNode(const etk::UString& _value); virtual ~EXmlNode(void) { }; protected: void AddIndent(etk::UString& _data, int32_t _indent); @@ -38,11 +46,6 @@ namespace exml */ virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos) = 0; virtual bool Generate(etk::UString& _data, int32_t _indent) { return true; }; - protected: - etk::UString m_name; - public: - virtual void SetName(etk::UString _name) { m_name = _name; }; - virtual const etk::UString& GetName(void) { return m_name; }; protected: etk::UString m_value; public: @@ -54,6 +57,20 @@ namespace exml void DrawElementParsed(const etk::UniChar& _val, const ivec2& _firstChar); bool CheckAvaillable(const etk::UniChar& _val, bool _firstChar); int32_t CountWhiteChar(const etk::UString& _data, int32_t _pos); + public: + exml::EXmlDocument* ToDocument(void); + exml::EXmlAttribute* ToAttribute(void); + exml::EXmlComment* ToComment(void); + exml::EXmlDeclaration* ToDeclaration(void); + exml::EXmlElement* ToElement(void); + exml::EXmlText* ToText(void); + + bool IsDocument(void) { return GetType()==exml::typeDocument; }; + bool IsAttribute(void) { return GetType()==exml::typeAttribute; }; + bool IsComment(void) { return GetType()==exml::typeComment; }; + bool IsDeclaration(void) { return GetType()==exml::typeDeclaration; }; + bool IsElement(void) { return GetType()==exml::typeElement; }; + bool IsText(void) { return GetType()==exml::typeText; }; }; }; diff --git a/exml/exml.h b/exml/exml.h new file mode 100644 index 0000000..25fbc86 --- /dev/null +++ b/exml/exml.h @@ -0,0 +1,15 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __ETK_XML_H__ +#define __ETK_XML_H__ + +#include + +#endif +