[DEV] error for the casting of element ==> request element type directly in API

This commit is contained in:
Edouard DUPIN 2013-06-26 22:24:46 +02:00
parent ce8bef3599
commit 628d28e7e1
15 changed files with 160 additions and 84 deletions

View File

@ -9,6 +9,9 @@
#include <exml/Attribute.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "Attribute"
exml::Attribute::Attribute(const etk::UString& _name, const etk::UString& _value) :
exml::Node(_value),
m_name(_name)
@ -19,7 +22,8 @@ exml::Attribute::Attribute(const etk::UString& _name, const etk::UString& _value
bool exml::Attribute::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'attribute'");
EXML_VERBOSE("start parse : 'attribute'");
m_pos = _filePos;
// search end of the comment :
int32_t lastElementName = _pos;
for (int32_t iii=_pos; iii<_data.Size(); iii++) {
@ -97,7 +101,7 @@ bool exml::Attribute::Parse(const etk::UString& _data, int32_t& _pos, bool _case
}
m_value = _data.Extract(lastElementName+3, lastAttributePos+1);
EXML_DEBUG("attribute : " << m_name << "=\"" << m_value << "\"");
EXML_VERBOSE("attribute : " << m_name << "=\"" << m_value << "\"");
_pos = lastAttributePos;
return true;

View File

@ -28,8 +28,8 @@ namespace exml
public:
virtual void SetName(etk::UString _name) { m_name = _name; };
virtual const etk::UString& GetName(void) const { return m_name; };
virtual operator exml::Attribute* () { return this; };
virtual operator const exml::Attribute* () const { return this; };
virtual exml::Attribute* ToAttribute(void) { return this; };
virtual const exml::Attribute* ToAttribute(void) const { return this; };
};
};

View File

@ -9,9 +9,13 @@
#include <exml/Comment.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "Comment"
bool exml::Comment::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'comment'");
EXML_VERBOSE("start parse : 'comment'");
m_pos = _filePos;
// search end of the comment :
for (int32_t iii=_pos; iii+2<_data.Size(); iii++) {
_filePos += ivec2(1,0);
@ -27,7 +31,7 @@ bool exml::Comment::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSe
&& _data[iii+2] == '>') {
// find end of value:
m_value = _data.Extract(_pos, iii-1);
EXML_DEBUG(" find comment '" << m_value << "'");
EXML_VERBOSE(" find comment '" << m_value << "'");
_pos = iii+2;
return true;
}

View File

@ -22,8 +22,8 @@ namespace exml
virtual nodeType_te GetType(void) const { return typeAttribute; };
virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos);
virtual bool Generate(etk::UString& _data, int32_t _indent) const;
virtual operator exml::Comment* () { return this; };
virtual operator const exml::Comment* () const { return this; };
virtual exml::Comment* ToComment(void) { return this; };
virtual const exml::Comment* ToComment(void) const { return this; };
};
};

View File

@ -9,6 +9,9 @@
#include <exml/Declaration.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "Declaration"
bool exml::Declaration::Generate(etk::UString& _data, int32_t _indent) const
{
AddIndent(_data, _indent);
@ -26,7 +29,8 @@ bool exml::Declaration::Generate(etk::UString& _data, int32_t _indent) const
bool exml::Declaration::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'declaration'");
EXML_VERBOSE("start parse : 'declaration'");
m_pos = _filePos;
// search end of the comment :
for (int32_t iii=_pos; iii+1<_data.Size(); iii++) {
_filePos += ivec2(1,0);

View File

@ -23,8 +23,8 @@ namespace exml
virtual nodeType_te GetType(void) const { return typeAttribute; };
virtual bool Generate(etk::UString& _data, int32_t _indent) const;
virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos);
virtual operator exml::Declaration* () { return this; };
virtual operator const exml::Declaration* () const { return this; };
virtual exml::Declaration* ToDeclaration(void) { return this; };
virtual const exml::Declaration* ToDeclaration(void) const { return this; };
};
};

View File

