Commit 974db14d authored by David Moreira's avatar David Moreira
Browse files

xml: Parsing function for rational expression.

* vcsn/algos/xml.hh: Static parsing for fsmxml.
* vcsn/ctx/instantiate.hh: Instantiate XML parsing.
* vcsn/algos/dyn.hh: Declare dynamic parsing function for XML.
* lib/vcsn/algos/xml.cc: Implement dynamic parsing function for XML.
parent e8298ea8
......@@ -39,22 +39,92 @@ namespace vcsn
namespace details
{
Registry<xml_string_t>&
xml_string_registry()
xml_write_string_registry()
{
static Registry<xml_string_t> instance{"xml_stream"};
static Registry<xml_string_t> instance{"xml_write_stream"};
return instance;
}
bool xml_register(const std::string& ctx, const xml_string_t& fn)
bool xml_register(const std::string& ctx,
const xml_string_t& fn)
{
return xml_string_registry().set(ctx, fn);
return xml_write_string_registry().set(ctx, fn);
}
} // namespace details
std::string
xml(const dyn::ratexp e)
xml(const ratexp e)
{
return details::xml_string_registry().call(e->ctx().vname(), e);
return details::xml_write_string_registry().call(e->ctx().vname(), e);
}
namespace details
{
Registry<xml_read_stream_t>&
xml_read_stream_registry()
{
static Registry<xml_read_stream_t> instance{"xml_read_stream"};
return instance;
}
bool
xml_register(const std::string& ctx,
const xml_read_stream_t& fn)
{
return xml_read_stream_registry().set(ctx, fn);
}
} // namespace details
ratexp
xml_read_stream(const context& ctx, std::istream& in)
{
return details::xml_read_stream_registry().call(ctx.vname(), ctx, in);
}
namespace details
{
Registry<xml_read_file_t>&
xml_read_file_registry()
{
static Registry<xml_read_file_t> instance{"xml_read_file"};
return instance;
}
bool
xml_file_register(const std::string& ctx,
const xml_read_file_t& fn)
{
return xml_read_file_registry().set(ctx, fn);
}
} // namespace details
ratexp
xml_read_file(const context& ctx, const std::string& path)
{
return details::xml_read_file_registry().call(ctx.vname(), ctx, path);
}
namespace details
{
Registry<xml_read_string_t>&
xml_read_string_registry()
{
static Registry<xml_read_string_t> instance{"xml_read_string"};
return instance;
}
bool
xml_string_register(const std::string& ctx,
const xml_read_string_t& fn)
{
return xml_read_string_registry().set(ctx, fn);
}
} // namespace details
ratexp
xml_read_string(const context& ctx, const std::string& exp)
{
return details::xml_read_string_registry().call(ctx.vname(), ctx, exp);
}
} // namespace dyn
......
......@@ -57,6 +57,10 @@ namespace vcsn
void xml(const dyn::ratexp e, std::ostream& out);
std::string xml(const dyn::ratexp e);
ratexp xml_read_stream(const context& ctx, std::istream& in);
ratexp xml_read_file(const context& ctx, const std::string& path);
ratexp xml_read_string(const context& ctx, const std::string& exp);
}
}
......
......@@ -2,9 +2,14 @@
# define VCSN_ALGOS_XML_HH
# include <sstream>
# include <fstream>
# include <xercesc/dom/DOMImplementationRegistry.hpp>
# include <xercesc/sax2/XMLReaderFactory.hpp>
# include <xercesc/framework/MemBufInputSource.hpp>
# include <vcsn/algos/xml/expression-handlers.hh>
# include <vcsn/algos/xml/error-handlers.hh>
# include <vcsn/algos/xml/ios.hh>
# include <vcsn/algos/xml/ratexpvisitor.hh>
# include <vcsn/algos/xml/structure.hh>
......@@ -140,6 +145,127 @@ namespace vcsn
} // namespace details
} // namespace dyn
/*------------.
| reading xml |
`------------*/
template <typename Context>
vcsn::rat::exp_t
xml(const Context& ctx, std::istream& in)
{
assert(false);
}
namespace details
{
template <typename Context>
vcsn::rat::exp_t
xml_file(const Context& ctx, const char* path)
{
using namespace xercesc;
// Create the parser
auto parser = std::unique_ptr<SAX2XMLReader>(XMLReaderFactory::createXMLReader());
// Define and set content and error handler
details::ExpressionHandler<Context> handler;
details::XMLErrorHandler error;
parser->setContentHandler(&handler);
parser->setErrorHandler(&error);
// Parse
parser->parse(path);
// return the result
// FIXME: return handler.getExp();
}
} // namespace details
template <typename Context>
vcsn::rat::exp_t
xml_file(const Context& ctx, const std::string& path)
{
return details::xml_file(ctx, path.c_str());
}
template <typename Context>
vcsn::rat::exp_t
xml_string(const Context& ctx, const std::string& exp)
{
using namespace xercesc;
MemBufInputSource input((XMLByte*) exp.c_str(), // The buffer location in bytes.
exp.size(), // The buffer size.
"xml_string", // A fake id.
false); // Xerces don't delete the buffer.
// Create the parser
auto parser
= std::unique_ptr<SAX2XMLReader>(XMLReaderFactory::createXMLReader());
// Define and set content and error handler
details::ExpressionHandler<Context> handler;
details::XMLErrorHandler error;
parser->setContentHandler(&handler);
parser->setErrorHandler(&error);
// Parse
parser->parse(input);
// return the result
// FIXME: return handler.getExp();
}
/*-------------.
| abstract xml |
`-------------*/
namespace dyn
{
namespace details
{
template <typename Ctx>
vcsn::dyn::ratexp
xml(const dyn::context& ctx, std::istream& in)
{
return make_ratexp(ctx, xml(dynamic_cast<const Ctx&>(ctx), in));
}
template <typename Ctx>
vcsn::dyn::ratexp
xml_file(const dyn::context& ctx, const std::string& path)
{
return make_ratexp(ctx, xml_file(dynamic_cast<const Ctx&>(ctx), path));
}
template <typename Ctx>
vcsn::dyn::ratexp
xml_string(const dyn::context& ctx, const std::string& exp)
{
return make_ratexp(ctx, xml_string(dynamic_cast<const Ctx&>(ctx), exp));
}
using xml_read_stream_t =
auto (const dyn::context& ctx, std::istream& in)
-> vcsn::dyn::ratexp;
using xml_read_file_t =
auto (const dyn::context& ctx, const std::string& path)
-> vcsn::dyn::ratexp;
using xml_read_string_t =
auto (const dyn::context& ctx, const std::string& exp)
-> vcsn::dyn::ratexp;
bool xml_register(const std::string& cxt,
const xml_read_stream_t& fn);
bool xml_file_register(const std::string& ctx,
const xml_read_file_t& fn);
bool xml_string_register(const std::string& cxt,
const xml_read_string_t& fn);
} // namespace details
} // namespace dyn
} // namespace vcsn
#endif // !VCSN_ALGOS_XML_HH
......@@ -46,6 +46,18 @@ namespace vcsn
std::string xml<Ctx>(const Ctx& cxt, \
const rat::exp_t exp); \
\
MAYBE_EXTERN template \
rat::exp_t xml<Ctx>(const Ctx& ctx, \
std::istream& in); \
\
MAYBE_EXTERN template \
rat::exp_t xml_file<Ctx>(const Ctx& ctx, \
const std::string& path); \
\
MAYBE_EXTERN template \
rat::exp_t xml_string<Ctx>(const Ctx& ctx, \
const std::string& exp); \
\
namespace dyn \
{ \
namespace details \
......@@ -55,6 +67,18 @@ namespace vcsn
\
MAYBE_EXTERN template \
std::string xml<Ctx>(const dyn::ratexp exp); \
\
MAYBE_EXTERN template \
dyn::ratexp xml<Ctx>(const dyn::context& ctx, \
std::istream& in); \
\
MAYBE_EXTERN template \
dyn::ratexp xml_file<Ctx>(const dyn::context& ctx, \
const std::string& path); \
\
MAYBE_EXTERN template \
dyn::ratexp xml_string<Ctx>(const dyn::context& ctx, \
const std::string& exp); \
} \
}
......@@ -188,10 +212,21 @@ namespace vcsn
transpose_exp_register(Ctx::sname(), abstract_transpose_exp<Ctx>);
// xml.
xml_register(Ctx::sname(),
static_cast<const xml_stream_t&>(xml<Ctx>));
xml_register(Ctx::sname(),
static_cast<const xml_string_t&>(xml<Ctx>));
xml_register
(Ctx::sname(),
static_cast<const xml_stream_t&>(xml<Ctx>));
xml_register
(Ctx::sname(),
static_cast<const xml_string_t&>(xml<Ctx>));
xml_register
(Ctx::sname(),
static_cast<const xml_read_stream_t&>(xml<Ctx>));
xml_file_register
(Ctx::sname(),
static_cast<const xml_read_file_t&>(xml_file<Ctx>));
xml_string_register
(Ctx::sname(),
static_cast<const xml_read_string_t&>(xml_string<Ctx>));
register_kind_functions<Ctx>(typename Ctx::kind_t());
return true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment