2008-11-05 02:33:28 +01:00
|
|
|
#ifndef ARABICA_XSLT_COMPILED_STYLESHEET_HPP
|
|
|
|
#define ARABICA_XSLT_COMPILED_STYLESHEET_HPP
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <XPath/XPath.hpp>
|
|
|
|
|
|
|
|
#include "xslt_execution_context.hpp"
|
|
|
|
#include "xslt_template.hpp"
|
|
|
|
#include "xslt_top_level_param.hpp"
|
2008-11-05 02:33:28 +01:00
|
|
|
#include "xslt_stylesheet.hpp"
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
namespace Arabica
|
|
|
|
{
|
|
|
|
namespace XSLT
|
|
|
|
{
|
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
class CompiledStylesheet : public Stylesheet
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
typedef Arabica::XPath::BoolValue<std::string, Arabica::default_string_adaptor<std::string> > BoolValue;
|
|
|
|
typedef Arabica::XPath::NumericValue<std::string, Arabica::default_string_adaptor<std::string> > NumericValue;
|
|
|
|
typedef Arabica::XPath::StringValue<std::string, Arabica::default_string_adaptor<std::string> > StringValue;
|
|
|
|
typedef Arabica::XPath::XPathValuePtr<std::string> ValuePtr;
|
|
|
|
|
|
|
|
public:
|
2008-11-05 02:33:28 +01:00
|
|
|
CompiledStylesheet() :
|
2007-07-19 19:01:42 +02:00
|
|
|
output_(new StreamSink(std::cout)),
|
2008-11-24 23:05:16 +01:00
|
|
|
error_output_(&std::cerr)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2008-11-05 02:33:28 +01:00
|
|
|
} // CompiledStylesheet
|
2007-07-19 19:01:42 +02:00
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual ~CompiledStylesheet()
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2008-08-06 12:48:06 +02:00
|
|
|
// let's clean up!
|
2008-11-19 18:26:07 +01:00
|
|
|
for(VariableDeclList::const_iterator ci = topLevelVars_.begin(), ce = topLevelVars_.end(); ci != ce; ++ci)
|
|
|
|
delete *ci;
|
2008-08-06 12:48:06 +02:00
|
|
|
for(ParamList::const_iterator pi = params_.begin(), pe = params_.end(); pi != pe; ++pi)
|
|
|
|
delete *pi;
|
|
|
|
for(TemplateList::const_iterator ti = all_templates_.begin(), te = all_templates_.end(); ti != te; ++ti)
|
|
|
|
delete *ti;
|
2008-11-05 02:33:28 +01:00
|
|
|
} // ~CompiledStylesheet
|
2007-07-19 19:01:42 +02:00
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_parameter(const std::string& name, bool value)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
set_parameter(name, ValuePtr(new BoolValue(value)));
|
|
|
|
} // set_parameter
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_parameter(const std::string& name, double value)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
set_parameter(name, ValuePtr(new NumericValue(value)));
|
|
|
|
} // set_parameter
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_parameter(const std::string& name, const char* value)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
set_parameter(name, ValuePtr(new StringValue(value)));
|
|
|
|
} // set_parameter
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_parameter(const std::string& name, const std::string& value)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
set_parameter(name, ValuePtr(new StringValue(value)));
|
|
|
|
} // set_parameter
|
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_output(Sink& sink)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
output_.reset(sink);
|
|
|
|
} // set_output
|
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void set_error_output(std::ostream& os)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
error_output_ = &os;
|
|
|
|
} // set_error_output
|
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
virtual void execute(const DOM::Node<std::string>& initialNode) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2008-06-04 22:30:36 +02:00
|
|
|
if(initialNode == 0)
|
|
|
|
throw std::runtime_error("Input document is empty");
|
|
|
|
|
2007-07-19 19:01:42 +02:00
|
|
|
Arabica::XPath::NodeSet<std::string> ns;
|
|
|
|
ns.push_back(initialNode);
|
|
|
|
|
|
|
|
ExecutionContext context(*this, output_.get(), *error_output_);
|
|
|
|
|
|
|
|
// set up variables and so forth
|
2008-08-06 12:48:06 +02:00
|
|
|
for(ParamList::const_iterator pi = params_.begin(), pe = params_.end(); pi != pe; ++pi)
|
|
|
|
(*pi)->declare(context);
|
2008-11-19 18:26:07 +01:00
|
|
|
for(VariableDeclList::const_iterator ci = topLevelVars_.begin(), ce = topLevelVars_.end(); ci != ce; ++ci)
|
|
|
|
(*ci)->execute(initialNode, context);
|
2007-07-19 19:01:42 +02:00
|
|
|
context.freezeTopLevel();
|
|
|
|
|
|
|
|
// go!
|
|
|
|
output_.get().asOutput().start_document(output_settings_);
|
2007-10-26 21:12:27 +02:00
|
|
|
applyTemplates(ns, context, std::pair<std::string, std::string>("", ""));
|
2007-07-19 19:01:42 +02:00
|
|
|
output_.get().asOutput().end_document();
|
|
|
|
} // execute
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
void add_template(Template* templat)
|
|
|
|
{
|
|
|
|
typedef std::vector<Arabica::XPath::MatchExpr<std::string> > MatchExprList;
|
|
|
|
typedef MatchExprList::const_iterator MatchIterator;
|
|
|
|
|
|
|
|
all_templates_.push_back(templat);
|
|
|
|
|
|
|
|
for(MatchIterator e = templat->compiled_matches().begin(), ee = templat->compiled_matches().end(); e != ee; ++e)
|
2008-11-24 23:05:16 +01:00
|
|
|
templates_[templat->precedence()][templat->mode()].push_back(MatchTemplate(*e, templat));
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
if(!templat->has_name())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(named_templates_.find(templat->name()) != named_templates_.end())
|
|
|
|
{
|
2007-10-26 14:28:48 +02:00
|
|
|
std::cerr << "Template named '";
|
|
|
|
if(!templat->name().first.empty())
|
|
|
|
std::cerr << "{" << templat->name().first << "}";
|
|
|
|
std::cerr << templat->name().second << "' already defined" << std::endl;
|
2007-07-19 19:01:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
named_templates_[templat->name()] = templat;
|
|
|
|
} // add_template
|
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
void add_variable(Item* item)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2008-11-19 18:26:07 +01:00
|
|
|
topLevelVars_.push_back(item);
|
2007-07-19 19:01:42 +02:00
|
|
|
} // add_item
|
|
|
|
|
|
|
|
void output_settings(const Output::Settings& settings)
|
|
|
|
{
|
|
|
|
output_settings_ = settings;
|
|
|
|
} // output_settings
|
|
|
|
|
|
|
|
void prepare()
|
|
|
|
{
|
|
|
|
for(TemplateStack::iterator ts = templates_.begin(), tse = templates_.end(); ts != tse; ++ts)
|
2008-11-24 23:05:16 +01:00
|
|
|
for(ModeTemplates::iterator ms = ts->second.begin(), mse = ts->second.end(); ms != mse; ++ms)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
MatchTemplates& matches = ms->second;
|
|
|
|
std::reverse(matches.begin(), matches.end());
|
|
|
|
std::stable_sort(matches.begin(), matches.end());
|
|
|
|
} // for ...
|
|
|
|
} // prepare
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
2007-10-26 21:12:27 +02:00
|
|
|
void applyTemplates(const Arabica::XPath::NodeSet<std::string>& nodes, ExecutionContext& context, const std::pair<std::string, std::string>& mode) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
// entirely simple so far
|
|
|
|
LastFrame last(context, nodes.size());
|
|
|
|
int p = 1;
|
|
|
|
for(Arabica::XPath::NodeSet<std::string>::const_iterator n = nodes.begin(), ne = nodes.end(); n != ne; ++n)
|
|
|
|
{
|
|
|
|
context.setPosition(*n, p++);
|
2008-11-24 23:05:16 +01:00
|
|
|
doApplyTemplates(*n, context, mode, Precedence::FrozenPrecedence());
|
2007-07-19 19:01:42 +02:00
|
|
|
}
|
|
|
|
} // applyTemplates
|
|
|
|
|
2007-10-26 21:12:27 +02:00
|
|
|
void applyTemplates(const DOM::NodeList<std::string>& nodes, ExecutionContext& context, const std::pair<std::string, std::string>& mode) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
// entirely simple so far
|
2007-09-08 10:16:24 +02:00
|
|
|
LastFrame last(context, (size_t)nodes.getLength());
|
2007-07-19 19:01:42 +02:00
|
|
|
for(int i = 0, ie = nodes.getLength(); i != ie; ++i)
|
|
|
|
{
|
|
|
|
context.setPosition(nodes.item(i), i+1);
|
2008-11-24 23:05:16 +01:00
|
|
|
doApplyTemplates(nodes.item(i), context, mode, Precedence::FrozenPrecedence());
|
2007-07-19 19:01:42 +02:00
|
|
|
}
|
|
|
|
} // applyTemplates
|
|
|
|
|
2007-10-26 21:12:27 +02:00
|
|
|
void applyTemplates(const DOM::Node<std::string>& node, ExecutionContext& context, const std::pair<std::string, std::string>& mode) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
LastFrame last(context, -1);
|
|
|
|
context.setPosition(node, 1);
|
2008-11-24 23:05:16 +01:00
|
|
|
doApplyTemplates(node, context, mode, Precedence::FrozenPrecedence());
|
2007-07-19 19:01:42 +02:00
|
|
|
} // applyTemplates
|
|
|
|
|
2007-10-26 14:28:48 +02:00
|
|
|
void callTemplate(const std::pair<std::string, std::string>& name, const DOM::Node<std::string>& node, ExecutionContext& context) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
StackFrame frame(context);
|
|
|
|
|
|
|
|
NamedTemplates::const_iterator t = named_templates_.find(name);
|
|
|
|
if(t == named_templates_.end())
|
|
|
|
{
|
2007-10-26 14:28:48 +02:00
|
|
|
std::cerr << "No template named '";
|
|
|
|
if(!name.first.empty())
|
|
|
|
std::cerr << "{" << name.first << "}";
|
2007-11-17 00:11:39 +01:00
|
|
|
std::cerr << name.second << "'. I should be a compile-time error!" << std::endl;
|
|
|
|
throw SAX::SAXException("No template named {" + name.first + "}" + name.second + ". I should be a compile-time error. Sorry!");
|
2007-07-19 19:01:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->second->execute(node, context);
|
|
|
|
} // callTemplate
|
|
|
|
|
|
|
|
void applyImports(const DOM::Node<std::string>& node, ExecutionContext& context) const
|
|
|
|
{
|
2008-11-24 23:05:16 +01:00
|
|
|
doApplyTemplates(node, context, current_mode_, current_generation_);
|
2007-07-19 19:01:42 +02:00
|
|
|
} // applyImports
|
|
|
|
|
|
|
|
private:
|
|
|
|
void doApplyTemplates(const DOM::Node<std::string>& node,
|
2007-10-26 21:12:27 +02:00
|
|
|
ExecutionContext& context,
|
|
|
|
const std::pair<std::string, std::string>& mode,
|
2008-11-25 13:27:33 +01:00
|
|
|
const Precedence& generation) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2007-11-27 00:17:11 +01:00
|
|
|
StackFrame frame(context);
|
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
std::vector<Precedence> lower_precedences;
|
2008-11-24 23:05:16 +01:00
|
|
|
for(TemplateStack::const_iterator ts = templates_.begin(), tse = templates_.end(); ts != tse; ++ts)
|
2008-11-25 13:27:33 +01:00
|
|
|
if(generation.is_descendant(ts->first))
|
|
|
|
lower_precedences.push_back(ts->first);
|
|
|
|
std::sort(lower_precedences.rbegin(), lower_precedences.rend());
|
2008-11-24 23:05:16 +01:00
|
|
|
|
2007-07-19 19:01:42 +02:00
|
|
|
current_mode_ = mode;
|
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
for(std::vector<Precedence>::const_iterator p = lower_precedences.begin(), pe = lower_precedences.end(); p != pe; ++p)
|
2008-11-24 23:05:16 +01:00
|
|
|
{
|
|
|
|
current_generation_ = *p;
|
|
|
|
ModeTemplates ts = templates_.find(current_generation_)->second;
|
|
|
|
ModeTemplates::const_iterator mt = ts.find(mode);
|
|
|
|
if(mt != ts.end())
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
const MatchTemplates& templates = mt->second;
|
|
|
|
for(MatchTemplates::const_iterator t = templates.begin(), te = templates.end(); t != te; ++t)
|
2007-10-14 22:06:27 +02:00
|
|
|
if(t->match().evaluate(node, context.xpathContext()))
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
t->action()->execute(node, context);
|
|
|
|
return;
|
|
|
|
} // if ...
|
|
|
|
} // if ...
|
|
|
|
} // for ...
|
|
|
|
defaultAction(node, context, mode);
|
|
|
|
} // doApplyTemplates
|
|
|
|
|
2007-10-26 21:12:27 +02:00
|
|
|
void defaultAction(const DOM::Node<std::string>& node,
|
|
|
|
ExecutionContext& context,
|
|
|
|
const std::pair<std::string, std::string>& mode) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
switch(node.getNodeType())
|
|
|
|
{
|
|
|
|
case DOM::Node<std::string>::DOCUMENT_NODE:
|
|
|
|
case DOM::Node<std::string>::DOCUMENT_FRAGMENT_NODE:
|
|
|
|
case DOM::Node<std::string>::ELEMENT_NODE:
|
|
|
|
applyTemplates(node.getChildNodes(), context, mode);
|
|
|
|
break;
|
|
|
|
case DOM::Node<std::string>::ATTRIBUTE_NODE:
|
|
|
|
case DOM::Node<std::string>::TEXT_NODE:
|
|
|
|
case DOM::Node<std::string>::CDATA_SECTION_NODE:
|
2008-08-05 19:29:18 +02:00
|
|
|
context.sink().characters(node.getNodeValue());
|
2008-08-01 20:33:52 +02:00
|
|
|
/*
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
const std::string& ch = node.getNodeValue();
|
|
|
|
for(std::string::const_iterator i = ch.begin(), e = ch.end(); i != e; ++i)
|
|
|
|
if(!Arabica::XML::is_space(*i))
|
|
|
|
{
|
|
|
|
context.sink().characters(ch);
|
|
|
|
return;
|
|
|
|
} // if ...
|
|
|
|
}
|
2008-08-01 20:33:52 +02:00
|
|
|
*/
|
2007-07-19 19:01:42 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;// nothing!
|
|
|
|
} // switch
|
|
|
|
} // defaultAction
|
|
|
|
|
|
|
|
void set_parameter(const std::string& name, ValuePtr value)
|
|
|
|
{
|
|
|
|
params_.push_back(new TopLevelParam("", name, value));
|
|
|
|
} // set_parameter
|
|
|
|
|
|
|
|
void set_parameter(const std::string& namespace_uri, const std::string& name, ValuePtr value)
|
|
|
|
{
|
|
|
|
params_.push_back(new TopLevelParam(namespace_uri, name, value));
|
|
|
|
} // set_parameter
|
|
|
|
|
|
|
|
private:
|
|
|
|
class MatchTemplate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MatchTemplate(const Arabica::XPath::MatchExpr<std::string>& matchExpr, Template* templat) :
|
2007-10-14 22:06:27 +02:00
|
|
|
match_(matchExpr),
|
2007-07-19 19:01:42 +02:00
|
|
|
template_(templat)
|
|
|
|
{
|
|
|
|
} // MatchTemplate
|
|
|
|
MatchTemplate(const MatchTemplate& rhs) :
|
|
|
|
match_(rhs.match_),
|
|
|
|
template_(rhs.template_)
|
|
|
|
{
|
|
|
|
} // MatchTemplate
|
|
|
|
|
2007-10-14 22:06:27 +02:00
|
|
|
const Arabica::XPath::MatchExpr<std::string>& match() const { return match_; }
|
2007-07-19 19:01:42 +02:00
|
|
|
Template* action() const { return template_; }
|
|
|
|
|
|
|
|
bool operator<(const MatchTemplate& rhs) const
|
|
|
|
{
|
|
|
|
// high priority first!
|
2007-10-14 22:06:27 +02:00
|
|
|
return match_.priority() > rhs.match_.priority();
|
2007-07-19 19:01:42 +02:00
|
|
|
} // operator<
|
|
|
|
private:
|
2007-10-14 22:06:27 +02:00
|
|
|
Arabica::XPath::MatchExpr<std::string> match_;
|
2007-07-19 19:01:42 +02:00
|
|
|
Template* template_;
|
|
|
|
}; // struct MatchTemplate
|
|
|
|
|
2008-08-06 12:48:06 +02:00
|
|
|
typedef std::vector<Template*> TemplateList;
|
2007-07-19 19:01:42 +02:00
|
|
|
typedef std::vector<MatchTemplate> MatchTemplates;
|
2007-10-26 21:12:27 +02:00
|
|
|
typedef std::map<std::pair<std::string, std::string>, MatchTemplates> ModeTemplates;
|
2008-11-24 23:05:16 +01:00
|
|
|
typedef std::map<Precedence, ModeTemplates> TemplateStack;
|
2007-10-26 14:28:48 +02:00
|
|
|
typedef std::map<std::pair<std::string, std::string>, Template*> NamedTemplates;
|
2008-08-05 19:29:18 +02:00
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
typedef std::vector<Item*> VariableDeclList;
|
2008-08-06 12:48:06 +02:00
|
|
|
typedef std::vector<TopLevelParam*> ParamList;
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
TemplateList all_templates_;
|
|
|
|
NamedTemplates named_templates_;
|
|
|
|
TemplateStack templates_;
|
2008-11-19 18:26:07 +01:00
|
|
|
VariableDeclList topLevelVars_;
|
2008-08-05 19:29:18 +02:00
|
|
|
ParamList params_;
|
2007-07-19 19:01:42 +02:00
|
|
|
|
2007-10-26 21:12:27 +02:00
|
|
|
mutable std::pair<std::string, std::string> current_mode_;
|
2008-11-24 23:05:16 +01:00
|
|
|
mutable Precedence current_generation_;
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
Output::Settings output_settings_;
|
|
|
|
SinkHolder output_;
|
|
|
|
mutable std::ostream* error_output_;
|
2008-11-05 02:33:28 +01:00
|
|
|
}; // class CompiledStylesheet
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
} // namespace XSLT
|
|
|
|
} // namespace Arabica
|
|
|
|
|
2008-11-05 02:33:28 +01:00
|
|
|
#endif // ARABICA_XSLT_COMPILED_STYLESHEET_HPP
|
2007-07-19 19:01:42 +02:00
|
|
|
|