Vaucanson  1.4.1
printers.hxx
1 // printers.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2005, 2006, 2007, 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 
18 #ifndef VCSN_XML_PRINTERS_HXX
19 # define VCSN_XML_PRINTERS_HXX
20 
21 # include <fstream>
22 
23 # include <vaucanson/xml/strings.hh>
24 # include <vaucanson/xml/tools.hh>
25 # include <vaucanson/xml/builders.hh>
26 
28 
29 namespace vcsn
30 {
31  namespace xml
32  {
33  /*
34  * Printer class.
35  */
36 
37  Printer::Printer ()
38  {
39  };
40 
41  Printer::~Printer ()
42  {
43  }
44 
45  /*
46  * AutPrinter class.
47  */
48  template <typename Auto>
49  AutPrinter<Auto>::AutPrinter (const Auto& aut, const std::string& name)
50  : aut_(aut), name_(name)
51  {
52  }
53 
54  template <typename Auto>
55  AutPrinter<Auto>::~AutPrinter ()
56  {
57  }
58 
59  template <typename Auto>
60  void
61  AutPrinter<Auto>::print (std::ostream& out)
62  {
63  AUTOMATON_TYPES(Auto);
64  using namespace xercesc;
65 
66  state2str_.clear();
67 
68  // Document creation.
69  impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
70  doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
71  transcode("fsmxml"), 0);
72  root_ = doc_->getDocumentElement();
73 
74  tools::set_attribute(root_, "version", "1.0"); // FIXME should be... a macro?
75 
76  DOMElement* automaton = tools::create_element(doc_, "automaton");
77  root_->appendChild(automaton);
78  tools::set_attribute(automaton, "name", aut_.geometry().name());
79 
80  DOMElement* valueType = tools::create_element(doc_, "valueType");
81  automaton->appendChild(valueType);
82  builders::create_type_writingData_node(aut_, doc_, valueType);
83  builders::create_semiring_node(aut_, doc_, valueType);
84  builders::create_monoid_node(aut_, doc_, valueType);
85 
86  DOMElement* content = tools::create_element(doc_, "automatonStruct");
87  automaton->appendChild(content);
88 
89  // Create states.
90  DOMElement* node = tools::create_element(doc_, "states");
91  content->appendChild(node);
92  for_all_states(s, aut_)
93  state2str_[*s] = create_state(*s, node);
94 
95  // Create transitions.
96  node = tools::create_element(doc_, "transitions");
97  content->appendChild(node);
98  for_all_transitions(e, aut_)
99  create_transition(*e, node);
100 
101  // Create initial transitions.
102  for_all_initial_states(i, aut_)
103  create_initial(*i, node);
104 
105  // Create final transitions.
106  for_all_final_states(f, aut_)
107  create_final(*f, node);
108 
109  // Print it!
110  print_xml(out, impl_, root_);
111  out << std::endl;
112 
113  delete doc_;
114  }
115 
116  template <class Auto>
117  void
118  AutPrinter<Auto>::create_geometry(hstate_t& key,
119  xercesc::DOMElement* root)
120  {
121  typedef typename Auto::geometry_t::states_geometry_map_t gmap_t;
122  typename gmap_t::const_iterator iter;
123  gmap_t map = aut_.geometry().states();
124  if ((iter = map.find(key)) != map.end())
125  {
126  std::ostringstream osx, osy;
127  osx << iter->second.first;
128  xercesc::DOMElement* nd = tools::create_element(doc_, "geometricData");
129  root->appendChild(nd);
130  tools::set_attribute(nd, "x", osx.str());
131  osy << iter->second.second;
132  tools::set_attribute(nd, "y", osy.str());
133  }
134  }
135 
136  template <class Auto>
137  std::string
138  AutPrinter<Auto>::create_state(hstate_t s,
139  xercesc::DOMElement* root)
140  {
141  xercesc::DOMElement* node = tools::create_element(doc_, "state");
142  root->appendChild(node);
143  std::ostringstream os;
144  os << "s" << s;
145  tools::set_attribute(node, "id", os.str());
146  create_geometry(s, node);
147  return os.str();
148  }
149 
150  template <class Auto>
151  void
152  AutPrinter<Auto>::create_transition(htransition_t e,
153  xercesc::DOMElement* root)
154  {
155  xercesc::DOMElement* node = tools::create_element(doc_, "transition");
156  root->appendChild(node);
157  tools::set_attribute(node, "source", state2str_[aut_.src_of(e)]);
158  tools::set_attribute(node, "target", state2str_[aut_.dst_of(e)]);
159  builders::create_regexp_node(aut_.series_of(e), doc_, node);
160  }
161  template <class Auto>
162  void
163  AutPrinter<Auto>::create_initial(hstate_t s,
164  xercesc::DOMElement* root)
165  {
166  xercesc::DOMElement* node = tools::create_element(doc_, "initial");
167  root->appendChild(node);
168  tools::set_attribute(node, "state", state2str_[s]);
169  typename Auto::series_set_elt_t tmp = aut_.get_initial(s);
170  if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.series()).value())
171  builders::create_regexp_node(tmp, doc_, node);
172  }
173  template <class Auto>
174  void
175  AutPrinter<Auto>::create_final(hstate_t s,
176  xercesc::DOMElement* root)
177  {
178  xercesc::DOMElement* node = tools::create_element(doc_, "final");
179  root->appendChild(node);
180  tools::set_attribute(node, "state", state2str_[s]);
181  typename Auto::series_set_elt_t tmp = aut_.get_final(s);
182  if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.series()).value())
183  builders::create_regexp_node(tmp, doc_, node);
184  }
185 
186  /*
187  * RegExpPrinter class.
188  */
189  template <typename RE>
190  RegExpPrinter<RE>::RegExpPrinter (const RE& regexp, const std::string& name)
191  : regexp_(regexp), name_(name)
192  {
193  }
194 
195  template <typename RE>
196  RegExpPrinter<RE>::~RegExpPrinter ()
197  {
198  }
199 
200  template <typename RE>
201  void
202  RegExpPrinter<RE>::print (std::ostream& out)
203  {
204  using namespace xercesc;
205 
206  // Document creation.
207  impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
208  doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
209  transcode("fsmxml"), 0);
210  root_ = doc_->getDocumentElement();
211 
212  tools::set_attribute(root_, "version", "1.0"); // FIXME should be... a macro?
213 
214  DOMElement* regexp = tools::create_element(doc_, "regExp");
215  root_->appendChild(regexp);
216 
217  DOMElement* valueType = tools::create_element(doc_, "valueType");
218  regexp->appendChild(valueType);
219  builders::create_type_writingData_node(regexp_, doc_, valueType);
220  builders::create_semiring_node(regexp_, doc_, valueType);
221  builders::create_monoid_node(regexp_, doc_, valueType);
222 
223  builders::create_regexp_node(regexp_, doc_, regexp, "typedRegExp");
224 
225  // Print it!
226  print_xml(out, impl_, root_);
227  out << std::endl;
228 
229  delete doc_;
230  }
231 
232  } // !xml
233 } // !vcsn
234 
235 #endif // !VCSN_XML_PRINTERS_HXX