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 #include <string>
17 
18 #include "XMLObject.hxx"
19 #include "XMLElement.hxx"
20 #include "XMLDocument.hxx"
21 #include "XMLAttr.hxx"
22 #include "XMLNs.hxx"
23 #include "XMLNotHandledElement.hxx"
24 #include "XMLNodeSet.hxx"
25 #include "VariableScope.hxx"
26 
27 namespace org_modules_xml
28 {
29 
XMLNodeSet(const XMLDocument & _doc,xmlXPathObject * _xpath)30 XMLNodeSet::XMLNodeSet(const XMLDocument & _doc, xmlXPathObject * _xpath): XMLList(), doc(_doc), xpath(_xpath)
31 {
32     nodeSet = xpath->nodesetval;
33     if (nodeSet)
34     {
35         scope->registerPointers(nodeSet, this);
36         size = nodeSet->nodeNr;
37     }
38     else
39     {
40         size = 0;
41     }
42     scilabType = XMLSET;
43     id = scope->getVariableId(*this);
44 }
45 
~XMLNodeSet()46 XMLNodeSet::~XMLNodeSet()
47 {
48     scope->unregisterPointer(nodeSet);
49     scope->removeId(id);
50     xmlXPathFreeObject(xpath);
51 }
52 
getRealXMLPointer() const53 void *XMLNodeSet::getRealXMLPointer() const
54 {
55     return static_cast < void *>(nodeSet);
56 }
57 
getContentFromList() const58 const char **XMLNodeSet::getContentFromList() const
59 {
60     const char **list = new const char *[size];
61     for (int i = 0; i < size; i++)
62     {
63         list[i] = (const char *)xmlNodeGetContent(nodeSet->nodeTab[i]);
64     }
65 
66     return list;
67 }
68 
getNameFromList() const69 const char **XMLNodeSet::getNameFromList() const
70 {
71     const char **list = new const char *[size];
72     for (int i = 0; i < size; i++)
73     {
74         list[i] = nodeSet->nodeTab[i]->name ? (const char *)nodeSet->nodeTab[i]->name : "";
75     }
76 
77     return list;
78 }
79 
setAttributeValue(const char ** prefix,const char ** name,const char ** value,int lsize) const80 void XMLNodeSet::setAttributeValue(const char **prefix, const char **name, const char **value, int lsize) const
81 {
82     for (int i = 0; i < size; i++)
83     {
84         XMLAttr::setAttributeValue(nodeSet->nodeTab[i], prefix, name, value, lsize);
85     }
86 }
87 
setAttributeValue(const char ** name,const char ** value,int lsize) const88 void XMLNodeSet::setAttributeValue(const char **name, const char **value, int lsize) const
89 {
90     for (int i = 0; i < size; i++)
91     {
92         XMLAttr::setAttributeValue(nodeSet->nodeTab[i], name, value, lsize);
93     }
94 }
95 
remove() const96 void XMLNodeSet::remove() const
97 {
98     for (int i = 0; i < size; i++)
99     {
100         xmlNode *node = nodeSet->nodeTab[i];
101         xmlUnlinkNode(node);
102         xmlFreeNode(node);
103     }
104 }
105 
getXMLObjectParent() const106 const XMLObject *XMLNodeSet::getXMLObjectParent() const
107 {
108     return &doc;
109 }
110 
getListElement(int index)111 const XMLObject *XMLNodeSet::getListElement(int index)
112 {
113     if (nodeSet && index >= 1 && index <= size)
114     {
115         XMLObject *obj = 0;
116         xmlNode *node = nodeSet->nodeTab[index - 1];
117 
118         switch (node->type)
119         {
120             case XML_ELEMENT_NODE:
121             case XML_TEXT_NODE:
122             case XML_CDATA_SECTION_NODE:
123             case XML_COMMENT_NODE:
124             case XML_ATTRIBUTE_NODE:
125                 obj = scope->getXMLObjectFromLibXMLPtr(node);
126                 if (obj)
127                 {
128                     return static_cast < XMLElement * >(obj);
129                 }
130 
131                 return new XMLElement(doc, node);
132             case XML_NAMESPACE_DECL:
133                 obj = scope->getXMLObjectFromLibXMLPtr(node);
134                 if (obj)
135                 {
136                     return static_cast < XMLNs * >(obj);
137                 }
138 
139                 return new XMLNs(doc, (xmlNs *) node);
140             case XML_ELEMENT_DECL:
141             case XML_ATTRIBUTE_DECL:
142             case XML_ENTITY_DECL:
143             case XML_XINCLUDE_START:
144             case XML_XINCLUDE_END:
145             case XML_DOCUMENT_NODE:
146                 obj = scope->getXMLObjectFromLibXMLPtr(node);
147                 if (obj)
148                 {
149                     return static_cast < XMLNotHandledElement * >(obj);
150                 }
151 
152                 return new XMLNotHandledElement(doc, node);
153             default:
154                 break;
155         }
156     }
157 
158     return 0;
159 }
160 }
161