[DEV] add declaration attribute

This commit is contained in:
Edouard DUPIN 2013-06-27 21:12:33 +02:00
parent 628d28e7e1
commit 5fce0e9ef2
10 changed files with 184 additions and 94 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

87
exml/AttributeList.cpp Normal file
View File

@ -0,0 +1,87 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <exml/AttributeList.h>
#include <exml/debug.h>
#undef __class__
#define __class__ "AttributeList"
exml::Attribute* exml::AttributeList::GetAttr(int32_t _id)
{
if (_id <0 || _id>m_listAttribute.Size()) {
return NULL;
}
return m_listAttribute[_id];
}
const exml::Attribute* exml::AttributeList::GetAttr(int32_t _id) const
{
if (_id <0 || _id>m_listAttribute.Size()) {
return NULL;
}
return m_listAttribute[_id];
}
void exml::AttributeList::AppendAttribute(exml::Attribute* _node)
{
if (_node == NULL) {
EXML_ERROR("Try to set an empty node");
return;
}
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if (m_listAttribute[iii] == _node) {
EXML_ERROR("Try to add a node that is already added befor !!!");
return;
}
}
m_listAttribute.PushBack(_node);
}
const etk::UString& exml::AttributeList::GetAttribute(const etk::UString& _name) const
{
static const etk::UString errorReturn("");
if (_name.Size()==0) {
return errorReturn;
}
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->GetName() == _name) {
return m_listAttribute[iii]->GetValue();
}
}
return errorReturn;
}
void exml::AttributeList::SetAttribute(const etk::UString& _name, const etk::UString& _value)
{
// check if attribute already det :
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->GetName() == _name) {
// update the value :
m_listAttribute[iii]->SetValue(_value);
return;
}
}
exml::Attribute* attr = new exml::Attribute(_name, _value);
if (NULL==attr) {
EXML_ERROR("memory allocation error...");
}
m_listAttribute.PushBack(attr);
}
bool exml::AttributeList::Generate(etk::UString& _data, int32_t _indent) const
{
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if (NULL!=m_listAttribute[iii]) {
m_listAttribute[iii]->Generate(_data, _indent);
}
}
return true;
}

40
exml/AttributeList.h Normal file
View File

@ -0,0 +1,40 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __ETK_XML_ATTRIBUTE_LIST_H__
#define __ETK_XML_ATTRIBUTE_LIST_H__
#include <exml/Node.h>
#include <etk/Vector.h>
#include <exml/Attribute.h>
namespace exml
{
class AttributeList : public Node
{
public:
AttributeList(void) { };
virtual ~AttributeList(void) { };
protected:
etk::Vector<exml::Attribute*> m_listAttribute;
public:
int32_t SizeAttribute(void) const { return m_listAttribute.Size(); };
void AppendAttribute(Attribute* _node);
Attribute* GetAttr(int32_t _id);
const Attribute* GetAttr(int32_t _id) const;
const etk::UString& GetAttribute(const etk::UString& _name) const;
void SetAttribute(const etk::UString& _name, const etk::UString& _value);
public:
// herited function:
bool Generate(etk::UString& _data, int32_t _indent) const;
};
};
#endif

View File

@ -12,17 +12,36 @@
#undef __class__
#define __class__ "Declaration"
/* basic declaration have 3 attributes:
version
encoding
standalone
<?xml version="1.0" encoding="UTF-8" ?>
*/
exml::Declaration::Declaration(const etk::UString& _version, unicode::charset_te _format, const etk::UString& _standalone)
{
m_value = "xml";
if (_version.Size()!=0) {
SetAttribute("version", _version);
}
if (_format!=unicode::EDN_CHARSET_UTF8) {
SetAttribute("encoding", "UTF-8");
} else {
EXML_ERROR("Actually does not supported other charset than UTF8");
SetAttribute("encoding", "UTF-8");
}
if (_standalone.Size()!=0) {
SetAttribute("standalone", _standalone);
}
}
bool exml::Declaration::Generate(etk::UString& _data, int32_t _indent) const
{
AddIndent(_data, _indent);
_data += "<?";
_data += m_value;
/*
if (m_value.Size()!=0) {
_data += " ";
_data += m_value;
}
*/
exml::AttributeList::Generate(_data, _indent);
_data += "?>\n";
return true;
}

View File

