arabica/include/XSLT/impl/xslt_compiled_stylesheet.hpp

353 lines
13 KiB
C++
Raw Normal View History

#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"
2009-02-16 23:10:00 +01:00
#include "xslt_key.hpp"
#include "xslt_stylesheet.hpp"
2007-07-19 19:01:42 +02:00
namespace Arabica
{
namespace XSLT
{
2012-11-06 21:03:15 +01:00
template<class string_type, class string_adaptor>
class CompiledStylesheet : public Stylesheet<string_type, string_adaptor>
2007-07-19 19:01:42 +02:00
{
2012-11-06 21:03:15 +01:00
typedef Arabica::XPath::BoolValue<string_type, string_adaptor> BoolValue;
typedef Arabica::XPath::NumericValue<string_type, string_adaptor> NumericValue;
typedef Arabica::XPath::StringValue<string_type, string_adaptor> StringValue;
typedef Arabica::XPath::XPathValue<string_type, string_adaptor> Value;
typedef Arabica::XPath::NodeSet<string_type, string_adaptor> NodeSet;
typedef DOM::Node<string_type, string_adaptor> DOMNode;
typedef DOM::NodeList<string_type, string_adaptor> DOMNodeList;
2007-07-19 19:01:42 +02:00
public:
CompiledStylesheet() :
2012-11-06 21:03:15 +01:00
output_(new StreamSink<string_type, string_adaptor>(std::cout)),
error_output_(&std::cerr)
2007-07-19 19:01:42 +02:00
{
} // CompiledStylesheet
2007-07-19 19:01:42 +02:00
virtual ~CompiledStylesheet()
2007-07-19 19:01:42 +02:00
{
// let's clean up!
for(VariableDeclList::const_iterator ci = topLevelVars_.begin(), ce = topLevelVars_.end(); ci != ce; ++ci)
delete *ci;
2012-11-06 21:06:07 +01:00
for(ParamListIterator pi = params_.begin(), pe = params_.end(); pi != pe; ++pi)
delete *pi;
2012-11-06 21:07:26 +01:00
for(TemplateListIterator ti = all_templates_.begin(), te = all_templates_.end(); ti != te; ++ti)
delete *ti;
} // ~CompiledStylesheet
2007-07-19 19:01:42 +02:00
2012-11-06 21:03:15 +01:00
virtual void set_parameter(const string_type& name, bool value)
2007-07-19 19:01:42 +02:00
{
2009-02-18 09:37:16 +01:00
set_parameter(name, Value(new BoolValue(value)));
2007-07-19 19:01:42 +02:00
} // set_parameter
2012-11-06 21:03:15 +01:00
virtual void set_parameter(const string_type& name, double value)
2007-07-19 19:01:42 +02:00
{
2009-02-18 09:37:16 +01:00
set_parameter(name, Value(new NumericValue(value)));
2007-07-19 19:01:42 +02:00
} // set_parameter
2012-11-06 21:03:15 +01:00
virtual void set_parameter(const string_type& name, const char* value)
2007-07-19 19:01:42 +02:00
{
2009-02-18 09:37:16 +01:00
set_parameter(name, Value(new StringValue(value)));
2007-07-19 19:01:42 +02:00
} // set_parameter
2012-11-06 21:03:15 +01:00
virtual void set_parameter(const string_type& name, const string_type& value)
2007-07-19 19:01:42 +02:00
{
2009-02-18 09:37:16 +01:00
set_parameter(name, Value(new StringValue(value)));
2007-07-19 19:01:42 +02:00
} // set_parameter
2012-11-06 21:03:15 +01:00
virtual void set_output(Sink<string_type, string_adaptor>& sink)
2007-07-19 19:01:42 +02:00
{
output_.reset(sink);
} // set_output
virtual void set_error_output(std::ostream& os)
2007-07-19 19:01:42 +02:00
{
error_output_ = &os;
} // set_error_output
2012-11-06 21:03:15 +01:00
virtual void execute(const DOMNode& initialNode) const
2007-07-19 19:01:42 +02:00
{
if(initialNode == 0)
throw std::runtime_error("Input document is empty");
2012-11-06 21:03:15 +01:00
NodeSet ns;
2007-07-19 19:01:42 +02:00
ns.push_back(initialNode);
ExecutionContext context(*this, output_.get(), *error_output_);
// set up variables and so forth
2012-11-06 21:06:07 +01:00
for(ParamListIterator pi = params_.begin(), pe = params_.end(); pi != pe; ++pi)
(*pi)->declare(context);
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_, output_cdata_elements_);
applyTemplates(ns, context, "");
2007-07-19 19:01:42 +02:00
output_.get().asOutput().end_document();
} // execute
////////////////////////////////////////
2012-11-06 21:03:15 +01:00
const DeclaredKeys<string_type, string_adaptor>& keys() const { return keys_; }
2012-11-06 21:03:15 +01:00
void add_template(Template<string_type, string_adaptor>* templat)
2007-07-19 19:01:42 +02:00
{
2012-11-06 21:03:15 +01:00
typedef typename Template<string_type, string_adaptor>::MatchExprList MatchExprList;
typedef typename MatchExprList::const_iterator MatchIterator;
2007-07-19 19:01:42 +02:00
all_templates_.push_back(templat);
for(MatchIterator e = templat->compiled_matches().begin(), ee = templat->compiled_matches().end(); e != ee; ++e)
templates_[templat->precedence()][templat->mode()].push_back(MatchTemplate(*e, templat));
2007-07-19 19:01:42 +02:00
if(!templat->has_name())
return;
2012-11-06 21:03:15 +01:00
NamedTemplatesIterator named_template = named_templates_.find(templat->name());
if(named_template != named_templates_.end())
2007-07-19 19:01:42 +02:00
{
const Precedence& existing_precedence = named_template->second->precedence();
if(existing_precedence > templat->precedence())
2012-11-06 21:03:15 +01:00
return;
if(existing_precedence == templat->precedence())
2012-11-06 21:03:15 +01:00
throw SAX::SAXException("Template named '" +
templat->name() +
"' already defined");
} // if ...
2007-07-19 19:01:42 +02:00
named_templates_[templat->name()] = templat;
} // add_template
void add_variable(Item* item)
2007-07-19 19:01:42 +02:00
{
topLevelVars_.push_back(item);
2007-07-19 19:01:42 +02:00
} // add_item
2012-11-06 21:03:15 +01:00
void add_key(const string_type& name,
Key<string_type, string_adaptor>* key)
2009-02-16 23:10:00 +01:00
{
keys_.add(name, key);
2009-02-16 23:10:00 +01:00
} // add_key
2012-11-06 21:03:15 +01:00
void output_settings(const typename Output<string_type, string_adaptor>::Settings& settings,
const typename Output<string_type, string_adaptor>::CDATAElements& cdata_elements)
2007-07-19 19:01:42 +02:00
{
output_settings_ = settings;
output_cdata_elements_.insert(cdata_elements.begin(), cdata_elements.end());
2012-11-06 21:03:15 +01:00
} // output_settingsp
2007-07-19 19:01:42 +02:00
void prepare()
{
2012-11-06 21:03:15 +01:00
for(typename TemplateStack::iterator ts = templates_.begin(), tse = templates_.end(); ts != tse; ++ts)
for(typename 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
////////////////////////////////////////
2012-11-06 21:03:15 +01:00
void applyTemplates(const NodeSet& nodes, ExecutionContext& context, const string_type& mode) const
2007-07-19 19:01:42 +02:00
{
// entirely simple so far
LastFrame last(context, nodes.size());
int p = 1;
2012-11-06 21:03:15 +01:00
for(typename NodeSet::const_iterator n = nodes.begin(), ne = nodes.end(); n != ne; ++n)
2007-07-19 19:01:42 +02:00
{
context.setPosition(*n, p++);
doApplyTemplates(*n, context, mode, Precedence::FrozenPrecedence());
2007-07-19 19:01:42 +02:00
}
} // applyTemplates
2012-11-06 21:03:15 +01:00
void applyTemplates(const DOMNodeList& nodes, ExecutionContext& context, const string_type& mode) const
2007-07-19 19:01:42 +02:00
{
// entirely simple so far
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);
doApplyTemplates(nodes.item(i), context, mode, Precedence::FrozenPrecedence());
2007-07-19 19:01:42 +02:00
}
} // applyTemplates
2012-11-06 21:03:15 +01:00
void applyTemplates(const DOMNode& node, ExecutionContext& context, const string_type& mode) const
2007-07-19 19:01:42 +02:00
{
LastFrame last(context, -1);
context.setPosition(node, 1);
doApplyTemplates(node, context, mode, Precedence::FrozenPrecedence());
2007-07-19 19:01:42 +02:00
} // applyTemplates
2012-11-06 21:03:15 +01:00
void callTemplate(const string_type& name, const DOMNode& node, ExecutionContext& context) const
2007-07-19 19:01:42 +02:00
{
StackFrame frame(context);
2012-11-06 21:03:15 +01:00
NamedTemplatesIterator t = named_templates_.find(name);
2007-07-19 19:01:42 +02:00
if(t == named_templates_.end())
{
2007-10-26 14:28:48 +02:00
std::cerr << "No template named '";
std::cerr << name << "'. I should be a compile-time error!" << std::endl;
throw SAX::SAXException("No template named " + name + ". I should be a compile-time error. Sorry!");
2007-07-19 19:01:42 +02:00
return;
}
t->second->execute(node, context);
} // callTemplate
2012-11-06 21:03:15 +01:00
void applyImports(const DOMNode& node, ExecutionContext& context) const
2007-07-19 19:01:42 +02:00
{
doApplyTemplates(node, context, current_mode_, current_generation_);
2007-07-19 19:01:42 +02:00
} // applyImports
private:
2012-11-06 21:03:15 +01:00
void doApplyTemplates(const DOMNode& node,
2007-10-26 21:12:27 +02:00
ExecutionContext& context,
2012-11-06 21:03:15 +01:00
const string_type& mode,
const Precedence& generation) const
2007-07-19 19:01:42 +02:00
{
StackFrame frame(context);
std::vector<Precedence> lower_precedences;
2012-11-06 21:03:15 +01:00
for(TemplateStackIterator ts = templates_.begin(), tse = templates_.end(); ts != tse; ++ts)
if(generation.is_descendant(ts->first))
lower_precedences.push_back(ts->first);
std::sort(lower_precedences.rbegin(), lower_precedences.rend());
2007-07-19 19:01:42 +02:00
current_mode_ = mode;
for(std::vector<Precedence>::const_iterator p = lower_precedences.begin(), pe = lower_precedences.end(); p != pe; ++p)
{
current_generation_ = *p;
ModeTemplates ts = templates_.find(current_generation_)->second;
2012-11-06 21:03:15 +01:00
ModeTemplatesIterator mt = ts.find(mode);
if(mt != ts.end())
2007-07-19 19:01:42 +02:00
{
const MatchTemplates& templates = mt->second;
2012-11-06 21:03:15 +01:00
for(MatchTemplatesIterator t = templates.begin(), te = templates.end(); t != te; ++t)
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
2012-11-06 21:03:15 +01:00
void defaultAction(const DOMNode& node,
2007-10-26 21:12:27 +02:00
ExecutionContext& context,
2012-11-06 21:03:15 +01:00
const string_type& mode) const
2007-07-19 19:01:42 +02:00
{
switch(node.getNodeType())
{
2012-11-06 21:03:15 +01:00
case DOM::Node_base::DOCUMENT_NODE:
case DOM::Node_base::DOCUMENT_FRAGMENT_NODE:
case DOM::Node_base::ELEMENT_NODE:
2007-07-19 19:01:42 +02:00
applyTemplates(node.getChildNodes(), context, mode);
break;
2012-11-06 21:03:15 +01:00
case DOM::Node_base::ATTRIBUTE_NODE:
case DOM::Node_base::TEXT_NODE:
case DOM::Node_base::CDATA_SECTION_NODE:
context.sink().characters(node.getNodeValue());
/*
2007-07-19 19:01:42 +02:00
{
2012-11-06 21:03:15 +01:00
const string_type& ch = node.getNodeValue();
for(string_type::const_iterator i = ch.begin(), e = ch.end(); i != e; ++i)
2007-07-19 19:01:42 +02:00
if(!Arabica::XML::is_space(*i))
{
context.sink().characters(ch);
return;
} // if ...
}
*/
2007-07-19 19:01:42 +02:00
break;
default:
;// nothing!
} // switch
} // defaultAction
2012-11-06 21:03:15 +01:00
void set_parameter(const string_type& name, Value value)
2007-07-19 19:01:42 +02:00
{
2012-11-06 21:03:15 +01:00
params_.push_back(new TopLevelParam<string_type, string_adaptor>("", name, value));
2007-07-19 19:01:42 +02:00
} // set_parameter
2012-11-06 21:03:15 +01:00
void set_parameter(const string_type& namespace_uri, const string_type& name, Value value)
2007-07-19 19:01:42 +02:00
{
2012-11-06 21:03:15 +01:00
params_.push_back(new TopLevelParam<string_type, string_adaptor>(namespace_uri, name, value));
2007-07-19 19:01:42 +02:00
} // set_parameter
private:
class MatchTemplate
{
public:
2012-11-06 21:03:15 +01:00
MatchTemplate(const Arabica::XPath::MatchExpr<string_type, string_adaptor>& matchExpr, Template<string_type, string_adaptor>* templat) :
match_(matchExpr),
2007-07-19 19:01:42 +02:00
template_(templat)
{
} // MatchTemplate
MatchTemplate(const MatchTemplate& rhs) :
match_(rhs.match_),
template_(rhs.template_)
{
} // MatchTemplate
2012-11-06 21:03:15 +01:00
const Arabica::XPath::MatchExpr<string_type, string_adaptor>& match() const { return match_; }
Template<string_type, string_adaptor>* action() const { return template_; }
2007-07-19 19:01:42 +02:00
bool operator<(const MatchTemplate& rhs) const
{
// high priority first!
return match_.priority() > rhs.match_.priority();
2007-07-19 19:01:42 +02:00
} // operator<
private:
2012-11-06 21:03:15 +01:00
Arabica::XPath::MatchExpr<string_type, string_adaptor> match_;
Template<string_type, string_adaptor>* template_;
2007-07-19 19:01:42 +02:00
}; // struct MatchTemplate
2012-11-06 21:03:15 +01:00
typedef std::vector<Template<string_type, string_adaptor>*> TemplateList;
2012-11-06 21:07:26 +01:00
typedef typename TemplateList::const_iterator TemplateListIterator;
2007-07-19 19:01:42 +02:00
typedef std::vector<MatchTemplate> MatchTemplates;
2012-11-06 21:03:15 +01:00
typedef typename MatchTemplates::const_iterator MatchTemplatesIterator;
typedef std::map<string_type, MatchTemplates> ModeTemplates;
typedef typename ModeTemplates::const_iterator ModeTemplatesIterator;
typedef std::map<Precedence, ModeTemplates> TemplateStack;
2012-11-06 21:03:15 +01:00
typedef typename TemplateStack::const_iterator TemplateStackIterator;
typedef std::map<string_type, Template<string_type, string_adaptor>*> NamedTemplates;
typedef typename NamedTemplates::const_iterator NamedTemplatesIterator;
typedef std::vector<Item*> VariableDeclList;
2012-11-06 21:03:15 +01:00
typedef std::vector<TopLevelParam<string_type, string_adaptor>*> ParamList;
2012-11-06 21:06:07 +01:00
typedef typename ParamList::const_iterator ParamListIterator;
2007-07-19 19:01:42 +02:00
TemplateList all_templates_;
NamedTemplates named_templates_;
TemplateStack templates_;
VariableDeclList topLevelVars_;
2012-11-06 21:03:15 +01:00
DeclaredKeys<string_type, string_adaptor> keys_;
ParamList params_;
2007-07-19 19:01:42 +02:00
2012-11-06 21:03:15 +01:00
mutable string_type current_mode_;
mutable Precedence current_generation_;
2007-07-19 19:01:42 +02:00
2012-11-06 21:03:15 +01:00
typename Output<string_type, string_adaptor>::Settings output_settings_;
typename Output<string_type, string_adaptor>::CDATAElements output_cdata_elements_;
SinkHolder<string_type, string_adaptor> output_;
2007-07-19 19:01:42 +02:00
mutable std::ostream* error_output_;
}; // class CompiledStylesheet
2007-07-19 19:01:42 +02:00
} // namespace XSLT
} // namespace Arabica
#endif // ARABICA_XSLT_COMPILED_STYLESHEET_HPP
2007-07-19 19:01:42 +02:00