18 #ifndef VCSN_XML_PRINTERS_HXX
19 # define VCSN_XML_PRINTERS_HXX
23 # include <vaucanson/xml/strings.hh>
48 template <
typename Auto>
49 AutPrinter<Auto>::AutPrinter (
const Auto& aut,
const std::string& name)
50 : aut_(aut), name_(name)
54 template <
typename Auto>
55 AutPrinter<Auto>::~AutPrinter ()
59 template <
typename Auto>
61 AutPrinter<Auto>::print (std::ostream& out)
63 AUTOMATON_TYPES(Auto);
64 using namespace xercesc;
69 impl_ = DOMImplementationRegistry::getDOMImplementation(transcode(
"LS"));
70 doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
71 transcode(
"fsmxml"), 0);
72 root_ = doc_->getDocumentElement();
74 tools::set_attribute(root_,
"version",
"1.0");
76 DOMElement* automaton = tools::create_element(doc_,
"automaton");
77 root_->appendChild(automaton);
78 tools::set_attribute(automaton,
"name", aut_.geometry().name());
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);
86 DOMElement* content = tools::create_element(doc_,
"automatonStruct");
87 automaton->appendChild(content);
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);
96 node = tools::create_element(doc_, "transitions");
97 content->appendChild(node);
98 for_all_transitions(e, aut_)
99 create_transition(*e, node);
102 for_all_initial_states(i, aut_)
103 create_initial(*i, node);
106 for_all_final_states(f, aut_)
107 create_final(*f, node);
110 print_xml(out, impl_, root_);
116 template <class Auto>
118 AutPrinter<Auto>::create_geometry(hstate_t& key,
119 xercesc::DOMElement* root)
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())
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());
136 template <
class Auto>
138 AutPrinter<Auto>::create_state(hstate_t s,
139 xercesc::DOMElement* root)
141 xercesc::DOMElement* node = tools::create_element(doc_,
"state");
142 root->appendChild(node);
143 std::ostringstream os;
145 tools::set_attribute(node,
"id", os.str());
146 create_geometry(s, node);
150 template <
class Auto>
152 AutPrinter<Auto>::create_transition(htransition_t e,
153 xercesc::DOMElement* root)
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);
161 template <
class Auto>
163 AutPrinter<Auto>::create_initial(hstate_t s,
164 xercesc::DOMElement* root)
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);
173 template <
class Auto>
175 AutPrinter<Auto>::create_final(hstate_t s,
176 xercesc::DOMElement* root)
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);
189 template <
typename RE>
190 RegExpPrinter<RE>::RegExpPrinter (
const RE& regexp,
const std::string& name)
191 : regexp_(regexp), name_(name)
195 template <
typename RE>
196 RegExpPrinter<RE>::~RegExpPrinter ()
200 template <
typename RE>
202 RegExpPrinter<RE>::print (std::ostream& out)
204 using namespace xercesc;
207 impl_ = DOMImplementationRegistry::getDOMImplementation(transcode(
"LS"));
208 doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
209 transcode(
"fsmxml"), 0);
210 root_ = doc_->getDocumentElement();
212 tools::set_attribute(root_,
"version",
"1.0");
214 DOMElement* regexp = tools::create_element(doc_,
"regExp");
215 root_->appendChild(regexp);
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);
223 builders::create_regexp_node(regexp_, doc_, regexp,
"typedRegExp");
226 print_xml(out, impl_, root_);
235 #endif // !VCSN_XML_PRINTERS_HXX