This commit is contained in:
Jez Higgins 2012-11-06 09:17:16 +00:00
parent c970605a0f
commit db1f9bac46
4 changed files with 143 additions and 130 deletions

View file

@ -35,10 +35,10 @@ public:
name_ = context_.processInternalQName(attrs["name"]).clarkName();
try
{
Key::MatchExprList matches = context_.xpath_match_no_variables(attrs["match"]);
Key<std::string, Arabica::default_string_adaptor<std::string> >::MatchExprList matches = context_.xpath_match_no_variables(attrs["match"]);
Arabica::XPath::XPathExpression<std::string> use = context_.xpath_expression_no_variables(attrs["use"]);
key_ = new Key(matches, use);
key_ = new Key<std::string, Arabica::default_string_adaptor<std::string> >(matches, use);
} // try
catch(const Arabica::XPath::UnboundVariableException&)
{
@ -62,7 +62,7 @@ public:
private:
CompilationContext<std::string>& context_;
std::string name_;
Key* key_;
Key<std::string, Arabica::default_string_adaptor<std::string> >* key_;
}; // class KeyHandler
} // namespace XSLT

View file

@ -92,7 +92,7 @@ public:
} // execute
////////////////////////////////////////
const DeclaredKeys& keys() const { return keys_; }
const DeclaredKeys<std::string, Arabica::default_string_adaptor<std::string> >& keys() const { return keys_; }
void add_template(Template<std::string, Arabica::default_string_adaptor<std::string> >* templat)
{
@ -130,7 +130,7 @@ public:
} // add_item
void add_key(const std::string& name,
Key* key)
Key<std::string, Arabica::default_string_adaptor<std::string> >* key)
{
keys_.add(name, key);
} // add_key
@ -322,7 +322,7 @@ private:
NamedTemplates named_templates_;
TemplateStack templates_;
VariableDeclList topLevelVars_;
DeclaredKeys keys_;
DeclaredKeys<std::string, Arabica::default_string_adaptor<std::string> > keys_;
ParamList params_;
mutable std::string current_mode_;

View file

@ -65,7 +65,7 @@ class KeyFunction : public Arabica::XPath::NodeSetXPathFunction<std::string>
{
typedef Arabica::XPath::NodeSetXPathFunction<std::string> baseT;
public:
KeyFunction(const DeclaredKeys& keys,
KeyFunction(const DeclaredKeys<std::string, Arabica::default_string_adaptor<std::string> >& keys,
const std::map<std::string, std::string>& inscopeNamespaces,
const std::vector<Arabica::XPath::XPathExpression<std::string> >& args) :
Arabica::XPath::NodeSetXPathFunction<std::string>(2, 2, args),
@ -103,7 +103,7 @@ protected:
} // nodeSetUnion
private:
const DeclaredKeys& keys_;
const DeclaredKeys<std::string, Arabica::default_string_adaptor<std::string> >& keys_;
std::map<std::string, std::string> namespaces_;
}; // class KeyFunction

View file

