00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_XML_PRINTERS_HXX
00019 # define VCSN_XML_PRINTERS_HXX
00020
00021 # include <fstream>
00022
00023 # include <vaucanson/xml/xml_xerces_stream.hh>
00024 # include <vaucanson/xml/strings.hh>
00025 # include <vaucanson/xml/tools.hh>
00026 # include <vaucanson/xml/builders.hh>
00027
00028 namespace vcsn
00029 {
00030 namespace xml
00031 {
00032
00033
00034
00035
00036 Printer::Printer ()
00037 {
00038 };
00039
00040 Printer::~Printer ()
00041 {
00042 }
00043
00044
00045
00046
00047 template <typename Auto>
00048 AutPrinter<Auto>::AutPrinter (const Auto& aut, const std::string& name)
00049 : aut_(aut), name_(name)
00050 {
00051 }
00052
00053 template <typename Auto>
00054 AutPrinter<Auto>::~AutPrinter ()
00055 {
00056 }
00057
00058 template <typename Auto>
00059 void
00060 AutPrinter<Auto>::print (std::ostream& out)
00061 {
00062 typedef typename Auto::state_iterator state_iterator;
00063 typedef typename Auto::transition_iterator transition_iterator;
00064 typedef typename Auto::initial_iterator initial_iterator;
00065 typedef typename Auto::final_iterator final_iterator;
00066 using namespace xercesc;
00067
00068 state2str_.clear();
00069
00070
00071 impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
00072 doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
00073 transcode("fsmxml"), 0);
00074 root_ = doc_->getDocumentElement();
00075
00076 tools::set_attribute(root_, "version", "1.0");
00077
00078 DOMElement* automaton = tools::create_element(doc_, "automaton");
00079 root_->appendChild(automaton);
00080 tools::set_attribute(automaton, "name", aut_.geometry().name());
00081
00082 DOMElement* valueType = tools::create_element(doc_, "valueType");
00083 automaton->appendChild(valueType);
00084 builders::create_type_writingData_node(aut_, doc_, valueType);
00085 builders::create_semiring_node(aut_, doc_, valueType);
00086 builders::create_monoid_node(aut_, doc_, valueType);
00087
00088 DOMElement* content = tools::create_element(doc_, "automatonStruct");
00089 automaton->appendChild(content);
00090
00091
00092 DOMElement* node = tools::create_element(doc_, "states");
00093 content->appendChild(node);
00094 for_all_states(s, aut_)
00095 state2str_[*s] = create_state(*s, node);
00096
00097
00098 node = tools::create_element(doc_, "transitions");
00099 content->appendChild(node);
00100 for_all_transitions(e, aut_)
00101 create_transition(*e, node);
00102
00103
00104 for_all_initial_states(i, aut_)
00105 create_initial(*i, node);
00106
00107
00108 for_all_final_states(f, aut_)
00109 create_final(*f, node);
00110
00111
00112
00113 XMLXercesStream<std::ostream>* target = new XMLXercesStream<std::ostream>(out);
00114 DOMWriter* writer = impl_->createDOMWriter();
00115
00116 if (writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00117 writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00118 writer->writeNode(target, *root_);
00119 writer->release();
00120 out << std::endl;
00121 delete doc_;
00122 delete target;
00123 }
00124
00125 template <class Auto>
00126 void
00127 AutPrinter<Auto>::create_geometry(hstate_t& key,
00128 xercesc::DOMElement* root)
00129 {
00130 typedef typename Auto::geometry_t::states_geometry_map_t gmap_t;
00131 typename gmap_t::const_iterator iter;
00132 gmap_t map = aut_.geometry().states();
00133 if ((iter = map.find(key)) != map.end())
00134 {
00135 std::ostringstream osx, osy;
00136 osx << iter->second.first;
00137 xercesc::DOMElement* nd = tools::create_element(doc_, "geometricData");
00138 root->appendChild(nd);
00139 tools::set_attribute(nd, "x", osx.str());
00140 osy << iter->second.second;
00141 tools::set_attribute(nd, "y", osy.str());
00142 }
00143 }
00144
00145 template <class Auto>
00146 std::string
00147 AutPrinter<Auto>::create_state(hstate_t s,
00148 xercesc::DOMElement* root)
00149 {
00150 xercesc::DOMElement* node = tools::create_element(doc_, "state");
00151 root->appendChild(node);
00152 std::ostringstream os;
00153 os << "s" << s;
00154 tools::set_attribute(node, "id", os.str());
00155 create_geometry(s, node);
00156 return os.str();
00157 }
00158
00159 template <class Auto>
00160 void
00161 AutPrinter<Auto>::create_transition(htransition_t e,
00162 xercesc::DOMElement* root)
00163 {
00164 xercesc::DOMElement* node = tools::create_element(doc_, "transition");
00165 root->appendChild(node);
00166 tools::set_attribute(node, "source", state2str_[aut_.src_of(e)]);
00167 tools::set_attribute(node, "target", state2str_[aut_.dst_of(e)]);
00168 builders::create_regexp_node(aut_.series_of(e), doc_, node);
00169 }
00170 template <class Auto>
00171 void
00172 AutPrinter<Auto>::create_initial(hstate_t s,
00173 xercesc::DOMElement* root)
00174 {
00175 xercesc::DOMElement* node = tools::create_element(doc_, "initial");
00176 root->appendChild(node);
00177 tools::set_attribute(node, "state", state2str_[s]);
00178 typename Auto::series_set_elt_t tmp = aut_.get_initial(s);
00179 if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.structure().series()).value())
00180 builders::create_regexp_node(tmp, doc_, node);
00181 }
00182 template <class Auto>
00183 void
00184 AutPrinter<Auto>::create_final(hstate_t s,
00185 xercesc::DOMElement* root)
00186 {
00187 xercesc::DOMElement* node = tools::create_element(doc_, "final");
00188 root->appendChild(node);
00189 tools::set_attribute(node, "state", state2str_[s]);
00190 typename Auto::series_set_elt_t tmp = aut_.get_final(s);
00191 if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.structure().series()).value())
00192 builders::create_regexp_node(tmp, doc_, node);
00193 }
00194 }
00195 }
00196
00197 #endif // !VCSN_XML_PRINTERS_HXX