Shuffled around the Stylesheet class. Stylesheet is now the public face, while CompiledStylesheet is what gets chucked around internally.

This commit is contained in:
jez 2008-11-05 01:33:28 +00:00
parent f74170e268
commit b74514bc58
8 changed files with 99 additions and 102 deletions

View file

@ -1,56 +1,56 @@
#ifndef ARABICA_XPATH_COMPILE_CONTEXT_HPP #ifndef ARABICA_XPATH_COMPILE_CONTEXT_HPP
#define ARABICA_XPATH_COMPILE_CONTEXT_HPP #define ARABICA_XPATH_COMPILE_CONTEXT_HPP
namespace Arabica namespace Arabica
{ {
namespace XPath namespace XPath
{ {
template<class string_type, class string_adaptor> class XPath; template<class string_type, class string_adaptor> class XPath;
template<class string_type, class string_adaptor> class NamespaceContext; template<class string_type, class string_adaptor> class NamespaceContext;
template<class string_type, class string_adaptor> class FunctionResolver; template<class string_type, class string_adaptor> class FunctionResolver;
namespace impl namespace impl
{ {
template<class string_type, class string_adaptor> template<class string_type, class string_adaptor>
class CompilationContext : private NamespaceContext<string_type, string_adaptor> class CompilationContext : private NamespaceContext<string_type, string_adaptor>
{ {
public: public:
CompilationContext(const XPath<string_type, string_adaptor>& xpathCompiler, CompilationContext(const XPath<string_type, string_adaptor>& xpathCompiler,
const NamespaceContext<string_type, string_adaptor>& namespaceContext, const NamespaceContext<string_type, string_adaptor>& namespaceContext,
const FunctionResolver<string_type, string_adaptor>& functionResolver) : const FunctionResolver<string_type, string_adaptor>& functionResolver) :
xpath_(xpathCompiler), xpath_(xpathCompiler),
namespaceContext_(namespaceContext), namespaceContext_(namespaceContext),
functionResolver_(functionResolver) functionResolver_(functionResolver)
{ {
} // CompilationContext } // CompilationContext
const XPath<string_type, string_adaptor>& xpath() const { return xpath_; } const XPath<string_type, string_adaptor>& xpath() const { return xpath_; }
const NamespaceContext<string_type, string_adaptor>& namespaceContext() const { return *this; } const NamespaceContext<string_type, string_adaptor>& namespaceContext() const { return *this; }
const FunctionResolver<string_type, string_adaptor>& functionResolver() const { return functionResolver_; } const FunctionResolver<string_type, string_adaptor>& functionResolver() const { return functionResolver_; }
private: private:
virtual string_type namespaceURI(const string_type& prefix) const virtual string_type namespaceURI(const string_type& prefix) const
{ {
string_type uri = namespaceContext_.namespaceURI(prefix); string_type uri = namespaceContext_.namespaceURI(prefix);
if(string_adaptor::empty(uri)) if(string_adaptor::empty(uri))
throw Arabica::XPath::UnboundNamespacePrefixException(string_adaptor::asStdString(prefix)); throw Arabica::XPath::UnboundNamespacePrefixException(string_adaptor::asStdString(prefix));
return uri; return uri;
} // namespaceURI } // namespaceURI
const XPath<string_type, string_adaptor>& xpath_; const XPath<string_type, string_adaptor>& xpath_;
const NamespaceContext<string_type, string_adaptor>& namespaceContext_; const NamespaceContext<string_type, string_adaptor>& namespaceContext_;
const FunctionResolver<string_type, string_adaptor>& functionResolver_; const FunctionResolver<string_type, string_adaptor>& functionResolver_;
CompilationContext(const CompilationContext&); CompilationContext(const CompilationContext&);
CompilationContext& operator=(const CompilationContext&); CompilationContext& operator=(const CompilationContext&);
bool operator==(const CompilationContext&) const; bool operator==(const CompilationContext&) const;
}; // class CompilationContext }; // class CompilationContext
} // namespace impl } // namespace impl
} // namespace XPath } // namespace XPath
} // namespace Arabica } // namespace Arabica
#endif #endif

View file

@ -6,7 +6,7 @@
#include <XPath/XPath.hpp> #include <XPath/XPath.hpp>
#include "../xslt_compilation_context.hpp" #include "../xslt_compilation_context.hpp"
#include "../xslt_stylesheet.hpp" #include "../xslt_compiled_stylesheet.hpp"
#include "xslt_constants.hpp" #include "xslt_constants.hpp"
namespace Arabica namespace Arabica

View file

@ -1,7 +1,7 @@
#ifndef ARABICA_XSLT_APPLY_TEMPLATES_HPP #ifndef ARABICA_XSLT_APPLY_TEMPLATES_HPP
#define ARABICA_XSLT_APPLY_TEMPLATES_HPP #define ARABICA_XSLT_APPLY_TEMPLATES_HPP
#include "xslt_stylesheet.hpp" #include "xslt_compiled_stylesheet.hpp"
#include "xslt_sort.hpp" #include "xslt_sort.hpp"
#include "xslt_with_param.hpp" #include "xslt_with_param.hpp"

View file

@ -1,7 +1,7 @@
#ifndef ARABICA_XSLT_CALL_TEMPLATE_HPP #ifndef ARABICA_XSLT_CALL_TEMPLATE_HPP
#define ARABICA_XSLT_CALL_TEMPLATE_HPP #define ARABICA_XSLT_CALL_TEMPLATE_HPP
#include "xslt_stylesheet.hpp" #include "xslt_compiled_stylesheet.hpp"
#include "xslt_with_param.hpp" #include "xslt_with_param.hpp"
namespace Arabica namespace Arabica

View file

@ -14,7 +14,7 @@ namespace Arabica
{ {
namespace XSLT namespace XSLT
{ {
class Stylesheet; class CompiledStylesheet;
class ItemContainer; class ItemContainer;
class CompilationContext : class CompilationContext :
@ -23,7 +23,7 @@ class CompilationContext :
{ {
public: public:
CompilationContext(StylesheetParser& parser, CompilationContext(StylesheetParser& parser,
Stylesheet& stylesheet) : CompiledStylesheet& stylesheet) :
parser_(parser), parser_(parser),
stylesheet_(stylesheet), stylesheet_(stylesheet),
autoNs_(1), autoNs_(1),
@ -56,7 +56,7 @@ public:
return xpath_.compile_match(match); return xpath_.compile_match(match);
} // xpath_match } // xpath_match
Arabica::XPath::XPathExpressionPtr<std::string> xpath_attribute_value_template(const std::string& expr) const { return xpath_.compile_attribute_value_template(expr); } Arabica::XPath::XPathExpressionPtr<std::string> xpath_attribute_value_template(const std::string& expr) const { return xpath_.compile_attribute_value_template(expr); }
Stylesheet& stylesheet() const { return stylesheet_; } CompiledStylesheet& stylesheet() const { return stylesheet_; }
std::pair<std::string, std::string> processInternalQName(const std::string& qName) const std::pair<std::string, std::string> processInternalQName(const std::string& qName) const
{ {
@ -181,7 +181,7 @@ private:
StylesheetParser& parser_; StylesheetParser& parser_;
Arabica::XPath::XPath<std::string> xpath_; Arabica::XPath::XPath<std::string> xpath_;
Stylesheet& stylesheet_; CompiledStylesheet& stylesheet_;
std::stack<SAX::DefaultHandler<std::string>*> handlerStack_; std::stack<SAX::DefaultHandler<std::string>*> handlerStack_;
std::stack<ItemContainer*> parentStack_; std::stack<ItemContainer*> parentStack_;
std::map<std::string, Namespace> namespaceRemap_; std::map<std::string, Namespace> namespaceRemap_;

View file

@ -1,5 +1,5 @@
#ifndef ARABICA_XSLT_STYLESHEET_HPP #ifndef ARABICA_XSLT_COMPILED_STYLESHEET_HPP
#define ARABICA_XSLT_STYLESHEET_HPP #define ARABICA_XSLT_COMPILED_STYLESHEET_HPP
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@ -8,13 +8,14 @@
#include "xslt_execution_context.hpp" #include "xslt_execution_context.hpp"
#include "xslt_template.hpp" #include "xslt_template.hpp"
#include "xslt_top_level_param.hpp" #include "xslt_top_level_param.hpp"
#include "xslt_stylesheet.hpp"
namespace Arabica namespace Arabica
{ {
namespace XSLT namespace XSLT
{ {
class Stylesheet class CompiledStylesheet : public Stylesheet
{ {
typedef Arabica::XPath::BoolValue<std::string, Arabica::default_string_adaptor<std::string> > BoolValue; 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::NumericValue<std::string, Arabica::default_string_adaptor<std::string> > NumericValue;
@ -22,14 +23,16 @@ class Stylesheet
typedef Arabica::XPath::XPathValuePtr<std::string> ValuePtr; typedef Arabica::XPath::XPathValuePtr<std::string> ValuePtr;
public: public:
Stylesheet() : CompiledStylesheet() :
output_(new StreamSink(std::cout)), output_(new StreamSink(std::cout)),
error_output_(&std::cerr) error_output_(&std::cerr),
current_import_precedence_(0),
total_imports_(0)
{ {
push_import_precedence(); push_import_precedence();
} // Stylesheet } // CompiledStylesheet
~Stylesheet() virtual ~CompiledStylesheet()
{ {
// let's clean up! // let's clean up!
for(ItemStack::const_iterator isi = items_.begin(), ise = items_.end(); isi != ise; ++isi) for(ItemStack::const_iterator isi = items_.begin(), ise = items_.end(); isi != ise; ++isi)
@ -39,36 +42,36 @@ public:
delete *pi; delete *pi;
for(TemplateList::const_iterator ti = all_templates_.begin(), te = all_templates_.end(); ti != te; ++ti) for(TemplateList::const_iterator ti = all_templates_.begin(), te = all_templates_.end(); ti != te; ++ti)
delete *ti; delete *ti;
} // ~Stylesheet } // ~CompiledStylesheet
void set_parameter(const std::string& name, bool value) virtual void set_parameter(const std::string& name, bool value)
{ {
set_parameter(name, ValuePtr(new BoolValue(value))); set_parameter(name, ValuePtr(new BoolValue(value)));
} // set_parameter } // set_parameter
void set_parameter(const std::string& name, double value) virtual void set_parameter(const std::string& name, double value)
{ {
set_parameter(name, ValuePtr(new NumericValue(value))); set_parameter(name, ValuePtr(new NumericValue(value)));
} // set_parameter } // set_parameter
void set_parameter(const std::string& name, const char* value) virtual void set_parameter(const std::string& name, const char* value)
{ {
set_parameter(name, ValuePtr(new StringValue(value))); set_parameter(name, ValuePtr(new StringValue(value)));
} // set_parameter } // set_parameter
void set_parameter(const std::string& name, const std::string& value) virtual void set_parameter(const std::string& name, const std::string& value)
{ {
set_parameter(name, ValuePtr(new StringValue(value))); set_parameter(name, ValuePtr(new StringValue(value)));
} // set_parameter } // set_parameter
void set_output(Sink& sink) virtual void set_output(Sink& sink)
{ {
output_.reset(sink); output_.reset(sink);
} // set_output } // set_output
void set_error_output(std::ostream& os) virtual void set_error_output(std::ostream& os)
{ {
error_output_ = &os; error_output_ = &os;
} // set_error_output } // set_error_output
void execute(DOM::Node<std::string>& initialNode) const virtual void execute(const DOM::Node<std::string>& initialNode) const
{ {
if(initialNode == 0) if(initialNode == 0)
throw std::runtime_error("Input document is empty"); throw std::runtime_error("Input document is empty");
@ -318,16 +321,19 @@ private:
ItemStack items_; ItemStack items_;
ParamList params_; ParamList params_;
std::vector<int> current_import_precedence_;
int total_imports_;
mutable std::pair<std::string, std::string> current_mode_; mutable std::pair<std::string, std::string> current_mode_;
mutable int current_generation_; mutable int current_generation_;
Output::Settings output_settings_; Output::Settings output_settings_;
SinkHolder output_; SinkHolder output_;
mutable std::ostream* error_output_; mutable std::ostream* error_output_;
}; // class Stylesheet }; // class CompiledStylesheet
} // namespace XSLT } // namespace XSLT
} // namespace Arabica } // namespace Arabica
#endif // ARABICA_XSLT_STYLESHEET_HPP #endif // ARABICA_XSLT_COMPILED_STYLESHEET_HPP

View file

@ -10,7 +10,7 @@ namespace Arabica
namespace XSLT namespace XSLT
{ {
class Stylesheet; class CompiledStylesheet;
class ExecutionContext; class ExecutionContext;
class Variable_declaration class Variable_declaration
@ -34,7 +34,7 @@ private:
class ExecutionContext class ExecutionContext
{ {
public: public:
ExecutionContext(const Stylesheet& stylesheet, ExecutionContext(const CompiledStylesheet& stylesheet,
Sink& output, Sink& output,
std::ostream& error_output) : std::ostream& error_output) :
stylesheet_(stylesheet), stylesheet_(stylesheet),
@ -63,7 +63,7 @@ public:
xpathContext_.setLast(rhs.xpathContext().last()); xpathContext_.setLast(rhs.xpathContext().last());
} // ExecutionContext } // ExecutionContext
const Stylesheet& stylesheet() const { return stylesheet_; } const CompiledStylesheet& stylesheet() const { return stylesheet_; }
Output& sink() Output& sink()
{ {
@ -104,7 +104,7 @@ private:
void popStackFrame() { stack_.popScope(); } void popStackFrame() { stack_.popScope(); }
private: private:
const Stylesheet& stylesheet_; const CompiledStylesheet& stylesheet_;
VariableStack stack_; VariableStack stack_;
int variable_precedence_; int variable_precedence_;
Arabica::XPath::ExecutionContext<std::string> xpathContext_; Arabica::XPath::ExecutionContext<std::string> xpathContext_;

View file

@ -6,7 +6,7 @@
#include <memory> #include <memory>
#include "xslt_stylesheet_parser.hpp" #include "xslt_stylesheet_parser.hpp"
#include "xslt_stylesheet.hpp" #include "xslt_compiled_stylesheet.hpp"
#include "xslt_compilation_context.hpp" #include "xslt_compilation_context.hpp"
#include "handler/xslt_template_handler.hpp" #include "handler/xslt_template_handler.hpp"
#include "handler/xslt_include_handler.hpp" #include "handler/xslt_include_handler.hpp"
@ -169,20 +169,19 @@ public:
{ {
error_ = ""; error_ = "";
std::auto_ptr<Stylesheet> stylesheet(new Stylesheet()); std::auto_ptr<CompiledStylesheet> stylesheet(new CompiledStylesheet());
CompilationContext context(parser_, StylesheetParser parser;
*stylesheet.get()); CompilationContext context(parser, *stylesheet.get());
StylesheetHandler stylesheetHandler(context); StylesheetHandler stylesheetHandler(context);
parser.setContentHandler(stylesheetHandler);
parser_.setContentHandler(stylesheetHandler); //parser.setErrorHandler(*this);
//parser_.setErrorHandler(*this);
//if(entityResolver_) //if(entityResolver_)
// parser_.setEntityResolver(*entityResolver_); // parser.setEntityResolver(*entityResolver_);
try { try {
parser_.parse(source); parser.parse(source);
} // try } // try
catch(std::exception& ex) catch(std::exception& ex)
{ {
@ -191,7 +190,7 @@ public:
stylesheet.reset(); stylesheet.reset();
} // catch } // catch
return stylesheet; return std::auto_ptr<Stylesheet>(stylesheet.release());
} // compile } // compile
const std::string& error() const const std::string& error() const
@ -200,14 +199,6 @@ public:
} // error } // error
private: private:
virtual void fatalError(const SAX::SAXException& exception)
{
std::cerr << "Error: " << exception.what() << std::endl;
stylesheet_.reset();
} // fatalError
StylesheetParser parser_;
std::auto_ptr<Stylesheet> stylesheet_;
std::string error_; std::string error_;
}; // class StylesheetCompiler }; // class StylesheetCompiler