gambas-source-code/gb.xml/src/CReader.cpp
Adrien Prokopowicz bcc54489c1 [GB.XML]
* NEW: New XML manipulation component

git-svn-id: svn://localhost/gambas/trunk@4644 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2012-04-20 22:57:18 +00:00

271 lines
6.6 KiB
C++

#include "CReader.h"
#undef THIS
#define THIS (static_cast<Reader*>(_object))
BEGIN_METHOD_VOID(CReader_new)
THIS->InitReader();
END_METHOD
BEGIN_METHOD_VOID(CReader_free)
THIS->ClearReader();
END_METHOD
BEGIN_METHOD(CReader_ReadChar, GB_STRING car)
GB.ReturnInteger(THIS->ReadChar(STRING(car)));
END_METHOD
BEGIN_PROPERTY(CReader_keepData)
if(READ_PROPERTY)
{
GB.ReturnBoolean(THIS->keepMemory);
}
else
{
THIS->keepMemory = VPROP(GB_BOOLEAN);
if(THIS->keepMemory && THIS->foundNode)
{
THIS->storedElements->push_back(THIS->foundNode);
GB.Ref(THIS->foundNode);
}
}
END_PROPERTY
BEGIN_PROPERTY(CReader_pos)
if(!READ_PROPERTY) return;
GB.ReturnInteger(THIS->pos);
END_PROPERTY
BEGIN_PROPERTY(CReader_node)
if(!READ_PROPERTY) return;
if(!THIS->foundNode) {GB.ReturnNull(); return;}
RETURN_SELF();
END_PROPERTY
BEGIN_METHOD_VOID(CReaderNodeAttr_next)
if(!THIS->foundNode->isElement())
{
GB.StopEnum(); return;
}
map<wstring,wstring>::iterator *it = *reinterpret_cast<map<wstring,wstring>::iterator**>((GB.GetEnum()));
if(it == 0)
{
it = new map<wstring,wstring>::iterator(THIS->foundNode->toElement()->attributes->begin());
*reinterpret_cast<map<wstring,wstring>::iterator**>(GB.GetEnum()) = it;
}
else
{
++(*it);
}
if(*it == THIS->foundNode->toElement()->attributes->end()) {GB.StopEnum(); delete it; return;}
GB.ReturnNewZeroString(WStringToString((*(*it)).second).c_str());
END_METHOD
BEGIN_METHOD(CReaderNodeAttr_get, GB_STRING name)
if(!THIS->foundNode->isElement()) return;
GB.ReturnNewZeroString(WStringToString(THIS->foundNode->toElement()->getAttribute(STRING(name))).c_str());
END_METHOD
BEGIN_METHOD(CReaderNodeAttr_put, GB_STRING value; GB_STRING name)
if(!THIS->foundNode->isElement()) return;
THIS->foundNode->toElement()->setAttribute(STRING(name), STRING(value));
END_METHOD
BEGIN_PROPERTY(CReaderNodeAttr_count)
if(READ_PROPERTY && THIS->foundNode->isElement())
{
GB.ReturnInteger(THIS->foundNode->toElement()->attributes->size());
}
END_PROPERTY
BEGIN_METHOD(CReaderReadFlags_get, GB_INTEGER flag)
int flag = VARG(flag);
if(flag > FLAGS_COUNT || flag < 0) return;
GB.ReturnBoolean(THIS->flags[flag]);
END_METHOD
BEGIN_METHOD(CReaderReadFlags_put, GB_BOOLEAN value; GB_INTEGER flag)
int flag = VARG(flag);
if(flag > FLAGS_COUNT || flag < 0 || flag == READ_ERR_EOF) return;
THIS->flags[flag] = VARG(value);
END_METHOD
BEGIN_PROPERTY(CReaderNode_type)
switch(THIS->foundNode->getType())
{
case Node::ElementNode:
GB.ReturnInteger(NODE_ELEMENT);break;
case Node::Comment:
GB.ReturnInteger(NODE_COMMENT);break;
case Node::NodeText:
GB.ReturnInteger(NODE_TEXT);break;
case Node::CDATA:
GB.ReturnInteger(NODE_CDATA);break;
case Node::BaseNode:
default:
GB.ReturnInteger(0);
}
END_PROPERTY
BEGIN_PROPERTY(CReaderNode_Value)
GB.ReturnNewZeroString(WStringToString(THIS->foundNode->textContent()).c_str());
END_PROPERTY
BEGIN_PROPERTY(CReaderNode_Name)
switch (THIS->foundNode->getType())
{
case Node::ElementNode:
GB.ReturnNewZeroString(WStringToString(*(THIS->foundNode->toElement()->tagName)).c_str());break;
case Node::NodeText:
GB.ReturnNewZeroString("#text");break;
case Node::Comment:
GB.ReturnNewZeroString("#comment");break;
case Node::CDATA:
GB.ReturnNewZeroString("#cdata");break;
case Node::BaseNode:
default:
GB.ReturnNull();
}
END_PROPERTY
BEGIN_PROPERTY(CReader_Depth)
GB.ReturnInteger(THIS->depth);
END_PROPERTY
BEGIN_PROPERTY(CReader_storedNodes)
if(!READ_PROPERTY) return;
GBI::ObjectArray<Node> *nodes = new GBI::ObjectArray<Node>("XmlNode", *(THIS->storedElements));
GB.ReturnObject(nodes->array);
//delete nodes;
END_PROPERTY
GB_DESC CReaderNodeTypeDesc[] =
{
GB_DECLARE("XmlReaderNodeType", 0), GB_VIRTUAL_CLASS(),
GB_CONSTANT("None", "i", 0),
GB_CONSTANT("Element", "i", NODE_ELEMENT),
GB_CONSTANT("Attribute", "i", READ_ATTRIBUTE),
GB_CONSTANT("Text", "i", NODE_TEXT),
GB_CONSTANT("CDATA", "i", NODE_CDATA),
GB_CONSTANT("EntityReference", "i", 0),
GB_CONSTANT("Entity", "i", 0),
GB_CONSTANT("ProcessingInstruction", "i", 0),
GB_CONSTANT("Comment", "i", NODE_COMMENT),
GB_CONSTANT("Document", "i", 0),
GB_CONSTANT("DocumentType", "i", 0),
GB_CONSTANT("DocumentFragment", "i", 0),
GB_CONSTANT("Notation", "i", 0),
GB_CONSTANT("Whitespace", "i",0),
GB_CONSTANT("SignificantWhitespace", "i",0),
GB_CONSTANT("EndElement", "i", READ_END_CUR_ELEMENT),
GB_CONSTANT("EndStream", "i", READ_ERR_EOF),
GB_CONSTANT("EndEntity", "i", 0),
GB_CONSTANT("XmlDeclaration", "i",0),
GB_END_DECLARE
};
GB_DESC CReaderReadFlagsDesc[] =
{
GB_DECLARE(".XmlReaderReadFlags", 0), GB_VIRTUAL_CLASS(),
GB_METHOD("_get", "b", CReaderReadFlags_get, "(Flag)i"),
GB_METHOD("_put", "b", CReaderReadFlags_put, "(Value)b(Flag)i"),
GB_END_DECLARE
};
GB_DESC CReaderNodeAttributesDesc[] =
{
GB_DECLARE(".XmlReader.Node.Attributes", 0), GB_VIRTUAL_CLASS(),
GB_METHOD("_get", "s", CReaderNodeAttr_get, "(Name)s"),
GB_METHOD("_put", "s", CReaderNodeAttr_put, "(Value)s(Name)s"),
GB_METHOD("_next", "s", CReaderNodeAttr_next, ""),
GB_PROPERTY_READ("Count", "i", CReaderNodeAttr_count),
GB_END_DECLARE
};
GB_DESC CReaderNodeDesc[] =
{
GB_DECLARE(".XmlReader.Node", 0), GB_VIRTUAL_CLASS(),
GB_PROPERTY_SELF("Attributes", ".XmlReader.Node.Attributes"),
//GB_PROPERTY_READ("BaseUri","s",CRNODE_BaseUri),
GB_PROPERTY_READ("Depth","i",CReader_Depth),
//GB_PROPERTY_READ("IsDefault","b",CRNODE_IsDefault),
//GB_PROPERTY_READ("IsEmptyElement","b",CRNODE_IsEmptyElement),
//GB_PROPERTY_READ("LocalName","s",CRNODE_LocalName),
GB_PROPERTY_READ("Name", "s", CReaderNode_Name),
//GB_PROPERTY_READ("NamespaceUri", "s", CRNODE_NamespaceUri),
//GB_PROPERTY_READ("Prefix", "s", CRNODE_Prefix),
//GB_PROPERTY_READ("QuoteChar", "s", CRNODE_QuoteChar),
GB_PROPERTY_READ("Type","i",CReaderNode_type),
GB_PROPERTY_READ("Value", "s", CReaderNode_Value),
//GB_PROPERTY_READ("XmlLang", "s", CRNODE_XmlLang),
GB_END_DECLARE
};
GB_DESC CReaderDesc[] =
{
GB_DECLARE("_XmlReader", sizeof(Reader)),
GB_METHOD("_new", "", CReader_new, ""),
GB_METHOD("_free", "", CReader_free, ""),
GB_METHOD("_ReadChar", "i", CReader_ReadChar, "(Char)s"),
GB_PROPERTY("KeepData", "b", CReader_keepData),
GB_PROPERTY_READ("Pos", "i", CReader_pos),
GB_PROPERTY("Node", ".XmlReader.Node", CReader_node),
GB_PROPERTY_READ("StoredNodes", "XmlNode[]", CReader_storedNodes),
GB_PROPERTY_READ("Depth","i",CReader_Depth),
GB_PROPERTY_SELF("ReadFlags", ".XmlReaderReadFlags"),
GB_END_DECLARE
};