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 __XMLELEMENT_HXX__
17 #define __XMLELEMENT_HXX__
18 
19 #include <string>
20 #include "dynlib_xml_scilab.h"
21 #include "XMLRemovable.hxx"
22 
23 #include "xml.h"
24 
25 namespace org_modules_xml
26 {
27 class XMLObject;
28 class XMLDocument;
29 class XMLNodeList;
30 class XMLNs;
31 class XMLAttr;
32 
33 /**
34  * @file
35  * @author Calixte DENIZET <calixte.denizet@scilab.org>
36  *
37  * Class to wrap a xmlNode
38  * @see http://xmlsoft.org/html/libxml-tree.html#xmlNode
39  */
40 class XML_SCILAB_IMPEXP XMLElement: public XMLObject, public XMLRemovable
41 {
42     bool allocated;
43     xmlNode *node;
44     const XMLDocument & doc;
45 
46 public:
47 
48     /**
49      * @param doc the owner document of this XMLElement
50      * @param node the xmlNode to wrap
51      */
52     XMLElement(const XMLDocument & doc, xmlNode * node);
53 
54     /**
55      * @param doc the owner document of this XMLElement
56      * @param name the name of the XMLElement
57      */
58     XMLElement(const XMLDocument & doc, const char *name);
59 
60     ~XMLElement();
61 
62     void *getRealXMLPointer() const;
63 
64     void remove() const;
65 
66     /**
67      * @return the node name
68      */
getNodeName(void) const69     const char *getNodeName(void) const
70     {
71         return node->name ? (const char *)node->name : "";
72     }
73 
74     /**
75      * Sets the node name
76      * @param name the node name
77      */
78     void setNodeName(const std::string & name) const;
79 
80     /**
81      * @return the namespace associated to this node
82      */
83     const XMLNs *getNodeNameSpace() const;
84 
85     /**
86      * Sets the namespace of this node
87      * @param ns the namespace
88      */
89     void setNodeNameSpace(const XMLNs & ns) const;
90 
91     /**
92      * Gets the node contents
93      * @see http://xmlsoft.org/html/libxml-tree.html#xmlNodeGetContent
94      * @return the node content
95      */
96     const char *getNodeContent() const;
97 
98     /**
99      * Sets the node content
100      * @param content the new node contents
101      */
102     void setNodeContent(const std::string & content) const;
103 
104     /**
105      * @return the node type
106      * @see http://xmlsoft.org/html/libxml-tree.html#xmlElementType
107      */
getNodeType(void) const108     int getNodeType(void) const
109     {
110         return node->type;
111     }
112 
113     /**
114      * @return the attributes of this node
115      */
116     const XMLAttr *getAttributes(void) const;
117 
118     /**
119      * Sets the attributes of this node
120      * @param attrs the new attributes
121      */
122     void setAttributes(const XMLAttr & attrs) const;
123 
124     /**
125      * @return the parent XMLElement
126      */
127     const XMLElement *getParentElement() const;
128 
129     /**
130      * @return a list of the children of this node
131      */
132     const XMLNodeList *getChildren() const;
133 
134     /**
135      * Replaces the children of this node by an XMLElement
136      * @param elem the new child
137      */
138     void setChildren(const XMLElement & elem) const;
139 
140     /**
141      * Replaces the children of this node by a list of nodes
142      * @param list the new children
143      */
144     void setChildren(const XMLNodeList & list) const;
145 
146     /**
147      * Replaces the children of this node by a new one given by a XML code
148      * @param xmlCode the XML code of the new child
149      */
150     void setChildren(const std::string & xmlCode) const;
151 
152     /**
153      * Adds a namespace to this node which can be used by the children
154      * @param ns the namespace to add
155      */
156     void addNamespace(const XMLNs & ns) const;
157 
158     /**
159      * Gets the namespace which has a given prefix. If it is not found in this
160      * node, then it will be searched in the parents.
161      * @see http://xmlsoft.org/html/libxml-tree.html#xmlSearchNs
162      * @param prefix the prefix
163      * @return the corresponding namespace or 0 if not found
164      */
165     const XMLNs *getNamespaceByPrefix(const char *prefix) const;
166 
167     /**
168      * Gets the namespace which has a given href. If it is not found in this
169      * node, then it will be searched in the parents.
170      * @see http://xmlsoft.org/html/libxml-tree.html#xmlSearchNsByHref
171      * @param href the href
172      * @return the corresponding namespace or 0 if not found
173      */
174     const XMLNs *getNamespaceByHref(const char *href) const;
175 
176     /**
177      * @return the libxml node behind this object
178      */
getRealNode() const179     xmlNode *getRealNode() const
180     {
181         return node;
182     }
183 
184     /**
185      * @return the XMLDocument which is the parent or this XMLElement
186      */
getXMLDocument() const187     const XMLDocument & getXMLDocument() const
188     {
189         return doc;
190     }
191 
192     /**
193      * @return the defintion line of this XMLElement
194      */
195     int getDefinitionLine() const;
196 
197     void setAttributeValue(const char **prefix, const char **name, const char **value, int size) const;
198     void setAttributeValue(const char **name, const char **value, int size) const;
199     void append(const XMLElement & elem) const;
200     const XMLObject *getXMLObjectParent() const;
201     const std::string dump(bool indent) const;
202     const std::string toString() const;
203 };
204 }
205 
206 #endif
207