45#include <scolPlugin.h>
49#include <scolMemoryHelper.hpp>
80 std::pair<std::set<tinyxml2::XMLNode*>::iterator,
bool> ret;
81 ret = nodeslist.insert(xmlNode);
88 nodeslist.erase(xmlNode);
99 std::set<tinyxml2::XMLNode*>::iterator it = nodeslist.find(xmlNode);
100 if (it != nodeslist.end())
111 std::string ToString()
114 xmlDoc->
Print(&stream);
115 return stream.
CStr();
120 std::set<tinyxml2::XMLNode*> nodeslist;
127 tinyxml2::XMLNode* xmlNode = MMgetPointer<tinyxml2::XMLNode*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 0)));
128 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
129 if (!xmlNode || !sXmlDoc)
134 int objDoc = OBJfindTH(m, OBJTYPSXML, SCOL_PTR sXmlDoc);
135 if (objDoc == NIL || (MMgetPointer<SXml*>(m, objDoc) == 0))
140 if (sXmlDoc->NodeExist(xmlNode))
146int destroyXmlObj(mmachine m, SCOL_PTR_TYPE handsys,
int objm)
148 SXml* xmlDoc = MMgetPointer<SXml*>(m, MTOP(objm));
151 MMsetPointer<SXml*>(m, MTOP(objm), 0);
166 MMechostr(MSKDEBUG,
"_CreateXml\n");
170 if (MMget(m, 0) == NIL)
176 MMechostr(MSKDEBUG,
"_CreateXml : New XMLDocument failed.\n");
184 MMechostr(MSKDEBUG,
"_CreateXml : New SXml failed.\n");
191 if ((MMpushPointer(m, sXmlDoc) != 0))
193 SAFE_DELETE(sXmlDoc);
199 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
214 MMechostr(MSKDEBUG,
"_OpenXml\n");
217 int ifile = MMpull(m);
218 if (MMget(m, 0) == NIL)
220 MMechostr(MSKDEBUG,
"_OpenXml : Channel nil.\n");
227 MMechostr(MSKDEBUG,
"_OpenXml : File nil.\n");
232 std::string path = MMstartstr(m, MTOP(ifile));
237 MMechostr(MSKDEBUG,
"_OpenXml : New XMLDocument failed.\n");
242 if(xmlDoc->
LoadFile(path.c_str()) != tinyxml2::XML_SUCCESS)
244 MMechostr(MSKDEBUG,
"_OpenXml : load of %s failed\n", path.c_str());
254 MMechostr(MSKDEBUG,
"_OpenXml : New SXml failed.\n");
261 if ((MMpushPointer(m, sXmlDoc) != 0))
263 SAFE_DELETE(sXmlDoc);
269 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
284 MMechostr(MSKDEBUG,
"_OpenXmlS\n");
287 int icont = MMpull(m);
288 if ((MMget(m, 0) == NIL) || (icont == NIL))
294 std::string content = MMstartstr(m, MTOP(icont));
299 MMechostr(MSKDEBUG,
"_OpenXmlS : New XMLDocument failed.\n");
304 if(xmlDoc->
Parse(content.c_str()) != tinyxml2::XML_NO_ERROR)
306 MMechostr(MSKDEBUG,
"_OpenXmlS : load of content failed\n");
315 MMechostr(MSKDEBUG,
"_OpenXmlS : New SXml failed.\n");
322 if ((MMpushPointer(m, sXmlDoc) != 0))
324 SAFE_DELETE(sXmlDoc);
330 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
345 MMechostr(MSKDEBUG,
"_SaveXml\n");
348 int ifile = MMpull(m);
349 int ixml = MMpull(m);
350 if ((ixml == NIL) || (ifile == NIL))
352 MMechostr(MSKDEBUG,
"_SaveXml : param nil.\n");
356 std::string path = MMstartstr(m, MTOP(ifile));
358 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
361 MMechostr(MSKDEBUG,
"_SaveXml : bad ObjSXML object.\n");
366 if(sXmlDoc->GetDocument()->
SaveFile(path.c_str()) != tinyxml2::XML_NO_ERROR)
368 MMechostr(MSKDEBUG,
"_SaveXml : error on save file.\n");
389MMechostr(MSKDEBUG,
"_DestroyXml\n");
392 int mlTab = MMget(m, 0);
395 MMechostr(MSKDEBUG,
"_DestroyXml : object NIL");
400 OBJdelTM(m, OBJTYPSXML, mlTab);
402 MMset(m, 0, ITOM(0));
405 MMechostr(MSKDEBUG,
"ok\n");
421 MMechostr(MSKDEBUG,
"_GetXmlRootNodes\n");
424 int ixml = MMpull(m);
431 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
434 MMechostr(MSKDEBUG,
"_GetXmlRootNodes : bad ObjSXML object.\n");
440 unsigned int count = 0;
451 sXmlDoc->AddNode(child);
453 if ((MMpushPointer(m, child) != 0))
456 if ((MMpushPointer(m, sXmlDoc) != 0))
468 for(
unsigned int j = 0; j < count; j++)
472 if ((k = MBdeftab(m)))
490 MMechostr(MSKDEBUG,
"_GetXmlNodeChilds\n");
493 int ixmlnode = MMpull(m);
503 MMechostr(MSKDEBUG,
"_GetXmlNodeChilds : bad ObjSXMLNode object.\n");
508 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
511 MMechostr(MSKDEBUG,
"_GetXmlNodeChilds : bad ObjSXMLNode object.\n");
517 unsigned int count = 0;
528 sXmlDoc->AddNode(child);
530 if ((MMpushPointer(m, child) != 0))
533 if ((MMpushPointer(m, sXmlDoc) != 0))
545 for(
unsigned int j = 0; j < count; j++)
567 MMechostr(MSKDEBUG,
"_GetXmlNodeValue\n");
570 int ixmlnode = MMpull(m);
580 MMechostr(MSKDEBUG,
"_GetXmlNodeValue : bad ObjSXMLNode object.\n");
585 Mpushstrbloc(m, (
char*)xmlNode->
Value());
601 MMechostr(MSKDEBUG,
"_SetXmlNodeValue\n");
604 int ivalue = MMpull(m);
605 int ixmlnode = MMget(m, 0);
606 if ((ixmlnode == NIL) || (ivalue == NIL))
611 std::string value = MMstartstr(m, MTOP(ivalue));
616 MMechostr(MSKDEBUG,
"_SetXmlNodeValue : bad ObjSXMLNode object.\n");
637 MMechostr(MSKDEBUG,
"_GetXmlNodeValue\n");
640 int ixmlnode = MMpull(m);
650 MMechostr(MSKDEBUG,
"_GetXmlNodeContent : bad ObjSXMLNode object.\n");
671 Mpushstrbloc(m, (
char*)xmlText->
Value());
687 MMechostr(MSKDEBUG,
"_SetXmlNodeContent\n");
690 int icontent = MMpull(m);
691 int ixmlnode = MMget(m, 0);
697 std::string content = (icontent == NIL) ?
"" : MMstartstr(m, MTOP(icontent));
702 MMechostr(MSKDEBUG,
"_SetXmlNodeContent : bad ObjSXMLNode object.\n");
725 xmlText = xmlContentNode->
ToText();
760 MMechostr(MSKDEBUG,
"_GetXmlNodeAttributes\n");
763 int ixmlnode = MMpull(m);
773 MMechostr(MSKDEBUG,
"_GetXmlNodeAttributes : bad ObjSXMLNode object.\n");
781 MMechostr(MSKDEBUG,
"_GetXmlNodeAttributes : Can not convert ObjXMLNode %s to Element.\n", xmlNode->
Value());
787 unsigned int count = 0;
791 for(xmlAttrib = xmlElem->
FirstAttribute(); xmlAttrib != 0; xmlAttrib = xmlAttrib->
Next(), count++)
793 Mpushstrbloc(m, (
char*)xmlAttrib->
Name());
794 Mpushstrbloc(m, (
char*)xmlAttrib->
Value());
803 for(
unsigned int j = 0; j < count; j++)
826 MMechostr(MSKDEBUG,
"_GetXmlNodeAttributes\n");
829 int iattributes = MMpull(m);
830 int ixmlnode = MMget(m, 0);
840 MMechostr(MSKDEBUG,
"_SetXmlNodeAttributes : bad ObjSXMLNode object.\n");
848 MMechostr(MSKDEBUG,
"_SetXmlNodeAttributes : Can not convert ObjXMLNode %s to Element.\n", xmlNode->
Value());
857 for(xmlAttrib = xmlElem->
FirstAttribute(); xmlAttrib != 0; xmlAttrib = xmlAttrib->
Next())
865 while (iattributes != NIL)
867 q = MMfetch(m, MTOP(iattributes), 0);
870 iname = MMfetch(m, MTOP(q), 0);
871 ivalue = MMfetch(m, MTOP(q), 1);
876 std::string sname = MMstartstr(m, MTOP(iname));
877 std::string svalue =
"";
879 svalue = MMstartstr(m, MTOP(ivalue));
884 iattributes = MMfetch(m, MTOP(iattributes), 1);
901 MMechostr(MSKDEBUG,
"_RemoveXmlNode\n");
904 int ixmlnode = MMpull(m);
914 MMechostr(MSKDEBUG,
"_RemoveXmlNode : bad ObjSXMLNode object.\n");
919 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
922 MMechostr(MSKDEBUG,
"_RemoveXmlNode : bad ObjSXMLNode object.\n");
927 sXmlDoc->RemoveNode(xmlNode);
929 MMstore(m, MTOP(ixmlnode), 0, 0);
930 MMstore(m, MTOP(ixmlnode), 1, 0);
948 MMechostr(MSKDEBUG,
"_AddXmlRootNode\n");
951 int ivalue = MMpull(m);
952 int ixml = MMpull(m);
953 if ((ixml == NIL) || (ivalue == NIL))
958 std::string value = MMstartstr(m, MTOP(ivalue));
960 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
963 MMechostr(MSKDEBUG,
"_AddXmlRootNode : bad ObjSXML object.\n");
970 sXmlDoc->AddNode(nXmlNode);
972 if ((MMpushPointer(m, nXmlNode) != 0))
975 if ((MMpushPointer(m, sXmlDoc) != 0))
978 int k = MMpush(m, 2 * 2);
996 MMechostr(MSKDEBUG,
"_AddXmlNode\n");
999 int ivalue = MMpull(m);
1000 int ixmlnode = MMpull(m);
1001 if ((ixmlnode == NIL) || (ivalue == NIL))
1006 std::string value = MMstartstr(m, MTOP(ivalue));
1011 MMechostr(MSKDEBUG,
"_AddXmlNode : bad ObjSXMLNode object.\n");
1016 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
1019 MMechostr(MSKDEBUG,
"_AddXmlNode : bad ObjSXMLNode object.\n");
1026 sXmlDoc->AddNode(nXmlNode);
1028 if ((MMpushPointer(m, nXmlNode) != 0))
1031 if ((MMpushPointer(m, sXmlDoc) != 0))
1034 int k = MMpush(m, 2 * 2);
1051 MMechostr(MSKDEBUG,
"_GetXmlContent\n");
1054 int ixml = MMpull(m);
1057 MMechostr(MSKDEBUG,
"_GetXmlContent : param nil.\n");
1062 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
1065 MMechostr(MSKDEBUG,
"_GetXmlContent : bad ObjSXML object.\n");
1070 std::string content = sXmlDoc->ToString();
1072 Mpushstrbloc(m, (
char*)content.c_str());
1078static NativeDefinition sxmlDef[] =
1080 {
"ObjSXML", TYPTYPE, NULL, NULL},
1081 {
"ObjSXMLNode", TYPTYPE, NULL, NULL},
1082 {
"_CreateXml", 1,
"fun [Chn] ObjSXML",
_CreateXml},
1083 {
"_OpenXml", 2,
"fun [Chn P] ObjSXML",
_OpenXml},
1084 {
"_OpenXmlS", 2,
"fun [Chn S] ObjSXML",
_OpenXmlS},
1085 {
"_SaveXml", 2,
"fun [ObjSXML W] I",
_SaveXml},
1086 {
"_DestroyXml", 1,
"fun [ObjSXML] I",
_DestroyXml},
1087 {
"_GetXmlRootNodes", 1,
"fun [ObjSXML] [ObjSXMLNode r1]",
_GetXmlRootNodes},
1088 {
"_GetXmlNodeChilds", 1,
"fun [ObjSXMLNode] [ObjSXMLNode r1]",
_GetXmlNodeChilds},
1090 {
"_SetXmlNodeValue", 2,
"fun [ObjSXMLNode S] ObjSXMLNode",
_SetXmlNodeValue},
1096 {
"_AddXmlRootNode", 2,
"fun [ObjSXML S] ObjSXMLNode",
_AddXmlRootNode},
1097 {
"_AddXmlNode", 2,
"fun [ObjSXMLNode S] ObjSXMLNode",
_AddXmlNode},
1104int InitSXML(mmachine m)
1106 OBJTYPSXML = OBJregister(0, 1, destroyXmlObj,
"OBJTYPSXML");
1107 return PKhardpak2(m,
"sXml.pkg-1.0",
sizeof(sxmlDef)/
sizeof(sxmlDef[0]), sxmlDef);
1114extern "C" SCOL_EXPORT
int ScolLoadPlugin(mmachine m, cbmachine w)
1116extern "C" SCOL_EXPORT
int ScolSxmlLoadPlugin(mmachine m, cbmachine w)
1128extern "C" SCOL_EXPORT
int ScolUnloadPlugin()
1130extern "C" SCOL_EXPORT
int ScolSxmlUnloadPlugin()
const char * Name() const
The name of the attribute.
const XMLAttribute * Next() const
The next attribute in the list.
const char * Value() const
The value of the attribute.
XMLError Parse(const char *xml, size_t nBytes=(size_t)(-1))
void Print(XMLPrinter *streamer=0)
XMLError LoadFile(const char *filename)
XMLElement * NewElement(const char *name)
XMLError SaveFile(const char *filename, bool compact=false)
void DeleteNode(XMLNode *node)
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
void DeleteAttribute(const char *name)
const char * Value() const
void SetValue(const char *val, bool staticMem=false)
virtual XMLText * ToText()
Safely cast to Text, or null.
void DeleteChild(XMLNode *node)
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
XMLNode * InsertFirstChild(XMLNode *addThis)
XMLNode * InsertEndChild(XMLNode *addThis)
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
const char * CStr() const
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
int _GetXmlRootNodes(mmachine m)
_GetXmlRootNodes : This function retrieve the root nodes of an ObjSXML object
int _AddXmlRootNode(mmachine m)
_AddXmlRootNode : This function adds a root ObjSXMLNode
int _GetXmlNodeContent(mmachine m)
_GetXmlNodeContent : This function retrieve the content of an ObjSXMLNode object
int _OpenXmlS(mmachine m)
_OpenXmlS : This function create an ObjSXML object from a string
int _CreateXml(mmachine m)
_CreateXml : This function create an empty ObjSXML object
int _GetXmlNodeAttributes(mmachine m)
_GetXmlNodeAttributes : This function retrieve the attrtibutes of an ObjSXMLNode object
int _DestroyXml(mmachine m)
_DestroyXml : This function destroy an ObjSXML object
int _AddXmlNode(mmachine m)
_AddXmlNode : This function add an ObjSXMLNode
int _SaveXml(mmachine m)
_SaveXml : This function save an ObjSXML object to a file
int _GetXmlContent(mmachine m)
_GetXmlContent : This function return an ObjSXML object content
int _GetXmlNodeChilds(mmachine m)
_GetXmlNodeChilds : This function retrieve the child nodes of an ObjSXMLNode object
int _OpenXml(mmachine m)
_OpenXml : This function create an ObjSXML object from a file
int _SetXmlNodeContent(mmachine m)
_SetXmlNodeContent : This function set the content of an ObjSXMLNode object
int _SetXmlNodeValue(mmachine m)
_SetXmlNodeValue : This function set the value of an ObjSXMLNode object
int _GetXmlNodeValue(mmachine m)
_GetXmlNodeValue : This function retrieve the value of an ObjSXMLNode object
int _RemoveXmlNode(mmachine m)
_RemoveXmlNode : This function remove an ObjSXMLNode
int _SetXmlNodeAttributes(mmachine m)
_SetXmlNodeAttributes : This function set the attrtibutes of an ObjSXMLNode object