1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #ifndef __XMLOBJECTS_HXX__
17 #define __XMLOBJECTS_HXX__
18 
19 #include <iostream>
20 #include <string>
21 #include <sstream>
22 #include <typeinfo>
23 #include <set>
24 
25 extern "C"
26 {
27 #include "xml_mlist.h"
28 #include "dynlib_xml_scilab.h"
29 }
30 
31 //#define SCILAB_DEBUG_XML
32 
33 namespace org_modules_xml
34 {
35 class VariableScope;
36 
37 /**
38  * @file
39  * @author Calixte DENIZET <calixte.denizet@scilab.org>
40  *
41  * Base class for the XML objects.
42  */
43 class XML_SCILAB_IMPEXP XMLObject
44 {
45 
46 public:
47 
48 #ifdef SCILAB_DEBUG_XML
49     static std::set<XMLObject *> pointers;
50 #endif
51 
52     /**
53      * Default constructor
54      */
55     XMLObject();
56 
57     /**
58      * Destructor
59      */
~XMLObject()60     virtual ~ XMLObject()
61     {
62 #ifdef SCILAB_DEBUG_XML
63         //std::cout << "Delete = " << (void*)this << std::endl;
64         pointers.erase(this);
65 #endif
66     }
67 
68     /**
69      * Get the libxml2 pointer
70      * @return the pointer
71      */
getRealXMLPointer() const72     virtual void *getRealXMLPointer() const
73     {
74         return 0;
75     }
76 
77     /**
78      * Gets a XML parent object. A set of dependencies is created between the objects
79      * to be sure that all the XML objects will be freed when a document will be destroyed.
80      * @return the parent XMLObject
81      */
getXMLObjectParent() const82     virtual const XMLObject *getXMLObjectParent() const
83     {
84         return 0;
85     }
86 
87     /**
88      * Sets the attribute value.
89      * @param name the attribute names
90      * @param value the attribute values
91      * @param size the number of names
92      */
setAttributeValue(const char ** name,const char ** value,int size) const93     virtual void setAttributeValue(const char **name, const char **value, int size) const
94     {
95         return;
96     }
97 
98     /**
99      * Sets the attribute value with a prefix namespace.
100      * @param prefix the namespace prefix or the namespace itself
101      * @param name the attribute names
102      * @param value the attribute values
103      * @param size the number of names
104      */
setAttributeValue(const char ** prefix,const char ** name,const char ** value,int size) const105     virtual void setAttributeValue(const char **prefix, const char **name, const char **value, int size) const
106     {
107         return;
108     }
109 
110     /**
111      * @return the string representation of this object
112      */
toString() const113     virtual const std::string toString() const
114     {
115         return std::string("");
116     }
117 
118     /**
119      * @return a dump of this object
120      */
dump(bool indent) const121     virtual const std::string dump(bool indent) const
122     {
123         return std::string("");
124     }
125 
126     /**
127      * @return the object id
128      */
getId() const129     inline int getId() const
130     {
131         return id;
132     }
133 
isValid() const134     inline bool isValid() const
135     {
136         return valid;
137     }
138 
invalid()139     inline void invalid()
140     {
141         valid = false;
142     }
143 
144     /**
145      * Creates the Scilab's variable corresponding to this object
146      * @param pos the stack position
147      * @return 1 if all is ok, else 0
148      */
149     int createOnStack(int pos, void *pvApiCtx) const;
150 
151     /**
152      * @param id the object id
153      * @return the object which has the corresponding id or 0 if not found
154      */
getFromId(int id)155     template < class T > static T *getFromId(int id)
156     {
157         return static_cast < T * >(getVariableFromId(id));
158     }
159 
160 protected:
161     int id;
162     int scilabType;
163     bool valid;
164 
165     static VariableScope *scope;
166 
167     /**
168      * Reset the scope
169      */
170     static void resetScope();
171 
172 private:
173 
174     /**
175      * @param id the id
176      * @return XMLObject corresponding to the id or 0 if not found
177      */
178     static XMLObject *getVariableFromId(int id);
179 };
180 }
181 
182 #endif
183