@ -10,6 +10,8 @@
#include <exml/debug.h>
#include <etk/os/FSNode.h>
#undef __class__
#define __class__ "Document"
exml::Document::Document(void) :
m_charset(unicode::EDN_CHARSET_UTF8),
@ -32,7 +34,8 @@ bool exml::Document::Generate(etk::UString& _data, int32_t _indent) const
bool exml::Document::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'document'");
EXML_VERBOSE("start parse : 'document'");
m_pos = _filePos;
// in this case : no main node ...
SubParse(_data, _pos, _caseSensitive, _filePos, true);
return true;
@ -41,7 +44,7 @@ bool exml::Document::Parse(const etk::UString& _data, int32_t& _pos, bool _caseS
bool exml::Document::Parse(const etk::UString& _data)
{
EXML_DEBUG("Start parsing document (type: string) size=" << _data.Size());
EXML_VERBOSE("Start parsing document (type: string) size=" << _data.Size());
// came from char ==> force in utf8 ...
m_charset = unicode::EDN_CHARSET_UTF8;
ivec2 filePos(0,1);
@ -57,7 +60,7 @@ bool exml::Document::Generate(etk::UString& _data)
bool exml::Document::Load(const etk::UString& _file)
{
// Start loading the XML :
EXML_DEBUG("open file (xml) \"" << _file << "\"");
EXML_VERBOSE("open file (xml) \"" << _file << "\"");
etk::FSNode tmpFile(_file);
if (false == tmpFile.Exist()) {
EXML_ERROR("File Does not exist : " << _file);
@ -91,7 +94,7 @@ bool exml::Document::Load(const etk::UString& _file)
delete(fileBuffer);
// parse the data :
bool ret = Parse(tmpDataUnicode);
Display();
//Display();
if (0==Size()) {
EXML_CRITICAL("lkjlkj");
}

View File

@ -64,8 +64,8 @@ namespace exml
void Display(void);
bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos);
bool Generate(etk::UString& _data, int32_t _indent) const;
virtual operator exml::Document* () { return this; };
virtual operator const exml::Document* () const { return this; };
virtual exml::Document* ToDocument(void) { return this; };
virtual const exml::Document* ToDocument(void) const { return this; };
};
};

View File

@ -13,8 +13,35 @@
#include <exml/Attribute.h>
#include <exml/Declaration.h>
#undef __class__
#define __class__ "Element"
exml::Node* exml::Element::Get(int32_t _id)
exml::nodeType_te exml::Element::GetType(int32_t _id)
{
exml::Node* tmpp = GetNode(_id);
if (NULL==tmpp) {
return exml::typeUnknow;
}
return tmpp->GetType();
}
const exml::nodeType_te exml::Element::GetType(int32_t _id) const
{
const exml::Node* tmpp = GetNode(_id);
if (NULL==tmpp) {
return exml::typeUnknow;
}
return tmpp->GetType();
}
exml::Node* exml::Element::GetNode(int32_t _id)
{
if (_id <0 || _id>m_listSub.Size()) {
return NULL;
}
return m_listSub[_id];
}
const exml::Node* exml::Element::GetNode(int32_t _id) const
{
if (_id <0 || _id>m_listSub.Size()) {
return NULL;
@ -22,12 +49,58 @@ exml::Node* exml::Element::Get(int32_t _id)
return m_listSub[_id];
}
const exml::Node* exml::Element::Get(int32_t _id) const
exml::Element* exml::Element::GetElement(int32_t _id)
{
if (_id <0 || _id>m_listSub.Size()) {
exml::Node* tmpp = GetNode(_id);
if (NULL==tmpp) {
return NULL;
}
return m_listSub[_id];
return tmpp->ToElement();
}
const exml::Element* exml::Element::GetElement(int32_t _id) const
{
const exml::Node* tmpp = GetNode(_id);
if (NULL==tmpp) {
return NULL;
}
return tmpp->ToElement();
}
exml::Element* exml::Element::GetNamed(const etk::UString& _name)
{
if (_name.Size()==0) {
return NULL;
}
for (int32_t iii=0; iii<m_listSub.Size(); iii++) {
if( NULL != m_listSub[iii]
&& m_listSub[iii]->GetType() == exml::typeElement
&& m_listSub[iii]->GetValue() == _name) {
if (NULL==m_listSub[iii]) {
return NULL;
}
return m_listSub[iii]->ToElement();
}
}
return NULL;
}
const exml::Element* exml::Element::GetNamed(const etk::UString& _name) const
{
if (_name.Size()==0) {
return NULL;
}
for (int32_t iii=0; iii<m_listSub.Size(); iii++) {
if( NULL != m_listSub[iii]
&& m_listSub[iii]->GetType() == exml::typeElement
&& m_listSub[iii]->GetValue() == _name) {
if (NULL==m_listSub[iii]) {
return NULL;
}
return m_listSub[iii]->ToElement();
}
}
return NULL;
}
void exml::Element::Append(exml::Node* _node)
@ -45,35 +118,6 @@ void exml::Element::Append(exml::Node* _node)
m_listSub.PushBack(_node);
}
exml::Node* exml::Element::GetNamed(const etk::UString& _name)
{
if (_name.Size()==0) {
return NULL;
}
for (int32_t iii=0; iii<m_listSub.Size(); iii++) {
if( NULL != m_listSub[iii]
&& m_listSub[iii]->GetType() == exml::typeElement
&& m_listSub[iii]->GetValue() == _name) {
return m_listSub[iii];
}
}
return NULL;
}
const exml::Node* exml::Element::GetNamed(const etk::UString& _name) const
{
if (_name.Size()==0) {
return NULL;
}
for (int32_t iii=0; iii<m_listSub.Size(); iii++) {
if( NULL != m_listSub[iii]
&& m_listSub[iii]->GetType() == exml::typeElement
&& m_listSub[iii]->GetValue() == _name) {
return m_listSub[iii];
}
}
return NULL;
}
exml::Attribute* exml::Element::GetAttr(int32_t _id)
{
if (_id <0 || _id>m_listAttribute.Size()) {
@ -191,7 +235,8 @@ bool exml::Element::Generate(etk::UString& _data, int32_t _indent) const
bool exml::Element::SubParse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos, bool _mainNode)
{
EXML_DEBUG(" start subParse ... " << _pos << " " << _filePos);
EXML_VERBOSE(" start subParse ... " << _pos << " " << _filePos);
m_pos = _filePos;
for (int32_t iii=_pos; iii<_data.Size(); iii++) {
_filePos += ivec2(1,0);
#ifdef ENABLE_DISPLAY_PARSED_ELEMENT
@ -319,6 +364,11 @@ bool exml::Element::SubParse(const etk::UString& _data, int32_t& _pos, bool _cas
if(_data[jjj] == '>') {
_pos = jjj;
return true;
} else if( _data[jjj] != '\r'
&& _data[jjj] != ' '
&& _data[jjj] != '\t') {
EXML_ERROR(_filePos << " ==> end node error : have data inside end node other than [ \\n\\t\\r] " << m_value << "'");
return false;
}
}
} else {
@ -401,7 +451,7 @@ bool exml::Element::SubParse(const etk::UString& _data, int32_t& _pos, bool _cas
bool exml::Element::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'element' named='" << m_value << "'");
EXML_VERBOSE("start parse : 'element' named='" << m_value << "'");
// note : When start parsing the upper element must have set the value of the element and set the position after this one
// find a normal node ...
@ -454,9 +504,3 @@ bool exml::Element::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSe
return false;
}

View File

@ -27,10 +27,15 @@ namespace exml
public:
int32_t Size(void) const { return m_listSub.Size(); };
void Append(Node* _node);
Node* Get(int32_t _id);
const Node* Get(int32_t _id) const;
Node* GetNamed(const etk::UString& _name);
const Node* GetNamed(const etk::UString& _name) const;
nodeType_te GetType(int32_t _id);
const nodeType_te GetType(int32_t _id) const;
Node* GetNode(int32_t _id);
const Node* GetNode(int32_t _id) const;
Element* GetElement(int32_t _id);
const Element* GetElement(int32_t _id) const;
Element* GetNamed(const etk::UString& _name);
const Element* GetNamed(const etk::UString& _name) const;
protected:
etk::Vector<exml::Attribute*> m_listAttribute;
public:
@ -47,8 +52,8 @@ namespace exml
virtual bool Generate(etk::UString& _data, int32_t _indent) const;
protected:
bool SubParse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos, bool _mainNode=false);
virtual operator exml::Element* () { return this; };
virtual operator const exml::Element* () const { return this; };
virtual exml::Element* ToElement(void) { return this; };
virtual const exml::Element* ToElement(void) const { return this; };
};
};

View File

@ -9,6 +9,8 @@
#include <exml/Node.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "Node"
exml::Node::Node(const etk::UString& _value) :
m_pos(0,0),

View File

@ -24,6 +24,7 @@ namespace exml
class Text;
typedef enum {
typeUnknow, //!< might be an error ...
typeNode, //!< might be an error ...
typeDocument, //!< all the file main access
typeDeclaration, //!< <?xml ... ?>
@ -47,7 +48,7 @@ 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) const { return true; };
private:
protected:
ivec2 m_pos; // position in the readed file
public:
const ivec2& Pos(void) { return m_pos; };
@ -63,18 +64,18 @@ namespace exml
bool CheckAvaillable(const etk::UniChar& _val, bool _firstChar) const;
int32_t CountWhiteChar(const etk::UString& _data, int32_t _pos) const;
public:
virtual operator exml::Document* () { return NULL; };
virtual operator const exml::Document* () const { return NULL; };
virtual operator exml::Attribute* () { return NULL; };
virtual operator const exml::Attribute* () const { return NULL; };
virtual operator exml::Comment* () { return NULL; };
virtual operator const exml::Comment* () const { return NULL; };
virtual operator exml::Declaration* () { return NULL; };
virtual operator const exml::Declaration* () const { return NULL; };
virtual operator exml::Element* () { return NULL; };
virtual operator const exml::Element* () const { return NULL; };
virtual operator exml::Text* () { return NULL; };
virtual operator const exml::Text* () const{ return NULL; };
virtual exml::Document* ToDocument(void) { return NULL; };
virtual const exml::Document* ToDocument(void) const { return NULL; };
virtual exml::Attribute* ToAttribute(void) { return NULL; };
virtual const exml::Attribute* ToAttribute(void) const { return NULL; };
virtual exml::Comment* ToComment(void) { return NULL; };
virtual const exml::Comment* ToComment(void) const { return NULL; };
virtual exml::Declaration* ToDeclaration(void) { return NULL; };
virtual const exml::Declaration* ToDeclaration(void) const { return NULL; };
virtual exml::Element* ToElement(void) { return NULL; };
virtual const exml::Element* ToElement(void) const { return NULL; };
virtual exml::Text* ToText(void) { return NULL; };
virtual const exml::Text* ToText(void) const{ return NULL; };
bool IsDocument(void) const { return GetType()==exml::typeDocument; };
bool IsAttribute(void) const { return GetType()==exml::typeAttribute; };

View File

@ -9,6 +9,9 @@
#include <exml/Text.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "Text"
bool exml::Text::Generate(etk::UString& _data, int32_t _indent) const
{
_data += m_value;
@ -28,7 +31,8 @@ int32_t exml::Text::CountLines(void) const
bool exml::Text::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'text'");
EXML_VERBOSE("start parse : 'text'");
m_pos = _filePos;
// search end of the comment :
for (int32_t iii=_pos; iii<_data.Size(); iii++) {
_filePos += ivec2(1,0);
@ -55,19 +59,21 @@ bool exml::Text::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensi
}
// find end of value:
m_value = _data.Extract(_pos, newEnd);
EXML_DEBUG(" find text '" << m_value << "'");
EXML_VERBOSE(" find text '" << m_value << "'");
_pos = iii-1;
return true;
}
}
_pos = _data.Size();
EXML_ERROR("Text got end of file without finding end node");
EXML_ERROR(" Data : " << _data);
return false;
}
bool exml::TextCDATA::Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos)
{
EXML_DEBUG("start parse : 'text::CDATA'");
EXML_VERBOSE("start parse : 'text::CDATA'");
m_pos = _filePos;
// search end of the comment :
for (int32_t iii=_pos; iii+2<_data.Size(); iii++) {
_filePos += ivec2(1,0);
@ -82,8 +88,8 @@ bool exml::TextCDATA::Parse(const etk::UString& _data, int32_t& _pos, bool _case
&& _data[iii+1] == ']'
&& _data[iii+2] == '>') {
// find end of value:
m_value = _data.Extract(_pos, iii-1);
EXML_DEBUG(" find text CDATA '" << m_value << "'");
m_value = _data.Extract(_pos, iii);
EXML_VERBOSE(" find text CDATA '" << m_value << "'");
_pos = iii+2;
return true;
}

View File

@ -23,8 +23,8 @@ namespace exml
virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos);
virtual bool Generate(etk::UString& _data, int32_t _indent) const;
int32_t CountLines(void) const;
virtual operator exml::Text* () { return this; };
virtual operator const exml::Text* () const { return this; };
virtual exml::Text* ToText(void) { return this; };
virtual const exml::Text* ToText(void) const{ return this; };
};
class TextCDATA : public Text
{

View File

@ -10,6 +10,9 @@
#include <etk/Debug.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "test"
int main(int argc, const char *argv[])
{
GeneralDebugSetLevel(etk::LOG_LEVEL_VERBOSE);