2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #ifndef __INSERTION_HPP__
14 #define __INSERTION_HPP__
16 #include "XMLRhsValue.hxx"
23 #include "api_scilab.h"
24 #include "xml_mlist.h"
25 #include "localization.h"
28 using namespace org_modules_xml;
30 /*--------------------------------------------------------------------------*/
31 #define __XML_CHECK_TYPE__(TYPEIN,REQUIREDTYPE,FIELD) if (typeid(TYPEIN) != typeid(REQUIREDTYPE)) \
33 Scierror(999, gettext("%s: Wrong type to set %s field.\n"), fname, FIELD); \
36 /*--------------------------------------------------------------------------*/
39 * Sets the properties of a XMLDocument
40 * A value will have the type T
41 * @param fname function name
42 * @param doc the XMLDocument
43 * @param field te field name
44 * @param value the new value
47 bool setProperty(char * fname, org_modules_xml::XMLDocument & doc, const char * field, T & value)
49 if (!strcmp("root", field))
51 if (typeid(T &) != typeid(XMLElement &) && typeid(T &) != typeid(std::string &))
53 Scierror(999, gettext("%s: Wrong type to set %s field.\n"), fname, "root");
56 if (typeid(T &) == typeid(XMLElement &))
58 doc.setRoot((XMLElement &)value);
63 doc.setRoot((std::string &)value, &error);
66 Scierror(999, gettext("%s: Not valid xml for root.\n"), fname);
71 else if (!strcmp("url", field))
73 __XML_CHECK_TYPE__(T &, std::string &, "url");
74 doc.setDocumentURL((std::string &)value);
78 Scierror(999, gettext("%s: Unknown field: %s\n"), fname, field);
84 /*--------------------------------------------------------------------------*/
87 * Sets the properties of a XMLElement
88 * A value will have the type T
89 * @param fname function name
90 * @param elem the XMLElement
91 * @param field te field name
92 * @param value the new value
95 bool setProperty(char * fname, XMLElement & elem, const char * field, T & value)
97 if (!strcmp("name", field))
99 __XML_CHECK_TYPE__(T &, std::string &, "name");
100 elem.setNodeName((std::string &)value);
102 else if (!strcmp("namespace", field))
104 __XML_CHECK_TYPE__(T &, XMLNs &, "namespace");
105 elem.setNodeNameSpace((XMLNs &)value);
107 else if (!strcmp("content", field))
109 __XML_CHECK_TYPE__(T &, std::string &, "content");
110 elem.setNodeContent((std::string &)value);
112 else if (!strcmp("type", field))
114 Scierror(999, gettext("%s: Field %s is not modifiable: %s\n"), fname, "type");
117 else if (!strcmp("parent", field))
119 Scierror(999, gettext("%s: Field %s is not modifiable: %s\n"), fname, "parent");
122 else if (!strcmp("attributes", field))
124 __XML_CHECK_TYPE__(T &, XMLAttr &, "attributes");
125 elem.setAttributes((XMLAttr &)value);
127 else if (!strcmp("children", field))
129 if (typeid(T &) != typeid(XMLElement &) && typeid(T &) != typeid(XMLNodeList &) && typeid(T &) != typeid(std::string &))
131 Scierror(999, gettext("%s: Wrong type to set %s field.\n"), fname, "children");
134 if (typeid(T &) == typeid(XMLElement &))
136 elem.setChildren((XMLElement &)value);
138 else if (typeid(T &) == typeid(XMLNodeList &))
140 elem.setChildren((XMLNodeList &)value);
144 elem.setChildren((std::string &)value);
149 Scierror(999, gettext("%s: Unknown field: %s\n"), fname, field);
155 /*--------------------------------------------------------------------------*/
158 * Function to handle insertion in different XMLObjects
159 * @param fname the function name
160 * @param fname_len the function name length
162 template<class T, class U>
163 int sci_insertion(char * fname, void* pvApiCtx)
178 err = getVarAddressFromPosition(pvApiCtx, 1, &fieldaddr);
182 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
186 if (!isStringType(pvApiCtx, fieldaddr))
188 Scierror(999, gettext("%s: Wrong type for input argument #%i: A string expected.\n"), fname, 1);
192 err = getVarAddressFromPosition(pvApiCtx, 2, &rhsaddr);
196 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
200 err = getVarAddressFromPosition(pvApiCtx, 3, &lhsaddr);
204 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
208 if (getAllocatedSingleString(pvApiCtx, fieldaddr, &field) != 0)
210 Scierror(999, _("%s: No more memory.\n"), fname);
213 lhsid = getXMLObjectId(lhsaddr, pvApiCtx);
215 a = XMLObject::getFromId<T>(lhsid);
218 freeAllocatedSingleString(field);
219 Scierror(999, gettext("%s: XML object does not exist.\n"), fname);
223 success = XMLRhsValue::get(fname, rhsaddr, &b, pvApiCtx);
226 freeAllocatedSingleString(field);
227 Scierror(999, gettext("%s: Error in getting rhs argument.\n"), fname);
231 success = setProperty<U>(fname, *a, const_cast<char *>(field), *b);
232 freeAllocatedSingleString(field);
234 if (typeid(U) == typeid(std::string))
239 if (a->createOnStack(Rhs + 1, pvApiCtx))