@ -9,16 +9,16 @@
#ifndef __ETK_XML_DECLARATION_H__
#define __ETK_XML_DECLARATION_H__
#include <exml/Node.h>
#include <etk/Vector.h>
#include <exml/AttributeList.h>
namespace exml
{
class Declaration : public exml::Node
class Declaration : public exml::AttributeList
{
public:
Declaration(void) { };
Declaration(const etk::UString& _version, const etk::UString& _format, const etk::UString& _validation) { };
// for xml generic declaration
Declaration(const etk::UString& _version, unicode::charset_te _format, const etk::UString& _standalone);
virtual ~Declaration(void) { };
virtual nodeType_te GetType(void) const { return typeAttribute; };
virtual bool Generate(etk::UString& _data, int32_t _indent) const;

View File

@ -54,7 +54,8 @@ bool exml::Document::Parse(const etk::UString& _data)
bool exml::Document::Generate(etk::UString& _data)
{
return false;
_data = "";
return Generate(_data,0);
}
bool exml::Document::Load(const etk::UString& _file)
@ -72,7 +73,7 @@ bool exml::Document::Load(const etk::UString& _file)
return false;
}
if (false == tmpFile.FileOpenRead()) {
EXML_ERROR("Can not open the file : " << _file);
EXML_ERROR("Can not open (r) the file : " << _file);
return false;
}
// allocate data
@ -103,7 +104,24 @@ bool exml::Document::Load(const etk::UString& _file)
bool exml::Document::Store(const etk::UString& _file)
{
return false;
etk::UString createData;
if (false == Generate(createData)) {
EXML_ERROR("Error while creating the XML : " << _file);
return false;
}
etk::FSNode tmpFile(_file);
if (false == tmpFile.FileOpenWrite()) {
EXML_ERROR("Can not open (w) the file : " << _file);
return false;
}
etk::Char endTable = createData.c_str();
if (tmpFile.FileWrite(endTable, sizeof(char), endTable.Size()) != endTable.Size()) {
EXML_ERROR("Error while writing output XML file : " << _file);
tmpFile.FileClose();
return false;
}
tmpFile.FileClose();
return true;
}
void exml::Document::Display(void)

View File

@ -118,70 +118,6 @@ void exml::Element::Append(exml::Node* _node)
m_listSub.PushBack(_node);
}
exml::Attribute* exml::Element::GetAttr(int32_t _id)
{
if (_id <0 || _id>m_listAttribute.Size()) {
return NULL;
}
return m_listAttribute[_id];
}
const exml::Attribute* exml::Element::GetAttr(int32_t _id) const
{
if (_id <0 || _id>m_listAttribute.Size()) {
return NULL;
}
return m_listAttribute[_id];
}
void exml::Element::AppendAttribute(exml::Attribute* _node)
{
if (_node == NULL) {
EXML_ERROR("Try to set an empty node");
return;
}
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if (m_listAttribute[iii] == _node) {
EXML_ERROR("Try to add a node that is already added befor !!!");
return;
}
}
m_listAttribute.PushBack(_node);
}
const etk::UString& exml::Element::GetAttribute(const etk::UString& _name) const
{
static const etk::UString errorReturn("");
if (_name.Size()==0) {
return errorReturn;
}
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->GetName() == _name) {
return m_listAttribute[iii]->GetValue();
}
}
return errorReturn;
}
void exml::Element::SetAttribute(const etk::UString& _name, const etk::UString& _value)
{
// check if attribute already det :
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->GetName() == _name) {
// update the value :
m_listAttribute[iii]->SetValue(_value);
return;
}
}
exml::Attribute* attr = new exml::Attribute(_name, _value);
if (NULL==attr) {
EXML_ERROR("memory allocation error...");
}
m_listAttribute.PushBack(attr);
}
etk::UString exml::Element::GetText(void)
{
// TODO : Add more capabilities ...
@ -200,11 +136,7 @@ bool exml::Element::Generate(etk::UString& _data, int32_t _indent) const
AddIndent(_data, _indent);
_data += "<";
_data += m_value;
for (int32_t iii=0; iii<m_listAttribute.Size(); iii++) {
if (NULL!=m_listAttribute[iii]) {
m_listAttribute[iii]->Generate(_data, _indent);
}
}
exml::AttributeList::Generate(_data, _indent);
if (m_listSub.Size()>0) {
if( m_listSub.Size()==1

View File

@ -11,15 +11,15 @@
#include <exml/Node.h>
#include <etk/Vector.h>
#include <exml/Attribute.h>
#include <exml/AttributeList.h>
namespace exml
{
class Element : public Node
class Element : public AttributeList
{
public:
Element(void) { };
Element(const etk::UString& _value) : exml::Node(_value) { };
Element(const etk::UString& _value) { m_value = _value; };
virtual ~Element(void) { };
virtual nodeType_te GetType(void) const { return typeElement; };
protected:
@ -36,15 +36,6 @@ namespace exml
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:
int32_t SizeAttribute(void) const { return m_listSub.Size(); };
void AppendAttribute(Attribute* _node);
Attribute* GetAttr(int32_t _id);
const Attribute* GetAttr(int32_t _id) const;
const etk::UString& GetAttribute(const etk::UString& _name) const;
void SetAttribute(const etk::UString& _name, const etk::UString& _value);
public:
etk::UString GetText(void);
public:

View File

@ -18,6 +18,7 @@ namespace exml
{
public:
Text(void) { };
Text(const etk::UString& _data) { m_value=_data; };
virtual ~Text(void) { };
virtual nodeType_te GetType(void) const { return typeText; };
virtual bool Parse(const etk::UString& _data, int32_t& _pos, bool _caseSensitive, ivec2& _filePos);

View File

@ -10,6 +10,7 @@ def Create(target):
myModule.AddSrcFile([
'exml/debug.cpp',
'exml/Attribute.cpp',
'exml/AttributeList.cpp',
'exml/Comment.cpp',
'exml/Declaration.cpp',
'exml/Document.cpp',