diff --git a/include/XSLT/impl/xslt_compilation_context.hpp b/include/XSLT/impl/xslt_compilation_context.hpp index ee785168..20995a85 100644 --- a/include/XSLT/impl/xslt_compilation_context.hpp +++ b/include/XSLT/impl/xslt_compilation_context.hpp @@ -204,35 +204,35 @@ private: const std::vector >& argExprs) const { if(!namespace_uri.empty()) - return new UndefinedFunction(namespace_uri, name, argExprs); + return new Functions::UndefinedFunction(namespace_uri, name, argExprs); // document if(name == "document") - return new DocumentFunction(parser_.currentBase(), argExprs); + return new Functions::DocumentFunction(parser_.currentBase(), argExprs); // key if(name == "key") - return new KeyFunction(stylesheet_.keys(), parser_.inScopeNamespaces(), argExprs); + return new Functions::KeyFunction(stylesheet_.keys(), parser_.inScopeNamespaces(), argExprs); // format-number // current if((name == "current") && (current_allowed_)) - return new CurrentFunction(argExprs); + return new Functions::CurrentFunction(argExprs); // unparsed-entity-uri //if(name == "unparsed-entity-uri") // return new UnparsedEntityUriFunction(argExprs); // generate-id if(name == "generate-id") - return new GenerateIdFunction(argExprs); + return new Functions::GenerateIdFunction(argExprs); if(name == "system-property") - return new SystemPropertyFunction(argExprs); + return new Functions::SystemPropertyFunction(argExprs); // element-available if(name == "element-available") { std::vector > dummy; - return new ElementAvailableFunction(dummy, parser_.inScopeNamespaces(), argExprs); + return new Functions::ElementAvailableFunction(dummy, parser_.inScopeNamespaces(), argExprs); } // function-available if(name == "function-available") - return new FunctionAvailableFunction(validNames(), parser_.inScopeNamespaces(), argExprs); + return new Functions::FunctionAvailableFunction(validNames(), parser_.inScopeNamespaces(), argExprs); return 0; } // resolveFunction diff --git a/include/XSLT/impl/xslt_functions.hpp b/include/XSLT/impl/xslt_functions.hpp index 62db42a1..89241f8d 100644 --- a/include/XSLT/impl/xslt_functions.hpp +++ b/include/XSLT/impl/xslt_functions.hpp @@ -9,25 +9,42 @@ namespace Arabica namespace XSLT { -// node-set document(object, node-set?) -class DocumentFunction : public Arabica::XPath::NodeSetXPathFunction +template +class Functions { - typedef Arabica::XPath::NodeSetXPathFunction baseT; +public: + +typedef Arabica::XPath::XPathExpression XPathExpression; +typedef std::vector ArgList; + +typedef Arabica::XPath::XPathValue XPathValue; + +typedef Arabica::XPath::NodeSet NodeSet; + +typedef Arabica::XPath::ExecutionContext XPathExecutionContext; +typedef DOM::Node DOMNode; + +typedef XML::QualifiedName QualifiedName; + +// node-set document(object, node-set?) +class DocumentFunction : public Arabica::XPath::NodeSetXPathFunction +{ + typedef Arabica::XPath::NodeSetXPathFunction baseT; public: - DocumentFunction(const std::string& currentBase, - const std::vector >& args) : - Arabica::XPath::NodeSetXPathFunction(1, 2, args), + DocumentFunction(const string_type& currentBase, + const ArgList& args) : + Arabica::XPath::NodeSetXPathFunction(1, 2, args), baseURI_(currentBase) { } protected: - virtual Arabica::XPath::NodeSet doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual NodeSet doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - Arabica::XPath::NodeSet nodes; + NodeSet nodes; - Arabica::XPath::XPathValue a0 = arg(0, context, executionContext); + XPathValue a0 = arg(0, context, executionContext); if(argCount() != 1) throw Arabica::XPath::UnsupportedException("two arg version of document()"); if(a0.type() != Arabica::XPath::STRING) @@ -38,64 +55,63 @@ protected: } // doEvaluate private: - void load_document(const std::string& location, - Arabica::XPath::NodeSet& nodes) const + void load_document(const string_type& location, NodeSet& nodes) const { - SAX2DOM::Parser domParser; - SAX::CatchErrorHandler eh; + SAX2DOM::Parser domParser; + SAX::CatchErrorHandler eh; domParser.setErrorHandler(eh); Arabica::io::URI base(baseURI_); Arabica::io::URI absolute(base, location); - SAX::InputSource is(absolute.as_string()); + SAX::InputSource is(absolute.as_string()); domParser.parse(is); if(!eh.errorsReported()) - nodes.push_back(domParser.getDocument()); + nodes.push_back(domParser.getDocument()); else - std::cerr << eh.errors() << std::endl; + std::cerr << eh.errors() << std::endl; } // load_document - std::string baseURI_; + string_type baseURI_; }; // DocumentFunction // node-set key(string, object) -class KeyFunction : public Arabica::XPath::NodeSetXPathFunction +class KeyFunction : public Arabica::XPath::NodeSetXPathFunction { - typedef Arabica::XPath::NodeSetXPathFunction baseT; + typedef Arabica::XPath::NodeSetXPathFunction baseT; public: - KeyFunction(const DeclaredKeys >& keys, - const std::map& inscopeNamespaces, - const std::vector >& args) : - Arabica::XPath::NodeSetXPathFunction(2, 2, args), + KeyFunction(const DeclaredKeys& keys, + const std::map& inscopeNamespaces, + const ArgList& args) : + Arabica::XPath::NodeSetXPathFunction(2, 2, args), keys_(keys), namespaces_(inscopeNamespaces) { } // KeyFunction protected: - virtual Arabica::XPath::NodeSet doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual NodeSet doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - std::string keyname = argAsString(0, context, executionContext); - std::string keyClarkName = XML::QualifiedName::parseQName(keyname, true, namespaces_).clarkName(); + string_type keyname = argAsString(0, context, executionContext); + string_type keyClarkName = QualifiedName::parseQName(keyname, true, namespaces_).clarkName(); - Arabica::XPath::XPathValue a1 = baseT::arg(1, context, executionContext); + XPathValue a1 = baseT::arg(1, context, executionContext); if(a1.type() == Arabica::XPath::NODE_SET) return nodeSetUnion(keyClarkName, a1.asNodeSet(), executionContext); return keys_.lookup(keyClarkName, a1.asString(), executionContext); } // doEvaluate - Arabica::XPath::NodeSet nodeSetUnion(const std::string& keyClarkName, - const Arabica::XPath::NodeSet nodes, - const Arabica::XPath::ExecutionContext& executionContext) const + NodeSet nodeSetUnion(const string_type& keyClarkName, + const NodeSet nodes, + const XPathExecutionContext& executionContext) const { - Arabica::XPath::NodeSet results; - for(Arabica::XPath::NodeSet::const_iterator n = nodes.begin(), ne = nodes.end(); n != ne; ++n) + NodeSet results; + for(typename NodeSet::const_iterator n = nodes.begin(), ne = nodes.end(); n != ne; ++n) { - std::string id = Arabica::XPath::impl::nodeStringValue >(*n); + string_type id = Arabica::XPath::impl::nodeStringValue(*n); results.push_back(keys_.lookup(keyClarkName, id, executionContext)); } // for ... results.to_document_order(); @@ -103,41 +119,41 @@ protected: } // nodeSetUnion private: - const DeclaredKeys >& keys_; - std::map namespaces_; + const DeclaredKeys& keys_; + std::map namespaces_; }; // class KeyFunction // string format-number(number, string, string?) // node-set current() -class CurrentFunction : public Arabica::XPath::NodeSetXPathFunction +class CurrentFunction : public Arabica::XPath::NodeSetXPathFunction { - typedef Arabica::XPath::NodeSetXPathFunction baseT; + typedef Arabica::XPath::NodeSetXPathFunction baseT; public: - CurrentFunction(const std::vector >& args) : - Arabica::XPath::NodeSetXPathFunction(0, 0, args) { } + CurrentFunction(const ArgList& args) : + Arabica::XPath::NodeSetXPathFunction(0, 0, args) { } protected: - virtual Arabica::XPath::NodeSet doEvaluate(const DOM::Node& /* context */, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual NodeSet doEvaluate(const DOMNode& /* context */, + const XPathExecutionContext& executionContext) const { - Arabica::XPath::NodeSet set; + NodeSet set; set.push_back(executionContext.currentNode()); return set; } // doEvaluate }; // CurrentFunction // string unparsed-entity-uri(string) -class UnparsedEntityUriFunction : public Arabica::XPath::StringXPathFunction +class UnparsedEntityUriFunction : public Arabica::XPath::StringXPathFunction { public: - UnparsedEntityUriFunction(const std::vector >& args) : - Arabica::XPath::StringXPathFunction(1, 1, args) { } + UnparsedEntityUriFunction(const ArgList& args) : + Arabica::XPath::StringXPathFunction(1, 1, args) { } protected: - virtual std::string doEvaluate(const DOM::Node& /* context */, - const Arabica::XPath::ExecutionContext& /* executionContext */) const + virtual string_type doEvaluate(const DOMNode& /* context */, + const XPathExecutionContext& /* executionContext */) const { // This is a minimal, but I think conformant, implementation return ""; @@ -145,23 +161,23 @@ protected: }; // UnparsedEntityUri // string generate-id(node-set?) -class GenerateIdFunction : public Arabica::XPath::StringXPathFunction +class GenerateIdFunction : public Arabica::XPath::StringXPathFunction { - typedef Arabica::XPath::StringXPathFunction baseT; + typedef Arabica::XPath::StringXPathFunction baseT; public: - GenerateIdFunction(const std::vector >& args) : - Arabica::XPath::StringXPathFunction(0, 1, args) { } + GenerateIdFunction(const ArgList& args) : + Arabica::XPath::StringXPathFunction(0, 1, args) { } protected: - virtual std::string doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual string_type doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - DOM::Node node; + DOMNode node; if(baseT::argCount() == 0) node = context; else { - Arabica::XPath::NodeSet ns = baseT::argAsNodeSet(0, context, executionContext); + NodeSet ns = baseT::argAsNodeSet(0, context, executionContext); if(ns.size() == 0) return ""; node = ns.top(); @@ -174,19 +190,19 @@ protected: }; // class GenerateIdFunction // object system-property(string) -class SystemPropertyFunction : public Arabica::XPath::StringXPathFunction +class SystemPropertyFunction : public Arabica::XPath::StringXPathFunction { - typedef Arabica::XPath::StringXPathFunction baseT; + typedef Arabica::XPath::StringXPathFunction baseT; public: - SystemPropertyFunction (const std::vector >& args) : - Arabica::XPath::StringXPathFunction(1, 1, args) { } + SystemPropertyFunction (const ArgList& args) : + Arabica::XPath::StringXPathFunction(1, 1, args) { } protected: - virtual std::string doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual string_type doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - std::string property = baseT::argAsString(0, context, executionContext); - std::string result; + string_type property = baseT::argAsString(0, context, executionContext); + string_type result; if(property == "xsl:version") return "1.0"; if(property == "xsl:vendor") @@ -198,25 +214,25 @@ protected: }; // SystemPropertyFunction // boolean element-available(string) -class ElementAvailableFunction : public Arabica::XPath::BooleanXPathFunction +class ElementAvailableFunction : public Arabica::XPath::BooleanXPathFunction { - typedef Arabica::XPath::BooleanXPathFunction baseT; + typedef Arabica::XPath::BooleanXPathFunction baseT; public: - ElementAvailableFunction(const std::vector >& names, - const std::map& inscopeNamespaces, - const std::vector >& args) : - Arabica::XPath::BooleanXPathFunction(1, 1, args), + ElementAvailableFunction(const std::vector >& names, + const std::map& inscopeNamespaces, + const ArgList& args) : + Arabica::XPath::BooleanXPathFunction(1, 1, args), namespaces_(inscopeNamespaces), elementNames_(names) { } // ElementAvailableFunction protected: - virtual bool doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual bool doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - const std::string functionName = baseT::argAsString(0, context, executionContext); - const XML::QualifiedName expandedName = XML::QualifiedName::parseQName(functionName, true, namespaces_); + const string_type functionName = baseT::argAsString(0, context, executionContext); + const QualifiedName expandedName = QualifiedName::parseQName(functionName, true, namespaces_); if((expandedName.namespaceUri() != StylesheetConstant::NamespaceURI()) && (!expandedName.namespaceUri().empty())) @@ -230,57 +246,57 @@ protected: for(int i = 0; XSLTNames[i] != 0; ++i) if(expandedName.localName() == XSLTNames[i]) - return true; + return true; return false; } // doEvaluate private: - const std::map namespaces_; - std::vector > elementNames_; + const std::map namespaces_; + std::vector > elementNames_; }; // class ElementAvailableFunction // boolean function-available(string) -class FunctionAvailableFunction : public Arabica::XPath::BooleanXPathFunction +class FunctionAvailableFunction : public Arabica::XPath::BooleanXPathFunction { - typedef Arabica::XPath::BooleanXPathFunction baseT; + typedef Arabica::XPath::BooleanXPathFunction baseT; public: - FunctionAvailableFunction(const std::vector >& names, - const std::map& inscopeNamespaces, - const std::vector >& args) : - Arabica::XPath::BooleanXPathFunction(1, 1, args), + FunctionAvailableFunction(const std::vector >& names, + const std::map& inscopeNamespaces, + const ArgList& args) : + Arabica::XPath::BooleanXPathFunction(1, 1, args), namespaces_(inscopeNamespaces), functionNames_(names) { - Arabica::XPath::StandardXPathFunctionResolver standardResolver; - const std::vector > standardNames = standardResolver.validNames(); + Arabica::XPath::StandardXPathFunctionResolver standardResolver; + const std::vector > standardNames = standardResolver.validNames(); functionNames_.insert(functionNames_.begin(), standardNames.begin(), standardNames.end()); } // FunctionAvailableFunction protected: - virtual bool doEvaluate(const DOM::Node& context, - const Arabica::XPath::ExecutionContext& executionContext) const + virtual bool doEvaluate(const DOMNode& context, + const XPathExecutionContext& executionContext) const { - const std::string functionName = baseT::argAsString(0, context, executionContext); - const XML::QualifiedName expandedName = XML::QualifiedName::parseQName(functionName, true, namespaces_); + const string_type functionName = baseT::argAsString(0, context, executionContext); + const QualifiedName expandedName = QualifiedName::parseQName(functionName, true, namespaces_); - const std::pair name_to_check = std::make_pair(expandedName.namespaceUri(), expandedName.localName()); + const std::pair name_to_check = std::make_pair(expandedName.namespaceUri(), expandedName.localName()); return (std::find(functionNames_.begin(), functionNames_.end(), name_to_check) != functionNames_.end()); } // doEvaluate private: - const std::map namespaces_; - std::vector > functionNames_; + const std::map namespaces_; + std::vector > functionNames_; }; // class FunctionAvailableFunction -class UndefinedFunction : public Arabica::XPath::BooleanXPathFunction +class UndefinedFunction : public Arabica::XPath::BooleanXPathFunction { public: - UndefinedFunction(const std::string namespace_uri, - const std::string name, - const std::vector >& args) : - Arabica::XPath::BooleanXPathFunction(-1, -1, args) + UndefinedFunction(const string_type namespace_uri, + const string_type name, + const ArgList& args) : + Arabica::XPath::BooleanXPathFunction(-1, -1, args) { if(!namespace_uri.empty()) { @@ -293,15 +309,17 @@ public: } // UndefinedFunction protected: - virtual bool doEvaluate(const DOM::Node&, - const Arabica::XPath::ExecutionContext&) const + virtual bool doEvaluate(const DOMNode&, + const XPathExecutionContext&) const { throw Arabica::XPath::UndefinedFunctionException(error_); } // doEvaluate - std::string error_; + string_type error_; }; // class UndefinedFunction +}; // class Functions + } // namespace XSLT } // namespace Arabica