inline elements

This commit is contained in:
Jez Higgins 2012-11-04 17:22:47 +00:00
parent 1dd953346b
commit ab2ae612c9
4 changed files with 47 additions and 41 deletions

View file

@ -10,21 +10,22 @@ namespace Arabica
namespace XSLT
{
class InlineElementHandler : public ItemContainerHandler<InlineElement>
template<class string_type, class string_adaptor>
class InlineElementHandler : public ItemContainerHandler<InlineElement<string_type, string_adaptor> >
{
public:
InlineElementHandler(CompilationContext<std::string>& context) :
ItemContainerHandler<InlineElement>(context)
InlineElementHandler(CompilationContext<string_type, string_adaptor>& context) :
ItemContainerHandler<InlineElement<string_type, string_adaptor> >(context)
{
} // InlineElementHandler
protected:
virtual InlineElement* createContainer(const std::string& namespaceURI,
const std::string& localName,
const std::string& qName,
const SAX::Attributes<std::string>& atts)
virtual InlineElement<string_type, string_adaptor>* createContainer(const string_type& namespaceURI,
const string_type& localName,
const string_type& qName,
const SAX::Attributes<string_type, string_adaptor>& atts)
{
std::vector<InlineAttribute> inlineAtts;
std::vector<InlineAttribute<string_type, string_adaptor> > inlineAtts;
for(int i = 0; i != atts.getLength(); ++i)
{
if(atts.getQName(i).find("xmlns:") == 0)
@ -32,45 +33,48 @@ protected:
if(atts.getURI(i) == StylesheetConstant::NamespaceURI())
continue;
if(!context().isRemapped(atts.getURI(i)))
inlineAtts.push_back(InlineAttribute(atts.getQName(i),
inlineAtts.push_back(InlineAttribute<string_type, string_adaptor>(atts.getQName(i),
atts.getURI(i),
context().xpath_attribute_value_template(atts.getValue(i))));
else
{
std::pair<std::string, std::string> remap = context().remappedNamespace(atts.getURI(i));
std::pair<string_type, string_type> remap = context().remappedNamespace(atts.getURI(i));
if(remap.first.empty() && !remap.second.empty())
remap.first = context().autoNamespacePrefix();
std::string name = remap.first + ":" + atts.getLocalName(i);
inlineAtts.push_back(InlineAttribute(name,
string_type name = remap.first + ":" + atts.getLocalName(i);
inlineAtts.push_back(InlineAttribute<string_type, string_adaptor>(name,
remap.second,
context().xpath_attribute_value_template(atts.getValue(i))));
} // if ...
} // for ...
if(!context().isRemapped(namespaceURI))
return new InlineElement(qName, namespaceURI, inlineAtts);
return new InlineElement<string_type, string_adaptor>(qName, namespaceURI, inlineAtts);
const std::pair<std::string, std::string>& remap = context().remappedNamespace(namespaceURI);
std::string name = remap.first + ":" + localName;
return new InlineElement(name, remap.second, inlineAtts);
const std::pair<string_type, string_type>& remap = context().remappedNamespace(namespaceURI);
string_type name = remap.first + ":" + localName;
return new InlineElement<string_type, string_adaptor>(name, remap.second, inlineAtts);
} // createContainer
}; // class InlineElementHandler
class LREStylesheetHandler : public InlineElementHandler
template<class string_type, class string_adaptor>
class LREStylesheetHandler : public InlineElementHandler<string_type, string_adaptor>
{
typedef InlineElementHandler<string_type, string_adaptor> baseT;
public:
LREStylesheetHandler(CompilationContext<std::string>& context, Template* lreStylesheet) :
InlineElementHandler(context),
LREStylesheetHandler(CompilationContext<string_type, string_adaptor>& context, Template* lreStylesheet) :
baseT(context),
lreStylesheet_(lreStylesheet)
{
} // LREStylesheetHandler
virtual void endElement(const std::string& namespaceURI,
const std::string& localName,
const std::string& qName)
virtual void endElement(const string_type& namespaceURI,
const string_type& localName,
const string_type& qName)
{
context().stylesheet().add_template(lreStylesheet_);
InlineElementHandler::endElement(namespaceURI, localName, qName);
baseT::context().stylesheet().add_template(lreStylesheet_);
baseT::endElement(namespaceURI, localName, qName);
} // endElement
private:

View file

@ -163,7 +163,7 @@ const ChildElement* AllowedChildren()
SAX::DefaultHandler<std::string>* createInlineElementHandler(CompilationContext<std::string>& context)
{
return new InlineElementHandler(context);
return new InlineElementHandler<std::string, Arabica::default_string_adaptor<std::string> >(context);
} // InlineElementHandler

View file

@ -9,12 +9,13 @@ namespace Arabica
namespace XSLT
{
template<class string_type, class string_adaptor>
class InlineAttribute
{
public:
InlineAttribute(const std::string& name,
const std::string& name_space,
Arabica::XPath::XPathExpressionPtr<std::string> value) :
InlineAttribute(const string_type& name,
const string_type& name_space,
Arabica::XPath::XPathExpressionPtr<string_type, string_adaptor> value) :
name_(name),
namespace_(name_space),
value_(value)
@ -36,7 +37,7 @@ public:
return *this;
} // operator==
void execute(const DOM::Node<std::string>& node, ExecutionContext& context) const
void execute(const DOM::Node<string_type, string_adaptor>& node, ExecutionContext& context) const
{
context.sink().start_attribute(name_, namespace_);
context.sink().characters(value_->evaluateAsString(node, context.xpathContext()));
@ -44,17 +45,18 @@ public:
} // execute
private:
std::string name_;
std::string namespace_;
Arabica::XPath::XPathExpressionPtr<std::string> value_;
string_type name_;
string_type namespace_;
Arabica::XPath::XPathExpressionPtr<string_type, string_adaptor> value_;
}; // class InlineAttribute
template<class string_type, class string_adaptor>
class InlineElement : public ItemContainer
{
public:
InlineElement(const std::string& name,
const std::string& name_space,
const std::vector<InlineAttribute>& attrs) :
InlineElement(const string_type& name,
const string_type& name_space,
const std::vector<InlineAttribute<string_type, string_adaptor> >& attrs) :
name_(name),
namespace_(name_space),
attrs_(attrs)
@ -63,11 +65,11 @@ public:
virtual ~InlineElement() { }
virtual void execute(const DOM::Node<std::string>& node, ExecutionContext& context) const
virtual void execute(const DOM::Node<string_type, string_adaptor>& node, ExecutionContext& context) const
{
if(context.sink().start_element(name_, namespace_))
{
for(std::vector<InlineAttribute>::const_iterator a = attrs_.begin(), ae = attrs_.end(); a != ae; ++a)
for(typename std::vector<InlineAttribute<string_type, string_adaptor> >::const_iterator a = attrs_.begin(), ae = attrs_.end(); a != ae; ++a)
a->execute(node, context);
ChainStackFrame frame(context);
@ -77,9 +79,9 @@ public:
} // execute
private:
std::string name_;
std::string namespace_;
std::vector<InlineAttribute> attrs_;
string_type name_;
string_type namespace_;
std::vector<InlineAttribute<string_type, string_adaptor> > attrs_;
}; // class InlineElement
} // namespace XSLT

View file

@ -126,7 +126,7 @@ private:
Template* lreStylesheet = new Template(context_.xpath_match("/"), "", "", "", context_.precedence());
context_.push(lreStylesheet,
new LREStylesheetHandler(context_, lreStylesheet),
new LREStylesheetHandler<string_type, string_adaptor>(context_, lreStylesheet),
namespaceURI,
localName,
qName,