gambas-source-code/gb.xml/src/CElement.cpp

356 lines
11 KiB
C++
Raw Normal View History

/***************************************************************************
(c) 2012 Adrien Prokopowicz <prokopy@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
***************************************************************************/
#include "CElement.h"
#include "element.h"
#include "gbi.h"
#define THIS (static_cast<CNode*>(_object)->node->toElement())
#define THISNODE (static_cast<CNode*>(_object)->node)
BEGIN_METHOD(CElement_new, GB_STRING tagName)
if(Node::NoInstanciate) return;
[GB.XML] * NEW : Added a new property "State" to XmlExplorer and XmlReader, that allows to know the reader state, without knowing Read() return value. * NEW : When enumerating XmlReader.Node.Attributes, XmlReader.Node represents the current attribute. * NEW : Added an new method Open() that loads an HtmlDocument from a file and defines it as the document to read. * NEW : The Attributes property has now moved from XmlElement to XmlNode. XmlNode.Attributes returns Null if the node isn't an element. * NEW : Then enumeration of XmlNode.Attributes now returns an .XmlElementAttribute instead of a string. * NEW : The XmlNode.Attribute constant is not obsolete anymore. * NEW : Two consecutives whitespaces are now ignored (only one is preserved). * NEW : When parsing a file, line-breaks and tabulations are replaced by spaces. * BUG : XmlExplorer flags are now correctly initialized. * BUG : Added an Eof property on XmlExplorer, that was missing. * BUG : Calling XmlExplorer.Read() after Eof just returns XmlReaderNodeType.Eof, not more. * BUG : Calling XmlNode.Next() when the node hasn't got any brother after him doesn't crash anymore, it just returns Null. * BUG : Calling XmlElement.NextSibling() when the node hasn't got any element brother after him doesn't crash anymore, it just returns Null. * BUG : By default, XmlReader correctly stops when reading the end of an element. * BUG : XmlExplorer now correctly initializes and releases itself. * BUG : XmlExplorer now correctly supports its read flags. * OPT : Solved a memory leak when releasing a XmlReader. * OPT : When setting text content to a XmlNode, the string is not duplicated anymore. * OPT : When testing if characters are whitespaces or names characters while parsing texts, puts the temporary variable into registers instead of memory. git-svn-id: svn://localhost/gambas/trunk@4669 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2012-04-25 01:47:14 +02:00
if(!MISSING(tagName))
{
THISNODE = new Element(STRING(tagName), LENGTH(tagName));
}
else
{
THISNODE = new Element;
}
THIS->GBObject = static_cast<CNode*>(_object);
END_METHOD
BEGIN_PROPERTY(CElement_tagName)
if(READ_PROPERTY)
{
GB.ReturnNewString(THIS->tagName, THIS->lenTagName);
if(!THIS->tagName || !THIS->lenTagName)
{
GB.ReturnNull();
return;
}
}
else
{
THIS->setTagName(PSTRING(), PLENGTH());
}
END_PROPERTY
BEGIN_PROPERTY(CElement_prefix)
if(READ_PROPERTY)
{
if(!THIS->prefix || !THIS->lenPrefix)
{
GB.ReturnNull();
return;
}
GB.ReturnNewString(THIS->prefix, THIS->lenPrefix);
}
else
{
THIS->setPrefix(PSTRING(), PLENGTH());
}
END_PROPERTY
BEGIN_METHOD(CElement_appendChild, GB_OBJECT newChild)
THIS->appendChild(VARGOBJ(CNode, newChild)->node);
END_METHOD
BEGIN_METHOD(CElement_getAttribute, GB_STRING attrName; GB_INTEGER mode)
Attribute *attr = THIS->getAttribute(STRING(attrName), LENGTH(attrName), VARG(mode));
if(attr)
{
GB.ReturnNewString(attr->attrValue, attr->lenAttrValue);
}
else
{
GB.ReturnNull();
}
END_METHOD
BEGIN_METHOD(CElement_setAttribute, GB_STRING attrName; GB_STRING attrValue)
THIS->setAttribute(STRING(attrName), LENGTH(attrName),
STRING(attrValue), LENGTH(attrValue));
END_METHOD
BEGIN_METHOD(CElement_appendFromText, GB_STRING data)
try
{
THIS->appendFromText(STRING(data), LENGTH(data));
}
catch(XMLParseException &e)
{
GB.Error(e.what());
}
END_METHOD
BEGIN_METHOD(CElement_appendChildren, GB_OBJECT children)
GB_ARRAY array = reinterpret_cast<GB_ARRAY>(VARG(children));
for(int i = 0; i < GB.Array.Count(array); i++)
{
THIS->appendChild((*(reinterpret_cast<CNode**>(GB.Array.Get(array, i))))->node);
}
END_METHOD
BEGIN_METHOD(CElement_prependChild, GB_OBJECT newChild)
THIS->prependChild(VARGOBJ(CNode, newChild)->node);
END_METHOD
BEGIN_METHOD(CElement_removeChild, GB_OBJECT oldChild)
THIS->removeChild(VARGOBJ(CNode, oldChild)->node);
END_METHOD
BEGIN_METHOD(CElement_replaceChild, GB_OBJECT oldChild; GB_OBJECT newChild)
THIS->replaceChild(VARGOBJ(CNode, oldChild)->node,
VARGOBJ(CNode, newChild)->node);
END_METHOD
BEGIN_METHOD(CElement_insertAfter, GB_OBJECT child; GB_OBJECT newChild)
THIS->insertAfter(VARGOBJ(CNode, child)->node, VARGOBJ(CNode, newChild)->node);
END_METHOD
BEGIN_METHOD(CElement_insertBefore, GB_OBJECT child; GB_OBJECT newChild)
THIS->insertBefore(VARGOBJ(CNode, child)->node, VARGOBJ(CNode, newChild)->node);
END_METHOD
BEGIN_METHOD(CElement_appendText, GB_STRING data)
THIS->appendText(STRING(data), LENGTH(data));
END_METHOD
BEGIN_METHOD_VOID(CElement_clearChildren)
THIS->clearChildren();
END_METHOD
BEGIN_PROPERTY(CElement_previousElement)
GBI::Return(THIS->previousElement());
END_PROPERTY
BEGIN_PROPERTY(CElement_nextElement)
GBI::Return(THIS->nextElement());
END_PROPERTY
BEGIN_METHOD(CElement_isAttributeSet, GB_STRING name)
GB.ReturnBoolean((bool)(THIS->getAttribute(STRING(name), LENGTH(name))));
END_METHOD
BEGIN_PROPERTY(CElement_allChildNodes)
GB_ARRAY array;
THIS->getGBAllChildren(&array);
GB.ReturnObject(array);
END_PROPERTY
BEGIN_PROPERTY(CElement_firstChildElement)
[GB.XML] OPT : A few compilation optimizations in headers inclusions. OPT : Use of separated internal classes of gambas objects. This might prevent from a lot of problems. OPT : Strings are not stored as wchar_t* anymore, but as simple char*, and are converted only when necessary (like UTF8 characters verification). OPT : Does not use STL classes anymore. They are too slow. Uses now handmade classes. OPT : Nodes are now internally linked to their brothers, so it makes find them really fasters, and allows no longer use of external linked list. OPT : When creating new nodes, linked Gambas objects are created only when necessary, so it makes internals instanciations very faster (such as the parser). OPT : Handmade string/memory classes and functions are now separated from the main.cpp component file. OPT : Use specialized memory management functions (like memcpy, memchr ...) into the parser. They are really faster than C loops, and speeds up the parser for documents that have a lot of plain text. OPT : Internally, the stream parser (XmlReader) takes only one char, not a unuseful wole string object. OPT : Does not updates debugging data when parsing a file. They will be generated only if an error is raised (not implemented yet). It will allow a speedier parser and more precise informations. BUG : When loading a document from a file, now correctly releases the file data. BUG : Solved a few XmlReader uninitialized variables, that could make the stream parser crash or returns strange results. git-svn-id: svn://localhost/gambas/trunk@4737 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2012-05-17 22:49:54 +02:00
GBI::Return(THIS->firstChildElement());
END_PROPERTY
BEGIN_PROPERTY(CElement_lastChildElement)
[GB.XML] OPT : A few compilation optimizations in headers inclusions. OPT : Use of separated internal classes of gambas objects. This might prevent from a lot of problems. OPT : Strings are not stored as wchar_t* anymore, but as simple char*, and are converted only when necessary (like UTF8 characters verification). OPT : Does not use STL classes anymore. They are too slow. Uses now handmade classes. OPT : Nodes are now internally linked to their brothers, so it makes find them really fasters, and allows no longer use of external linked list. OPT : When creating new nodes, linked Gambas objects are created only when necessary, so it makes internals instanciations very faster (such as the parser). OPT : Handmade string/memory classes and functions are now separated from the main.cpp component file. OPT : Use specialized memory management functions (like memcpy, memchr ...) into the parser. They are really faster than C loops, and speeds up the parser for documents that have a lot of plain text. OPT : Internally, the stream parser (XmlReader) takes only one char, not a unuseful wole string object. OPT : Does not updates debugging data when parsing a file. They will be generated only if an error is raised (not implemented yet). It will allow a speedier parser and more precise informations. BUG : When loading a document from a file, now correctly releases the file data. BUG : Solved a few XmlReader uninitialized variables, that could make the stream parser crash or returns strange results. git-svn-id: svn://localhost/gambas/trunk@4737 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2012-05-17 22:49:54 +02:00
GBI::Return(THIS->lastChildElement());
END_PROPERTY
BEGIN_PROPERTY(CElement_childElements)
GB_ARRAY array;
THIS->getGBChildElements(&array);
GB.ReturnObject(array);
END_PROPERTY
BEGIN_METHOD(CElement_fromText, GB_STRING data)
GB_ARRAY array;
Element::GBfromText(STRING(data), LENGTH(data), &array);
GB.ReturnObject(array);
END_METHOD
BEGIN_METHOD(CElement_getChildrenByTagName, GB_STRING tagName; GB_INTEGER mode ; GB_INTEGER depth;)
GB_ARRAY array;
THIS->getGBChildrenByTagName(STRING(tagName), LENGTH(tagName), &array, VARGOPT(mode, GB_STRCOMP_BINARY), VARGOPT(depth, -1));
GB.ReturnObject(array);
END_METHOD
BEGIN_METHOD(CElement_getChildrenByNamespace, GB_STRING name; GB_INTEGER mode ; GB_INTEGER depth;)
GB_ARRAY array;
THIS->getGBChildrenByNamespace(STRING(name), LENGTH(name), &array, VARGOPT(mode, GB_STRCOMP_BINARY), VARGOPT(depth, -1));
GB.ReturnObject(array);
END_METHOD
BEGIN_METHOD(CElement_getChildrenByAttributeValue, GB_STRING name; GB_STRING value; GB_INTEGER mode ; GB_INTEGER depth;)
GB_ARRAY array;
THIS->getGBChildrenByAttributeValue(STRING(name), LENGTH(name),
STRING(value), LENGTH(value),
&array, VARGOPT(mode, GB_STRCOMP_BINARY), VARGOPT(depth, -1));
GB.ReturnObject(array);
END_METHOD
BEGIN_PROPERTY(CElement_firstChild)
GBI::Return(THIS->firstChild);
END_METHOD
BEGIN_PROPERTY(CElement_lastChild)
GBI::Return(THIS->lastChild);
END_PROPERTY
GB_DESC CElementDesc[] =
{
GB_DECLARE("XmlElement", sizeof(CNode)), GB_INHERITS("XmlNode"),
GB_METHOD("_new", "", CElement_new, "[(TagName)s]"),
GB_METHOD("AppendChild", "", CElement_appendChild, "(NewChild)XmlNode"),
GB_METHOD("AppendChildren", "", CElement_appendChildren, "(NewChildren)XmlNode[]"),
GB_METHOD("PrependChild", "", CElement_prependChild, "(NewChild)XmlNode"),
GB_METHOD("InsertAfter", "", CElement_insertAfter, "(Child)XmlNode;(NewChild)XmlNode"),
GB_METHOD("InsertBefore", "", CElement_insertBefore, "(Child)XmlNode;(NewChild)XmlNode"),
GB_METHOD("RemoveChild", "", CElement_removeChild, "(OldChild)XmlNode"),
GB_METHOD("ReplaceChild", "", CElement_replaceChild, "(OldChild)XmlNode;(NewChild)XmlNode"),
GB_METHOD("ClearChildren", "", CElement_clearChildren, ""),
GB_METHOD("AppendText", "", CElement_appendText, "(Data)s"),
GB_METHOD("AppendFromText", "", CElement_appendFromText, "(Data)s"),
GB_METHOD("GetAttribute", "s", CElement_getAttribute, "(Name)s[(Mode)i]"),
GB_METHOD("SetAttribute", "", CElement_setAttribute, "(Name)s(Value)s"),
GB_METHOD("IsAttributeSet", "b", CElement_isAttributeSet, "(Name)s"),
GB_PROPERTY_READ("ChildElements", "XmlElement[]", CElement_childElements),
GB_PROPERTY_READ("AllChildNodes", "XmlNode[]", CElement_allChildNodes),
GB_PROPERTY_READ("FirstChild", "XmlNode", CElement_firstChild),
GB_PROPERTY_READ("LastChild", "XmlNode", CElement_lastChild),
GB_PROPERTY_READ("FirstChildElement", "XmlElement", CElement_firstChildElement),
GB_PROPERTY_READ("LastChildElement", "XmlElement", CElement_lastChildElement),
GB_PROPERTY("TagName", "s", CElement_tagName),
GB_PROPERTY("Prefix", "s", CElement_prefix),
GB_PROPERTY("PreviousElement", "XmlElement", CElement_previousElement),
GB_PROPERTY("NextElement", "XmlElement", CElement_nextElement),
GB_METHOD("GetChildrenByNamespace", "XmlElement[]", CElement_getChildrenByNamespace, "(Namespace)s[(Mode)i(Depth)i]"),
GB_METHOD("GetChildrenByTagName", "XmlElement[]", CElement_getChildrenByTagName, "(TagName)s[(Mode)i(Depth)i]"),
GB_METHOD("GetChildrenByAttributeValue", "XmlElement[]", CElement_getChildrenByAttributeValue, "(Attribute)s(Value)s[(Mode)i(Depth)i]"),
GB_STATIC_METHOD("FromText", "XmlNode[]", CElement_fromText, "(Data)s"),
GB_END_DECLARE
};
/*
GB_DESC CElementDesc[] =
{
[GB.XML] OPT : A few compilation optimizations in headers inclusions. OPT : Use of separated internal classes of gambas objects. This might prevent from a lot of problems. OPT : Strings are not stored as wchar_t* anymore, but as simple char*, and are converted only when necessary (like UTF8 characters verification). OPT : Does not use STL classes anymore. They are too slow. Uses now handmade classes. OPT : Nodes are now internally linked to their brothers, so it makes find them really fasters, and allows no longer use of external linked list. OPT : When creating new nodes, linked Gambas objects are created only when necessary, so it makes internals instanciations very faster (such as the parser). OPT : Handmade string/memory classes and functions are now separated from the main.cpp component file. OPT : Use specialized memory management functions (like memcpy, memchr ...) into the parser. They are really faster than C loops, and speeds up the parser for documents that have a lot of plain text. OPT : Internally, the stream parser (XmlReader) takes only one char, not a unuseful wole string object. OPT : Does not updates debugging data when parsing a file. They will be generated only if an error is raised (not implemented yet). It will allow a speedier parser and more precise informations. BUG : When loading a document from a file, now correctly releases the file data. BUG : Solved a few XmlReader uninitialized variables, that could make the stream parser crash or returns strange results. git-svn-id: svn://localhost/gambas/trunk@4737 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2012-05-17 22:49:54 +02:00
GB_DECLARE("XmlElement", sizeof(CElement)), GB_INHERITS("XmlNode"),
GB_METHOD("_new", "", CElement_new, "[(TagName)s]"),
GB_METHOD("_free", "", CElement_free, ""),
GB_METHOD("AppendChild", "", CElement_AppendChild, "(NewChild)XmlNode"),
GB_METHOD("AppendChildren", "", CElement_AppendChildren, "(NewChildren)XmlNode[]"),
GB_METHOD("PrependChild", "", CElement_prependChild, "(NewChild)XmlNode"),
GB_METHOD("InsertAfter", "", CElement_insertAfter, "(Child)XmlNode;(NewChild)XmlNode"),
GB_METHOD("InsertBefore", "", CElement_insertBefore, "(Child)XmlNode;(NewChild)XmlNode"),
GB_METHOD("AppendText", "", CElement_AppendText, "(NewText)s"),
GB_METHOD("AppendFromText", "", CElement_appendFromText, "(Data)s"),
GB_METHOD("RemoveChild", "", CElement_removeChild, "(OldChild)XmlNode"),
GB_METHOD("ReplaceChild", "", CElement_replaceChild, "(OldChild)XmlNode;(NewChild)XmlNode"),
GB_METHOD("NewElement", "", CElement_newElement, "(Name)s[(Value)s]"),
GB_PROPERTY("TagName", "s", CElement_tagName),
GB_PROPERTY_READ("PreviousSibling", "XmlElement", CElement_previousSibling),
GB_PROPERTY_READ("NextSibling", "XmlElement", CElement_nextSibling),
GB_METHOD("GetAttribute", "s", CElement_getAttribute, "(Name)s"),
GB_METHOD("SetAttribute", "", CElement_setAttribute, "(Name)s(Value)s"),
GB_METHOD("NewAttribute", "", CElement_setAttribute, "(Name)s(Value)s"),
GB_METHOD("IsAttributeSet", "b", CElement_isAttributeSet, "(Name)s"),
GB_PROPERTY_READ("ChildNodes", "XmlNode[]", CElement_childNodes),
GB_PROPERTY_READ("Children", "XmlNode[]", CElement_childNodes),
GB_PROPERTY_READ("ChildElements", "XmlElement[]", CElement_childElements),
GB_PROPERTY_READ("AllChildNodes", "XmlNode[]", CElement_allChildNodes),
GB_PROPERTY_READ("FirstChildElement", "XmlElement", CElement_firstChildElement),
GB_PROPERTY_READ("LastChildElement", "XmlElement", CElement_lastChildElement),
GB_METHOD("MatchXPathFilter", "b", CElement_matchXPathFilter ,"(Filter)s"),
GB_METHOD("GetChildrenByTagName", "XmlElement[]", CElement_getChildrenByTagName, "(TagName)s[(Depth)i]"),
GB_METHOD("GetChildrenByAttributeValue", "XmlElement[]", CElement_getChildrenByAttributeValue, "(Attribute)s(Value)s[(Depth)i]"),
GB_STATIC_METHOD("FromText", "XmlNode[]", CElement_fromText, "(Data)s"),
GB_END_DECLARE
};*/