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 __XMLDOCUMENT_HXX__
17 #define __XMLDOCUMENT_HXX__
18 
19 #include <cstdio>
20 #include <list>
21 #include <cstring>
22 #include <string>
23 
24 #include "dynlib_xml_scilab.h"
25 
26 extern "C"
27 {
28 #include "xml.h"
29 #ifndef XML_XPATH_CHECKNS
30 #define XML_XPATH_CHECKNS
31 #endif
32 }
33 
34 #include "XMLObject.hxx"
35 
36 namespace org_modules_xml
37 {
38 class XMLElement;
39 class XMLObject;
40 class XMLXPath;
41 class XMLValidation;
42 
43 /**
44  * @file
45  * @author Calixte DENIZET <calixte.denizet@scilab.org>
46  *
47  * Class to wrap a xmlDoc
48  * @see http://xmlsoft.org/html/libxml-tree.html#xmlDoc
49  */
50 class XML_SCILAB_IMPEXP XMLDocument: public XMLObject
51 {
52     static std::list < XMLDocument * > openDocs;
53     xmlDoc *document;
54 
55 public:
56     /**
57      * Gets the list of open docs
58      * @return the list
59      */
60     static const std::list < XMLDocument * >&getOpenDocuments();
61 
62     /**
63      * Closes all the open documents
64      */
65     static void closeAllDocuments();
66 
67     /**
68      * Builds a document with a given path (can be an url)
69      * @param path the document path
70      * @param validate a boolean to indicate if the document must be validated in using a DTD
71      * @param error a pointer to a string which will receive the error message
72      */
73     XMLDocument(const char *path, bool validate, std::string * error, const char * encoding = 0, const bool html = false);
74 
75     /**
76      * Builds a document with a given code
77      * @param xmlCode the XML code
78      * @param validate a boolean to indicate if the document must be validated in using a DTD
79      * @param error a pointer to a string which will receive the error message
80      */
81     XMLDocument(const std::string & xmlCode, bool validate, std::string * error, const char * encoding = 0, const bool html = false);
82 
83     /**
84      * Builds a simple document
85      * @param uri the document uri
86      * @param version the xml version
87      */
88     XMLDocument(char *uri, char *version);
89 
90     ~XMLDocument();
91 
92     void *getRealXMLPointer() const;
93 
94     /**
95      * @return the xmlDoc behind this XMLDocument
96      */
getRealDocument() const97     xmlDoc *getRealDocument() const
98     {
99         return document;
100     }
101 
102     /**
103      * @param filename the file where to write xml
104      * @param indent if true, the xml is indented
105      * @return true if all is ok
106      */
107     bool saveToFile(const std::string & filename, const bool indent) const;
108 
109     /**
110      * @param filename the file where to write xml
111      * @param indent if true, the xml is indented
112      * @return true if all is ok
113      */
114     bool saveToHTMLFile(const std::string & filename, const bool indent) const;
115 
116     /**
117      * @return the document root
118      */
119     const XMLElement *getRoot() const;
120 
121     /**
122      * @param value the root to set
123      */
124     void setRoot(const XMLElement & value) const;
125 
126     /**
127      * Replaces the root element by the root of the xmlCode/
128      * @param xmlCode the XML code
129      * @param error a pointer to a string which will receive the error message
130      */
131     void setRoot(const std::string & xmlCode, std::string * error) const;
132 
133     /**
134      * @return the document URL
135      */
136     const char *getDocumentURL() const;
137 
138     /**
139      * @param value the document URL to set
140      */
141     void setDocumentURL(const std::string & value) const;
142 
143     /**
144      * Makes an XPath query on the document
145      * @param query the XPath query
146      * @param namespaces an a matrix nx2 containing mapping between prefix and href
147      * @param length the number of namespaces
148      * @param the node from where start the query
149      * @param error a pointer to a string which will receive the error message
150      * @return a pointer on a XPath object
151      */
152     const XMLXPath *makeXPathQuery(const char *query, char **namespaces, int length, const XMLElement * e, std::string * error);
153 
154     const XMLObject *getXMLObjectParent() const;
155     const std::string dump(bool indent) const;
156     const std::string dumpHTML(bool indent) const;
157     const std::string toString() const;
158 
159 private:
160     /**
161      * Error function for the XML parser
162      * @see http://xmlsoft.org/html/libxml-xmlerror.html#xmlGenericErrorFunc
163      */
164     static void errorFunction(void *ctx, const char *msg, ...);
165 
166     /**
167      * Error function which does nothing for the XML parser
168      * @see http://xmlsoft.org/html/libxml-xmlerror.html#xmlGenericErrorFunc
169      */
errorFunctionWithoutOutput(void * ctx,const char * msg,...)170     static void errorFunctionWithoutOutput(void *ctx, const char *msg, ...)
171     {
172     }
173 
174     /**
175      * Error function used when the XPath query is compiled/
176      * @see http://xmlsoft.org/html/libxml-xmlerror.html#xmlStructuredErrorFunc
177      */
178     static void errorXPathFunction(void *ctx, xmlError * error);
179 
180     /**
181      * Reads and parses a document given in a file.
182      * @param filename the file name
183      * @param encoding the file encoding
184      * @param validate a boolean to indicate if the document must be validated in using a DTD
185      * @param error a string where to write the parsing errors
186      * @return a pointer on a xmlDoc
187      */
188     static xmlDoc *readDocument(const char *filename, const char * encoding, bool validate, std::string * error);
189 
190     /**
191      * Read and parse a document given in a string.
192      * @param xmlCode the XML code
193      * @param validate a boolean to indicate if the document must be validated in using a DTD
194      * @param error a string where to write the parsing errors
195      * @return a pointer on a xmlDoc
196      */
197     static xmlDoc *readDocument(const std::string & xmlCode, const char * encoding, bool validate, std::string * error);
198 
199     /**
200      * Reads and parses a document given in a file.
201      * @param filename the file name
202      * @param validate a boolean to indicate if the document must be validated in using a DTD
203      * @param error a string where to write the parsing errors
204      * @return a pointer on a xmlDoc
205      */
206     static xmlDoc *readHTMLDocument(const char *filename, const char * encoding, std::string * error);
207 
208     /**
209      * Read and parse a document given in a string.
210      * @param xmlCode the XML code
211      * @param validate a boolean to indicate if the document must be validated in using a DTD
212      * @param error a string where to write the parsing errors
213      * @return a pointer on a xmlDoc
214      */
215     static xmlDoc *readHTMLDocument(const std::string & xmlCode, const char * encoding, std::string * error);
216 
217     /**
218      * Initializes the context
219      * @param error a string where to write the parsing errors
220      * @param validate a boolean to indicate if the document must be validated in using a DTD
221      * @return a pointer on a context
222      */
223     static xmlParserCtxt *initContext(std::string * error, bool validate);
224 
225     /**
226      * Initializes the context
227      * @param error a string where to write the parsing errors
228      * @param validate a boolean to indicate if the document must be validated in using a DTD
229      * @return a pointer on a context
230      */
231     static htmlParserCtxt *initHTMLContext(std::string * error);
232 
233     static std::string errorBuffer;
234     static std::string errorXPathBuffer;
235 };
236 }
237 
238 #endif
239