2007-07-19 19:01:42 +02:00
|
|
|
#ifndef ARABICA_XSLT_COMPILATION_CONTEXT
|
|
|
|
#define ARABICA_XSLT_COMPILATION_CONTEXT
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <SAX/XMLReader.hpp>
|
|
|
|
#include <SAX/helpers/DefaultHandler.hpp>
|
2008-08-01 20:20:28 +02:00
|
|
|
#include <XML/strings.hpp>
|
2007-07-19 19:01:42 +02:00
|
|
|
#include <XPath/XPath.hpp>
|
|
|
|
#include <stack>
|
|
|
|
|
2007-08-22 14:38:20 +02:00
|
|
|
#include "xslt_stylesheet_parser.hpp"
|
2007-11-22 20:24:18 +01:00
|
|
|
#include "xslt_functions.hpp"
|
2007-08-22 14:38:20 +02:00
|
|
|
|
2007-07-19 19:01:42 +02:00
|
|
|
namespace Arabica
|
|
|
|
{
|
|
|
|
namespace XSLT
|
|
|
|
{
|
2008-11-05 02:33:28 +01:00
|
|
|
class CompiledStylesheet;
|
2007-07-19 19:01:42 +02:00
|
|
|
class ItemContainer;
|
|
|
|
|
2007-11-22 20:24:18 +01:00
|
|
|
class CompilationContext :
|
|
|
|
private Arabica::XPath::FunctionResolver<std::string>,
|
|
|
|
private Arabica::XPath::NamespaceContext<std::string, Arabica::default_string_adaptor<std::string> >
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-08-22 14:38:20 +02:00
|
|
|
CompilationContext(StylesheetParser& parser,
|
2008-11-05 02:33:28 +01:00
|
|
|
CompiledStylesheet& stylesheet) :
|
2007-07-19 19:01:42 +02:00
|
|
|
parser_(parser),
|
|
|
|
stylesheet_(stylesheet),
|
2007-11-22 20:24:18 +01:00
|
|
|
autoNs_(1),
|
2008-11-25 13:27:33 +01:00
|
|
|
current_allowed_(false),
|
|
|
|
precedence_(Precedence::InitialPrecedence())
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2007-11-22 20:24:18 +01:00
|
|
|
xpath_.setNamespaceContext(*this);
|
|
|
|
xpath_.setFunctionResolver(*this);
|
2007-07-19 19:01:42 +02:00
|
|
|
} // CompilationContext
|
|
|
|
|
|
|
|
~CompilationContext()
|
|
|
|
{
|
|
|
|
// delete any left over - will only be the case if unwinding
|
|
|
|
while(handlerStack_.size() > 1)
|
|
|
|
{
|
|
|
|
delete handlerStack_.top();
|
|
|
|
handlerStack_.pop();
|
|
|
|
} // while ...
|
|
|
|
} // ~CompilationContext
|
|
|
|
|
2007-09-05 14:57:07 +02:00
|
|
|
void root(SAX::DefaultHandler<std::string>& root)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
handlerStack_.push(&root);
|
|
|
|
} // root
|
|
|
|
|
2007-08-22 14:38:20 +02:00
|
|
|
StylesheetParser& parser() const { return parser_; }
|
2007-11-22 18:50:25 +01:00
|
|
|
Arabica::XPath::XPathExpressionPtr<std::string> xpath_expression(const std::string& expr) const { return xpath_.compile_expr(expr); }
|
2007-11-22 20:24:18 +01:00
|
|
|
std::vector<Arabica::XPath::MatchExpr<std::string> > xpath_match(const std::string& match) const
|
|
|
|
{
|
|
|
|
DisallowCurrent guard(current_allowed_);
|
|
|
|
return xpath_.compile_match(match);
|
|
|
|
} // xpath_match
|
2007-11-22 18:50:25 +01:00
|
|
|
Arabica::XPath::XPathExpressionPtr<std::string> xpath_attribute_value_template(const std::string& expr) const { return xpath_.compile_attribute_value_template(expr); }
|
2008-11-05 02:33:28 +01:00
|
|
|
CompiledStylesheet& stylesheet() const { return stylesheet_; }
|
2007-07-19 19:01:42 +02:00
|
|
|
|
2009-04-07 14:39:31 +02:00
|
|
|
XML::QualifiedName<std::string> processElementQName(const std::string& qName) const
|
|
|
|
{
|
|
|
|
return parser_.processElementQName(qName);
|
|
|
|
} // processElementQName
|
|
|
|
|
2009-02-24 13:21:35 +01:00
|
|
|
XML::QualifiedName<std::string> processInternalQName(const std::string& qName) const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2009-02-24 13:21:35 +01:00
|
|
|
return parser_.processInternalQName(qName);
|
2008-08-05 23:03:33 +02:00
|
|
|
} // processInternalQName
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
std::string makeAbsolute(const std::string& href) const
|
|
|
|
{
|
2007-08-22 14:38:20 +02:00
|
|
|
return parser_.makeAbsolute(href);
|
2007-07-19 19:01:42 +02:00
|
|
|
} // makeAbsolute
|
|
|
|
|
2007-08-24 14:37:32 +02:00
|
|
|
std::string setBase(const std::string& href) const
|
|
|
|
{
|
|
|
|
return parser_.setBase(href);
|
|
|
|
} // setBase
|
|
|
|
|
2008-12-29 23:29:04 +01:00
|
|
|
std::string currentBase() const
|
|
|
|
{
|
|
|
|
return parser_.currentBase();
|
|
|
|
} // currentBase
|
|
|
|
|
2007-07-19 19:01:42 +02:00
|
|
|
void push(ItemContainer* parent,
|
2007-09-05 14:57:07 +02:00
|
|
|
SAX::DefaultHandler<std::string>* newHandler,
|
2007-07-19 19:01:42 +02:00
|
|
|
const std::string& namespaceURI,
|
|
|
|
const std::string& localName,
|
|
|
|
const std::string& qName,
|
2007-09-05 14:57:07 +02:00
|
|
|
const SAX::Attributes<std::string>& atts)
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
|
|
|
parentStack_.push(parent);
|
|
|
|
handlerStack_.push(newHandler);
|
|
|
|
parser_.setContentHandler(*newHandler);
|
2009-04-24 20:02:14 +02:00
|
|
|
newHandler->startElement(namespaceURI, localName, qName, atts);
|
2007-07-19 19:01:42 +02:00
|
|
|
} // push
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
{
|
|
|
|
parentStack_.pop();
|
|
|
|
delete handlerStack_.top();
|
|
|
|
handlerStack_.pop();
|
|
|
|
parser_.setContentHandler(*handlerStack_.top());
|
|
|
|
} // pop
|
|
|
|
|
|
|
|
ItemContainer& parentContainer() const
|
|
|
|
{
|
|
|
|
return *parentStack_.top();
|
|
|
|
} // parentContainer
|
|
|
|
|
2007-09-05 14:57:07 +02:00
|
|
|
SAX::ContentHandler<std::string>& parentHandler() const
|
2007-07-19 19:01:42 +02:00
|
|
|
{
|
2007-08-24 14:37:32 +02:00
|
|
|
parser_.setContentHandler(*handlerStack_.top());
|
|
|
|
return parser_.contentHandler();
|
2007-07-19 19:01:42 +02:00
|
|
|
} // parentHandler
|
|
|
|
|
|
|
|
std::map<std::string, std::string> inScopeNamespaces() const
|
|
|
|
{
|
|
|
|
return parser_.inScopeNamespaces();
|
|
|
|
} // inScopeNamespaces
|
|
|
|
|
|
|
|
void addNamespaceAlias(const std::string& stylesheet_namespace,
|
|
|
|
const std::string& result_prefix,
|
|
|
|
const std::string& result_namespace)
|
|
|
|
{
|
|
|
|
namespaceRemap_[stylesheet_namespace] = std::make_pair(result_prefix, result_namespace);
|
|
|
|
} // addNamespaceAlias
|
|
|
|
|
|
|
|
bool isRemapped(const std::string& namespaceURI) const
|
|
|
|
{
|
|
|
|
return namespaceRemap_.find(namespaceURI) != namespaceRemap_.end();
|
|
|
|
} // isRemapped
|
|
|
|
|
|
|
|
const std::pair<std::string, std::string>& remappedNamespace(const std::string& namespaceURI)
|
|
|
|
{
|
|
|
|
return namespaceRemap_[namespaceURI];
|
|
|
|
} // remappedNamespace
|
|
|
|
|
|
|
|
std::string autoNamespacePrefix() const
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "auto-ns" << autoNs_++;
|
|
|
|
return ss.str();
|
|
|
|
} // autoNamespacePrefix
|
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
void set_precedence(const Precedence& prec)
|
2008-11-19 18:26:07 +01:00
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
precedence_ = prec;
|
|
|
|
} // set_precedence
|
2008-11-19 18:26:07 +01:00
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
Precedence next_precedence()
|
2008-11-19 18:26:07 +01:00
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
return precedence_.next_generation();
|
|
|
|
} // next_precedence
|
2008-11-25 00:11:22 +01:00
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
const Precedence& precedence() const
|
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
return precedence_;
|
2008-11-19 18:26:07 +01:00
|
|
|
} // precedence
|
2007-07-19 19:01:42 +02:00
|
|
|
|
|
|
|
private:
|
2007-11-22 20:24:18 +01:00
|
|
|
// FunctionResolver
|
|
|
|
virtual Arabica::XPath::XPathFunction<std::string>* resolveFunction(
|
|
|
|
const std::string& namespace_uri,
|
|
|
|
const std::string& name,
|
|
|
|
const std::vector<Arabica::XPath::XPathExpression<std::string> >& argExprs) const
|
|
|
|
{
|
|
|
|
if(!namespace_uri.empty())
|
2010-02-21 00:25:36 +01:00
|
|
|
return new UndefinedFunction(namespace_uri, name, argExprs);
|
2007-11-22 20:24:18 +01:00
|
|
|
|
|
|
|
// document
|
|
|
|
if(name == "document")
|
|
|
|
return new DocumentFunction(parser_.currentBase(), argExprs);
|
|
|
|
// key
|
2009-02-18 09:37:16 +01:00
|
|
|
if(name == "key")
|
2009-02-26 10:32:55 +01:00
|
|
|
return new KeyFunction(stylesheet_.keys(), parser_.inScopeNamespaces(), argExprs);
|
2007-11-22 20:24:18 +01:00
|
|
|
// format-number
|
2010-02-20 22:56:47 +01:00
|
|
|
// current
|
2007-11-22 20:24:18 +01:00
|
|
|
if((name == "current") && (current_allowed_))
|
|
|
|
return new CurrentFunction(argExprs);
|
|
|
|
// unparsed-entity-uri
|
2008-11-05 03:55:44 +01:00
|
|
|
//if(name == "unparsed-entity-uri")
|
|
|
|
// return new UnparsedEntityUriFunction(argExprs);
|
2007-11-22 20:24:18 +01:00
|
|
|
// generate-id
|
2008-10-27 20:13:47 +01:00
|
|
|
if(name == "generate-id")
|
|
|
|
return new GenerateIdFunction(argExprs);
|
2007-11-22 20:24:18 +01:00
|
|
|
if(name == "system-property")
|
|
|
|
return new SystemPropertyFunction(argExprs);
|
2007-12-25 23:23:25 +01:00
|
|
|
// element-available
|
2010-02-21 19:35:58 +01:00
|
|
|
if(name == "element-available")
|
|
|
|
{
|
|
|
|
std::vector<std::pair<std::string, std::string> > dummy;
|
|
|
|
return new ElementAvailableFunction(dummy, parser_.inScopeNamespaces(), argExprs);
|
|
|
|
}
|
2007-12-25 23:23:25 +01:00
|
|
|
// function-available
|
2010-02-20 00:21:30 +01:00
|
|
|
if(name == "function-available")
|
2010-02-20 22:56:47 +01:00
|
|
|
return new FunctionAvailableFunction(validNames(), parser_.inScopeNamespaces(), argExprs);
|
|
|
|
|
2007-11-22 20:24:18 +01:00
|
|
|
return 0;
|
|
|
|
} // resolveFunction
|
|
|
|
|
2010-02-20 22:56:47 +01:00
|
|
|
virtual std::vector<std::pair<std::string, std::string> > validNames() const
|
2010-02-20 14:03:22 +01:00
|
|
|
{
|
2010-02-20 22:56:47 +01:00
|
|
|
static const char* functionNames[] = { "document", "key", /* format-number, */ "current",
|
|
|
|
/* unparsed-entity-uri, */ "generate-id", "system-property",
|
|
|
|
/* element-available, */ "function-available", 0 };
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string,std::string> > names;
|
|
|
|
|
|
|
|
for(int i = 0; functionNames[i] != 0; ++i)
|
|
|
|
names.push_back(std::make_pair("", functionNames[i]));
|
|
|
|
|
|
|
|
return names;
|
|
|
|
} // validNames
|
2010-02-20 14:03:22 +01:00
|
|
|
|
2007-11-22 20:24:18 +01:00
|
|
|
// NamespaceContext
|
|
|
|
virtual std::string namespaceURI(const std::string& prefix) const
|
|
|
|
{
|
|
|
|
return parser_.namespaceURI(prefix);
|
|
|
|
} // namespaceURI
|
|
|
|
|
2007-07-19 19:01:42 +02:00
|
|
|
typedef std::pair<std::string, std::string> Namespace;
|
|
|
|
|
2007-08-22 14:38:20 +02:00
|
|
|
StylesheetParser& parser_;
|
2008-11-05 02:33:28 +01:00
|
|
|
CompiledStylesheet& stylesheet_;
|
2010-01-10 22:25:35 +01:00
|
|
|
mutable int autoNs_;
|
|
|
|
mutable bool current_allowed_;
|
|
|
|
Precedence precedence_;
|
|
|
|
Arabica::XPath::XPath<std::string> xpath_;
|
2007-09-05 14:57:07 +02:00
|
|
|
std::stack<SAX::DefaultHandler<std::string>*> handlerStack_;
|
2007-07-19 19:01:42 +02:00
|
|
|
std::stack<ItemContainer*> parentStack_;
|
|
|
|
std::map<std::string, Namespace> namespaceRemap_;
|
|
|
|
|
|
|
|
CompilationContext(const CompilationContext&);
|
2007-11-22 20:24:18 +01:00
|
|
|
|
|
|
|
class DisallowCurrent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DisallowCurrent(bool& allow) : allow_(allow) { allow_ = false; }
|
|
|
|
~DisallowCurrent() { allow_ = true; }
|
|
|
|
private:
|
|
|
|
bool& allow_;
|
|
|
|
}; // DisallowCurrent
|
2007-07-19 19:01:42 +02:00
|
|
|
}; // class CompilationContext
|
|
|
|
|
|
|
|
} // namespace XSLT
|
|
|
|
} // namespace Arabica
|
|
|
|
|
|
|
|
#endif // ARABICA_XSLT_COMPILATION_CONTEXT
|
|
|
|
|