| 1 | // file : xsd/cxx/parser/non-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_NON_VALIDATING_PARSER_HXX |
|---|
| 7 | #define XSD_CXX_PARSER_NON_VALIDATING_PARSER_HXX |
|---|
| 8 | |
|---|
| 9 | #include <stack> |
|---|
| 10 | #include <string> |
|---|
| 11 | #include <cstddef> // std::size_t |
|---|
| 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 non_validating |
|---|
| 23 | { |
|---|
| 24 | // |
|---|
| 25 | // |
|---|
| 26 | template <typename C> |
|---|
| 27 | struct empty_content: virtual parser_base<C> |
|---|
| 28 | { |
|---|
| 29 | // The _*_any_* functions are called when wildcard content |
|---|
| 30 | // is encountered. Use them to handle mixed content models, |
|---|
| 31 | // any/anyAttribute, and anyType/anySimpleType. By default |
|---|
| 32 | // 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>& ns, |
|---|
| 82 | const ro_string<C>& name, |
|---|
| 83 | const ro_string<C>* type); |
|---|
| 84 | |
|---|
| 85 | virtual void |
|---|
| 86 | _end_element (const ro_string<C>& ns, |
|---|
| 87 | const ro_string<C>& name); |
|---|
| 88 | |
|---|
| 89 | virtual void |
|---|
| 90 | _attribute (const ro_string<C>& ns, |
|---|
| 91 | const ro_string<C>& name, |
|---|
| 92 | const ro_string<C>& value); |
|---|
| 93 | |
|---|
| 94 | virtual void |
|---|
| 95 | _characters (const ro_string<C>& s); |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | // |
|---|
| 100 | // |
|---|
| 101 | template <typename C> |
|---|
| 102 | struct simple_content: virtual empty_content<C> |
|---|
| 103 | { |
|---|
| 104 | // |
|---|
| 105 | // |
|---|
| 106 | virtual void |
|---|
| 107 | _attribute (const ro_string<C>& ns, |
|---|
| 108 | const ro_string<C>& name, |
|---|
| 109 | const ro_string<C>& value); |
|---|
| 110 | |
|---|
| 111 | virtual void |
|---|
| 112 | _characters (const ro_string<C>&); |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | // |
|---|
| 117 | // |
|---|
| 118 | template <typename C> |
|---|
| 119 | struct complex_content: virtual empty_content<C> |
|---|
| 120 | { |
|---|
| 121 | // |
|---|
| 122 | // |
|---|
| 123 | virtual void |
|---|
| 124 | _start_element (const ro_string<C>& ns, |
|---|
| 125 | const ro_string<C>& name, |
|---|
| 126 | const ro_string<C>* type); |
|---|
| 127 | |
|---|
| 128 | virtual void |
|---|
| 129 | _end_element (const ro_string<C>& ns, |
|---|
| 130 | const ro_string<C>& name); |
|---|
| 131 | |
|---|
| 132 | virtual void |
|---|
| 133 | _attribute (const ro_string<C>& ns, |
|---|
| 134 | const ro_string<C>& name, |
|---|
| 135 | const ro_string<C>& value); |
|---|
| 136 | |
|---|
| 137 | virtual void |
|---|
| 138 | _characters (const ro_string<C>&); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | // |
|---|
| 142 | // |
|---|
| 143 | virtual void |
|---|
| 144 | _pre_impl (); |
|---|
| 145 | |
|---|
| 146 | virtual void |
|---|
| 147 | _post_impl (); |
|---|
| 148 | |
|---|
| 149 | protected: |
|---|
| 150 | struct state |
|---|
| 151 | { |
|---|
| 152 | state () |
|---|
| 153 | : any_ (false), depth_ (0), parser_ (0) |
|---|
| 154 | { |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | bool any_; |
|---|
| 158 | std::size_t depth_; |
|---|
| 159 | parser_base<C>* parser_; |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | // Optimized state stack for non-recursive case (one element). |
|---|
| 163 | // |
|---|
| 164 | struct state_stack |
|---|
| 165 | { |
|---|
| 166 | state_stack () |
|---|
| 167 | : size_ (0) |
|---|
| 168 | { |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void |
|---|
| 172 | push (const state& s) |
|---|
| 173 | { |
|---|
| 174 | if (size_ > 0) |
|---|
| 175 | rest_.push (top_); |
|---|
| 176 | |
|---|
| 177 | top_ = s; |
|---|
| 178 | ++size_; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | void |
|---|
| 182 | pop () |
|---|
| 183 | { |
|---|
| 184 | if (size_ > 1) |
|---|
| 185 | { |
|---|
| 186 | top_ = rest_.top (); |
|---|
| 187 | rest_.pop (); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | --size_; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | const state& |
|---|
| 194 | top () const |
|---|
| 195 | { |
|---|
| 196 | return top_; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | state& |
|---|
| 200 | top () |
|---|
| 201 | { |
|---|
| 202 | return top_; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | state& |
|---|
| 206 | under_top () |
|---|
| 207 | { |
|---|
| 208 | return rest_.top (); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | private: |
|---|
| 212 | state top_; |
|---|
| 213 | std::stack<state> rest_; |
|---|
| 214 | std::size_t size_; |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | state_stack context_; |
|---|
| 218 | }; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | // Base for xsd:list. |
|---|
| 222 | // |
|---|
| 223 | template <typename C> |
|---|
| 224 | struct list_base: virtual simple_content<C> |
|---|
| 225 | { |
|---|
| 226 | virtual void |
|---|
| 227 | _xsd_parse_item (const ro_string<C>&) = 0; |
|---|
| 228 | |
|---|
| 229 | virtual void |
|---|
| 230 | _pre (); |
|---|
| 231 | |
|---|
| 232 | virtual void |
|---|
| 233 | _characters (const ro_string<C>&); |
|---|
| 234 | |
|---|
| 235 | virtual void |
|---|
| 236 | _post (); |
|---|
| 237 | |
|---|
| 238 | protected: |
|---|
| 239 | std::basic_string<C> buf_; |
|---|
| 240 | }; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | #include <xsd/cxx/parser/non-validating/parser.txx> |
|---|
| 247 | |
|---|
| 248 | #endif // XSD_CXX_PARSER_NON_VALIDATING_PARSER_HXX |
|---|