1 | // file : xsd/cxx/tree/type-factory-map.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 | #include <xercesc/validators/schema/SchemaSymbols.hpp> |
---|
7 | |
---|
8 | #include <xsd/cxx/xml/string.hxx> // xml::{string, transcode} |
---|
9 | #include <xsd/cxx/xml/elements.hxx> // xml::{prefix, uq_name} |
---|
10 | #include <xsd/cxx/xml/bits/literals.hxx> // xml::bits::{xml_namespace, etc} |
---|
11 | |
---|
12 | #include <xsd/cxx/tree/types.hxx> |
---|
13 | #include <xsd/cxx/tree/bits/literals.hxx> |
---|
14 | |
---|
15 | namespace xsd |
---|
16 | { |
---|
17 | namespace cxx |
---|
18 | { |
---|
19 | namespace tree |
---|
20 | { |
---|
21 | // type_factory_map |
---|
22 | // |
---|
23 | template <typename C> |
---|
24 | type_factory_map<C>:: |
---|
25 | type_factory_map () |
---|
26 | { |
---|
27 | // Register factories for default instantiations of built-in, |
---|
28 | // non-fundamental types. |
---|
29 | // |
---|
30 | |
---|
31 | std::basic_string<C> xsd (bits::xml_schema<C> ()); |
---|
32 | |
---|
33 | |
---|
34 | // anyType and anySimpleType. |
---|
35 | // |
---|
36 | register_type ( |
---|
37 | qualified_name (bits::any_type<C> (), xsd), |
---|
38 | &factory_impl<type>, |
---|
39 | false); |
---|
40 | |
---|
41 | typedef simple_type<type> simple_type; |
---|
42 | register_type ( |
---|
43 | qualified_name (bits::any_simple_type<C> (), xsd), |
---|
44 | &factory_impl<simple_type>, |
---|
45 | false); |
---|
46 | |
---|
47 | |
---|
48 | // Strings |
---|
49 | // |
---|
50 | typedef string<C, simple_type> string; |
---|
51 | register_type ( |
---|
52 | qualified_name (bits::string<C> (), xsd), |
---|
53 | &factory_impl<string>, |
---|
54 | false); |
---|
55 | |
---|
56 | typedef normalized_string<C, string> normalized_string; |
---|
57 | register_type ( |
---|
58 | qualified_name (bits::normalized_string<C> (), xsd), |
---|
59 | &factory_impl<normalized_string>, |
---|
60 | false); |
---|
61 | |
---|
62 | typedef token<C, normalized_string> token; |
---|
63 | register_type ( |
---|
64 | qualified_name (bits::token<C> (), xsd), |
---|
65 | &factory_impl<token>, |
---|
66 | false); |
---|
67 | |
---|
68 | typedef name<C, token> name; |
---|
69 | register_type ( |
---|
70 | qualified_name (bits::name<C> (), xsd), |
---|
71 | &factory_impl<name>, |
---|
72 | false); |
---|
73 | |
---|
74 | typedef nmtoken<C, token> nmtoken; |
---|
75 | register_type ( |
---|
76 | qualified_name (bits::nmtoken<C> (), xsd), |
---|
77 | &factory_impl<nmtoken>, |
---|
78 | false); |
---|
79 | |
---|
80 | typedef nmtokens<C, simple_type, nmtoken> nmtokens; |
---|
81 | register_type ( |
---|
82 | qualified_name (bits::nmtokens<C> (), xsd), |
---|
83 | &factory_impl<nmtokens>, |
---|
84 | false); |
---|
85 | |
---|
86 | typedef ncname<C, name> ncname; |
---|
87 | register_type ( |
---|
88 | qualified_name (bits::ncname<C> (), xsd), |
---|
89 | &factory_impl<ncname>, |
---|
90 | false); |
---|
91 | |
---|
92 | typedef language<C, token> language; |
---|
93 | register_type ( |
---|
94 | qualified_name (bits::language<C> (), xsd), |
---|
95 | &factory_impl<language>, |
---|
96 | false); |
---|
97 | |
---|
98 | |
---|
99 | // ID/IDREF. |
---|
100 | // |
---|
101 | typedef id<C, ncname> id; |
---|
102 | register_type ( |
---|
103 | qualified_name (bits::id<C> (), xsd), |
---|
104 | &factory_impl<id>, |
---|
105 | false); |
---|
106 | |
---|
107 | typedef idref<type, C, ncname> idref; |
---|
108 | register_type ( |
---|
109 | qualified_name (bits::idref<C> (), xsd), |
---|
110 | &factory_impl<idref>, |
---|
111 | false); |
---|
112 | |
---|
113 | typedef idrefs<C, simple_type, idref> idrefs; |
---|
114 | register_type ( |
---|
115 | qualified_name (bits::idrefs<C> (), xsd), |
---|
116 | &factory_impl<idrefs>, |
---|
117 | false); |
---|
118 | |
---|
119 | |
---|
120 | // URI. |
---|
121 | // |
---|
122 | typedef uri<C, simple_type> uri; |
---|
123 | register_type ( |
---|
124 | qualified_name (bits::any_uri<C> (), xsd), |
---|
125 | &factory_impl<uri>, |
---|
126 | false); |
---|
127 | |
---|
128 | |
---|
129 | // Qualified name. |
---|
130 | // |
---|
131 | typedef qname<C, simple_type, uri, ncname> qname; |
---|
132 | register_type ( |
---|
133 | qualified_name (bits::qname<C> (), xsd), |
---|
134 | &factory_impl<qname>, |
---|
135 | false); |
---|
136 | |
---|
137 | |
---|
138 | // Binary. |
---|
139 | // |
---|
140 | typedef base64_binary<C, simple_type> base64_binary; |
---|
141 | register_type ( |
---|
142 | qualified_name (bits::base64_binary<C> (), xsd), |
---|
143 | &factory_impl<base64_binary>, |
---|
144 | false); |
---|
145 | |
---|
146 | typedef hex_binary<C, simple_type> hex_binary; |
---|
147 | register_type ( |
---|
148 | qualified_name (bits::hex_binary<C> (), xsd), |
---|
149 | &factory_impl<hex_binary>, |
---|
150 | false); |
---|
151 | |
---|
152 | |
---|
153 | // Date/time. |
---|
154 | // |
---|
155 | typedef gday<C, simple_type> gday; |
---|
156 | register_type ( |
---|
157 | qualified_name (bits::gday<C> (), xsd), |
---|
158 | &factory_impl<gday>, |
---|
159 | false); |
---|
160 | |
---|
161 | typedef gmonth<C, simple_type> gmonth; |
---|
162 | register_type ( |
---|
163 | qualified_name (bits::gmonth<C> (), xsd), |
---|
164 | &factory_impl<gmonth>, |
---|
165 | false); |
---|
166 | |
---|
167 | typedef gyear<C, simple_type> gyear; |
---|
168 | register_type ( |
---|
169 | qualified_name (bits::gyear<C> (), xsd), |
---|
170 | &factory_impl<gyear>, |
---|
171 | false); |
---|
172 | |
---|
173 | typedef gmonth_day<C, simple_type> gmonth_day; |
---|
174 | register_type ( |
---|
175 | qualified_name (bits::gmonth_day<C> (), xsd), |
---|
176 | &factory_impl<gmonth_day>, |
---|
177 | false); |
---|
178 | |
---|
179 | typedef gyear_month<C, simple_type> gyear_month; |
---|
180 | register_type ( |
---|
181 | qualified_name (bits::gyear_month<C> (), xsd), |
---|
182 | &factory_impl<gyear_month>, |
---|
183 | false); |
---|
184 | |
---|
185 | typedef date<C, simple_type> date; |
---|
186 | register_type ( |
---|
187 | qualified_name (bits::date<C> (), xsd), |
---|
188 | &factory_impl<date>, |
---|
189 | false); |
---|
190 | |
---|
191 | typedef time<C, simple_type> time; |
---|
192 | register_type ( |
---|
193 | qualified_name (bits::time<C> (), xsd), |
---|
194 | &factory_impl<time>, |
---|
195 | false); |
---|
196 | |
---|
197 | typedef date_time<C, simple_type> date_time; |
---|
198 | register_type ( |
---|
199 | qualified_name (bits::date_time<C> (), xsd), |
---|
200 | &factory_impl<date_time>, |
---|
201 | false); |
---|
202 | |
---|
203 | typedef duration<C, simple_type> duration; |
---|
204 | register_type ( |
---|
205 | qualified_name (bits::duration<C> (), xsd), |
---|
206 | &factory_impl<duration>, |
---|
207 | false); |
---|
208 | |
---|
209 | |
---|
210 | // Entity. |
---|
211 | // |
---|
212 | typedef entity<C, ncname> entity; |
---|
213 | register_type ( |
---|
214 | qualified_name (bits::entity<C> (), xsd), |
---|
215 | &factory_impl<entity>, |
---|
216 | false); |
---|
217 | |
---|
218 | typedef entities<C, simple_type, entity> entities; |
---|
219 | register_type ( |
---|
220 | qualified_name (bits::entities<C> (), xsd), |
---|
221 | &factory_impl<entities>, |
---|
222 | false); |
---|
223 | } |
---|
224 | |
---|
225 | template <typename C> |
---|
226 | void type_factory_map<C>:: |
---|
227 | register_type (const qualified_name& name, |
---|
228 | factory f, |
---|
229 | bool override) |
---|
230 | { |
---|
231 | if (override || type_map_.find (name) == type_map_.end ()) |
---|
232 | type_map_[name] = f; |
---|
233 | } |
---|
234 | |
---|
235 | template <typename C> |
---|
236 | void type_factory_map<C>:: |
---|
237 | register_element (const qualified_name& root, |
---|
238 | const qualified_name& subst, |
---|
239 | factory f) |
---|
240 | { |
---|
241 | element_map_[root][subst] = f; |
---|
242 | } |
---|
243 | |
---|
244 | template <typename C> |
---|
245 | typename type_factory_map<C>::factory type_factory_map<C>:: |
---|
246 | find (const qualified_name& name) const |
---|
247 | { |
---|
248 | typename type_map::const_iterator i (type_map_.find (name)); |
---|
249 | return i == type_map_.end () ? 0 : i->second; |
---|
250 | } |
---|
251 | |
---|
252 | template <typename C> |
---|
253 | std::auto_ptr<type> type_factory_map<C>:: |
---|
254 | create (const C* name, |
---|
255 | const C* ns, |
---|
256 | factory static_type, |
---|
257 | bool global, |
---|
258 | bool qualified, |
---|
259 | const xercesc::DOMElement& e, |
---|
260 | const qualified_name& qn, |
---|
261 | tree::flags flags, |
---|
262 | container* c) const |
---|
263 | { |
---|
264 | factory f = 0; |
---|
265 | |
---|
266 | // See if we've got a straight match. |
---|
267 | // |
---|
268 | if (qn.name () == name && |
---|
269 | (qualified ? qn.namespace_ () == ns : ns[0] == C ('\0'))) |
---|
270 | { |
---|
271 | f = static_type; |
---|
272 | } |
---|
273 | else if (global) |
---|
274 | { |
---|
275 | // See if we have a substitution. |
---|
276 | // |
---|
277 | typename element_map::const_iterator i ( |
---|
278 | element_map_.find (qualified_name (name, ns))); |
---|
279 | |
---|
280 | if (i != element_map_.end ()) |
---|
281 | { |
---|
282 | f = find_substitution (i->second, qn); |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | if (f == 0) |
---|
287 | return std::auto_ptr<type> (0); // No match. |
---|
288 | |
---|
289 | // Check for xsi:type |
---|
290 | // |
---|
291 | { |
---|
292 | const XMLCh* v ( |
---|
293 | e.getAttributeNS ( |
---|
294 | xercesc::SchemaSymbols::fgURI_XSI, |
---|
295 | xercesc::SchemaSymbols::fgXSI_TYPE)); |
---|
296 | |
---|
297 | if (v != 0 && v[0] != XMLCh (0)) |
---|
298 | f = find_type (xml::transcode<C> (v), e); |
---|
299 | } |
---|
300 | |
---|
301 | return f (e, flags, c); |
---|
302 | } |
---|
303 | |
---|
304 | template <typename C> |
---|
305 | template <typename X> |
---|
306 | std::auto_ptr<type> type_factory_map<C>:: |
---|
307 | traits_adapter (const xercesc::DOMElement& e, flags f, container* c) |
---|
308 | { |
---|
309 | std::auto_ptr<X> r (traits<X, C>::create (e, f, c)); |
---|
310 | return std::auto_ptr<type> (r.release ()); |
---|
311 | } |
---|
312 | |
---|
313 | template <typename C> |
---|
314 | typename type_factory_map<C>::factory type_factory_map<C>:: |
---|
315 | find_substitution (const subst_map& start, |
---|
316 | const qualified_name& name) const |
---|
317 | { |
---|
318 | typename subst_map::const_iterator i (start.find (name)); |
---|
319 | |
---|
320 | if (i != start.end ()) |
---|
321 | return i->second; |
---|
322 | else |
---|
323 | { |
---|
324 | for (i = start.begin (); i != start.end (); ++i) |
---|
325 | { |
---|
326 | typename element_map::const_iterator j ( |
---|
327 | element_map_.find (i->first)); |
---|
328 | |
---|
329 | if (j != element_map_.end ()) |
---|
330 | { |
---|
331 | if (factory f = find_substitution (j->second, name)) |
---|
332 | return f; |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | return 0; |
---|
338 | } |
---|
339 | |
---|
340 | template <typename C> |
---|
341 | typename type_factory_map<C>::factory type_factory_map<C>:: |
---|
342 | find_type (const std::basic_string<C>& name, |
---|
343 | const xercesc::DOMElement& e) const |
---|
344 | { |
---|
345 | using std::basic_string; |
---|
346 | |
---|
347 | basic_string<C> ns_name, uq_name (xml::uq_name (name)); |
---|
348 | |
---|
349 | // Copied with modifications from xml/dom/elements.hxx. |
---|
350 | // |
---|
351 | std::basic_string<C> p (xml::prefix (name)); |
---|
352 | |
---|
353 | // 'xml' prefix requires special handling and Xerces folks refuse |
---|
354 | // to handle this in DOM so I have to do it myself. |
---|
355 | // |
---|
356 | if (p == xml::bits::xml_prefix<C> ()) |
---|
357 | ns_name = xml::bits::xml_namespace<C> (); |
---|
358 | else |
---|
359 | { |
---|
360 | const XMLCh* xns ( |
---|
361 | e.lookupNamespaceURI ( |
---|
362 | p.empty () ? 0 : xml::string (p).c_str ())); |
---|
363 | |
---|
364 | if (xns != 0) |
---|
365 | ns_name = xml::transcode<C> (xns); |
---|
366 | else |
---|
367 | { |
---|
368 | // See if we've got any no-namespace types. |
---|
369 | // |
---|
370 | if (!p.empty ()) |
---|
371 | throw no_prefix_mapping<C> (p); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | factory f (find (qualified_name (uq_name, ns_name))); |
---|
376 | |
---|
377 | if (f == 0) |
---|
378 | throw no_type_info<C> (uq_name, ns_name); |
---|
379 | |
---|
380 | return f; |
---|
381 | } |
---|
382 | |
---|
383 | |
---|
384 | // type_factory_plate |
---|
385 | // |
---|
386 | template<unsigned long id, typename C> |
---|
387 | type_factory_plate<id, C>:: |
---|
388 | type_factory_plate () |
---|
389 | { |
---|
390 | if (count == 0) |
---|
391 | map = new type_factory_map<C>; |
---|
392 | |
---|
393 | ++count; |
---|
394 | } |
---|
395 | |
---|
396 | template<unsigned long id, typename C> |
---|
397 | type_factory_plate<id, C>:: |
---|
398 | ~type_factory_plate () |
---|
399 | { |
---|
400 | if (--count == 0) |
---|
401 | delete map; |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | // |
---|
406 | // |
---|
407 | template<typename X> |
---|
408 | std::auto_ptr<type> |
---|
409 | factory_impl (const xercesc::DOMElement& e, flags f, container* c) |
---|
410 | { |
---|
411 | return std::auto_ptr<type> (new X (e, f, c)); |
---|
412 | } |
---|
413 | |
---|
414 | // |
---|
415 | // |
---|
416 | template<unsigned long id, typename C, typename X> |
---|
417 | type_factory_initializer<id, C, X>:: |
---|
418 | type_factory_initializer (const C* name, const C* ns) |
---|
419 | { |
---|
420 | type_factory_map_instance<id, C> ().register_type ( |
---|
421 | xml::qualified_name<C> (name, ns), &factory_impl<X>); |
---|
422 | } |
---|
423 | |
---|
424 | template<unsigned long id, typename C, typename X> |
---|
425 | type_factory_initializer<id, C, X>:: |
---|
426 | type_factory_initializer (const C* root_name, const C* root_ns, |
---|
427 | const C* subst_name, const C* subst_ns) |
---|
428 | { |
---|
429 | type_factory_map_instance<id, C> ().register_element ( |
---|
430 | xml::qualified_name<C> (root_name, root_ns), |
---|
431 | xml::qualified_name<C> (subst_name, subst_ns), |
---|
432 | &factory_impl<X>); |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|
436 | } |
---|