Vaucanson  1.4.1
xml/handlers.hxx
1 // handlers.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 
18 #ifndef VCSN_XML_HANDLERS_HXX
19 # define VCSN_XML_HANDLERS_HXX
20 
21 # include <xercesc/util/XMLString.hpp>
22 
23 # include <vaucanson/xml/tools.hh>
24 
25 namespace vcsn
26 {
27  namespace xml
28  {
29  /*
30  * DocRegExpHandler
31  */
32  template <typename T>
33  DocRegExpHandler<T>::DocRegExpHandler (xercesc::SAX2XMLReader* parser,
34  xercesc::DefaultHandler& root,
35  T& regexp,
36  XMLEq& eq)
37  : Handler(parser, root, eq),
38  regexp_(regexp),
39  regexph_(parser, *this, regexp)
40  {
41  }
42 
43  template <typename T>
44  void
45  DocRegExpHandler<T>::start (const XMLCh* const,
46  const XMLCh* const localname,
47  const XMLCh* const,
48  const xercesc::Attributes&)
49  {
50  using namespace xercesc;
51  if (XMLString::equals(eq_.fsmxml, localname))
52  {
53  // T version
54  // T xmlns
55  parser_->setContentHandler(&regexph_);
56  }
57  else
58  error::token(localname);
59  }
60 
61  template <typename T>
62  void
63  DocRegExpHandler<T>::end (const XMLCh* const,
64  const XMLCh* const,
65  const XMLCh* const)
66  {
67  }
68 
69  /*
70  * DocAutHandler
71  */
72  template <typename T>
73  DocAutHandler<T>::DocAutHandler (xercesc::SAX2XMLReader* parser,
74  xercesc::DefaultHandler& root,
75  T& aut,
76  XMLEq& eq)
77  : Handler(parser, root, eq),
78  aut_(aut),
79  auth_(parser, *this, aut)
80  {
81  }
82 
83  template <typename T>
84  void
85  DocAutHandler<T>::start (const XMLCh* const,
86  const XMLCh* const localname,
87  const XMLCh* const,
88  const xercesc::Attributes&)
89  {
90  using namespace xercesc;
91  if (XMLString::equals(eq_.fsmxml, localname))
92  {
93  // T version
94  // T xmlns
95  parser_->setContentHandler(&auth_);
96  }
97  else
98  error::token(localname);
99  }
100 
101  template <typename T>
102  void
103  DocAutHandler<T>::end (const XMLCh* const,
104  const XMLCh* const,
105  const XMLCh* const)
106  {
107  }
108 
112  template <typename T>
113  RegExpHandler<T>::RegExpHandler (xercesc::SAX2XMLReader* parser,
114  Handler& root,
115  T& regexp)
116  : Handler(parser, root),
117  regexp_(regexp),
118  typeh_(parser, *this, regexp),
119  contenth_(parser, *this, regexp, eq_.typedRegExp),
120  unsuph_(parser, *this)
121  {
122  }
123 
124  template <typename T>
125  void
126  RegExpHandler<T>::start (const XMLCh* const uri,
127  const XMLCh* const localname,
128  const XMLCh* const,
129  const xercesc::Attributes& attrs)
130  {
131  using namespace xercesc;
132  if (XMLString::equals(eq_.regExp, localname))
133  {
134  // nothing to do ?
135  }
136  else if (XMLString::equals(eq_.drawing, localname))
137  parser_->setContentHandler(&unsuph_);
138  else if (XMLString::equals(eq_.geometry, localname))
139  parser_->setContentHandler(&unsuph_);
140  else if (XMLString::equals(eq_.valueType, localname))
141  parser_->setContentHandler(&typeh_);
142  else if (XMLString::equals(eq_.typedRegExp, localname))
143  {
144  contenth_.series(regexp_);
145  parser_->setContentHandler(&contenth_);
146  }
147  else
148  error::token(localname);
149  }
150 
151  template <typename T>
152  void
153  RegExpHandler<T>::end (const XMLCh* const,
154  const XMLCh* const localname,
155  const XMLCh* const)
156  {
157  using namespace xercesc;
158  if (XMLString::equals(eq_.regExp, localname))
159  {
160  parser_->setContentHandler(&root_);
161  regexp_ = contenth_.series();
162  }
163  }
164 
168  template <typename T>
169  AutHandler<T>::AutHandler (xercesc::SAX2XMLReader* parser,
170  Handler& root,
171  T& aut)
172  : Handler(parser, root),
173  aut_(aut),
174  typeh_(parser, *this, aut),
175  contenth_(parser, *this, aut),
176  unsuph_(parser, *this)
177  {
178  }
179 
180  template <typename T>
181  void
182  AutHandler<T>::start (const XMLCh* const uri,
183  const XMLCh* const localname,
184  const XMLCh* const,
185  const xercesc::Attributes& attrs)
186  {
187  using namespace xercesc;
188  if (XMLString::equals(eq_.automaton, localname))
189  {
190  const XMLCh* autname = tools::get_attribute(attrs, "name", uri);
191  if (autname != 0)
192  aut_.geometry().name() = xmlstr(autname);
193  // T readingDirection
194  }
195  else if (XMLString::equals(eq_.drawing, localname))
196  parser_->setContentHandler(&unsuph_);
197  else if (XMLString::equals(eq_.geometry, localname))
198  parser_->setContentHandler(&unsuph_);
199  else if (XMLString::equals(eq_.valueType, localname))
200  parser_->setContentHandler(&typeh_);
201  else if (XMLString::equals(eq_.content, localname))
202  parser_->setContentHandler(&contenth_);
203  else
204  error::token(localname);
205  }
206 
207  template <typename T>
208  void
209  AutHandler<T>::end (const XMLCh* const,
210  const XMLCh* const localname,
211  const XMLCh* const)
212  {
213  using namespace xercesc;
214  if (XMLString::equals(eq_.automaton, localname))
215  parser_->setContentHandler(&root_);
216  }
217 
221  template <typename T>
222  TypeHandler<T>::TypeHandler (xercesc::SAX2XMLReader* parser,
223  Handler& root,
224  T& param)
225  : Handler(parser, root),
226  monoid_(0),
227  semiring_(0),
228  rep_(0),
229  param_(param),
230  monoidh_(0),
231  reph_(0),
232  semiringh_(0),
233  unsuph_(parser, *this)
234  {
235  }
236 
237  template <typename T>
238  void
239  TypeHandler<T>::start(const XMLCh* const,
240  const XMLCh* const localname,
241  const XMLCh* const,
242  const xercesc::Attributes& attrs)
243  {
244  using namespace xercesc;
245 
246  if (XMLString::equals(eq_.semiring, localname))
247  {
248  semiring_ = builders::create_semiring(param_, localname, attrs);
249  semiringh_ = builders::create_semiringh(*semiring_, attrs, parser_, *this);
250  parser_->setContentHandler(semiringh_);
251  }
252  else if (XMLString::equals(eq_.monoid, localname))
253  {
254  monoid_ = builders::create_monoid(param_, localname, attrs, eq_);
255  monoidh_ = builders::create_monoidh(*monoid_, attrs, parser_, *this);
256  parser_->setContentHandler(monoidh_);
257  }
258  else if (XMLString::equals(eq_.writingData, localname))
259  {
260  rep_ = builders::create_series_representation(param_, localname, attrs, eq_);
261  reph_ = builders::create_series_representationh(*rep_, attrs, parser_, *this, eq_);
262  parser_->setContentHandler(reph_);
263  }
264  else
265  error::token(localname);
266  }
267 
268  template <typename T>
269  void
270  TypeHandler<T>::end(const XMLCh* const,
271  const XMLCh* const localname,
272  const XMLCh* const)
273  {
274  using namespace xercesc;
275 
276 
277  // The series representation is optional.
278  if (rep_)
279  {
280  typename T::series_set_t series(*semiring_, *monoid_, *rep_);
281  param_.attach(series);
282  }
283  else
284  {
285  typename T::series_set_t series(*semiring_, *monoid_);
286  param_.attach(series);
287  }
288 
289  if (XMLString::equals(eq_.valueType, localname))
290  parser_->setContentHandler(&root_);
291 
292  delete monoid_;
293  delete monoidh_;
294  delete rep_;
295  delete reph_;
296  delete semiring_;
297  delete semiringh_;
298  }
299 
303  template <typename T>
304  ContHandler<T>::ContHandler (xercesc::SAX2XMLReader* parser,
305  Handler& root,
306  T& aut)
307  : Handler(parser, root),
308  aut_(aut),
309  statesh_(parser, *this, aut, map_),
310  transitionsh_(parser, *this, aut, map_)
311  {
312  }
313 
314  template <typename T>
315  void
316  ContHandler<T>::start (const XMLCh* const,
317  const XMLCh* const localname,
318  const XMLCh* const,
319  const xercesc::Attributes&)
320  {
321  using namespace xercesc;
322  if (XMLString::equals(eq_.states, localname))
323  parser_->setContentHandler(&statesh_);
324  else if (XMLString::equals(eq_.transitions, localname))
325  parser_->setContentHandler(&transitionsh_);
326  else
327  error::token(localname);
328  }
329 
330  template <typename T>
331  void
332  ContHandler<T>::end (const XMLCh* const,
333  const XMLCh* const localname,
334  const XMLCh* const)
335  {
336  using namespace xercesc;
337  if (XMLString::equals(eq_.content, localname))
338  parser_->setContentHandler(&root_);
339  else
340  error::token(localname);
341  }
342 
346  template <typename T>
347  StatesHandler<T>::StatesHandler (xercesc::SAX2XMLReader* parser,
348  Handler& root,
349  T& aut,
350  map_t& map)
351  : Handler(parser, root),
352  aut_(aut),
353  map_(map),
354  stateh_(0)
355  {
356  }
357 
358  template <typename T>
359  void
360  StatesHandler<T>::start (const XMLCh* const,
361  const XMLCh* const localname,
362  const XMLCh* const,
363  const xercesc::Attributes& attrs)
364  {
365  using namespace xercesc;
366  if (XMLString::equals(eq_.state, localname))
367  {
368  hstate_t state = aut_.add_state();
369  map_[xmlstr(tools::get_attribute(attrs, eq_.id))] = state;
370  delete stateh_;
371  stateh_ = new StateHandler<T>(parser_, *this, aut_, state);
372  parser_->setContentHandler(stateh_);
373  }
374  else
375  error::token(localname);
376  }
377 
378  template <typename T>
379  void
380  StatesHandler<T>::end (const XMLCh* const,
381  const XMLCh* const localname,
382  const XMLCh* const)
383  {
384  using namespace xercesc;
385  if (XMLString::equals(eq_.states, localname))
386  parser_->setContentHandler(&root_);
387  else
388  error::token(localname);
389  delete stateh_;
390  }
391 
395  template <typename T>
396  StateHandler<T>::StateHandler (xercesc::SAX2XMLReader* parser,
397  Handler& root,
398  T& aut,
399  hstate_t state)
400  : Handler(parser, root),
401  aut_(aut),
402  state_(state),
403  geometryh_(parser, *this, aut, state),
404  unsuph_(parser, *this)
405  {
406  }
407 
408  template <typename T>
409  void
410  StateHandler<T>::start (const XMLCh* const,
411  const XMLCh* const localname,
412  const XMLCh* const,
413  const xercesc::Attributes& attrs)
414  {
415  using namespace xercesc;
416  if (XMLString::equals(eq_.drawing, localname))
417  parser_->setContentHandler(&unsuph_);
418  else if (XMLString::equals(eq_.geometry, localname))
419  {
420  typedef typename T::geometry_t::states_geometry_map_t gmap_t;
421 
422  double y = 0;
423  double x = 0;
424  if (tools::has_attribute(attrs, eq_.x))
425  {
426  std::istringstream xstr(xmlstr(tools::get_attribute(attrs, eq_.x)));
427  xstr >> x;
428  }
429  if (tools::has_attribute(attrs, eq_.y))
430  {
431  std::istringstream ystr(xmlstr(tools::get_attribute(attrs, eq_.y)));
432  ystr >> y;
433  }
434  gmap_t& map = aut_.geometry().states();
435  map[state_] = std::make_pair(x,y);
436  parser_->setContentHandler(&geometryh_);
437  }
438  else
439  error::token(localname);
440  }
441 
442  template <typename T>
443  void
444  StateHandler<T>::end (const XMLCh* const,
445  const XMLCh* const localname,
446  const XMLCh* const)
447  {
448  using namespace xercesc;
449  if (XMLString::equals(eq_.state, localname))
450  parser_->setContentHandler(&root_);
451  else
452  error::token(localname);
453  }
454 
458  template <typename T>
459  TransitionsHandler<T>::TransitionsHandler (xercesc::SAX2XMLReader* parser,
460  Handler& root,
461  T& aut,
462  map_t& map)
463  : Handler(parser, root),
464  aut_(aut),
465  map_(map),
466  transitionh_(0)
467  {
468  }
469 
470  template <typename T>
471  void
472  TransitionsHandler<T>::start (const XMLCh* const,
473  const XMLCh* const localname,
474  const XMLCh* const,
475  const xercesc::Attributes& attrs)
476  {
477  using namespace xercesc;
478  if (XMLString::equals(eq_.transition, localname))
479  {
480  hstate_t src = map_[xmlstr(tools::get_attribute(attrs, eq_.source))];
481  hstate_t dst = map_[xmlstr(tools::get_attribute(attrs, eq_.target))];
482  delete transitionh_;
483  transitionh_ = new TransitionHandler<T>(parser_, *this, aut_, src, dst);
484  parser_->setContentHandler(transitionh_);
485  }
486  else if (XMLString::equals(eq_.final, localname))
487  {
488  hstate_t state = map_[xmlstr(tools::get_attribute(attrs, eq_.state))];
489  delete transitionh_;
490  transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, false);
491  parser_->setContentHandler(transitionh_);
492  }
493  else if (XMLString::equals(eq_.initial, localname))
494  {
495  hstate_t state = map_[xmlstr(tools::get_attribute(attrs, eq_.state))];
496  delete transitionh_;
497  transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, true);
498  parser_->setContentHandler(transitionh_);
499  }
500  else
501  error::token(localname);
502  }
503 
504  template <typename T>
505  void
506  TransitionsHandler<T>::end (const XMLCh* const,
507  const XMLCh* const localname,
508  const XMLCh* const)
509  {
510  using namespace xercesc;
511  if (XMLString::equals(eq_.transitions, localname))
512  parser_->setContentHandler(&root_);
513  else
514  error::token(localname);
515  delete transitionh_;
516  }
517 
521  template <typename T>
522  TransitionHandler<T>::TransitionHandler (xercesc::SAX2XMLReader* parser,
523  Handler& root,
524  T& aut,
525  hstate_t src,
526  hstate_t dst)
527  : Handler(parser, root),
528  aut_(aut),
529  src_(src),
530  dst_(dst),
531  s_(aut.series()),
532  labelh_(parser, *this, s_, eq_.label),
533  unsuph_(parser, *this)
534  {
535  }
536 
537  template <typename T>
538  void
539  TransitionHandler<T>::start (const XMLCh* const,
540  const XMLCh* const localname,
541  const XMLCh* const,
542  const xercesc::Attributes&)
543  {
544  using namespace xercesc;
545  if (XMLString::equals(eq_.drawing, localname))
546  parser_->setContentHandler(&unsuph_);
547  else if (XMLString::equals(eq_.geometry, localname))
548  parser_->setContentHandler(&unsuph_);
549  else if (XMLString::equals(eq_.label, localname))
550  parser_->setContentHandler(&labelh_);
551  else
552  error::token(localname);
553  }
554 
555  template <typename T>
556  void
557  TransitionHandler<T>::end (const XMLCh* const,
558  const XMLCh* const localname,
559  const XMLCh* const)
560  {
561  using namespace xercesc;
562  if (XMLString::equals(eq_.transition, localname))
563  {
564  parser_->setContentHandler(&root_);
565  aut_.add_series_transition(src_, dst_, labelh_.series());
566  }
567  else
568  error::token(localname);
569  }
570 
574  template <typename T>
575  InitFinalHandler<T>::InitFinalHandler (xercesc::SAX2XMLReader* parser,
576  Handler& root,
577  T& aut,
578  hstate_t state,
579  bool initial)
580  : Handler(parser, root),
581  aut_(aut),
582  state_(state),
583  initial_(initial),
584  s_(aut.series().one_), // hack shouldn't exist.
585  labelh_(parser, *this, s_, eq_.label),
586  unsuph_(parser, *this)
587  {
588  }
589 
590  template <typename T>
591  void
592  InitFinalHandler<T>::start (const XMLCh* const,
593  const XMLCh* const localname,
594  const XMLCh* const,
595  const xercesc::Attributes&)
596  {
597  using namespace xercesc;
598  if (XMLString::equals(eq_.drawing, localname))
599  parser_->setContentHandler(&unsuph_);
600  else if (XMLString::equals(eq_.geometry, localname))
601  parser_->setContentHandler(&unsuph_);
602  else if (XMLString::equals(eq_.label, localname))
603  parser_->setContentHandler(&labelh_);
604  else
605  error::token(localname);
606  }
607 
608  template <typename T>
609  void
610  InitFinalHandler<T>::end (const XMLCh* const,
611  const XMLCh* const localname,
612  const XMLCh* const)
613  {
614  using namespace xercesc;
615  if (XMLString::equals(eq_.initial, localname) && initial_)
616  {
617  parser_->setContentHandler(&root_);
618  aut_.set_initial(state_, labelh_.series());
619  }
620  else if (XMLString::equals(eq_.final, localname) && !initial_)
621  {
622  parser_->setContentHandler(&root_);
623  aut_.set_final(state_, labelh_.series());
624  }
625  else
626  error::token(localname);
627  }
628 
632  template <typename T>
633  GeometryHandler<T>::GeometryHandler (xercesc::SAX2XMLReader* parser,
634  Handler& root,
635  T& aut,
636  hstate_t state)
637  : Handler(parser, root),
638  aut_(aut),
639  state_(state)
640  {
641  }
642 
643  template <typename T>
644  void
645  GeometryHandler<T>::start (const XMLCh* const,
646  const XMLCh* const localname,
647  const XMLCh* const,
648  const xercesc::Attributes&)
649  {
650  error::token(localname);
651  }
652 
653  template <typename T>
654  void
655  GeometryHandler<T>::end (const XMLCh* const,
656  const XMLCh* const localname,
657  const XMLCh* const)
658  {
659  using namespace xercesc;
660  if (XMLString::equals(eq_.geometry, localname))
661  parser_->setContentHandler(&root_);
662  else
663  error::token(localname);
664  }
665 
669  template <typename T>
670  RegexpHandler<T>*
671  RegexpHandler<T>::create (const XMLCh* const localname)
672  {
673  using namespace xercesc;
674  if (XMLString::equals(eq_.monElmt, localname))
675  return builders::create_monElmth(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
676  else if (XMLString::equals(eq_.star, localname))
677  return new StarHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
678  else if (XMLString::equals(eq_.leftExtMul, localname))
679  return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), true);
680  else if (XMLString::equals(eq_.rightExtMul, localname))
681  return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), false);
682  else if (XMLString::equals(eq_.sum, localname))
683  return new SumHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
684  else if (XMLString::equals(eq_.product, localname))
685  return new ProductHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
686  else if (XMLString::equals(eq_.one, localname))
687  return new AtomHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), eq_.one);
688  else if (XMLString::equals(eq_.zero, localname))
689  return new AtomHandler<T>(parser_, *this, algebra::zero_as<typename T::value_t>::of(param_.structure()), eq_.zero);
690  else
691  return 0;
692  }
693 
694  template <typename T>
696  RegexpHandler<T>::create_weight (const xercesc::Attributes& attrs)
697  {
698  using namespace xercesc;
699  return builders::create_weighth(parser_, *this,
700  algebra::identity_as<typename T::value_t>::of(param_.structure()), attrs);
701  }
702  } // !xml
703 } // !vcsn
704 
705 #endif // !VCSN_XML_HANDLERS_HXX