| 1 | // file : xsd/cxx/parser/validating/parser.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_VALIDATING_PARSER_HXX |
|---|
| 7 | #define XSD_CXX_PARSER_VALIDATING_PARSER_HXX |
|---|
| 8 | |
|---|
| 9 | #include <stack> |
|---|
| 10 | #include <cstddef> // std::size_t |
|---|
| 11 | #include <cstring> // std::memcpy |
|---|
| 12 | |
|---|
| 13 | #include <xsd/cxx/ro-string.hxx> |
|---|
| 14 | #include <xsd/cxx/parser/elements.hxx> |
|---|
| 15 | |
|---|
| 16 | namespace xsd |
|---|
| 17 | { |
|---|
| 18 | namespace cxx |
|---|
| 19 | { |
|---|
| 20 | namespace parser |
|---|
| 21 | { |
|---|
| 22 | namespace validating |
|---|
| 23 | { |
|---|
| 24 | // |
|---|
| 25 | // |
|---|
| 26 | template <typename C> |
|---|
| 27 | struct empty_content: virtual parser_base<C> |
|---|
| 28 | { |
|---|
| 29 | // These functions are called when wildcard content |
|---|
| 30 | // is encountered. Use them to handle mixed content |
|---|
| 31 | // models, any/anyAttribute, and anyType/anySimpleType. |
|---|
| 32 | // By default these functions do nothing. |
|---|
| 33 | // |
|---|
| 34 | |
|---|
| 35 | // The type argument is a type name and namespace from the |
|---|
| 36 | // xsi:type attribute in the form "<name> <namespace>" with |
|---|
| 37 | // the space and namespace part absent if the type does not |
|---|
| 38 | // have a namespace or 0 if xsi:type is not present. |
|---|
| 39 | // |
|---|
| 40 | virtual void |
|---|
| 41 | _start_any_element (const ro_string<C>& ns, |
|---|
| 42 | const ro_string<C>& name, |
|---|
| 43 | const ro_string<C>* type); |
|---|
| 44 | |
|---|
| 45 | virtual void |
|---|
| 46 | _end_any_element (const ro_string<C>& ns, |
|---|
| 47 | const ro_string<C>& name); |
|---|
| 48 | |
|---|
| 49 | virtual void |
|---|
| 50 | _any_attribute (const ro_string<C>& ns, |
|---|
| 51 | const ro_string<C>& name, |
|---|
| 52 | const ro_string<C>& value); |
|---|
| 53 | |
|---|
| 54 | virtual void |
|---|
| 55 | _any_characters (const ro_string<C>&); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | // |
|---|
| 59 | // |
|---|
| 60 | virtual bool |
|---|
| 61 | _start_element_impl (const ro_string<C>&, |
|---|
| 62 | const ro_string<C>&, |
|---|
| 63 | const ro_string<C>*); |
|---|
| 64 | |
|---|
| 65 | virtual bool |
|---|
| 66 | _end_element_impl (const ro_string<C>&, |
|---|
| 67 | const ro_string<C>&); |
|---|
| 68 | |
|---|
| 69 | virtual bool |
|---|
| 70 | _attribute_impl (const ro_string<C>&, |
|---|
| 71 | const ro_string<C>&, |
|---|
| 72 | const ro_string<C>&); |
|---|
| 73 | |
|---|
| 74 | virtual bool |
|---|
| 75 | _characters_impl (const ro_string<C>&); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | // |
|---|
| 79 | // |
|---|
| 80 | virtual void |
|---|
| 81 | _start_element (const ro_string<C>&, |
|---|
| 82 | const ro_string<C>&, |
|---|
| 83 | const ro_string<C>*); |
|---|
| 84 | |
|---|
| 85 | virtual void |
|---|
| 86 | _end_element (const ro_string<C>&, |
|---|
| 87 | const ro_string<C>&); |
|---|
| 88 | |
|---|
| 89 | virtual void |
|---|
| 90 | _attribute (const ro_string<C>&, |
|---|
| 91 | const ro_string<C>&, |
|---|
| 92 | const ro_string<C>&); |
|---|
| 93 | |
|---|
| 94 | virtual void |
|---|
| 95 | _characters (const ro_string<C>&); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | // |
|---|
| 99 | // |
|---|
| 100 | virtual void |
|---|
| 101 | _expected_element (const C* expected_ns, |
|---|
| 102 | const C* expected_name); |
|---|
| 103 | |
|---|
| 104 | virtual void |
|---|
| 105 | _expected_element (const C* expected_ns, |
|---|
| 106 | const C* expected_name, |
|---|
| 107 | const ro_string<C>& encountered_ns, |
|---|
| 108 | const ro_string<C>& encountered_name); |
|---|
| 109 | |
|---|
| 110 | virtual void |
|---|
| 111 | _unexpected_element (const ro_string<C>& ns, |
|---|
| 112 | const ro_string<C>& name); |
|---|
| 113 | |
|---|
| 114 | virtual void |
|---|
| 115 | _expected_attribute (const C* expected_ns, |
|---|
| 116 | const C* expected_name); |
|---|
| 117 | |
|---|
| 118 | virtual void |
|---|
| 119 | _unexpected_attribute (const ro_string<C>& ns, |
|---|
| 120 | const ro_string<C>& name, |
|---|
| 121 | const ro_string<C>& value); |
|---|
| 122 | |
|---|
| 123 | virtual void |
|---|
| 124 | _unexpected_characters (const ro_string<C>&); |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | // |
|---|
| 129 | // |
|---|
| 130 | template <typename C> |
|---|
| 131 | struct simple_content: virtual empty_content<C> |
|---|
| 132 | { |
|---|
| 133 | // |
|---|
| 134 | // |
|---|
| 135 | virtual void |
|---|
| 136 | _attribute (const ro_string<C>& ns, |
|---|
| 137 | const ro_string<C>& name, |
|---|
| 138 | const ro_string<C>& value); |
|---|
| 139 | |
|---|
| 140 | virtual void |
|---|
| 141 | _characters (const ro_string<C>&); |
|---|
| 142 | |
|---|
| 143 | // |
|---|
| 144 | // |
|---|
| 145 | virtual bool |
|---|
| 146 | _attribute_impl (const ro_string<C>&, |
|---|
| 147 | const ro_string<C>&, |
|---|
| 148 | const ro_string<C>&); |
|---|
| 149 | |
|---|
| 150 | // |
|---|
| 151 | // |
|---|
| 152 | virtual void |
|---|
| 153 | _pre_impl (); |
|---|
| 154 | |
|---|
| 155 | virtual void |
|---|
| 156 | _post_impl (); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | // Implementation callbacks. |
|---|
| 160 | // |
|---|
| 161 | virtual void |
|---|
| 162 | _pre_a_validate (); |
|---|
| 163 | |
|---|
| 164 | virtual void |
|---|
| 165 | _post_a_validate (); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | // Attribute validation: during phase one we are searching for |
|---|
| 169 | // matching attributes (Structures, section 3.4.4, clause 2.1). |
|---|
| 170 | // During phase two we are searching for attribute wildcards |
|---|
| 171 | // (section 3.4.4, clause 2.2). Both phases run across |
|---|
| 172 | // inheritance hierarchy from derived to base for extension |
|---|
| 173 | // only. Both functions return true if the match was found and |
|---|
| 174 | // validation has been performed. |
|---|
| 175 | // |
|---|
| 176 | virtual bool |
|---|
| 177 | _attribute_impl_phase_one (const ro_string<C>& ns, |
|---|
| 178 | const ro_string<C>& name, |
|---|
| 179 | const ro_string<C>& value); |
|---|
| 180 | |
|---|
| 181 | virtual bool |
|---|
| 182 | _attribute_impl_phase_two (const ro_string<C>& ns, |
|---|
| 183 | const ro_string<C>& name, |
|---|
| 184 | const ro_string<C>& value); |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | // |
|---|
| 189 | // |
|---|
| 190 | template <typename C> |
|---|
| 191 | struct complex_content: virtual empty_content<C> |
|---|
| 192 | { |
|---|
| 193 | // |
|---|
| 194 | // |
|---|
| 195 | virtual void |
|---|
| 196 | _start_element (const ro_string<C>& ns, |
|---|
| 197 | const ro_string<C>& name, |
|---|
| 198 | const ro_string<C>* type); |
|---|
| 199 | |
|---|
| 200 | virtual void |
|---|
| 201 | _end_element (const ro_string<C>& ns, |
|---|
| 202 | const ro_string<C>& name); |
|---|
| 203 | |
|---|
| 204 | virtual void |
|---|
| 205 | _attribute (const ro_string<C>& ns, |
|---|
| 206 | const ro_string<C>& name, |
|---|
| 207 | const ro_string<C>& value); |
|---|
| 208 | |
|---|
| 209 | virtual void |
|---|
| 210 | _characters (const ro_string<C>&); |
|---|
| 211 | |
|---|
| 212 | // |
|---|
| 213 | // |
|---|
| 214 | virtual bool |
|---|
| 215 | _attribute_impl (const ro_string<C>&, |
|---|
| 216 | const ro_string<C>&, |
|---|
| 217 | const ro_string<C>&); |
|---|
| 218 | |
|---|
| 219 | // |
|---|
| 220 | // |
|---|
| 221 | virtual void |
|---|
| 222 | _pre_impl (); |
|---|
| 223 | |
|---|
| 224 | virtual void |
|---|
| 225 | _post_impl (); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | // Implementation callbacks. |
|---|
| 229 | // |
|---|
| 230 | virtual void |
|---|
| 231 | _pre_e_validate (); |
|---|
| 232 | |
|---|
| 233 | virtual void |
|---|
| 234 | _post_e_validate (); |
|---|
| 235 | |
|---|
| 236 | virtual void |
|---|
| 237 | _pre_a_validate (); |
|---|
| 238 | |
|---|
| 239 | virtual void |
|---|
| 240 | _post_a_validate (); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | // Attribute validation: during phase one we are searching for |
|---|
| 244 | // matching attributes (Structures, section 3.4.4, clause 2.1). |
|---|
| 245 | // During phase two we are searching for attribute wildcards |
|---|
| 246 | // (section 3.4.4, clause 2.2). Both phases run across |
|---|
| 247 | // inheritance hierarchy from derived to base for extension |
|---|
| 248 | // only. Both functions return true if the match was found and |
|---|
| 249 | // validation has been performed. |
|---|
| 250 | // |
|---|
| 251 | virtual bool |
|---|
| 252 | _attribute_impl_phase_one (const ro_string<C>& ns, |
|---|
| 253 | const ro_string<C>& name, |
|---|
| 254 | const ro_string<C>& value); |
|---|
| 255 | |
|---|
| 256 | virtual bool |
|---|
| 257 | _attribute_impl_phase_two (const ro_string<C>& ns, |
|---|
| 258 | const ro_string<C>& name, |
|---|
| 259 | const ro_string<C>& value); |
|---|
| 260 | protected: |
|---|
| 261 | struct state |
|---|
| 262 | { |
|---|
| 263 | state () |
|---|
| 264 | : any_ (false), depth_ (0), parser_ (0) |
|---|
| 265 | { |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | bool any_; |
|---|
| 269 | std::size_t depth_; |
|---|
| 270 | parser_base<C>* parser_; |
|---|
| 271 | }; |
|---|
| 272 | |
|---|
| 273 | // Optimized state stack for non-recursive case (one element). |
|---|
| 274 | // |
|---|
| 275 | struct state_stack |
|---|
| 276 | { |
|---|
| 277 | state_stack () |
|---|
| 278 | : size_ (0) |
|---|
| 279 | { |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | void |
|---|
| 283 | push (const state& s) |
|---|
| 284 | { |
|---|
| 285 | if (size_ > 0) |
|---|
| 286 | rest_.push (top_); |
|---|
| 287 | |
|---|
| 288 | top_ = s; |
|---|
| 289 | ++size_; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | void |
|---|
| 293 | pop () |
|---|
| 294 | { |
|---|
| 295 | if (size_ > 1) |
|---|
| 296 | { |
|---|
| 297 | top_ = rest_.top (); |
|---|
| 298 | rest_.pop (); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | --size_; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | const state& |
|---|
| 305 | top () const |
|---|
| 306 | { |
|---|
| 307 | return top_; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | state& |
|---|
| 311 | top () |
|---|
| 312 | { |
|---|
| 313 | return top_; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | state& |
|---|
| 317 | under_top () |
|---|
| 318 | { |
|---|
| 319 | return rest_.top (); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | private: |
|---|
| 323 | state top_; |
|---|
| 324 | std::stack<state> rest_; |
|---|
| 325 | std::size_t size_; |
|---|
| 326 | }; |
|---|
| 327 | |
|---|
| 328 | state_stack context_; |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | // Base for xsd:list. |
|---|
| 332 | // |
|---|
| 333 | template <typename C> |
|---|
| 334 | struct list_base: virtual simple_content<C> |
|---|
| 335 | { |
|---|
| 336 | virtual void |
|---|
| 337 | _xsd_parse_item (const ro_string<C>&) = 0; |
|---|
| 338 | |
|---|
| 339 | virtual void |
|---|
| 340 | _pre (); |
|---|
| 341 | |
|---|
| 342 | virtual void |
|---|
| 343 | _characters (const ro_string<C>&); |
|---|
| 344 | |
|---|
| 345 | virtual void |
|---|
| 346 | _post (); |
|---|
| 347 | |
|---|
| 348 | protected: |
|---|
| 349 | std::basic_string<C> buf_; |
|---|
| 350 | }; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | // POD stack with pre-allocated first element. You may |
|---|
| 354 | // need to pad your elements to get the proper alignment. |
|---|
| 355 | // |
|---|
| 356 | struct pod_stack |
|---|
| 357 | { |
|---|
| 358 | ~pod_stack () |
|---|
| 359 | { |
|---|
| 360 | delete[] data_; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | pod_stack (std::size_t element_size, void* first_element) |
|---|
| 364 | : el_size_ (element_size), first_ (first_element), |
|---|
| 365 | data_ (0), size_ (0), capacity_ (0) |
|---|
| 366 | { |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | public: |
|---|
| 370 | void |
|---|
| 371 | pop () |
|---|
| 372 | { |
|---|
| 373 | --size_; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | void |
|---|
| 377 | push () |
|---|
| 378 | { |
|---|
| 379 | if (size_ > capacity_) |
|---|
| 380 | grow (); |
|---|
| 381 | |
|---|
| 382 | ++size_; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | void* |
|---|
| 386 | top () |
|---|
| 387 | { |
|---|
| 388 | return size_ == 1 ? first_ : data_ + (size_ - 1) * el_size_; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | void* |
|---|
| 392 | under_top () |
|---|
| 393 | { |
|---|
| 394 | return size_ == 2 ? first_ : data_ + (size_ - 2) * el_size_; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | std::size_t |
|---|
| 398 | element_size () const |
|---|
| 399 | { |
|---|
| 400 | return el_size_; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | private: |
|---|
| 404 | void |
|---|
| 405 | grow () |
|---|
| 406 | { |
|---|
| 407 | std::size_t c (capacity_ ? capacity_ * 2 : 8); |
|---|
| 408 | char* d (new char[c * el_size_]); |
|---|
| 409 | |
|---|
| 410 | if (size_ > 1) |
|---|
| 411 | std::memcpy (d, data_, (size_ - 1) * el_size_); |
|---|
| 412 | |
|---|
| 413 | delete[] data_; |
|---|
| 414 | |
|---|
| 415 | data_ = d; |
|---|
| 416 | capacity_ = c; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | private: |
|---|
| 420 | std::size_t el_size_; |
|---|
| 421 | void* first_; |
|---|
| 422 | char* data_; |
|---|
| 423 | std::size_t size_; |
|---|
| 424 | std::size_t capacity_; |
|---|
| 425 | }; |
|---|
| 426 | |
|---|
| 427 | namespace validating |
|---|
| 428 | { |
|---|
| 429 | // Validation state stack for the 'all' particle. |
|---|
| 430 | // |
|---|
| 431 | struct all_stack |
|---|
| 432 | { |
|---|
| 433 | all_stack (std::size_t n, unsigned char* first) |
|---|
| 434 | : stack_ (n, first) |
|---|
| 435 | { |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | void |
|---|
| 439 | push () |
|---|
| 440 | { |
|---|
| 441 | stack_.push (); |
|---|
| 442 | |
|---|
| 443 | unsigned char* p (static_cast<unsigned char*> (stack_.top ())); |
|---|
| 444 | |
|---|
| 445 | for (std::size_t i (0); i < stack_.element_size (); ++i) |
|---|
| 446 | p[i] = 0; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | void |
|---|
| 450 | pop () |
|---|
| 451 | { |
|---|
| 452 | stack_.pop (); |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | unsigned char* |
|---|
| 456 | top () |
|---|
| 457 | { |
|---|
| 458 | return static_cast<unsigned char*> (stack_.top ()); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | private: |
|---|
| 462 | pod_stack stack_; |
|---|
| 463 | }; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | #include <xsd/cxx/parser/validating/parser.txx> |
|---|
| 470 | |
|---|
| 471 | #endif // XSD_CXX_PARSER_VALIDATING_PARSER_HXX |
|---|