Vaucanson  1.4.1
xml_exp_visitor.hxx
1 // xml_exp_visitor.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2006, 2008, 2009 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 #ifndef VCSN_XML_XML_EXP_VISITOR_HXX
18 # define VCSN_XML_XML_EXP_VISITOR_HXX
19 
20 # include <algorithm>
21 # include <sstream>
22 
23 # include <vaucanson/xml/tools.hh>
24 # include <vaucanson/xml/strings.hh>
25 # include <vaucanson/xml/builders.hh>
26 
27 namespace vcsn {
28 
29  namespace rat {
30 
31  using xml::transcode;
32 
33  template<typename M_, typename W_>
34  XmlExpVisitor<M_,W_>::XmlExpVisitor(xercesc::DOMDocument* doc, const char* node_name) :
35  doc_(doc),
36  label_(xml::tools::create_element(doc_, node_name)),
37  current_(label_)
38  {}
39 
40 
41  template<typename M_, typename W_>
42  void
43  XmlExpVisitor<M_,W_>::sum_or_product(const Node<M_, W_>* left_,
44  const Node<M_, W_>* right_)
45  {
46  left_->accept(*this);
47  right_->accept(*this);
48  }
49 
50  template<typename M_, typename W_>
51  void
52  XmlExpVisitor<M_, W_>::weight_or_star(const Node<M_, W_>* node)
53  {
54  node->accept(*this);
55  }
56 
57  template<typename M_, typename W_>
58  void
59  XmlExpVisitor<M_, W_>::product(const Node<M_, W_>* left_,
60  const Node<M_, W_>* right_)
61  {
62  xercesc::DOMElement* tmp = current_;
63  current_ = xml::tools::create_element(doc_, "product");
64  sum_or_product(left_, right_);
65  tmp->appendChild(current_);
66  current_ = tmp;
67  }
68 
69  template<typename M_, typename W_>
70  void
71  XmlExpVisitor<M_, W_>::sum(const Node<M_, W_>* left_,
72  const Node<M_, W_>* right_)
73  {
74  xercesc::DOMElement* tmp = current_;
75  current_ = xml::tools::create_element(doc_, "sum");
76  sum_or_product(left_, right_);
77  tmp->appendChild(current_);
78  current_ = tmp;
79  }
80 
81  template<typename M_, typename W_>
82  void
83  XmlExpVisitor<M_, W_>::star(const Node<M_, W_>* node)
84  {
85  xercesc::DOMElement* tmp = current_;
86  current_ = xml::tools::create_element(doc_, "star");
87  weight_or_star(node);
88  tmp->appendChild(current_);
89  current_ = tmp;
90  }
91 
92  template <typename M_, typename W_>
93  template <typename T>
94  void
95  XmlExpVisitor<M_, W_>::set_weight(const T& w, xercesc::DOMElement* node)
96  {
97  xercesc::DOMElement* weight = xml::tools::create_element(doc_, "weight");
98  std::stringstream ss;
99  ss << w;
100  weight->setAttribute(transcode("value"), transcode(ss.str()));
101  node->appendChild(weight);
102  }
103 
104  template <typename M_, typename W_>
105  template <typename S, typename T>
106  void
107  XmlExpVisitor<M_, W_>::set_weight(const rat::exp<S, T>& w, xercesc::DOMElement* node)
108  {
109  XmlExpVisitor<S, T> v(doc_, "weight");
110  w.accept(v);
111  node->appendChild(v.get());
112  }
113 
114  template<typename M_, typename W_>
115  void
116  XmlExpVisitor<M_, W_>::left_weight(const W_& w, const Node<M_, W_>* node)
117  {
118  xercesc::DOMElement* tmp = current_;
119  current_ = xml::tools::create_element(doc_, "leftExtMul");
120  set_weight(w, current_);
121  weight_or_star(node);
122  tmp->appendChild(current_);
123  current_ = tmp;
124  }
125 
126  template<typename M_, typename W_>
127  void
128  XmlExpVisitor<M_, W_>::right_weight(const W_& w, const Node<M_, W_>* node)
129  {
130  xercesc::DOMElement* tmp = current_;
131  current_ = xml::tools::create_element(doc_, "rightExtMul");
132  set_weight(w, current_);
133  weight_or_star(node);
134  tmp->appendChild(current_);
135  current_ = tmp;
136  }
137 
138  template<typename M_, typename W_>
139  void
140  XmlExpVisitor<M_, W_>::constant(const M_& m)
141  {
142  xml::builders::create_monElmt_node(m, doc_, current_);
143  }
144 
145  template<typename M_, typename W_>
146  void XmlExpVisitor<M_, W_>::zero()
147  {
148  xercesc::DOMElement* zero = xml::tools::create_element(doc_, "zero");
149  current_->appendChild(zero);
150  }
151 
152  template<typename M_, typename W_>
153  void XmlExpVisitor<M_, W_>::one()
154  {
155  xercesc::DOMElement* identity = xml::tools::create_element(doc_, "one");
156  current_->appendChild(identity);
157  }
158 
159  template<typename M_, typename W_>
160  xercesc::DOMElement*
162  {
163  return label_;
164  }
165 
166  template<typename M_, typename W_>
167  xercesc::DOMDocument*
168  XmlExpVisitor<M_, W_>::set(xercesc::DOMDocument* v)
169  {
170  doc_ = v;
171  return doc_;
172  }
173 
174 
175  } // End of namespace rat.
176 
177 } // End of namespace vcsn.
178 
179 #endif // ! VCSN_XML_XML_EXP_VISITOR_HXX