Vaucanson  1.4.1
regexp.hxx
Go to the documentation of this file.
1 // regexp.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_REGEXP_HXX
19 # define VCSN_XML_REGEXP_HXX
20 
21 # include <set>
22 
23 # include <xercesc/util/XMLString.hpp>
24 
25 # include <vaucanson/algebra/concept/monoid_base.hh>
26 # include <vaucanson/algebra/concept/freemonoid_base.hh>
27 
28 # include <vaucanson/xml/tools.hh>
29 
30 namespace vcsn
31 {
32  namespace xml
33  {
34  /*
35  * RegexpHandler
36  */
37  template <typename T>
38  RegexpHandler<T>::RegexpHandler (xercesc::SAX2XMLReader* parser,
39  Handler& root,
40  T param)
41  : Handler(parser, root),
42  end_(eq_.typedRegExp),
43  param_(param),
44  lefth_(0)
45  {
46  }
47 
48  template <typename T>
49  RegexpHandler<T>::RegexpHandler (xercesc::SAX2XMLReader* parser,
50  Handler& root,
51  T param,
52  XMLCh* end)
53  : Handler(parser, root),
54  end_(end),
55  param_(param),
56  lefth_(0)
57  {
58  }
59 
60  template <typename T>
61  RegexpHandler<T>::~RegexpHandler()
62  {
63  delete lefth_;
64  }
65 
66  template <typename T>
67  void
68  RegexpHandler<T>::start (const XMLCh* const,
69  const XMLCh* const localname,
70  const XMLCh* const,
71  const xercesc::Attributes&)
72  {
73  if (!(lefth_ = create(localname)))
74  error::token(localname);
75  else
76  parser_->setContentHandler(lefth_);
77  }
78 
79  template <typename T>
80  void
81  RegexpHandler<T>::end (const XMLCh* const,
82  const XMLCh* const localname,
83  const XMLCh* const)
84  {
85  using namespace xercesc;
86  if (XMLString::equals(end_, localname))
87  {
88  param_ = lefth_->series();
89  parser_->setContentHandler(&root_);
90  }
91  else
92  error::token(localname);
93  }
94 
95  template <typename T>
96  T&
97  RegexpHandler<T>::series()
98  {
99  return param_;
100  }
101 
102  template <typename T>
103  T&
104  RegexpHandler<T>::series(T& param)
105  {
106  param_ = param;
107  return param_;
108  }
109 
110  /*
111  * StarHandler
112  */
113  template <typename T>
114  StarHandler<T>::StarHandler (xercesc::SAX2XMLReader* parser,
115  Handler& root,
116  T param)
117  : RegexpHandler<T>(parser, root, param),
118  in_(0)
119  {
120  end_ = eq_.star;
121  }
122 
123  template <typename T>
124  void
125  StarHandler<T>::start (const XMLCh* const,
126  const XMLCh* const localname,
127  const XMLCh* const,
128  const xercesc::Attributes&)
129  {
130  using namespace xercesc;
131  in_++;
132  if (in_ == 1 && (lefth_ = create(localname)))
133  parser_->setContentHandler(lefth_);
134  else
135  error::token(localname);
136  }
137 
138  template <typename T>
139  void
140  StarHandler<T>::end (const XMLCh* const,
141  const XMLCh* const localname,
142  const XMLCh* const)
143  {
144  using namespace xercesc;
145  if (XMLString::equals(end_, localname))
146  {
147  param_ = lefth_->series().star();
148  parser_->setContentHandler(&root_);
149  }
150  else
151  error::token(localname);
152  }
153 
154  /*
155  * AtomHandler
156  */
157  template <typename T>
158  AtomHandler<T>::AtomHandler (xercesc::SAX2XMLReader* parser,
159  Handler& root,
160  T param,
161  XMLCh* end)
162  : RegexpHandler<T>(parser, root, param, end)
163  {
164  }
165 
166  template <typename T>
167  void
168  AtomHandler<T>::start (const XMLCh* const,
169  const XMLCh* const localname,
170  const XMLCh* const,
171  const xercesc::Attributes&)
172  {
173  error::token(localname);
174  }
175 
176  template <typename T>
177  void
178  AtomHandler<T>::end (const XMLCh* const,
179  const XMLCh* const localname,
180  const XMLCh* const)
181  {
182  using namespace xercesc;
183  if (XMLString::equals(end_, localname))
184  parser_->setContentHandler(&root_);
185  else
186  error::token(localname);
187  }
188 
189  /*
190  * WeightHandler
191  */
192  template <typename T>
193  WeightHandler<T>::WeightHandler (xercesc::SAX2XMLReader* parser,
194  Handler& root,
195  T param)
196  : RegexpHandler<T>(parser, root, param)
197  {
198  end_ = eq_.weight;
199  }
200 
201  template <typename T>
202  void
203  WeightHandler<T>::start (const XMLCh* const,
204  const XMLCh* const localname,
205  const XMLCh* const,
206  const xercesc::Attributes&)
207  {
208  error::token(localname);
209  }
210 
211  template <typename T>
212  void
213  WeightHandler<T>::end (const XMLCh* const,
214  const XMLCh* const localname,
215  const XMLCh* const)
216  {
217  using namespace xercesc;
218  if (XMLString::equals(end_, localname))
219  parser_->setContentHandler(&root_);
220  else
221  error::token(localname);
222  }
223 
224  /*
225  * ExtMulHandler
226  */
227  template <typename T>
228  ExtMulHandler<T>::ExtMulHandler (xercesc::SAX2XMLReader* parser,
229  Handler& root,
230  T param,
231  bool left)
232  : RegexpHandler<T>(parser, root, param),
233  left_(left),
234  righth_(0)
235  {
236  if (left)
237  end_ = eq_.leftExtMul;
238  else
239  end_ = eq_.rightExtMul;
240  }
241 
242  template <typename T>
243  ExtMulHandler<T>::~ExtMulHandler ()
244  {
245  delete righth_;
246  }
247 
248  template <typename T>
249  void
250  ExtMulHandler<T>::start (const XMLCh* const,
251  const XMLCh* const localname,
252  const XMLCh* const,
253  const xercesc::Attributes& attrs)
254  {
255  using namespace xercesc;
256  if (XMLString::equals(eq_.weight, localname) &&
257  (lefth_ = create_weight(attrs)))
258  parser_->setContentHandler(lefth_);
259  else if ((righth_ = create(localname)))
260  parser_->setContentHandler(righth_);
261  else
262  error::token(localname);
263  }
264 
265  template <typename T>
266  void
267  ExtMulHandler<T>::end (const XMLCh* const,
268  const XMLCh* const localname,
269  const XMLCh* const)
270  {
271  using namespace xercesc;
272  if (XMLString::equals(end_, localname))
273  {
274  if (left_)
275  param_ = lefth_->series() * righth_->series();
276  else
277  param_ = righth_->series() * lefth_->series();
278  parser_->setContentHandler(&root_);
279  }
280  else
281  error::token(localname);
282  }
283 
284  /*
285  * ProductHandler
286  */
287  // FIXME: should factorize
288  template <typename T>
289  ProductHandler<T>::ProductHandler (xercesc::SAX2XMLReader* parser,
290  Handler& root,
291  T param)
292  : RegexpHandler<T>(parser, root, param),
293  in_(1),
294  righth_(0)
295  {
296  end_ = eq_.product;
297  }
298 
299  template <typename T>
300  ProductHandler<T>::~ProductHandler()
301  {
302  delete righth_;
303  }
304 
305  template <typename T>
306  void
307  ProductHandler<T>::start (const XMLCh* const,
308  const XMLCh* const localname,
309  const XMLCh* const,
310  const xercesc::Attributes&)
311  {
312  using namespace xercesc;
313  if (in_ == 1 && (lefth_ = create(localname)))
314  parser_->setContentHandler(lefth_);
315  else if (in_ == 2 && (righth_ = create(localname)))
316  parser_->setContentHandler(righth_);
317  else
318  error::token(localname);
319  in_++;
320  }
321 
322  template <typename T>
323  void
324  ProductHandler<T>::end (const XMLCh* const,
325  const XMLCh* const localname,
326  const XMLCh* const)
327  {
328  using namespace xercesc;
329  if (XMLString::equals(end_, localname))
330  {
331  param_ = lefth_->series() * righth_->series();
332  parser_->setContentHandler(&root_);
333  }
334  else if (!XMLString::equals(eq_.monGen, localname))
335  error::token(localname);
336  }
337  /*
338  * SumHandler
339  */
340  template <typename T>
341  SumHandler<T>::SumHandler (xercesc::SAX2XMLReader* parser,
342  Handler& root,
343  T param)
344  : RegexpHandler<T>(parser, root, param),
345  in_(1),
346  righth_(0)
347  {
348  end_ = eq_.sum;
349  }
350 
351  template <typename T>
352  SumHandler<T>::~SumHandler ()
353  {
354  delete righth_;
355  }
356 
357  template <typename T>
358  void
359  SumHandler<T>::start (const XMLCh* const,
360  const XMLCh* const localname,
361  const XMLCh* const,
362  const xercesc::Attributes&)
363  {
364  using namespace xercesc;
365 
366  if (in_ == 1 && (lefth_ = create(localname)))
367  parser_->setContentHandler(lefth_);
368  else if (in_ == 2 && (righth_ = create(localname)))
369  parser_->setContentHandler(righth_);
370  else
371  error::token(localname);
372 
373  in_++;
374  }
375 
376  template <typename T>
377  void
378  SumHandler<T>::end (const XMLCh* const,
379  const XMLCh* const localname,
380  const XMLCh* const)
381  {
382  using namespace xercesc;
383 
384  if (XMLString::equals(end_, localname))
385  {
386  param_ = lefth_->series() + righth_->series();
387  parser_->setContentHandler(&root_);
388  }
389  else if (!XMLString::equals(eq_.monGen, localname))
390  error::token(localname);
391  }
392 
393  /*
394  * MonElmtHandler
395  */
396  template <typename T>
397  MonElmtHandler<T>::MonElmtHandler (xercesc::SAX2XMLReader* parser,
398  Handler& root,
399  T param)
400  : RegexpHandler<T>(parser, root, param),
401  mongenh_(0)
402  {
403  end_ = eq_.monElmt;
404  }
405 
406  template <typename T>
407  void
408  MonElmtHandler<T>::start (const XMLCh* const,
409  const XMLCh* const localname,
410  const XMLCh* const,
411  const xercesc::Attributes& attrs)
412  {
413  // FIXME: This code is very similar to the one used by
414  // FreeMonoidHandler::start. Ultimately we should provide a way to
415  // parse "sequences of" (to promote reusability, and ease transcription
416  // from the XSD to parser code).
417  using namespace xercesc;
418 
419  if (XMLString::equals(eq_.monGen, localname))
420  {
421  typedef typename T::set_t::monoid_t monoid_t;
422  typedef T actee_t;
423 
424  // When we have a monGen, we will concatenate param_ with it.
425  monGenAction<actee_t> action(param_);
426 
427  // Delete the old handler.
428  delete mongenh_;
429 
430  // Choose statically the kind of generator.
431  if (algebra::letter_traits<typename monoid_t::alphabet_t::letter_t>::kind() == "simple")
432  {
433  const XMLCh* value = tools::get_attribute(attrs, "value");
434  mongenh_ = new monGenHandler<monoid_t, actee_t>(parser_, *this, action, value);
435  }
436  else
437  mongenh_ = new monGenTupleHandler<monoid_t, actee_t>(parser_, *this, action);
438 
439  // Setup the new handler.
440  parser_->setContentHandler(mongenh_);
441  }
442  else
443  error::token(localname);
444  }
445 
446  template <typename T>
447  void
448  MonElmtHandler<T>::end (const XMLCh* const,
449  const XMLCh* const localname,
450  const XMLCh* const)
451  {
452  using namespace xercesc;
453 
454  if (XMLString::equals(end_, localname))
455  {
456  // We are done with the "parent", so delete remaining data.
457  delete mongenh_;
458 
459  // Go up one level.
460  parser_->setContentHandler(&root_);
461  }
462  else if (!XMLString::equals(eq_.monGen, localname))
463  error::token(localname);
464  }
465 
466  } // !xml
467 } // !vcsn
468 
469 #endif // !VCSN_XML_REGEXP_HXX