@ -1,122 +1,135 @@
#ifndef ARABICA_XSLT_KEY_HPP
#define ARABICA_XSLT_KEY_HPP
#include "xslt_execution_context.hpp"
namespace Arabica
{
namespace XSLT
{
class Key
{
public:
typedef std::vector<Arabica::XPath::MatchExpr<std::string> > MatchExprList;
Key(MatchExprList& matches,
Arabica::XPath::XPathExpression<std::string>& use) :
matches_(matches),
use_(use)
{
} // Key
Arabica::XPath::NodeSet<std::string> lookup(const std::string& value,
const Arabica::XPath::ExecutionContext<std::string>& context) const
{
DOM::Node<std::string> doc = XPath::impl::get_owner_document(context.currentNode());
DocumentNodeMap::const_iterator nm = nodesPerDocument_.find(doc.underlying_impl());
if(nm == nodesPerDocument_.end())
populate(nodesPerDocument_[doc.underlying_impl()], context);
const NodeMap& nodes = nodesPerDocument_[doc.underlying_impl()];
NodeMap::const_iterator f = nodes.find(value);
if(f == nodes.end())
return Arabica::XPath::NodeSet<std::string>(0);
return f->second;
} // lookup
private:
typedef std::map<std::string, Arabica::XPath::NodeSet<std::string> > NodeMap;
typedef std::map<DOM::Node_impl<std::string, Arabica::default_string_adaptor<std::string> >*, NodeMap> DocumentNodeMap;
void populate(NodeMap& nodes,
const Arabica::XPath::ExecutionContext<std::string>& context) const
{
typedef XPath::AxisEnumerator<std::string> AxisEnum;
DOM::Node<std::string> current = XPath::impl::get_owner_document(context.currentNode());
for(AxisEnum ae(current, XPath::DESCENDANT_OR_SELF); *ae != 0; ++ae)
{
DOM::Node<std::string> node = *ae;
for(MatchExprList::const_iterator me = matches_.begin(), mee = matches_.end(); me != mee; ++me)
if(me->evaluate(node, context))
{
Arabica::XPath::NodeSet<std::string> ids = use_.evaluateAsNodeSet(node, context);
for(Arabica::XPath::NodeSet<std::string>::const_iterator i = ids.begin(), ie = ids.end(); i != ie; ++i)
{
std::string id = Arabica::XPath::impl::nodeStringValue<std::string, Arabica::default_string_adaptor<std::string> >(*i);
nodes[id].push_back(node);
} // for ...
break;
} // if ...
} // for
} // populate
MatchExprList matches_;
Arabica::XPath::XPathExpression<std::string> use_;
mutable DocumentNodeMap nodesPerDocument_;
}; // class Key
class DeclaredKeys
{
public:
DeclaredKeys() { }
~DeclaredKeys()
{
for(Keys::const_iterator i = keys_.begin(), ie = keys_.end(); i != ie; ++i)
for(KeyList::const_iterator k = i->second.begin(), ke = i->second.end(); k != ke; ++k)
delete (*k);
} // ~DeclaredKeys
void add(const std::string& name, Key* key)
{
keys_[name].push_back(key);
} // add_key
Arabica::XPath::NodeSet<std::string> lookup(const std::string& name,
const std::string& id,
const Arabica::XPath::ExecutionContext<std::string>& context) const
{
const Keys::const_iterator k = keys_.find(name);
if(k == keys_.end())
throw SAX::SAXException("No key named '" + name + "' has been defined.");
if(k->second.size() == 1)
return k->second[0]->lookup(id, context);
Arabica::XPath::NodeSet<std::string> nodes;
for(KeyList::const_iterator key = k->second.begin(), keye = k->second.end(); key != keye; ++key)
nodes.push_back((*key)->lookup(id, context));
nodes.to_document_order();
return nodes;
} // lookup
private:
typedef std::vector<Key*> KeyList;
typedef std::map<std::string, KeyList> Keys;
Keys keys_;
DeclaredKeys(const DeclaredKeys&);
DeclaredKeys& operator=(const DeclaredKeys&);
bool operator==(const DeclaredKeys&) const;
}; // class DeclaredKeys
} // namespace XSLT
} // namespace Arabica
#endif
#ifndef ARABICA_XSLT_KEY_HPP
#define ARABICA_XSLT_KEY_HPP
#include "xslt_execution_context.hpp"
namespace Arabica
{
namespace XSLT
{
template<class string_type, class string_adaptor>
class Key
{
public:
typedef std::vector<Arabica::XPath::MatchExpr<string_type, string_adaptor> > MatchExprList;
typedef Arabica::XPath::NodeSet<string_type, string_adaptor> NodeSet;
typedef Arabica::XPath::XPathExpression<string_type, string_adaptor> XPathExpression;
typedef DOM::Node<string_type, string_adaptor> DOMNode;
typedef Arabica::XPath::ExecutionContext<string_type, string_adaptor> XPathContext;
Key(MatchExprList& matches,
XPathExpression& use) :
matches_(matches),
use_(use)
{
} // Key
NodeSet lookup(const string_type& value, const XPathContext& context) const
{
DOMNode doc = XPath::impl::get_owner_document(context.currentNode());
DocumentNodeMapIterator nm = nodesPerDocument_.find(doc.underlying_impl());
if(nm == nodesPerDocument_.end())
populate(nodesPerDocument_[doc.underlying_impl()], context);
const NodeMap& nodes = nodesPerDocument_[doc.underlying_impl()];
NodeMapIterator f = nodes.find(value);
if(f == nodes.end())
return NodeSet(0);
return f->second;
} // lookup
private:
typedef std::map<string_type, NodeSet> NodeMap;
typedef typename NodeMap::const_iterator NodeMapIterator;
typedef std::map<DOM::Node_impl<string_type, string_adaptor>*, NodeMap> DocumentNodeMap;
typedef typename DocumentNodeMap::const_iterator DocumentNodeMapIterator;
typedef typename NodeSet::iterator NodeSetIterator;
typedef typename MatchExprList::const_iterator MatchExprListIterator;
void populate(NodeMap& nodes, const XPathContext& context) const
{
typedef XPath::AxisEnumerator<string_type, string_adaptor> AxisEnum;
DOMNode current = XPath::impl::get_owner_document(context.currentNode());
for(AxisEnum ae(current, XPath::DESCENDANT_OR_SELF); *ae != 0; ++ae)
{
DOMNode node = *ae;
for(MatchExprListIterator me = matches_.begin(), mee = matches_.end(); me != mee; ++me)
if(me->evaluate(node, context))
{
NodeSet ids = use_.evaluateAsNodeSet(node, context);
for(NodeSetIterator i = ids.begin(), ie = ids.end(); i != ie; ++i)
{
string_type id = Arabica::XPath::impl::nodeStringValue<string_type, string_adaptor>(*i);
nodes[id].push_back(node);
} // for ...
break;
} // if ...
} // for
} // populate
MatchExprList matches_;
XPathExpression use_;
mutable DocumentNodeMap nodesPerDocument_;
}; // class Key
template<class string_type, class string_adaptor>
class DeclaredKeys
{
public:
typedef Arabica::XPath::NodeSet<string_type, string_adaptor> NodeSet;
typedef Arabica::XPath::ExecutionContext<string_type, string_adaptor> XPathContext;
DeclaredKeys() { }
~DeclaredKeys()
{
for(KeysIterator i = keys_.begin(), ie = keys_.end(); i != ie; ++i)
for(KeyListIterator k = i->second.begin(), ke = i->second.end(); k != ke; ++k)
delete (*k);
} // ~DeclaredKeys
void add(const string_type& name, Key<string_type, string_adaptor>* key)
{
keys_[name].push_back(key);
} // add_key
NodeSet lookup(const string_type& name,
const string_type& id,
const XPathContext& context) const
{
const KeysIterator k = keys_.find(name);
if(k == keys_.end())
throw SAX::SAXException("No key named '" + name + "' has been defined.");
if(k->second.size() == 1)
return k->second[0]->lookup(id, context);
NodeSet nodes;
for(KeyListIterator key = k->second.begin(), keye = k->second.end(); key != keye; ++key)
nodes.push_back((*key)->lookup(id, context));
nodes.to_document_order();
return nodes;
} // lookup
private:
typedef std::vector<Key<string_type, string_adaptor>*> KeyList;
typedef typename std::vector<Key<string_type, string_adaptor>*>::const_iterator KeyListIterator;
typedef std::map<string_type, KeyList> Keys;
typedef typename Keys::const_iterator KeysIterator;
Keys keys_;
DeclaredKeys(const DeclaredKeys&);
DeclaredKeys& operator=(const DeclaredKeys&);
bool operator==(const DeclaredKeys&) const;
}; // class DeclaredKeys
} // namespace XSLT
} // namespace Arabica
#endif