[111] | 1 | // file : xsd/cxx/parser/xerces/elements.hxx |
---|
| 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 | #ifndef XSD_CXX_PARSER_XERCES_ELEMENTS_HXX |
---|
| 7 | #define XSD_CXX_PARSER_XERCES_ELEMENTS_HXX |
---|
| 8 | |
---|
| 9 | #include <memory> // std::auto_ptr |
---|
| 10 | #include <string> |
---|
| 11 | #include <iosfwd> |
---|
| 12 | #include <vector> |
---|
| 13 | |
---|
| 14 | #include <xercesc/sax/Locator.hpp> |
---|
| 15 | #include <xercesc/sax/InputSource.hpp> |
---|
| 16 | #include <xercesc/sax2/SAX2XMLReader.hpp> |
---|
| 17 | #include <xercesc/sax2/DefaultHandler.hpp> |
---|
| 18 | |
---|
| 19 | #include <xsd/cxx/xml/elements.hxx> |
---|
| 20 | #include <xsd/cxx/xml/error-handler.hxx> |
---|
| 21 | |
---|
| 22 | #include <xsd/cxx/parser/exceptions.hxx> |
---|
| 23 | #include <xsd/cxx/parser/elements.hxx> |
---|
| 24 | #include <xsd/cxx/parser/document.hxx> |
---|
| 25 | |
---|
| 26 | namespace xsd |
---|
| 27 | { |
---|
| 28 | namespace cxx |
---|
| 29 | { |
---|
| 30 | namespace parser |
---|
| 31 | { |
---|
| 32 | namespace xerces |
---|
| 33 | { |
---|
| 34 | // |
---|
| 35 | // |
---|
| 36 | struct flags |
---|
| 37 | { |
---|
| 38 | // Use the following flags to modify the default behavior |
---|
| 39 | // of the parsing functions. |
---|
| 40 | // |
---|
| 41 | |
---|
| 42 | // Do not try to validate instance documents. |
---|
| 43 | // |
---|
| 44 | static const unsigned long dont_validate = 0x00000001; |
---|
| 45 | |
---|
| 46 | // Do not initialize the Xerces-C++ runtime. |
---|
| 47 | // |
---|
| 48 | static const unsigned long dont_initialize = 0x00000002; |
---|
| 49 | |
---|
| 50 | public: |
---|
| 51 | flags (unsigned long x = 0) |
---|
| 52 | : x_ (x) |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | operator unsigned long () const |
---|
| 57 | { |
---|
| 58 | return x_; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | private: |
---|
| 62 | unsigned long x_; |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | // Parsing properties. Refer to xsd/cxx/xml/elements.hxx for |
---|
| 67 | // XML-related properties. |
---|
| 68 | // |
---|
| 69 | template <typename C> |
---|
| 70 | class properties: public xml::properties<C> |
---|
| 71 | { |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | // |
---|
| 75 | // |
---|
| 76 | template <typename C> |
---|
| 77 | struct document: cxx::parser::document<C> // VC 7.1 likes it qualified |
---|
| 78 | { |
---|
| 79 | public: |
---|
| 80 | document (parser_base<C>& root, |
---|
| 81 | const C* root_element_name, |
---|
| 82 | bool polymorphic = false); |
---|
| 83 | |
---|
| 84 | document (parser_base<C>& root, |
---|
| 85 | const std::basic_string<C>& root_element_name, |
---|
| 86 | bool polymorphic = false); |
---|
| 87 | |
---|
| 88 | document (parser_base<C>& root, |
---|
| 89 | const C* root_element_namespace, |
---|
| 90 | const C* root_element_name, |
---|
| 91 | bool polymorphic = false); |
---|
| 92 | |
---|
| 93 | document (parser_base<C>& root, |
---|
| 94 | const std::basic_string<C>& root_element_namespace, |
---|
| 95 | const std::basic_string<C>& root_element_name, |
---|
| 96 | bool polymorphic = false); |
---|
| 97 | |
---|
| 98 | protected: |
---|
| 99 | explicit |
---|
| 100 | document (bool polymorphic = false); |
---|
| 101 | |
---|
| 102 | public: |
---|
| 103 | // Parse URI or a local file. We have to overload it for const C* |
---|
| 104 | // bacause xercesc::InputSource has an implicit constructor that |
---|
| 105 | // takes const char*. |
---|
| 106 | // |
---|
| 107 | void |
---|
| 108 | parse (const std::basic_string<C>& uri, |
---|
| 109 | flags = 0, |
---|
| 110 | const properties<C>& = properties<C> ()); |
---|
| 111 | |
---|
| 112 | void |
---|
| 113 | parse (const C* uri, |
---|
| 114 | flags = 0, |
---|
| 115 | const properties<C>& = properties<C> ()); |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | // Parse URI or a local file with a user-provided error_handler |
---|
| 119 | // object. |
---|
| 120 | // |
---|
| 121 | void |
---|
| 122 | parse (const std::basic_string<C>& uri, |
---|
| 123 | xml::error_handler<C>&, |
---|
| 124 | flags = 0, |
---|
| 125 | const properties<C>& = properties<C> ()); |
---|
| 126 | |
---|
| 127 | void |
---|
| 128 | parse (const C* uri, |
---|
| 129 | xml::error_handler<C>&, |
---|
| 130 | flags = 0, |
---|
| 131 | const properties<C>& = properties<C> ()); |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | // Parse URI or a local file with a user-provided ErrorHandler |
---|
| 135 | // object. Note that you must initialize the Xerces-C++ runtime |
---|
| 136 | // before calling these functions. |
---|
| 137 | // |
---|
| 138 | void |
---|
| 139 | parse (const std::basic_string<C>& uri, |
---|
| 140 | xercesc::ErrorHandler&, |
---|
| 141 | flags = 0, |
---|
| 142 | const properties<C>& = properties<C> ()); |
---|
| 143 | |
---|
| 144 | void |
---|
| 145 | parse (const C* uri, |
---|
| 146 | xercesc::ErrorHandler&, |
---|
| 147 | flags = 0, |
---|
| 148 | const properties<C>& = properties<C> ()); |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | // Parse URI or a local file using a user-provided SAX2XMLReader |
---|
| 152 | // object. Note that you must initialize the Xerces-C++ runtime |
---|
| 153 | // before calling these functions. |
---|
| 154 | // |
---|
| 155 | void |
---|
| 156 | parse (const std::basic_string<C>& uri, |
---|
| 157 | xercesc::SAX2XMLReader&, |
---|
| 158 | flags = 0, |
---|
| 159 | const properties<C>& = properties<C> ()); |
---|
| 160 | |
---|
| 161 | void |
---|
| 162 | parse (const C* uri, |
---|
| 163 | xercesc::SAX2XMLReader&, |
---|
| 164 | flags = 0, |
---|
| 165 | const properties<C>& = properties<C> ()); |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | public: |
---|
| 169 | // System id is a "system" identifier of the resources (e.g., |
---|
| 170 | // URI or a full file path). Public id is a "public" identifier |
---|
| 171 | // of the resource (e.g., an application-specific name or a |
---|
| 172 | // relative file path). System id is used to resolve relative |
---|
| 173 | // paths. In diagnostics messages system id is used if public |
---|
| 174 | // id is not available. Otherwise public id is used. |
---|
| 175 | // |
---|
| 176 | |
---|
| 177 | // Parse std::istream. |
---|
| 178 | // |
---|
| 179 | void |
---|
| 180 | parse (std::istream&, |
---|
| 181 | flags = 0, |
---|
| 182 | const properties<C>& = properties<C> ()); |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | // Parse std::istream with a user-provided error_handler object. |
---|
| 186 | // |
---|
| 187 | void |
---|
| 188 | parse (std::istream&, |
---|
| 189 | xml::error_handler<C>&, |
---|
| 190 | flags = 0, |
---|
| 191 | const properties<C>& = properties<C> ()); |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | // Parse std::istream with a user-provided ErrorHandler object. |
---|
| 195 | // Note that you must initialize the Xerces-C++ runtime before |
---|
| 196 | // calling this function. |
---|
| 197 | // |
---|
| 198 | void |
---|
| 199 | parse (std::istream&, |
---|
| 200 | xercesc::ErrorHandler&, |
---|
| 201 | flags = 0, |
---|
| 202 | const properties<C>& = properties<C> ()); |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | // Parse std::istream using a user-provided SAX2XMLReader object. |
---|
| 206 | // Note that you must initialize the Xerces-C++ runtime before |
---|
| 207 | // calling this function. |
---|
| 208 | // |
---|
| 209 | void |
---|
| 210 | parse (std::istream&, |
---|
| 211 | xercesc::SAX2XMLReader&, |
---|
| 212 | flags = 0, |
---|
| 213 | const properties<C>& = properties<C> ()); |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | public: |
---|
| 217 | // Parse std::istream with a system id. |
---|
| 218 | // |
---|
| 219 | void |
---|
| 220 | parse (std::istream&, |
---|
| 221 | const std::basic_string<C>& system_id, |
---|
| 222 | flags = 0, |
---|
| 223 | const properties<C>& = properties<C> ()); |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | // Parse std::istream with a system id and a user-provided |
---|
| 227 | // error_handler object. |
---|
| 228 | // |
---|
| 229 | void |
---|
| 230 | parse (std::istream&, |
---|
| 231 | const std::basic_string<C>& system_id, |
---|
| 232 | xml::error_handler<C>&, |
---|
| 233 | flags = 0, |
---|
| 234 | const properties<C>& = properties<C> ()); |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | // Parse std::istream with a system id and a user-provided |
---|
| 238 | // ErrorHandler object. Note that you must initialize the |
---|
| 239 | // Xerces-C++ runtime before calling this function. |
---|
| 240 | // |
---|
| 241 | void |
---|
| 242 | parse (std::istream&, |
---|
| 243 | const std::basic_string<C>& system_id, |
---|
| 244 | xercesc::ErrorHandler&, |
---|
| 245 | flags = 0, |
---|
| 246 | const properties<C>& = properties<C> ()); |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | // Parse std::istream with a system id using a user-provided |
---|
| 250 | // SAX2XMLReader object. Note that you must initialize the |
---|
| 251 | // Xerces-C++ runtime before calling this function. |
---|
| 252 | // |
---|
| 253 | void |
---|
| 254 | parse (std::istream&, |
---|
| 255 | const std::basic_string<C>& system_id, |
---|
| 256 | xercesc::SAX2XMLReader&, |
---|
| 257 | flags = 0, |
---|
| 258 | const properties<C>& = properties<C> ()); |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | public: |
---|
| 263 | // Parse std::istream with system and public ids. |
---|
| 264 | // |
---|
| 265 | void |
---|
| 266 | parse (std::istream&, |
---|
| 267 | const std::basic_string<C>& system_id, |
---|
| 268 | const std::basic_string<C>& public_id, |
---|
| 269 | flags = 0, |
---|
| 270 | const properties<C>& = properties<C> ()); |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | // Parse std::istream with system and public ids and a user-provided |
---|
| 274 | // error_handler object. |
---|
| 275 | // |
---|
| 276 | void |
---|
| 277 | parse (std::istream&, |
---|
| 278 | const std::basic_string<C>& system_id, |
---|
| 279 | const std::basic_string<C>& public_id, |
---|
| 280 | xml::error_handler<C>&, |
---|
| 281 | flags = 0, |
---|
| 282 | const properties<C>& = properties<C> ()); |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | // Parse std::istream with system and public ids and a user-provided |
---|
| 286 | // ErrorHandler object. Note that you must initialize the Xerces-C++ |
---|
| 287 | // runtime before calling this function. |
---|
| 288 | // |
---|
| 289 | void |
---|
| 290 | parse (std::istream&, |
---|
| 291 | const std::basic_string<C>& system_id, |
---|
| 292 | const std::basic_string<C>& public_id, |
---|
| 293 | xercesc::ErrorHandler&, |
---|
| 294 | flags = 0, |
---|
| 295 | const properties<C>& = properties<C> ()); |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | // Parse std::istream with system and public ids using a user- |
---|
| 299 | // provided SAX2XMLReader object. Note that you must initialize |
---|
| 300 | // the Xerces-C++ runtime before calling this function. |
---|
| 301 | // |
---|
| 302 | void |
---|
| 303 | parse (std::istream&, |
---|
| 304 | const std::basic_string<C>& system_id, |
---|
| 305 | const std::basic_string<C>& public_id, |
---|
| 306 | xercesc::SAX2XMLReader&, |
---|
| 307 | flags = 0, |
---|
| 308 | const properties<C>& = properties<C> ()); |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | public: |
---|
| 312 | // Parse InputSource. Note that you must initialize the Xerces-C++ |
---|
| 313 | // runtime before calling this function. |
---|
| 314 | // |
---|
| 315 | void |
---|
| 316 | parse (const xercesc::InputSource&, |
---|
| 317 | flags = 0, |
---|
| 318 | const properties<C>& = properties<C> ()); |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | // Parse InputSource with a user-provided error_handler object. |
---|
| 322 | // Note that you must initialize the Xerces-C++ runtime before |
---|
| 323 | // calling this function. |
---|
| 324 | // |
---|
| 325 | void |
---|
| 326 | parse (const xercesc::InputSource&, |
---|
| 327 | xml::error_handler<C>&, |
---|
| 328 | flags = 0, |
---|
| 329 | const properties<C>& = properties<C> ()); |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | // Parse InputSource with a user-provided ErrorHandler object. |
---|
| 333 | // Note that you must initialize the Xerces-C++ runtime before |
---|
| 334 | // calling this function. |
---|
| 335 | // |
---|
| 336 | void |
---|
| 337 | parse (const xercesc::InputSource&, |
---|
| 338 | xercesc::ErrorHandler&, |
---|
| 339 | flags = 0, |
---|
| 340 | const properties<C>& = properties<C> ()); |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | // Parse InputSource using a user-provided SAX2XMLReader object. |
---|
| 344 | // Note that you must initialize the Xerces-C++ runtime before |
---|
| 345 | // calling this function. |
---|
| 346 | // |
---|
| 347 | void |
---|
| 348 | parse (const xercesc::InputSource&, |
---|
| 349 | xercesc::SAX2XMLReader&, |
---|
| 350 | flags = 0, |
---|
| 351 | const properties<C>& = properties<C> ()); |
---|
| 352 | |
---|
| 353 | private: |
---|
| 354 | void |
---|
| 355 | parse (const std::basic_string<C>& uri, |
---|
| 356 | xercesc::ErrorHandler&, |
---|
| 357 | xercesc::SAX2XMLReader&, |
---|
| 358 | flags, |
---|
| 359 | const properties<C>&); |
---|
| 360 | |
---|
| 361 | void |
---|
| 362 | parse (const xercesc::InputSource&, |
---|
| 363 | xercesc::ErrorHandler&, |
---|
| 364 | xercesc::SAX2XMLReader&, |
---|
| 365 | flags, |
---|
| 366 | const properties<C>&); |
---|
| 367 | |
---|
| 368 | private: |
---|
| 369 | std::auto_ptr<xercesc::SAX2XMLReader> |
---|
| 370 | create_sax_ (flags, const properties<C>&); |
---|
| 371 | |
---|
| 372 | private: |
---|
| 373 | bool polymorphic_; |
---|
| 374 | }; |
---|
| 375 | |
---|
| 376 | // |
---|
| 377 | // |
---|
| 378 | template <typename C> |
---|
| 379 | struct event_router: xercesc::DefaultHandler |
---|
| 380 | { |
---|
| 381 | event_router (cxx::parser::document<C>&, bool polymorphic); |
---|
| 382 | |
---|
| 383 | // I know, some of those consts are stupid. But that's what |
---|
| 384 | // Xerces folks put into their interfaces and VC 7.1 thinks |
---|
| 385 | // there are different signatures if one strips this fluff off. |
---|
| 386 | // |
---|
| 387 | virtual void |
---|
| 388 | setDocumentLocator (const xercesc::Locator* const); |
---|
| 389 | |
---|
| 390 | virtual void |
---|
| 391 | startElement (const XMLCh* const uri, |
---|
| 392 | const XMLCh* const lname, |
---|
| 393 | const XMLCh* const qname, |
---|
| 394 | const xercesc::Attributes& attributes); |
---|
| 395 | |
---|
| 396 | virtual void |
---|
| 397 | endElement (const XMLCh* const uri, |
---|
| 398 | const XMLCh* const lname, |
---|
| 399 | const XMLCh* const qname); |
---|
| 400 | |
---|
| 401 | #if _XERCES_VERSION >= 30000 |
---|
| 402 | virtual void |
---|
| 403 | characters (const XMLCh* const s, const XMLSize_t length); |
---|
| 404 | #else |
---|
| 405 | virtual void |
---|
| 406 | characters (const XMLCh* const s, const unsigned int length); |
---|
| 407 | #endif |
---|
| 408 | |
---|
| 409 | virtual void |
---|
| 410 | startPrefixMapping (const XMLCh* const prefix, |
---|
| 411 | const XMLCh* const uri); |
---|
| 412 | |
---|
| 413 | virtual void |
---|
| 414 | endPrefixMapping (const XMLCh* const prefix); |
---|
| 415 | |
---|
| 416 | private: |
---|
| 417 | void |
---|
| 418 | set_location (schema_exception<C>&); |
---|
| 419 | |
---|
| 420 | private: |
---|
| 421 | const xercesc::Locator* loc_; |
---|
| 422 | cxx::parser::document<C>& consumer_; |
---|
| 423 | bool polymorphic_; |
---|
| 424 | |
---|
| 425 | // Namespace-prefix mapping. Only maintained in the polymorphic |
---|
| 426 | // case. |
---|
| 427 | // |
---|
| 428 | struct ns_decl |
---|
| 429 | { |
---|
| 430 | ns_decl (const std::basic_string<C>& p, |
---|
| 431 | const std::basic_string<C>& n) |
---|
| 432 | : prefix (p), ns (n) |
---|
| 433 | { |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | std::basic_string<C> prefix; |
---|
| 437 | std::basic_string<C> ns; |
---|
| 438 | }; |
---|
| 439 | |
---|
| 440 | typedef std::vector<ns_decl> ns_decls; |
---|
| 441 | |
---|
| 442 | ns_decls ns_decls_; |
---|
| 443 | }; |
---|
| 444 | } |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | #include <xsd/cxx/parser/xerces/elements.txx> |
---|
| 450 | |
---|
| 451 | #endif // XSD_CXX_PARSER_XERCES_ELEMENTS_HXX |
---|