From e5fcad302d86d316390c6b0f62759a067313e8a9 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 23 Mar 2009 10:18:55 +0100 Subject: Long live Qt 4.5! --- src/xmlpatterns/data/qatomiccasters_p.h | 705 ++++++++++++++++++++++++++++++++ 1 file changed, 705 insertions(+) create mode 100644 src/xmlpatterns/data/qatomiccasters_p.h (limited to 'src/xmlpatterns/data/qatomiccasters_p.h') diff --git a/src/xmlpatterns/data/qatomiccasters_p.h b/src/xmlpatterns/data/qatomiccasters_p.h new file mode 100644 index 0000000000..888ad326f4 --- /dev/null +++ b/src/xmlpatterns/data/qatomiccasters_p.h @@ -0,0 +1,705 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_AtomicCasters_H +#define Patternist_AtomicCasters_H + +#include "qatomiccaster_p.h" +#include "qdecimal_p.h" +#include "qderivedinteger_p.h" +#include "qderivedstring_p.h" +#include "qinteger_p.h" +#include "qvalidationerror_p.h" + +/** + * @file + * @short Contains classes sub-classing AtomicCaster and which + * are responsible of casting an atomic value to another type. + */ + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short Casts any atomic value to @c xs:string. + * + * This class uses Item::stringValue() for retrieving a string + * representation, and thus supports casting from atomic values + * of any type. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class ToStringCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + Q_ASSERT(from); + return DerivedString::fromLexical(context->namePool(), from.stringValue()); + } + }; + + /** + * @short Casts any atomic value to @c xs:untypedAtomic. + * + * This class uses Item::stringValue() for retrieving a string + * representation, and thus supports casting from atomic values + * of any type. The implementation is similar to ToStringCaster. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class ToUntypedAtomicCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a string value to @c xs:anyURI. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class ToAnyURICaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:hexBinary atomic value to @c xs:base64Binary. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class HexBinaryToBase64BinaryCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:base64Binary atomic value to @c xs:hexBinary. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class Base64BinaryToHexBinaryCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:base64Binary. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToBase64BinaryCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:hexBinary. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToHexBinaryCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts any @c numeric value to @c xs:boolean. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class NumericToBooleanCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts any string value, @c xs:string or @c xs:untypedAtomic, to @c xs:boolean. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToBooleanCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c numeric value, such as @c xs:double or @c xs:decimal, to @c xs:integer or + * @c xs:decimal, depending on IsInteger. + * + * castFrom() uses Numeric::toInteger() for doing the actual casting. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class NumericToDecimalCaster : public AtomicCaster + { + public: + /** + * Used by NumericToDerivedIntegerCaster in addition to this class. + */ + static inline QString errorMessage() + { + return QtXmlPatterns::tr("When casting to %1 from %2, the source value cannot be %3."); + } + + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + const ItemType::Ptr t(from.type()); + const Numeric *const num = from.template as(); + + if(BuiltinTypes::xsDouble->xdtTypeMatches(t) || BuiltinTypes::xsFloat->xdtTypeMatches(t)) + { + if(num->isInf() || num->isNaN()) + { + return ValidationError::createError(errorMessage() + .arg(formatType(context->namePool(), IsInteger ? BuiltinTypes::xsInteger : BuiltinTypes::xsDecimal)) + .arg(formatType(context->namePool(), t)) + .arg(formatData(num->stringValue())), + ReportContext::FOCA0002); + } + } + + if(IsInteger) + return Integer::fromValue(num->toInteger()); + else + return toItem(Decimal::fromValue(num->toDecimal())); + } + }; + + /** + * @short Casts a string value, @c xs:string or @c xs:untypedAtomic, to @c xs:decimal. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToDecimalCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a string value, @c xs:string or @c xs:untypedAtomic, to @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToIntegerCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a value of type @c xs:boolean to @c xs:decimal. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class BooleanToDecimalCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a value of type @c xs:boolean to @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class BooleanToIntegerCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a value to itself. Essentially, this AtomicCaster does nothing. + * + * Casting a value to the type of itself is defined to be a noop, + * no operation. When it can be statically detected that will be done, + * CastAs rewrites itself appropriately during compilation, but + * in some cases insufficent data is available at compile time and then + * is this class need on a case-per-case base at evaluation time. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class SelfToSelfCaster : public AtomicCaster + { + public: + + /** + * This function simply returns @p from. + */ + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:gYear. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToGYearCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:gDay. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToGDayCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:gMonth. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToGMonthCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:gYearMonth. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToGYearMonthCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:gYearMonth. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToGMonthDayCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:dateTime. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToDateTimeCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:time. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToTimeCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:date. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToDateCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:duration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:dayTimeDuration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToDayTimeDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:string or @c xs:untypedAtomic atomic value to @c xs:yearMonthDuration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class StringToYearMonthDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + + /** + * @short Casts a @c xs:date or @c xs:dateTime atomic value to @c xs:gYear. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToGYearCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:date or @c xs:dateTime atomic value to @c xs:gYearMonth. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToGYearMonthCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:date or @c xs:dateTime atomic value to @c xs:gMonth. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToGMonthCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:date or @c xs:dateTime atomic value to @c xs:gMonthDay. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToGMonthDayCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts a @c xs:date or @c xs:dateTime atomic value to @c xs:gDay. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToGDayCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDateTime instance to DateTime. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToDateTimeCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDateTime instance to SchemaTime. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToDateCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDateTime instance to SchemaTime. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDateTimeToTimeCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDuration instance to Duration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDurationToDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDuration instance to DayTimeDuration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDurationToDayTimeDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an AbstractDuration instance to YearMonthDuration. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + class AbstractDurationToYearMonthDurationCaster : public AtomicCaster + { + public: + virtual Item castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const; + }; + + /** + * @short Casts an @c xs:string instance to a derived type of @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class StringToDerivedIntegerCaster : public AtomicCaster + { + public: + virtual Item + castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + return DerivedInteger::fromLexical(context->namePool(), from.stringValue()); + } + }; + + /** + * @short Casts an @c xs:boolean instance to a derived type of @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class BooleanToDerivedIntegerCaster : public AtomicCaster + { + public: + virtual Item + castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + return DerivedInteger::fromValue(context->namePool(), from.template as()->evaluateEBV(context) ? 1 : 0); + } + }; + + /** + * @short Casts an @c xs:boolean instance to a derived type of @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class AnyToDerivedStringCaster : public AtomicCaster + { + public: + virtual Item + castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + return DerivedString::fromLexical(context->namePool(), from.stringValue()); + } + }; + + /** + * @short Casts any @c numeric instance to a derived type of @c xs:integer. + * + * @ingroup Patternist_xdm + * @author Frans Englich + */ + template + class NumericToDerivedIntegerCaster : public AtomicCaster + { + public: + virtual Item + castFrom(const Item &from, + const QExplicitlySharedDataPointer &context) const + { + const ItemType::Ptr t(from.type()); + const Numeric *const num = from.template as(); + + if(BuiltinTypes::xsDouble->xdtTypeMatches(t) || BuiltinTypes::xsFloat->xdtTypeMatches(t)) + { + if(num->isInf() || num->isNaN()) + { + return ValidationError::createError(NumericToDecimalCaster::errorMessage() + .arg(formatType(context->namePool(), DerivedInteger::itemType())) + .arg(formatType(context->namePool(), t)) + .arg(formatData(num->stringValue())), + ReportContext::FOCA0002); + } + } + + return toItem(DerivedInteger::fromValue(context->namePool(), from.template as()->toInteger())); + } + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif -- cgit v1.2.3