-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDOMReaderImplL3.cc
296 lines (268 loc) · 6.84 KB
/
DOMReaderImplL3.cc
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* vim:set ft=cpp:syn on */
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/framework/StdInInputSource.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>
#include "Error.h"
#include "readstream.h"
#include "UnicodeTools.h"
#include "DOMReaderImplL3.h"
#define XN XERCES_CPP_NAMESPACE_QUALIFIER
// Inkrement der Puffergröße beim Auslesen einer Datei
#define FREAD_BUFFER_SIZE 2048
#define ERROR_MAXCHARS 1024
namespace flux {
namespace xml {
DOMReaderImplL3::DOMReaderImplL3()
: DOM_doc_(0)
{
static const XMLCh utf_LS[] = { XN chLatin_L, XN chLatin_S, XN chNull };
impl_ = XN DOMImplementationRegistry::getDOMImplementation(utf_LS);
parser_ = ((XN DOMImplementationLS*)impl_)->createLSParser(
XN DOMImplementationLS::MODE_SYNCHRONOUS, 0
);
XN DOMConfiguration * config = parser_->getDomConfig();
config->setParameter(XN XMLUni::fgDOMNamespaces, true);
config->setParameter(XN XMLUni::fgDOMValidate, true);
config->setParameter(XN XMLUni::fgXercesSchema, true);
// config->setParameter(XN XMLUni::fgXercesSchemaFullChecking, false);
config->setParameter(XN XMLUni::fgDOMValidateIfSchema, true);
config->setParameter(XN XMLUni::fgDOMDatatypeNormalization, true);
config->setParameter(XN XMLUni::fgDOMErrorHandler, &dom_err_handler_);
config->setParameter(XN XMLUni::fgDOMResourceResolver, &entity_resolver_);
}
DOMReaderImplL3::DOMReaderImplL3(
XN DOMImplementation * impl
) : impl_(impl), DOM_doc_(0)
{
parser_ = ((XN DOMImplementationLS*)impl_)->createLSParser(
XN DOMImplementationLS::MODE_SYNCHRONOUS, 0
);
XN DOMConfiguration * config = parser_->getDomConfig();
config->setParameter(XN XMLUni::fgDOMNamespaces, true);
config->setParameter(XN XMLUni::fgDOMValidate, true);
config->setParameter(XN XMLUni::fgXercesSchema, true);
// config->setParameter(XN XMLUni::fgXercesSchemaFullChecking, false);
config->setParameter(XN XMLUni::fgDOMValidateIfSchema, true);
config->setParameter(XN XMLUni::fgDOMDatatypeNormalization, true);
config->setParameter(XN XMLUni::fgDOMErrorHandler, &dom_err_handler_);
config->setParameter(XN XMLUni::fgDOMResourceResolver, &entity_resolver_);
}
DOMReaderImplL3::~DOMReaderImplL3()
{
// den Parser und den DOM-Tree löschen
parser_->release();
}
void DOMReaderImplL3::parseFromURI(
char const * uri
)
{
parser_->resetDocumentPool();
try
{
// die XML-Datei parsen ...
DOM_doc_ = parser_->parseURI(uri);
}
catch (XN XMLException const & e)
{
U2A u2a(e.getMessage());
fTHROW(XMLException,"%s", (char const *)u2a);
}
catch (XN DOMException const & e)
{
XMLCh errText[2048];
errText[0] = XN chNull;
XN DOMImplementation::loadDOMExceptionMsg(e.code, errText, 2047);
U2A u2a(errText);
fTHROW(XMLException,"%s", (char const *)u2a);
}
catch (...)
{
fTHROW(XMLException,"unexpected exception during parsing");
}
if (DOM_doc_ == 0)
fTHROW(XMLException,"error downloading XML from URI [%s]", uri);
if (DOM_doc_->getDocumentElement() == 0)
fTHROW(XMLException,"downloaded XML not well-formed?!");
}
void DOMReaderImplL3::parseFromMemory(
unsigned char const * buf,
size_t len
)
{
static const char * fake_sys_id = "fake";
try
{
XN Wrapper4InputSource is(
new XN MemBufInputSource(
(const XMLByte *)buf,
len,
fake_sys_id,
false
)
);
parseFromInputSourceWrapper(is);
}
catch (XN XMLException const & e)
{
U2A asc_msg(e.getMessage());
fTHROW(XMLException,"%s", (char const*)asc_msg);
}
catch (...)
{
fTHROW(XMLException,"unexpected exception during parsing");
}
}
void DOMReaderImplL3::parseFromStream(
FILE * in_file
)
{
char * buffer = 0;
size_t buffer_length = 0;
if (in_file == 0)
fTHROW(XMLException,"0-pointer is not a valid file input stream!");
buffer = readstream(in_file, buffer_length);
if (buffer == 0)
fTHROW(XMLException,"error reading file descriptor");
if (buffer_length == 0)
{
delete[] buffer;
fTHROW(XMLException,"empty file");
}
// DOMInputSource erzeugen und den XML-Parser anwerfen
try
{
XN Wrapper4InputSource is(
new XN MemBufInputSource(
reinterpret_cast< const XMLByte *>(buffer),
buffer_length,
"fake", false
)
);
parseFromInputSourceWrapper(is);
}
catch (XN XMLException const & e)
{
delete[] buffer;
U2A asc_msg(e.getMessage());
fTHROW(XMLException,"%s", (char const*)asc_msg);
}
catch (XMLException const & e)
{
throw e;
}
catch (...)
{
delete[] buffer;
fTHROW(XMLException,"unexpected exception during parsing");
}
delete[] buffer;
}
void DOMReaderImplL3::parseFromFile(
char const * file_name
)
{
A2U utf_file_name(file_name);
try
{
// die Wrapper4InputSource-Klasse möchte das übergebene InputSource-
// Objekt selbst löschen!
XN Wrapper4InputSource is(new XN LocalFileInputSource(utf_file_name));
parseFromInputSourceWrapper(is);
}
catch (XN XMLException const & e)
{
U2A asc_msg(e.getMessage());
fTHROW(XMLException,"%s", (char const*)asc_msg);
}
catch (XMLException & e)
{
throw e;
}
catch (...)
{
fTHROW(XMLException,"unexpected exception during parsing");
}
}
void DOMReaderImplL3::parseFromStdIn()
{
try
{
// die Wrapper4InputSource-Klasse möchte das übergebene InputSource-
// Objekt selbst löschen!
XN Wrapper4InputSource is(new XN StdInInputSource());
parseFromInputSourceWrapper(is);
}
catch (XN XMLException const & e)
{
U2A asc_msg(e.getMessage());
fTHROW(XMLException,"%s", (char const*)asc_msg);
}
catch (XMLException const & e)
{
throw e;
}
catch (...)
{
fTHROW(XMLException,"unexpected exception during parsing");
}
}
bool DOMReaderImplL3::mapEntity(
char const * from,
char const * to
)
{
return entity_resolver_.map(from,to);
}
bool DOMReaderImplL3::mapEntity(
XMLCh const * from,
XMLCh const * to
)
{
return entity_resolver_.map(from,to);
}
void DOMReaderImplL3::parseFromInputSourceWrapper(
XN Wrapper4InputSource & is
)
{
parser_->resetDocumentPool();
try
{
// die XML-Datei parsen ...
DOM_doc_ = parser_->parse(&is);
}
catch (XN XMLException const & e)
{
U2A u2a(e.getMessage());
fTHROW(XMLException,"%s", (char const *)u2a);
}
catch (XN DOMException const & e)
{
XMLCh errText[2048];
errText[0] = XN chNull;
XN DOMImplementation::loadDOMExceptionMsg(e.code, errText, 2047);
U2A u2a(errText);
fTHROW(XMLException,"%s", (char const *)u2a);
}
catch (...)
{
fTHROW(XMLException,"unexpected exception during parsing");
}
if (DOM_doc_ == 0)
fTHROW(XMLException,"error with XML input source");
}
void DOMReaderImplL3::setResolveXInclude(bool tf)
{
f_resolve_xinclude_ = tf;
if (f_resolve_xinclude_)
{
XN DOMConfiguration * config = parser_->getDomConfig();
if(config->canSetParameter(XN XMLUni::fgXercesDoXInclude, true))
config->setParameter(XN XMLUni::fgXercesDoXInclude, true);
else
f_resolve_xinclude_ = false;
}
}
} // namespace flux::xml
} // namespace flux