2005-08-04 22:42:30 +02:00
|
|
|
#ifndef ARABICA_XPATHIC_XPATH_VALUE_H
|
|
|
|
#define ARABICA_XPATHIC_XPATH_VALUE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <DOM/Node.h>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <vector>
|
2005-08-17 00:11:27 +02:00
|
|
|
#include <Utils/StringAdaptor.h>
|
2005-08-04 22:42:30 +02:00
|
|
|
#include "xpath_object.hpp"
|
2005-08-16 13:40:56 +02:00
|
|
|
#include "xpath_expression.hpp"
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
namespace Arabica
|
|
|
|
{
|
|
|
|
namespace XPath
|
|
|
|
{
|
|
|
|
|
2005-08-17 00:11:27 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2005-08-17 12:13:05 +02:00
|
|
|
class BoolValue : public XPathValue<string_type>, public XPathExpression<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BoolValue(bool value) :
|
|
|
|
value_(value) { }
|
|
|
|
|
2005-08-17 00:11:27 +02:00
|
|
|
static XPathValuePtr<string_type> createValue(bool value) { return XPathValuePtr<string_type>(new BoolValue(value)); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-08-17 12:13:05 +02:00
|
|
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
|
|
|
const ExecutionContext<string_type, string_adaptor>& executionContext) const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
2005-08-17 00:11:27 +02:00
|
|
|
return XPathValuePtr<string_type>(new BoolValue(value_));
|
2005-08-04 22:42:30 +02:00
|
|
|
} // evaluate
|
2005-08-17 00:11:27 +02:00
|
|
|
virtual bool evaluateAsBool(const DOM::Node<string_type>& context) { return asBool(); }
|
|
|
|
virtual double evaluateAsNumber(const DOM::Node<string_type>& context) { return asNumber(); }
|
2005-09-08 23:43:21 +02:00
|
|
|
virtual string_type evaluateAsString(const DOM::Node<string_type>& context) { return asString(); }
|
|
|
|
virtual NodeSet<string_type> evaluateAsNodeSet(const DOM::Node<string_type>& context) { return asNodeSet(); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual bool asBool() const { return value_; }
|
|
|
|
virtual double asNumber() const { return value_ ? 1 : 0; }
|
2005-09-30 23:36:11 +02:00
|
|
|
virtual string_type asString() const { return string_adaptor::construct_from_utf8(value_ ? "true" : "false"); }
|
2005-08-17 00:11:27 +02:00
|
|
|
virtual const NodeSet<string_type>& asNodeSet() const { static NodeSet<string_type> empty; return empty; }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual ValueType type() const { return BOOL; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool value_;
|
|
|
|
}; // class BoolValue
|
|
|
|
|
2005-08-17 09:01:27 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2005-08-17 12:13:05 +02:00
|
|
|
class NumericValue : public XPathValue<string_type>, public XPathExpression<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NumericValue(double value) :
|
|
|
|
value_(value) { }
|
|
|
|
|
2005-08-17 09:01:27 +02:00
|
|
|
static XPathValuePtr<string_type> createValue(double value) { return XPathValuePtr<string_type>(new NumericValue(value)); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-08-17 12:13:05 +02:00
|
|
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
|
|
|
const ExecutionContext<string_type, string_adaptor>& executionContext) const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
return createValue(value_);
|
|
|
|
} // evaluate
|
2005-08-17 09:01:27 +02:00
|
|
|
virtual bool evaluateAsBool(const DOM::Node<string_type>& context) { return asBool(); }
|
|
|
|
virtual double evaluateAsNumber(const DOM::Node<string_type>& context) { return asNumber(); }
|
|
|
|
virtual string_type evaluateAsString(const DOM::Node<string_type>& context) { return asString(); }
|
|
|
|
virtual NodeSet<string_type> evaluateAsNodeSet(const DOM::Node<string_type>& context) { return asNodeSet(); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual bool asBool() const { return (value_ != 0.0); }
|
|
|
|
virtual double asNumber() const { return value_; }
|
2005-08-17 09:01:27 +02:00
|
|
|
virtual string_type asString() const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
if(isNaN(value_))
|
2005-09-30 23:36:11 +02:00
|
|
|
return string_adaptor::construct_from_utf8("NaN");
|
2005-08-04 22:42:30 +02:00
|
|
|
if(isInfinity(value_))
|
2005-09-30 23:36:11 +02:00
|
|
|
return string_adaptor::construct_from_utf8("Infinity");
|
2005-08-04 22:42:30 +02:00
|
|
|
if(isNegativeInfinity(value_))
|
2005-09-30 23:36:11 +02:00
|
|
|
return string_adaptor::construct_from_utf8("-Infinity");
|
|
|
|
return string_adaptor::construct_from_utf8(boost::lexical_cast<std::string>(value_).c_str());
|
2005-08-04 22:42:30 +02:00
|
|
|
} // asString
|
2005-08-17 09:01:27 +02:00
|
|
|
virtual const NodeSet<string_type>& asNodeSet() const { static NodeSet<string_type> empty; return empty; }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual ValueType type() const { return NUMBER; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
double value_;
|
|
|
|
}; // class NumberValue
|
|
|
|
|
2005-08-17 10:25:24 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2005-08-17 12:13:05 +02:00
|
|
|
class StringValue : public XPathValue<string_type>, public XPathExpression<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StringValue(const char* value) :
|
2005-09-30 23:36:11 +02:00
|
|
|
value_(string_adaptor::construct_from_utf8(value)) { }
|
2005-08-17 10:25:24 +02:00
|
|
|
StringValue(const string_type& value) :
|
2005-08-04 22:42:30 +02:00
|
|
|
value_(value) { }
|
|
|
|
|
2005-08-17 12:13:05 +02:00
|
|
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
|
|
|
const ExecutionContext<string_type, string_adaptor>& executionContext) const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
2005-08-17 10:25:24 +02:00
|
|
|
return XPathValuePtr<string_type>(new StringValue(value_));
|
2005-08-04 22:42:30 +02:00
|
|
|
} // evaluate
|
2005-08-17 10:25:24 +02:00
|
|
|
virtual bool evaluateAsBool(const DOM::Node<string_type>& context) { return asBool(); }
|
|
|
|
virtual double evaluateAsNumber(const DOM::Node<string_type>& context) { return asNumber(); }
|
|
|
|
virtual string_type evaluateAsString(const DOM::Node<string_type>& context) { return asString(); }
|
|
|
|
virtual NodeSet<string_type> evaluateAsNodeSet() const { return asNodeSet(); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-09-08 23:43:21 +02:00
|
|
|
virtual bool asBool() const { return !string_adaptor::empty(value_); }
|
2005-08-04 22:42:30 +02:00
|
|
|
virtual double asNumber() const
|
|
|
|
{
|
2005-08-22 20:54:53 +02:00
|
|
|
return impl::stringAsNumber(value_);
|
2005-08-04 22:42:30 +02:00
|
|
|
} // asNumber
|
2005-08-17 10:25:24 +02:00
|
|
|
virtual string_type asString() const { return value_; }
|
|
|
|
virtual const NodeSet<string_type>& asNodeSet() const { static NodeSet<string_type> empty; return empty; }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual ValueType type() const { return STRING; }
|
|
|
|
|
|
|
|
private:
|
2005-08-17 10:25:24 +02:00
|
|
|
string_type value_;
|
2005-08-04 22:42:30 +02:00
|
|
|
}; // class StringValue
|
|
|
|
|
2005-08-17 10:31:47 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2005-08-17 12:13:05 +02:00
|
|
|
class NodeSetValue : public XPathValue<string_type>, public XPathExpression<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
2005-08-17 10:31:47 +02:00
|
|
|
NodeSetValue(const NodeSet<string_type>& set) : set_(set) { }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-08-17 12:13:05 +02:00
|
|
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
|
|
|
const ExecutionContext<string_type, string_adaptor>& executionContext) const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
2005-08-17 10:31:47 +02:00
|
|
|
return XPathValuePtr<string_type>(this);
|
2005-08-04 22:42:30 +02:00
|
|
|
} // evaluate
|
2005-08-17 10:31:47 +02:00
|
|
|
virtual bool evaluateAsBool(const DOM::Node<string_type>& context) const{ return asBool(); }
|
|
|
|
virtual double evaluateAsNumber(const DOM::Node<string_type>& context) const { return asNumber(); }
|
|
|
|
virtual string_type evaluateAsString(const DOM::Node<string_type>& context) const { return asString(); }
|
|
|
|
virtual const NodeSet<string_type>& evaluateAsNodeSet() const { return asNodeSet(); }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual bool asBool() const
|
|
|
|
{
|
|
|
|
return !set_.empty();
|
|
|
|
} // asBool
|
|
|
|
virtual double asNumber() const
|
|
|
|
{
|
2005-08-22 20:54:53 +02:00
|
|
|
return impl::stringAsNumber(asString());
|
2005-08-04 22:42:30 +02:00
|
|
|
} // asNumber
|
2005-08-17 10:31:47 +02:00
|
|
|
virtual string_type asString() const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
2005-09-30 23:36:11 +02:00
|
|
|
return !set_.empty() ? impl::nodeStringValue<string_type, string_adaptor>(set_.top()) : string_adaptor::construct_from_utf8("");
|
2005-08-04 22:42:30 +02:00
|
|
|
} // asStringx
|
2005-08-17 10:31:47 +02:00
|
|
|
virtual const NodeSet<string_type>& asNodeSet() const { return set_; }
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
virtual ValueType type() const { return NODE_SET; }
|
|
|
|
|
|
|
|
private:
|
2005-08-17 10:31:47 +02:00
|
|
|
NodeSet<string_type> set_;
|
2005-08-04 22:42:30 +02:00
|
|
|
}; // NodeSetValue
|
|
|
|
|
|
|
|
} // namespace XPath
|
|
|
|
} // namespace Arabica
|
|
|
|
|
|
|
|
#endif
|