arabica/include/XPath/impl/xpath_node_test.hpp

277 lines
8.9 KiB
C++
Raw Normal View History

2005-08-04 22:42:30 +02:00
#ifndef ARABICA_XPATHIC_XPATH_NODE_TEST_HPP
#define ARABICA_XPATHIC_XPATH_NODE_TEST_HPP
2007-09-05 00:55:47 +02:00
#include <DOM/Node.hpp>
2005-08-04 22:42:30 +02:00
#include "xpath_namespace_node.hpp"
#include <boost/shared_ptr.hpp>
namespace Arabica
{
namespace XPath
{
namespace impl
{
2005-08-04 22:42:30 +02:00
template<class string_type, class string_adaptor>
2005-08-04 22:42:30 +02:00
class NodeTest
{
protected:
NodeTest() { }
public:
virtual ~NodeTest() { }
virtual NodeTest* clone() const = 0;
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const = 0;
2005-08-04 22:42:30 +02:00
private:
NodeTest(NodeTest&);
bool operator==(const NodeTest&);
NodeTest& operator=(const NodeTest&);
}; // class NodeTest
template<class string_type, class string_adaptor>
class AnyNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new AnyNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
return true;
} // matches
}; // class AnyNodeTest
template<class string_type, class string_adaptor>
class NodeNodeTest : public NodeTest<string_type, string_adaptor>
2007-07-19 19:01:31 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new NodeNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2007-07-19 19:01:31 +02:00
{
int type = node.getNodeType();
if((type == DOM::Node_base::DOCUMENT_NODE) ||
(type == DOM::Node_base::DOCUMENT_FRAGMENT_NODE))
return false;
return true;
} // matches
}; // class NodeNodeTest
template<class string_type, class string_adaptor>
class NameNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
2005-08-18 18:17:20 +02:00
NameNodeTest(const string_type& name) : name_(name) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new NameNodeTest(name_); }
2005-08-04 22:42:30 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
int type = node.getNodeType();
return (type == DOM::Node_base::ELEMENT_NODE || type == NAMESPACE_NODE_TYPE) &&
(name_ == node.getNodeName()) &&
(string_adaptor::empty(node.getNamespaceURI()));
2005-08-04 22:42:30 +02:00
} // test
private:
2005-08-18 18:17:20 +02:00
string_type name_;
2005-08-04 22:42:30 +02:00
}; // NameNodeTest
2007-07-19 19:01:31 +02:00
template<class string_type, class string_adaptor>
class AttributeNameNodeTest : public NodeTest<string_type, string_adaptor>
2007-07-19 19:01:31 +02:00
{
public:
AttributeNameNodeTest(const string_type& name) : name_(name) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new AttributeNameNodeTest(name_); }
2007-07-19 19:01:31 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2007-07-19 19:01:31 +02:00
{
return node.getNodeType() == DOM::Node_base::ATTRIBUTE_NODE &&
(name_ == node.getNodeName()) &&
(string_adaptor::empty(node.getNamespaceURI()));
} // operator()
private:
string_type name_;
}; // class AttributeNameNodeTest
template<class string_type, class string_adaptor>
class QNameNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
2007-07-19 19:01:31 +02:00
QNameNodeTest(const string_type& namespace_uri, const string_type& name) :
uri_(namespace_uri), name_(name) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new QNameNodeTest(uri_, name_); }
2005-08-04 22:42:30 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
int type = node.getNodeType();
return (type == DOM::Node_base::ELEMENT_NODE || type == NAMESPACE_NODE_TYPE) &&
(name_ == node.getLocalName()) &&
2005-08-04 22:42:30 +02:00
(uri_ == node.getNamespaceURI());
} // test
private:
2005-08-18 18:17:20 +02:00
string_type uri_;
string_type name_;
2005-08-04 22:42:30 +02:00
}; // QNameNodeTest
template<class string_type, class string_adaptor>
class AttributeQNameNodeTest : public NodeTest<string_type, string_adaptor>
2007-07-19 19:01:31 +02:00
{
public:
AttributeQNameNodeTest(const string_type& namespace_uri, const string_type& name) :
uri_(namespace_uri), name_(name) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new AttributeQNameNodeTest(uri_, name_); }
2007-07-19 19:01:31 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2007-07-19 19:01:31 +02:00
{
return node.getNodeType() == DOM::Node_base::ATTRIBUTE_NODE &&
(name_ == node.getLocalName()) &&
(uri_ == node.getNamespaceURI());
} // test
private:
string_type uri_;
string_type name_;
}; // class AttributeQNameNodeTest
template<class string_type, class string_adaptor>
class StarNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new StarNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
int type = node.getNodeType();
return (type == DOM::Node_base::ELEMENT_NODE ||
2007-07-19 19:01:31 +02:00
type == NAMESPACE_NODE_TYPE);
2005-08-04 22:42:30 +02:00
} // test
}; // class StarNodeTest
template<class string_type, class string_adaptor>
class QStarNodeTest : public StarNodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef StarNodeTest<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
2007-07-19 19:01:31 +02:00
QStarNodeTest(const string_type& namespace_uri) : baseT(), uri_(namespace_uri) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new QStarNodeTest(uri_); }
2005-08-04 22:42:30 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
return (uri_ == node.getNamespaceURI()) &&
baseT::operator()(node);
2005-08-04 22:42:30 +02:00
} // test
private:
2005-08-18 18:17:20 +02:00
string_type uri_;
2005-08-04 22:42:30 +02:00
}; // clase QStarNodeTest
template<class string_type, class string_adaptor>
class TextNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new TextNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
return node.getNodeType() == DOM::Node<string_type, string_adaptor>::TEXT_NODE;
2005-08-04 22:42:30 +02:00
} // test
}; // class TextNodeTest
template<class string_type, class string_adaptor>
class CommentNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new CommentNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
return node.getNodeType() == DOM::Node_base::COMMENT_NODE;
2005-08-04 22:42:30 +02:00
} // operator()
}; // CommentNodeTest
template<class string_type, class string_adaptor>
class AttributeNodeTest : public NodeTest<string_type, string_adaptor>
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new AttributeNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
{
return node.getNodeType() == DOM::Node_base::ATTRIBUTE_NODE;
} // operator()
}; // AttributeNodeTest
template<class string_type, class string_adaptor>
class AttributeQStarNodeTest : public AttributeNodeTest<string_type, string_adaptor>
2007-07-19 19:01:31 +02:00
{
typedef AttributeNodeTest<string_type, string_adaptor> baseT;
2007-07-19 19:01:31 +02:00
public:
AttributeQStarNodeTest(const string_type& namespace_uri) : baseT(), uri_(namespace_uri) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new AttributeQStarNodeTest(uri_); }
2007-07-19 19:01:31 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2007-07-19 19:01:31 +02:00
{
return (uri_ == node.getNamespaceURI()) &&
baseT::operator()(node);
} // test
private:
string_type uri_;
}; // clase AttributeQStarNodeTest
template<class string_type, class string_adaptor>
class NotAttributeNodeTest : public NodeTest<string_type, string_adaptor>
2006-01-05 17:24:11 +01:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new NotAttributeNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2006-01-05 17:24:11 +01:00
{
return node.getNodeType() != DOM::Node_base::ATTRIBUTE_NODE;
2006-01-05 17:24:11 +01:00
} // operator()
}; // NotAttributeNodeTest
template<class string_type, class string_adaptor>
class ProcessingInstructionNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
ProcessingInstructionNodeTest() : target_() { }
2005-08-18 18:17:20 +02:00
ProcessingInstructionNodeTest(const string_type& target) : target_(target) { }
virtual NodeTest<string_type, string_adaptor>* clone() const { return new ProcessingInstructionNodeTest(target_); }
2005-08-04 22:42:30 +02:00
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
if(node.getNodeType() != DOM::Node_base::PROCESSING_INSTRUCTION_NODE)
2005-08-04 22:42:30 +02:00
return false;
if(string_adaptor::empty(target_))
2005-08-04 22:42:30 +02:00
return true;
return node.getNodeName() == target_;
} // test
private:
2005-08-18 18:17:20 +02:00
string_type target_;
2005-08-04 22:42:30 +02:00
}; // ProcessingInstructionNodeTest
template<class string_type, class string_adaptor>
class RootNodeTest : public NodeTest<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
public:
virtual NodeTest<string_type, string_adaptor>* clone() const { return new RootNodeTest(); }
virtual bool operator()(const DOM::Node<string_type, string_adaptor>& node) const
2005-08-04 22:42:30 +02:00
{
int type = node.getNodeType();
return (type == DOM::Node_base::DOCUMENT_NODE) ||
(type == DOM::Node_base::DOCUMENT_FRAGMENT_NODE);
2005-08-04 22:42:30 +02:00
} // operator()
}; // RootNodeTest
} // namespace impl
2005-08-04 22:42:30 +02:00
} // namespace XPath
} // namespace Arabica
#endif