1 | // file : xsd/cxx/xml/dom/parsing-source.txx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #if _XERCES_VERSION >= 30000 |
---|
7 | # include <xercesc/dom/DOMLSParser.hpp> |
---|
8 | #else |
---|
9 | # include <xercesc/dom/DOMBuilder.hpp> |
---|
10 | #endif |
---|
11 | #include <xercesc/dom/DOMNamedNodeMap.hpp> |
---|
12 | #include <xercesc/dom/DOMImplementation.hpp> |
---|
13 | #include <xercesc/dom/DOMImplementationRegistry.hpp> |
---|
14 | |
---|
15 | #include <xercesc/util/XMLUni.hpp> // xercesc::fg* |
---|
16 | #include <xercesc/util/XMLUniDefs.hpp> // chLatin_L, etc |
---|
17 | |
---|
18 | #include <xercesc/framework/Wrapper4InputSource.hpp> |
---|
19 | |
---|
20 | #include <xsd/cxx/xml/string.hxx> |
---|
21 | #include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx> |
---|
22 | |
---|
23 | namespace xsd |
---|
24 | { |
---|
25 | namespace cxx |
---|
26 | { |
---|
27 | namespace xml |
---|
28 | { |
---|
29 | namespace dom |
---|
30 | { |
---|
31 | // parser |
---|
32 | // |
---|
33 | template <typename C> |
---|
34 | parser<C>:: |
---|
35 | parser (const xercesc::DOMElement& e) |
---|
36 | : element_ (e), |
---|
37 | next_element_ (e.getFirstChild ()), |
---|
38 | a_ (e.getAttributes ()), ai_ (0) |
---|
39 | { |
---|
40 | using xercesc::DOMNode; |
---|
41 | |
---|
42 | for (; next_element_ != 0 && |
---|
43 | next_element_->getNodeType () != DOMNode::ELEMENT_NODE; |
---|
44 | next_element_ = next_element_->getNextSibling ()); |
---|
45 | } |
---|
46 | |
---|
47 | template <typename C> |
---|
48 | void parser<C>:: |
---|
49 | next_element () |
---|
50 | { |
---|
51 | using xercesc::DOMNode; |
---|
52 | |
---|
53 | for (next_element_ = next_element_->getNextSibling (); |
---|
54 | next_element_ != 0 && |
---|
55 | next_element_->getNodeType () != DOMNode::ELEMENT_NODE; |
---|
56 | next_element_ = next_element_->getNextSibling ()); |
---|
57 | } |
---|
58 | |
---|
59 | // parse() |
---|
60 | // |
---|
61 | template <typename C> |
---|
62 | xml::dom::auto_ptr<xercesc::DOMDocument> |
---|
63 | parse (xercesc::InputSource& is, |
---|
64 | error_handler<C>& eh, |
---|
65 | const properties<C>& prop, |
---|
66 | unsigned long flags) |
---|
67 | { |
---|
68 | bits::error_handler_proxy<C> ehp (eh); |
---|
69 | return xml::dom::parse (is, ehp, prop, flags); |
---|
70 | } |
---|
71 | |
---|
72 | template <typename C> |
---|
73 | auto_ptr<xercesc::DOMDocument> |
---|
74 | parse (xercesc::InputSource& is, |
---|
75 | xercesc::DOMErrorHandler& eh, |
---|
76 | const properties<C>& prop, |
---|
77 | unsigned long flags) |
---|
78 | { |
---|
79 | // HP aCC cannot handle using namespace xercesc; |
---|
80 | // |
---|
81 | using xercesc::DOMImplementationRegistry; |
---|
82 | using xercesc::DOMImplementationLS; |
---|
83 | using xercesc::DOMImplementation; |
---|
84 | using xercesc::DOMDocument; |
---|
85 | #if _XERCES_VERSION >= 30000 |
---|
86 | using xercesc::DOMLSParser; |
---|
87 | using xercesc::DOMConfiguration; |
---|
88 | #else |
---|
89 | using xercesc::DOMBuilder; |
---|
90 | #endif |
---|
91 | |
---|
92 | using xercesc::Wrapper4InputSource; |
---|
93 | using xercesc::XMLUni; |
---|
94 | |
---|
95 | |
---|
96 | // Instantiate the DOM parser. |
---|
97 | // |
---|
98 | const XMLCh ls_id[] = {xercesc::chLatin_L, |
---|
99 | xercesc::chLatin_S, |
---|
100 | xercesc::chNull}; |
---|
101 | |
---|
102 | // Get an implementation of the Load-Store (LS) interface. |
---|
103 | // |
---|
104 | DOMImplementation* impl ( |
---|
105 | DOMImplementationRegistry::getDOMImplementation (ls_id)); |
---|
106 | |
---|
107 | #if _XERCES_VERSION >= 30000 |
---|
108 | auto_ptr<DOMLSParser> parser ( |
---|
109 | impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); |
---|
110 | |
---|
111 | DOMConfiguration* conf (parser->getDomConfig ()); |
---|
112 | |
---|
113 | // Discard comment nodes in the document. |
---|
114 | // |
---|
115 | conf->setParameter (XMLUni::fgDOMComments, false); |
---|
116 | |
---|
117 | // Enable datatype normalization. |
---|
118 | // |
---|
119 | conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); |
---|
120 | |
---|
121 | // Do not create EntityReference nodes in the DOM tree. No |
---|
122 | // EntityReference nodes will be created, only the nodes |
---|
123 | // corresponding to their fully expanded substitution text |
---|
124 | // will be created. |
---|
125 | // |
---|
126 | conf->setParameter (XMLUni::fgDOMEntities, false); |
---|
127 | |
---|
128 | // Perform namespace processing. |
---|
129 | // |
---|
130 | conf->setParameter (XMLUni::fgDOMNamespaces, true); |
---|
131 | |
---|
132 | // Do not include ignorable whitespace in the DOM tree. |
---|
133 | // |
---|
134 | conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); |
---|
135 | |
---|
136 | if (flags & dont_validate) |
---|
137 | { |
---|
138 | conf->setParameter (XMLUni::fgDOMValidate, false); |
---|
139 | conf->setParameter (XMLUni::fgXercesSchema, false); |
---|
140 | conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | conf->setParameter (XMLUni::fgDOMValidate, true); |
---|
145 | conf->setParameter (XMLUni::fgXercesSchema, true); |
---|
146 | |
---|
147 | // This feature checks the schema grammar for additional |
---|
148 | // errors. We most likely do not need it when validating |
---|
149 | // instances (assuming the schema is valid). |
---|
150 | // |
---|
151 | conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); |
---|
152 | } |
---|
153 | |
---|
154 | // We will release DOM ourselves. |
---|
155 | // |
---|
156 | conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); |
---|
157 | |
---|
158 | |
---|
159 | // Transfer properies if any. |
---|
160 | // |
---|
161 | |
---|
162 | if (!prop.schema_location ().empty ()) |
---|
163 | { |
---|
164 | xml::string sl (prop.schema_location ()); |
---|
165 | const void* v (sl.c_str ()); |
---|
166 | |
---|
167 | conf->setParameter ( |
---|
168 | XMLUni::fgXercesSchemaExternalSchemaLocation, |
---|
169 | const_cast<void*> (v)); |
---|
170 | } |
---|
171 | |
---|
172 | if (!prop.no_namespace_schema_location ().empty ()) |
---|
173 | { |
---|
174 | xml::string sl (prop.no_namespace_schema_location ()); |
---|
175 | const void* v (sl.c_str ()); |
---|
176 | |
---|
177 | conf->setParameter ( |
---|
178 | XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, |
---|
179 | const_cast<void*> (v)); |
---|
180 | } |
---|
181 | |
---|
182 | // Set error handler. |
---|
183 | // |
---|
184 | bits::error_handler_proxy<C> ehp (eh); |
---|
185 | conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); |
---|
186 | |
---|
187 | #else // _XERCES_VERSION >= 30000 |
---|
188 | |
---|
189 | // Same as above but for Xerces-C++ 2 series. |
---|
190 | // |
---|
191 | auto_ptr<DOMBuilder> parser ( |
---|
192 | impl->createDOMBuilder (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); |
---|
193 | |
---|
194 | parser->setFeature (XMLUni::fgDOMComments, false); |
---|
195 | parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); |
---|
196 | parser->setFeature (XMLUni::fgDOMEntities, false); |
---|
197 | parser->setFeature (XMLUni::fgDOMNamespaces, true); |
---|
198 | parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); |
---|
199 | |
---|
200 | if (flags & dont_validate) |
---|
201 | { |
---|
202 | parser->setFeature (XMLUni::fgDOMValidation, false); |
---|
203 | parser->setFeature (XMLUni::fgXercesSchema, false); |
---|
204 | parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | parser->setFeature (XMLUni::fgDOMValidation, true); |
---|
209 | parser->setFeature (XMLUni::fgXercesSchema, true); |
---|
210 | parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
211 | } |
---|
212 | |
---|
213 | parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); |
---|
214 | |
---|
215 | if (!prop.schema_location ().empty ()) |
---|
216 | { |
---|
217 | xml::string sl (prop.schema_location ()); |
---|
218 | const void* v (sl.c_str ()); |
---|
219 | |
---|
220 | parser->setProperty ( |
---|
221 | XMLUni::fgXercesSchemaExternalSchemaLocation, |
---|
222 | const_cast<void*> (v)); |
---|
223 | } |
---|
224 | |
---|
225 | if (!prop.no_namespace_schema_location ().empty ()) |
---|
226 | { |
---|
227 | xml::string sl (prop.no_namespace_schema_location ()); |
---|
228 | const void* v (sl.c_str ()); |
---|
229 | |
---|
230 | parser->setProperty ( |
---|
231 | XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, |
---|
232 | const_cast<void*> (v)); |
---|
233 | } |
---|
234 | |
---|
235 | bits::error_handler_proxy<C> ehp (eh); |
---|
236 | parser->setErrorHandler (&ehp); |
---|
237 | |
---|
238 | #endif // _XERCES_VERSION >= 30000 |
---|
239 | |
---|
240 | xercesc::Wrapper4InputSource wrap (&is, false); |
---|
241 | |
---|
242 | #if _XERCES_VERSION >= 30000 |
---|
243 | auto_ptr<DOMDocument> doc (parser->parse (&wrap)); |
---|
244 | #else |
---|
245 | auto_ptr<DOMDocument> doc (parser->parse (wrap)); |
---|
246 | #endif |
---|
247 | if (ehp.failed ()) |
---|
248 | doc.reset (); |
---|
249 | |
---|
250 | return doc; |
---|
251 | } |
---|
252 | |
---|
253 | template <typename C> |
---|
254 | xml::dom::auto_ptr<xercesc::DOMDocument> |
---|
255 | parse (const std::basic_string<C>& uri, |
---|
256 | error_handler<C>& eh, |
---|
257 | const properties<C>& prop, |
---|
258 | unsigned long flags) |
---|
259 | { |
---|
260 | bits::error_handler_proxy<C> ehp (eh); |
---|
261 | return xml::dom::parse (uri, ehp, prop, flags); |
---|
262 | } |
---|
263 | |
---|
264 | template <typename C> |
---|
265 | auto_ptr<xercesc::DOMDocument> |
---|
266 | parse (const std::basic_string<C>& uri, |
---|
267 | xercesc::DOMErrorHandler& eh, |
---|
268 | const properties<C>& prop, |
---|
269 | unsigned long flags) |
---|
270 | { |
---|
271 | // HP aCC cannot handle using namespace xercesc; |
---|
272 | // |
---|
273 | using xercesc::DOMImplementationRegistry; |
---|
274 | using xercesc::DOMImplementationLS; |
---|
275 | using xercesc::DOMImplementation; |
---|
276 | using xercesc::DOMDocument; |
---|
277 | #if _XERCES_VERSION >= 30000 |
---|
278 | using xercesc::DOMLSParser; |
---|
279 | using xercesc::DOMConfiguration; |
---|
280 | #else |
---|
281 | using xercesc::DOMBuilder; |
---|
282 | #endif |
---|
283 | using xercesc::XMLUni; |
---|
284 | |
---|
285 | |
---|
286 | // Instantiate the DOM parser. |
---|
287 | // |
---|
288 | const XMLCh ls_id[] = {xercesc::chLatin_L, |
---|
289 | xercesc::chLatin_S, |
---|
290 | xercesc::chNull}; |
---|
291 | |
---|
292 | // Get an implementation of the Load-Store (LS) interface. |
---|
293 | // |
---|
294 | DOMImplementation* impl ( |
---|
295 | DOMImplementationRegistry::getDOMImplementation (ls_id)); |
---|
296 | |
---|
297 | #if _XERCES_VERSION >= 30000 |
---|
298 | auto_ptr<DOMLSParser> parser ( |
---|
299 | impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0)); |
---|
300 | |
---|
301 | DOMConfiguration* conf (parser->getDomConfig ()); |
---|
302 | |
---|
303 | // Discard comment nodes in the document. |
---|
304 | // |
---|
305 | conf->setParameter (XMLUni::fgDOMComments, false); |
---|
306 | |
---|
307 | // Enable datatype normalization. |
---|
308 | // |
---|
309 | conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); |
---|
310 | |
---|
311 | // Do not create EntityReference nodes in the DOM tree. No |
---|
312 | // EntityReference nodes will be created, only the nodes |
---|
313 | // corresponding to their fully expanded substitution text |
---|
314 | // will be created. |
---|
315 | // |
---|
316 | conf->setParameter (XMLUni::fgDOMEntities, false); |
---|
317 | |
---|
318 | // Perform namespace processing. |
---|
319 | // |
---|
320 | conf->setParameter (XMLUni::fgDOMNamespaces, true); |
---|
321 | |
---|
322 | // Do not include ignorable whitespace in the DOM tree. |
---|
323 | // |
---|
324 | conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); |
---|
325 | |
---|
326 | if (flags & dont_validate) |
---|
327 | { |
---|
328 | conf->setParameter (XMLUni::fgDOMValidate, false); |
---|
329 | conf->setParameter (XMLUni::fgXercesSchema, false); |
---|
330 | conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | conf->setParameter (XMLUni::fgDOMValidate, true); |
---|
335 | conf->setParameter (XMLUni::fgXercesSchema, true); |
---|
336 | |
---|
337 | // This feature checks the schema grammar for additional |
---|
338 | // errors. We most likely do not need it when validating |
---|
339 | // instances (assuming the schema is valid). |
---|
340 | // |
---|
341 | conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); |
---|
342 | } |
---|
343 | |
---|
344 | // We will release DOM ourselves. |
---|
345 | // |
---|
346 | conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); |
---|
347 | |
---|
348 | |
---|
349 | // Transfer properies if any. |
---|
350 | // |
---|
351 | |
---|
352 | if (!prop.schema_location ().empty ()) |
---|
353 | { |
---|
354 | xml::string sl (prop.schema_location ()); |
---|
355 | const void* v (sl.c_str ()); |
---|
356 | |
---|
357 | conf->setParameter ( |
---|
358 | XMLUni::fgXercesSchemaExternalSchemaLocation, |
---|
359 | const_cast<void*> (v)); |
---|
360 | } |
---|
361 | |
---|
362 | if (!prop.no_namespace_schema_location ().empty ()) |
---|
363 | { |
---|
364 | xml::string sl (prop.no_namespace_schema_location ()); |
---|
365 | const void* v (sl.c_str ()); |
---|
366 | |
---|
367 | conf->setParameter ( |
---|
368 | XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, |
---|
369 | const_cast<void*> (v)); |
---|
370 | } |
---|
371 | |
---|
372 | // Set error handler. |
---|
373 | // |
---|
374 | bits::error_handler_proxy<C> ehp (eh); |
---|
375 | conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); |
---|
376 | |
---|
377 | #else // _XERCES_VERSION >= 30000 |
---|
378 | |
---|
379 | // Same as above but for Xerces-C++ 2 series. |
---|
380 | // |
---|
381 | auto_ptr<DOMBuilder> parser ( |
---|
382 | impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0)); |
---|
383 | |
---|
384 | parser->setFeature (XMLUni::fgDOMComments, false); |
---|
385 | parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); |
---|
386 | parser->setFeature (XMLUni::fgDOMEntities, false); |
---|
387 | parser->setFeature (XMLUni::fgDOMNamespaces, true); |
---|
388 | parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); |
---|
389 | |
---|
390 | if (flags & dont_validate) |
---|
391 | { |
---|
392 | parser->setFeature (XMLUni::fgDOMValidation, false); |
---|
393 | parser->setFeature (XMLUni::fgXercesSchema, false); |
---|
394 | parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | parser->setFeature (XMLUni::fgDOMValidation, true); |
---|
399 | parser->setFeature (XMLUni::fgXercesSchema, true); |
---|
400 | parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
401 | } |
---|
402 | |
---|
403 | parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); |
---|
404 | |
---|
405 | if (!prop.schema_location ().empty ()) |
---|
406 | { |
---|
407 | xml::string sl (prop.schema_location ()); |
---|
408 | const void* v (sl.c_str ()); |
---|
409 | |
---|
410 | parser->setProperty ( |
---|
411 | XMLUni::fgXercesSchemaExternalSchemaLocation, |
---|
412 | const_cast<void*> (v)); |
---|
413 | } |
---|
414 | |
---|
415 | if (!prop.no_namespace_schema_location ().empty ()) |
---|
416 | { |
---|
417 | xml::string sl (prop.no_namespace_schema_location ()); |
---|
418 | const void* v (sl.c_str ()); |
---|
419 | |
---|
420 | parser->setProperty ( |
---|
421 | XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, |
---|
422 | const_cast<void*> (v)); |
---|
423 | } |
---|
424 | |
---|
425 | bits::error_handler_proxy<C> ehp (eh); |
---|
426 | parser->setErrorHandler (&ehp); |
---|
427 | |
---|
428 | #endif // _XERCES_VERSION >= 30000 |
---|
429 | |
---|
430 | auto_ptr<DOMDocument> doc ( |
---|
431 | parser->parseURI (string (uri).c_str ())); |
---|
432 | |
---|
433 | if (ehp.failed ()) |
---|
434 | doc.reset (); |
---|
435 | |
---|
436 | return doc; |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | } |
---|
441 | } |
---|