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