#ifndef ARABICA_XPATHIC_XPATH_NODE_TEST_HPP #define ARABICA_XPATHIC_XPATH_NODE_TEST_HPP #include #include "xpath_namespace_node.hpp" #include namespace Arabica { namespace XPath { namespace impl { template class NodeTest { protected: NodeTest() { } public: virtual ~NodeTest() { } virtual NodeTest* clone() const = 0; virtual bool operator()(const DOM::Node& node) const = 0; private: NodeTest(NodeTest&); bool operator==(const NodeTest&); NodeTest& operator=(const NodeTest&); }; // class NodeTest template class AnyNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new AnyNodeTest(); } virtual bool operator()(const DOM::Node& node) const { return true; } // matches }; // class AnyNodeTest template class NodeNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new NodeNodeTest(); } virtual bool operator()(const DOM::Node& node) const { 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 NameNodeTest : public NodeTest { public: NameNodeTest(const string_type& name) : name_(name) { } virtual NodeTest* clone() const { return new NameNodeTest(name_); } virtual bool operator()(const DOM::Node& node) const { int type = node.getNodeType(); return (type == DOM::Node_base::ELEMENT_NODE || type == NAMESPACE_NODE_TYPE) && (name_ == node.getNodeName()) && (string_adaptor::empty(node.getNamespaceURI())); } // test private: string_type name_; }; // NameNodeTest template class AttributeNameNodeTest : public NodeTest { public: AttributeNameNodeTest(const string_type& name) : name_(name) { } virtual NodeTest* clone() const { return new AttributeNameNodeTest(name_); } virtual bool operator()(const DOM::Node& node) const { 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 QNameNodeTest : public NodeTest { public: QNameNodeTest(const string_type& namespace_uri, const string_type& name) : uri_(namespace_uri), name_(name) { } virtual NodeTest* clone() const { return new QNameNodeTest(uri_, name_); } virtual bool operator()(const DOM::Node& node) const { int type = node.getNodeType(); return (type == DOM::Node_base::ELEMENT_NODE || type == NAMESPACE_NODE_TYPE) && (name_ == node.getLocalName()) && (uri_ == node.getNamespaceURI()); } // test private: string_type uri_; string_type name_; }; // QNameNodeTest template class AttributeQNameNodeTest : public NodeTest { public: AttributeQNameNodeTest(const string_type& namespace_uri, const string_type& name) : uri_(namespace_uri), name_(name) { } virtual NodeTest* clone() const { return new AttributeQNameNodeTest(uri_, name_); } virtual bool operator()(const DOM::Node& node) const { 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 StarNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new StarNodeTest(); } virtual bool operator()(const DOM::Node& node) const { int type = node.getNodeType(); return (type == DOM::Node_base::ELEMENT_NODE || type == NAMESPACE_NODE_TYPE); } // test }; // class StarNodeTest template class QStarNodeTest : public StarNodeTest { typedef StarNodeTest baseT; public: QStarNodeTest(const string_type& namespace_uri) : baseT(), uri_(namespace_uri) { } virtual NodeTest* clone() const { return new QStarNodeTest(uri_); } virtual bool operator()(const DOM::Node& node) const { return (uri_ == node.getNamespaceURI()) && baseT::operator()(node); } // test private: string_type uri_; }; // clase QStarNodeTest template class TextNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new TextNodeTest(); } virtual bool operator()(const DOM::Node& node) const { return nodeIsText(node); } // test }; // class TextNodeTest template class CommentNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new CommentNodeTest(); } virtual bool operator()(const DOM::Node& node) const { return node.getNodeType() == DOM::Node_base::COMMENT_NODE; } // operator() }; // CommentNodeTest template class AttributeNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new AttributeNodeTest(); } virtual bool operator()(const DOM::Node& node) const { return node.getNodeType() == DOM::Node_base::ATTRIBUTE_NODE; } // operator() }; // AttributeNodeTest template class AttributeQStarNodeTest : public AttributeNodeTest { typedef AttributeNodeTest baseT; public: AttributeQStarNodeTest(const string_type& namespace_uri) : baseT(), uri_(namespace_uri) { } virtual NodeTest* clone() const { return new AttributeQStarNodeTest(uri_); } virtual bool operator()(const DOM::Node& node) const { return (uri_ == node.getNamespaceURI()) && baseT::operator()(node); } // test private: string_type uri_; }; // clase AttributeQStarNodeTest template class NotAttributeNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new NotAttributeNodeTest(); } virtual bool operator()(const DOM::Node& node) const { return node.getNodeType() != DOM::Node_base::ATTRIBUTE_NODE; } // operator() }; // NotAttributeNodeTest template class ProcessingInstructionNodeTest : public NodeTest { public: ProcessingInstructionNodeTest() : target_() { } ProcessingInstructionNodeTest(const string_type& target) : target_(target) { } virtual NodeTest* clone() const { return new ProcessingInstructionNodeTest(target_); } virtual bool operator()(const DOM::Node& node) const { if(node.getNodeType() != DOM::Node_base::PROCESSING_INSTRUCTION_NODE) return false; if(string_adaptor::empty(target_)) return true; return node.getNodeName() == target_; } // test private: string_type target_; }; // ProcessingInstructionNodeTest template class RootNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new RootNodeTest(); } virtual bool operator()(const DOM::Node& node) const { int type = node.getNodeType(); return (type == DOM::Node_base::DOCUMENT_NODE) || (type == DOM::Node_base::DOCUMENT_FRAGMENT_NODE); } // operator() }; // RootNodeTest template class FailNodeTest : public NodeTest { public: virtual NodeTest* clone() const { return new FailNodeTest(); } virtual bool operator()(const DOM::Node&) const { return false; } // operator() }; // class FailNodeTest } // namespace impl } // namespace XPath } // namespace Arabica #endif