diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /src/xmlpatterns/utils | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'src/xmlpatterns/utils')
23 files changed, 3487 insertions, 0 deletions
diff --git a/src/xmlpatterns/utils/qautoptr.cpp b/src/xmlpatterns/utils/qautoptr.cpp new file mode 100644 index 0000000..1e3d773 --- /dev/null +++ b/src/xmlpatterns/utils/qautoptr.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +/*! + \class QPatternist::AutoPtr + \brief A smart pointer very similar to std::auto_ptr. + \internal + \since 4.4 + \ingroup misc + + */ + diff --git a/src/xmlpatterns/utils/qautoptr_p.h b/src/xmlpatterns/utils/qautoptr_p.h new file mode 100644 index 0000000..9f40400 --- /dev/null +++ b/src/xmlpatterns/utils/qautoptr_p.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** 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 QPatternist_AutoPtr_p_h +#define QPatternist_AutoPtr_p_h + +#include <QtGlobal> + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + template<typename T> + class AutoPtrRef + { + public: + explicit AutoPtrRef(T *const other) : m_ptr(other) + { + } + + T* m_ptr; + }; + + template<typename T> + class AutoPtr + { + public: + explicit inline AutoPtr(T *pointer = 0) : m_ptr(pointer) + { + } + + inline AutoPtr(AutoPtr<T> &other) : m_ptr(other.release()) + { + } + + inline ~AutoPtr() + { + delete m_ptr; + } + + inline T &operator*() const + { + Q_ASSERT_X(m_ptr, Q_FUNC_INFO, ""); + return *m_ptr; + } + + T *operator->() const + { + Q_ASSERT_X(m_ptr, Q_FUNC_INFO, ""); + return m_ptr; + } + + inline bool operator!() const + { + return !m_ptr; + } + + inline operator bool() const + { + return m_ptr != 0; + } + + AutoPtr(AutoPtrRef<T> ref) : m_ptr(ref.m_ptr) + { + } + + AutoPtr& operator=(AutoPtrRef<T> ref) + { + if(ref.m_ptr != m_ptr) + { + delete m_ptr; + m_ptr = ref.m_ptr; + } + return *this; + } + +#ifndef QT_NO_MEMBER_TEMPLATES + template<typename L> + operator AutoPtrRef<L>() + { + return AutoPtrRef<L>(this->release()); + } + + template<typename L> + operator AutoPtr<L>() + { + return AutoPtr<L>(this->release()); + } + + template<typename L> + inline AutoPtr(AutoPtr<L>& other) : m_ptr(other.release()) + { + } +#endif + + inline T *release() + { + T *const retval = m_ptr; + m_ptr = 0; + return retval; + } + + inline T *data() const + { + return m_ptr; + } + + void reset(T * other = 0) + { + if(other != m_ptr) + { + delete m_ptr; + m_ptr = other; + } + } + + inline AutoPtr &operator=(AutoPtr &other) + { + reset(other.release()); + return *this; + } + + private: + T *m_ptr; + }; +} + +QT_END_NAMESPACE +#endif diff --git a/src/xmlpatterns/utils/qcommonnamespaces_p.h b/src/xmlpatterns/utils/qcommonnamespaces_p.h new file mode 100644 index 0000000..6c9cf57 --- /dev/null +++ b/src/xmlpatterns/utils/qcommonnamespaces_p.h @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** 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_CommonNamespaces_H +#define Patternist_CommonNamespaces_H + +#include <QLatin1String> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Contains common, standardized XML namespaces. + * + * @author Frans Englich <fenglich@trolltech.com> + */ + namespace CommonNamespaces + { + /** + * Namespace for the special XML namespace. It is by definition + * bound to the "xml" prefix, and should have no usage in + * ordinary code. + * + * Specification: http://www.w3.org/TR/REC-xml-names/ + */ + const QLatin1String XML("http://www.w3.org/XML/1998/namespace"); + + /** + * The namespace for the xmlns prefix. The Namespaces in XML recommendation + * explicitly states that xmlns should not have a namespace, but has since + * been changed. See: + * + * http://www.w3.org/2000/xmlns/ + */ + const QLatin1String XMLNS("http://www.w3.org/2000/xmlns/"); + + /** + * The namespace for W3C XML Schema. This is used for the XML language it + * is, as well as its built-in data types. + * + * Specification: http://www.w3.org/TR/xmlschema-2/ + * @see <a href="http://www.w3.org/TR/xmlschema-2/datatypes.html#namespaces">XML Schema + * Part 2: Datatypes Second Edition, 3.1 Namespace considerations</a> + */ + const QLatin1String WXS("http://www.w3.org/2001/XMLSchema"); + + /** + * The namespace for W3C XML Schema attributes used in schema instances. + * + * Specification: http://www.w3.org/TR/xmlschema-2/ + * + * @see <a href="http://www.w3.org/TR/xmlschema-1/structures.html#Instance_Document_Constructions">XML + * Schema Part 1: Structures Second Edition, 2.6 Schema-Related + * Markup in Documents Being Validated</a> + */ + const QLatin1String XSI("http://www.w3.org/2001/XMLSchema-instance"); + + /** + * The namespace for built-in XPath functions, as defined in for example + * XQuery 1.0 and XPath 2.0 Functions and Operators and XSLT. + * + * Specification: http://www.w3.org/TR/xquery-operators/ + */ + const QLatin1String XFN("http://www.w3.org/2005/xpath-functions"); + + /** + * The namespace for XSL-T 1.0 and 2.0. + * + * @see <a href="http://www.w3.org/TR/xslt20/#xslt-namespace">XSL + * Transformations (XSLT) Version 2.0, 3.1 XSLT Namespace</a> + * @see <a href="http://www.w3.org/TR/xslt">XSL Transformations (XSLT) Version 1.0</a> + */ + const QLatin1String XSLT("http://www.w3.org/1999/XSL/Transform"); + + /** + * The namespace for identifying errors in XPath. + * + * @see <a href="http://www.w3.org/TR/xpath20/#id-identifying-errors">XML Path Language (XPath) + * 2.0, 2.3.2 Identifying and Reporting Errors</a> + */ + const QLatin1String XPERR("http://www.w3.org/2005/xqt-errors"); + + /** + * The XPath 2.0 Unicode codepoint collation URI identifier. Collations + * specifies how strings are compared and ordered. + */ + const char *const UNICODE_COLLATION = "http://www.w3.org/2005/xpath-functions/collation/codepoint"; + + /** + * A namespace provided in XQuery 1.0, to easily declare local + * variables and functions. + */ + const QLatin1String XDT_LOCAL("http://www.w3.org/2005/xquery-local-functions"); + } +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qcppcastinghelper_p.h b/src/xmlpatterns/utils/qcppcastinghelper_p.h new file mode 100644 index 0000000..7979bf0 --- /dev/null +++ b/src/xmlpatterns/utils/qcppcastinghelper_p.h @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** 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_CppCastingHelper_H +#define Patternist_CppCastingHelper_H + +#include <QtCore/QtGlobal> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Provides convenience methods for performing static casts between C++ classes. + * + * In Patternist, it is very common to do up-casts from Expression or Item, which typically + * involves writing messy code. Such an old-way cast looks like this: + * + * @code + * static_cast<const MyClass *>(myInstance.data())->myClassMember() + * @endcode + * + * CppCastingHelper provides the convenience method as() for this, which is functionally + * equivalent to the above code, but simpler: + * + * @code + * myInstance->as<MyClass>()->myClassMember() + * @endcode + * + * The as() function performs a static cast. + * + * By using CppCastingHelper, this is achieved: + * + * - Const correctness is automatically taken care of + * - Less code to write + * - When compiling in debug mode, the as() functions uses a @c dynamic_cast to verify that the + * static casts are properly done, such that sensible error messages are given when the casts + * are invalid. It also traps invalid casts which nevertheless happen to work on a particular + * platform/compiler/hardware architecture. + * + * CppCastingHelper is a template class where the TSubClass parameter must be the class + * inheriting CppCastingHelper. See Item or Expression for demonstration. + * + * @author Frans Englich <fenglich@trolltech.com> + */ + template<typename TSubClass> + class CppCastingHelper + { + public: + + /** + * Casts this instance to: + * + * @code + * const TCastTarget * + * @endcode + * + * and returns the result. + * + * When compiled in debug mode, this function perform a @c dynamic_cast, in order to + * check the correctness of the cast. + */ + template<typename TCastTarget> + inline const TCastTarget *as() const + { +#if defined(Patternist_DEBUG) && !defined(Q_CC_XLC) +/* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */ + Q_ASSERT_X(dynamic_cast<const TCastTarget *>(static_cast<const TSubClass *>(this)), + Q_FUNC_INFO, + "The cast is invalid. This class does not inherit the cast target."); +#endif + return static_cast<const TCastTarget *>(static_cast<const TSubClass *>(this)); + } + + /** + * Casts this instance to: + * + * @code + * TCastTarget * + * @endcode + * + * and returns the result. + * + * When compiled in debug mode, a @c dynamic_cast is attempted, in order to + * check the correctness of the cast. + */ + template<typename TCastTarget> + inline TCastTarget *as() + { +#if defined(Patternist_DEBUG) && !defined(Q_CC_XLC) +/* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */ + Q_ASSERT_X(dynamic_cast<TCastTarget *>(static_cast<TSubClass *>(this)), + Q_FUNC_INFO, + "The cast is invalid. This class does not inherit the cast target."); +#endif + return static_cast<TCastTarget *>(static_cast<TSubClass *>(this)); + } + + protected: + /** + * This constructor is protected because this class must be sub-classed. + */ + inline CppCastingHelper() {} + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qdebug_p.h b/src/xmlpatterns/utils/qdebug_p.h new file mode 100644 index 0000000..d270250 --- /dev/null +++ b/src/xmlpatterns/utils/qdebug_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** 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_Debug_H +#define Patternist_Debug_H + +#include <QtDebug> +/** + * @file + * @short Contains macros for debugging. + */ + +QT_BEGIN_NAMESPACE + +/** + * @short Enables detailed parser debug output. + * + * If this macro is defined, @em a @em lot of debugging information will be outputted. + * This is all the state transitions, token shifting, and rule reductions that + * the parser do. + * + * This is automatically disabled if @c QT_NO_DEBUG is defined. + */ +#define Patternist_DEBUG_PARSER + +/** + * @short Enables debug printing statements. + * + * Patternist does not use qDebug(), but pDebug() instead. It only output + * if this define is defined. + * + * It is automatically disabled if @c QT_NO_DEBUG is defined. + */ +#define Patternist_DEBUG + +#undef Patternist_DEBUG // disable it for now + +#ifdef QT_NO_DEBUG +# undef Patternist_DEBUG_PARSER +# undef Patternist_DEBUG +#endif + +namespace QPatternist +{ +#ifdef Patternist_DEBUG + inline QDebug pDebug() + { + return qDebug(); + } +#else + inline QNoDebug pDebug() + { + return QNoDebug(); + } +#endif +} + +QT_END_NAMESPACE +#endif diff --git a/src/xmlpatterns/utils/qdelegatingnamespaceresolver.cpp b/src/xmlpatterns/utils/qdelegatingnamespaceresolver.cpp new file mode 100644 index 0000000..fd67cf3 --- /dev/null +++ b/src/xmlpatterns/utils/qdelegatingnamespaceresolver.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qnamepool_p.h" + +#include "qdelegatingnamespaceresolver_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +DelegatingNamespaceResolver::DelegatingNamespaceResolver(const NamespaceResolver::Ptr &resolver) : m_nsResolver(resolver) +{ + Q_ASSERT(m_nsResolver); +} + +DelegatingNamespaceResolver::DelegatingNamespaceResolver(const NamespaceResolver::Ptr &ns, + const Bindings &overrides) : m_nsResolver(ns) + , m_bindings(overrides) +{ + Q_ASSERT(m_nsResolver); +} + +QXmlName::NamespaceCode DelegatingNamespaceResolver::lookupNamespaceURI(const QXmlName::PrefixCode prefix) const +{ + const QXmlName::NamespaceCode val(m_bindings.value(prefix, NoBinding)); + + if(val == NoBinding) + return m_nsResolver->lookupNamespaceURI(prefix); + else + return val; +} + +NamespaceResolver::Bindings DelegatingNamespaceResolver::bindings() const +{ + Bindings bs(m_nsResolver->bindings()); + const Bindings::const_iterator end(m_bindings.constEnd()); + Bindings::const_iterator it(m_bindings.constBegin()); + + for(; it != end; ++it) + bs.insert(it.key(), it.value()); + + return bs; +} + +void DelegatingNamespaceResolver::addBinding(const QXmlName nb) +{ + if(nb.namespaceURI() == StandardNamespaces::UndeclarePrefix) + m_bindings.remove(nb.prefix()); + else + m_bindings.insert(nb.prefix(), nb.namespaceURI()); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qdelegatingnamespaceresolver_p.h b/src/xmlpatterns/utils/qdelegatingnamespaceresolver_p.h new file mode 100644 index 0000000..ef7d4de --- /dev/null +++ b/src/xmlpatterns/utils/qdelegatingnamespaceresolver_p.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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_DelegatingNamespaceResolver_H +#define Patternist_DelegatingNamespaceResolver_H + +#include <QHash> + +#include "qnamespaceresolver_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short Contains a set of bindings, plus a pointer to another resolver + * which is delegates requests to, in case it can't handle a lookup on its + * own. + * + * @ingroup Patternist + * @author Frans Englich <fenglich@trolltech.com> + */ + class DelegatingNamespaceResolver : public NamespaceResolver + { + public: + DelegatingNamespaceResolver(const NamespaceResolver::Ptr &ns); + DelegatingNamespaceResolver(const NamespaceResolver::Ptr &ns, + const Bindings &overrides); + + virtual void addBinding(const QXmlName nb); + + virtual QXmlName::NamespaceCode lookupNamespaceURI(const QXmlName::PrefixCode prefix) const; + virtual Bindings bindings() const; + + private: + const NamespaceResolver::Ptr m_nsResolver; + Bindings m_bindings; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qgenericnamespaceresolver.cpp b/src/xmlpatterns/utils/qgenericnamespaceresolver.cpp new file mode 100644 index 0000000..dd1ab9f --- /dev/null +++ b/src/xmlpatterns/utils/qgenericnamespaceresolver.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qnamepool_p.h" + +#include "qgenericnamespaceresolver_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +GenericNamespaceResolver::GenericNamespaceResolver(const Bindings &list) : m_bindings(list) +{ +} + +void GenericNamespaceResolver::addBinding(const QXmlName nb) +{ + if(nb.namespaceURI() == StandardNamespaces::UndeclarePrefix) + m_bindings.remove(nb.prefix()); + else + m_bindings.insert(nb.prefix(), nb.namespaceURI()); +} + +QXmlName::NamespaceCode GenericNamespaceResolver::lookupNamespaceURI(const QXmlName::PrefixCode prefix) const +{ + return m_bindings.value(prefix, NoBinding); +} + +NamespaceResolver::Ptr GenericNamespaceResolver::defaultXQueryBindings() +{ + Bindings list; + + list.insert(StandardPrefixes::xml, StandardNamespaces::xml); + list.insert(StandardPrefixes::xs, StandardNamespaces::xs); + list.insert(StandardPrefixes::xsi, StandardNamespaces::xsi); + list.insert(StandardPrefixes::fn, StandardNamespaces::fn); + list.insert(StandardPrefixes::local, StandardNamespaces::local); + list.insert(StandardPrefixes::empty, StandardNamespaces::empty); + + return NamespaceResolver::Ptr(new GenericNamespaceResolver(list)); +} + +NamespaceResolver::Ptr GenericNamespaceResolver::defaultXSLTBindings() +{ + Bindings list; + + list.insert(StandardPrefixes::xml, StandardNamespaces::xml); + list.insert(StandardPrefixes::empty, StandardNamespaces::empty); + + return NamespaceResolver::Ptr(new GenericNamespaceResolver(list)); +} + +NamespaceResolver::Bindings GenericNamespaceResolver::bindings() const +{ + return m_bindings; +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qgenericnamespaceresolver_p.h b/src/xmlpatterns/utils/qgenericnamespaceresolver_p.h new file mode 100644 index 0000000..2bb6aff --- /dev/null +++ b/src/xmlpatterns/utils/qgenericnamespaceresolver_p.h @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** 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_GenericNamespaceResolver_H +#define Patternist_GenericNamespaceResolver_H + +#include <QHash> + +#include "qnamespaceresolver_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Generic namespace resolver which resolves lookups against entries in a QHash. + * + * @ingroup Patternist + * @author Frans Englich <fenglich@trolltech.com> + */ + class GenericNamespaceResolver : public NamespaceResolver + { + public: + GenericNamespaceResolver(const Bindings &list); + virtual void addBinding(const QXmlName nb); + + virtual QXmlName::NamespaceCode lookupNamespaceURI(const QXmlName::PrefixCode prefix) const; + + /** + * Returns a GenericNamespaceResolver containing the following bindings: + * + * - <tt>xml</tt> = <tt>http://www.w3.org/XML/1998/namespace</tt> + * - <tt>xs</tt> = <tt>http://www.w3.org/2001/XMLSchema</tt> + * - <tt>xsi</tt> = <tt>http://www.w3.org/2001/XMLSchema-instance</tt> + * - <tt>fn</tt> = <tt>http://www.w3.org/2005/xpath-functions</tt> + * - <tt>xdt</tt> = <tt>http://www.w3.org/2005/xpath-datatypes</tt> + * - no prefix = empty namespace + */ + static NamespaceResolver::Ptr defaultXQueryBindings(); + + /** + * Returns a GenericNamespaceResolver containing the following bindings: + * + * - <tt>xml</tt> = <tt>http://www.w3.org/XML/1998/namespace</tt> + * - no prefix = empty namespace + */ + static NamespaceResolver::Ptr defaultXSLTBindings(); + + virtual Bindings bindings() const; + + private: + /** + * The key is the prefix, the value the namespace URI. + */ + Bindings m_bindings; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qnamepool.cpp b/src/xmlpatterns/utils/qnamepool.cpp new file mode 100644 index 0000000..a6f1b21 --- /dev/null +++ b/src/xmlpatterns/utils/qnamepool.cpp @@ -0,0 +1,418 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include <QtDebug> + +#include "private/qxmlutils_p.h" +#include "qxpathhelper_p.h" + +#include "qnamepool_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +NamePool::NamePool() +{ + m_localNameMapping .reserve(DefaultLocalNameCapacity + StandardLocalNameCount); + m_localNames .reserve(DefaultLocalNameCapacity + StandardLocalNameCount); + m_namespaceMapping .reserve(DefaultURICapacity + StandardNamespaceCount); + m_namespaces .reserve(DefaultURICapacity + StandardNamespaceCount); + m_prefixes .reserve(DefaultPrefixCapacity + StandardPrefixCount); + m_prefixMapping .reserve(DefaultPrefixCapacity + StandardPrefixCount); + + /* Namespaces. */ + { + unlockedAllocateNamespace(QString()); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2005/xpath-functions")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2005/xquery-local-functions")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/XML/1998/namespace")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2000/xmlns/")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2001/XMLSchema")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2001/XMLSchema-instance")); + unlockedAllocateNamespace(QLatin1String("http://www.w3.org/1999/XSL/Transform")); + + /* For UndeclarePrefix, StopNamespaceInheritance and InternalXSLT. We use two + * arbitrary strings that aren't used. For instance, if we just used an + * empty QString, unlockedAllocateNamespace() would assign it + * StandardNamespaces::empty. However, it's important that the string + * is an invalid namespace, since otherwise user strings would get + * assigned the same IDs. */ + unlockedAllocateNamespace(QLatin1String(" | 1 ")); + unlockedAllocateNamespace(QLatin1String(" | 2 ")); + unlockedAllocateNamespace(QLatin1String(" | InternalXSLT")); + + Q_ASSERT_X(m_namespaces.count() == StandardNamespaceCount, Q_FUNC_INFO, + qPrintable(QString::fromLatin1("Expected is %1, actual is %2.").arg(StandardNamespaceCount).arg(m_namespaces.count()))); + } + + /* Prefixes. */ + { + unlockedAllocatePrefix(QString()); + unlockedAllocatePrefix(QLatin1String("fn")); + unlockedAllocatePrefix(QLatin1String("local")); + unlockedAllocatePrefix(QLatin1String("xml")); + unlockedAllocatePrefix(QLatin1String("xmlns")); + unlockedAllocatePrefix(QLatin1String("xs")); + unlockedAllocatePrefix(QLatin1String("xsi")); + unlockedAllocatePrefix(QLatin1String("ns0")); + unlockedAllocatePrefix(QLatin1String("|||")); /* Invalid NCName, for StopNamespaceInheritance. */ + + Q_ASSERT_X(m_prefixes.count() == StandardPrefixCount, Q_FUNC_INFO, + qPrintable(QString::fromLatin1("Expected is %1, actual is %2.").arg(StandardPrefixCount).arg(m_prefixes.count()))); + } + + /* Local names. */ + { + /* Same order as the enum in StandardLocalNames. That is, alphabetically. */ + unlockedAllocateLocalName(QLatin1String("abs")); + unlockedAllocateLocalName(QLatin1String("adjust-dateTime-to-timezone")); + unlockedAllocateLocalName(QLatin1String("adjust-date-to-timezone")); + unlockedAllocateLocalName(QLatin1String("adjust-time-to-timezone")); + unlockedAllocateLocalName(QLatin1String("all")); + unlockedAllocateLocalName(QLatin1String("arity")); + unlockedAllocateLocalName(QLatin1String("avg")); + unlockedAllocateLocalName(QLatin1String("base")); + unlockedAllocateLocalName(QLatin1String("base-uri")); + unlockedAllocateLocalName(QLatin1String("boolean")); + unlockedAllocateLocalName(QLatin1String("ceiling")); + unlockedAllocateLocalName(QLatin1String("codepoint-equal")); + unlockedAllocateLocalName(QLatin1String("codepoints-to-string")); + unlockedAllocateLocalName(QLatin1String("collection")); + unlockedAllocateLocalName(QLatin1String("compare")); + unlockedAllocateLocalName(QLatin1String("concat")); + unlockedAllocateLocalName(QLatin1String("contains")); + unlockedAllocateLocalName(QLatin1String("count")); + unlockedAllocateLocalName(QLatin1String("current")); + unlockedAllocateLocalName(QLatin1String("current-date")); + unlockedAllocateLocalName(QLatin1String("current-dateTime")); + unlockedAllocateLocalName(QLatin1String("current-time")); + unlockedAllocateLocalName(QLatin1String("data")); + unlockedAllocateLocalName(QLatin1String("dateTime")); + unlockedAllocateLocalName(QLatin1String("day-from-date")); + unlockedAllocateLocalName(QLatin1String("day-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("days-from-duration")); + unlockedAllocateLocalName(QLatin1String("deep-equal")); + unlockedAllocateLocalName(QLatin1String("default")); + unlockedAllocateLocalName(QLatin1String("default-collation")); + unlockedAllocateLocalName(QLatin1String("distinct-values")); + unlockedAllocateLocalName(QLatin1String("doc")); + unlockedAllocateLocalName(QLatin1String("doc-available")); + unlockedAllocateLocalName(QLatin1String("document")); + unlockedAllocateLocalName(QLatin1String("document-uri")); + unlockedAllocateLocalName(QLatin1String("element-available")); + unlockedAllocateLocalName(QLatin1String("empty")); + unlockedAllocateLocalName(QLatin1String("encode-for-uri")); + unlockedAllocateLocalName(QLatin1String("ends-with")); + unlockedAllocateLocalName(QLatin1String("error")); + unlockedAllocateLocalName(QLatin1String("escape-html-uri")); + unlockedAllocateLocalName(QLatin1String("exactly-one")); + unlockedAllocateLocalName(QLatin1String("exists")); + unlockedAllocateLocalName(QLatin1String("false")); + unlockedAllocateLocalName(QLatin1String("floor")); + unlockedAllocateLocalName(QLatin1String("function-available")); + unlockedAllocateLocalName(QLatin1String("function-name")); + unlockedAllocateLocalName(QLatin1String("generate-id")); + unlockedAllocateLocalName(QLatin1String("generic-string-join")); + unlockedAllocateLocalName(QLatin1String("hours-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("hours-from-duration")); + unlockedAllocateLocalName(QLatin1String("hours-from-time")); + unlockedAllocateLocalName(QLatin1String("id")); + unlockedAllocateLocalName(QLatin1String("idref")); + unlockedAllocateLocalName(QLatin1String("implicit-timezone")); + unlockedAllocateLocalName(QLatin1String("index-of")); + unlockedAllocateLocalName(QLatin1String("in-scope-prefixes")); + unlockedAllocateLocalName(QLatin1String("insert-before")); + unlockedAllocateLocalName(QLatin1String("iri-to-uri")); + unlockedAllocateLocalName(QLatin1String("is-schema-aware")); + unlockedAllocateLocalName(QLatin1String("key")); + unlockedAllocateLocalName(QLatin1String("lang")); + unlockedAllocateLocalName(QLatin1String("last")); + unlockedAllocateLocalName(QLatin1String("local-name")); + unlockedAllocateLocalName(QLatin1String("local-name-from-QName")); + unlockedAllocateLocalName(QLatin1String("lower-case")); + unlockedAllocateLocalName(QLatin1String("matches")); + unlockedAllocateLocalName(QLatin1String("max")); + unlockedAllocateLocalName(QLatin1String("min")); + unlockedAllocateLocalName(QLatin1String("minutes-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("minutes-from-duration")); + unlockedAllocateLocalName(QLatin1String("minutes-from-time")); + unlockedAllocateLocalName(QLatin1String("month-from-date")); + unlockedAllocateLocalName(QLatin1String("month-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("months-from-duration")); + unlockedAllocateLocalName(QLatin1String("name")); + unlockedAllocateLocalName(QLatin1String("namespace-uri")); + unlockedAllocateLocalName(QLatin1String("namespace-uri-for-prefix")); + unlockedAllocateLocalName(QLatin1String("namespace-uri-from-QName")); + unlockedAllocateLocalName(QLatin1String("nilled")); + unlockedAllocateLocalName(QLatin1String("node-name")); + unlockedAllocateLocalName(QLatin1String("normalize-space")); + unlockedAllocateLocalName(QLatin1String("normalize-unicode")); + unlockedAllocateLocalName(QLatin1String("not")); + unlockedAllocateLocalName(QLatin1String("number")); + unlockedAllocateLocalName(QLatin1String("one-or-more")); + unlockedAllocateLocalName(QLatin1String("position")); + unlockedAllocateLocalName(QLatin1String("prefix-from-QName")); + unlockedAllocateLocalName(QLatin1String("product-name")); + unlockedAllocateLocalName(QLatin1String("product-version")); + unlockedAllocateLocalName(QLatin1String("property-name")); + unlockedAllocateLocalName(QLatin1String("QName")); + unlockedAllocateLocalName(QLatin1String("remove")); + unlockedAllocateLocalName(QLatin1String("replace")); + unlockedAllocateLocalName(QLatin1String("resolve-QName")); + unlockedAllocateLocalName(QLatin1String("resolve-uri")); + unlockedAllocateLocalName(QLatin1String("reverse")); + unlockedAllocateLocalName(QLatin1String("root")); + unlockedAllocateLocalName(QLatin1String("round")); + unlockedAllocateLocalName(QLatin1String("round-half-to-even")); + unlockedAllocateLocalName(QLatin1String("seconds-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("seconds-from-duration")); + unlockedAllocateLocalName(QLatin1String("seconds-from-time")); + unlockedAllocateLocalName(QLatin1String("sourceValue")); + unlockedAllocateLocalName(QLatin1String("starts-with")); + unlockedAllocateLocalName(QLatin1String("static-base-uri")); + unlockedAllocateLocalName(QLatin1String("string")); + unlockedAllocateLocalName(QLatin1String("string-join")); + unlockedAllocateLocalName(QLatin1String("string-length")); + unlockedAllocateLocalName(QLatin1String("string-to-codepoints")); + unlockedAllocateLocalName(QLatin1String("subsequence")); + unlockedAllocateLocalName(QLatin1String("substring")); + unlockedAllocateLocalName(QLatin1String("substring-after")); + unlockedAllocateLocalName(QLatin1String("substring-before")); + unlockedAllocateLocalName(QLatin1String("sum")); + unlockedAllocateLocalName(QLatin1String("supports-backwards-compatibility")); + unlockedAllocateLocalName(QLatin1String("supports-serialization")); + unlockedAllocateLocalName(QLatin1String("system-property")); + unlockedAllocateLocalName(QLatin1String("timezone-from-date")); + unlockedAllocateLocalName(QLatin1String("timezone-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("timezone-from-time")); + unlockedAllocateLocalName(QLatin1String("tokenize")); + unlockedAllocateLocalName(QLatin1String("trace")); + unlockedAllocateLocalName(QLatin1String("translate")); + unlockedAllocateLocalName(QLatin1String("true")); + unlockedAllocateLocalName(QLatin1String("type-available")); + unlockedAllocateLocalName(QLatin1String("unordered")); + unlockedAllocateLocalName(QLatin1String("unparsed-entity-public-id")); + unlockedAllocateLocalName(QLatin1String("unparsed-entity-uri")); + unlockedAllocateLocalName(QLatin1String("unparsed-text")); + unlockedAllocateLocalName(QLatin1String("unparsed-text-available")); + unlockedAllocateLocalName(QLatin1String("upper-case")); + unlockedAllocateLocalName(QLatin1String("vendor")); + unlockedAllocateLocalName(QLatin1String("vendor-url")); + unlockedAllocateLocalName(QLatin1String("version")); + unlockedAllocateLocalName(QLatin1String("xml")); + unlockedAllocateLocalName(QLatin1String("xmlns")); + unlockedAllocateLocalName(QLatin1String("year-from-date")); + unlockedAllocateLocalName(QLatin1String("year-from-dateTime")); + unlockedAllocateLocalName(QLatin1String("years-from-duration")); + unlockedAllocateLocalName(QLatin1String("zero-or-one")); + Q_ASSERT(m_localNames.count() == StandardLocalNameCount); + } +} + +QXmlName NamePool::allocateQName(const QString &uri, const QString &localName, const QString &prefix) +{ + QWriteLocker l(&lock); + + /* + QString codepoints; + for(int i = 0; i < localName.length(); ++i) + codepoints.append(QString::number(localName.at(i).unicode()) + QLatin1Char(' ')); + + pDebug() << Q_FUNC_INFO << localName << "codepoints:" << codepoints; + */ + + Q_ASSERT_X(QXmlUtils::isNCName(localName), Q_FUNC_INFO, + qPrintable(QString::fromLatin1("'%1' is an invalid NCName.").arg(localName))); + + const QXmlName::NamespaceCode nsCode = unlockedAllocateNamespace(uri); + const QXmlName::LocalNameCode localCode = unlockedAllocateLocalName(localName); + + Q_ASSERT(prefix.isEmpty() || QXmlUtils::isNCName(prefix)); + const QXmlName::PrefixCode prefixCode = unlockedAllocatePrefix(prefix); + + return QXmlName(nsCode, localCode, prefixCode); +} + +QXmlName NamePool::allocateBinding(const QString &prefix, const QString &uri) +{ + QWriteLocker l(&lock); + + Q_ASSERT_X(prefix.isEmpty() || QXmlUtils::isNCName(prefix), Q_FUNC_INFO, + qPrintable(QString::fromLatin1("%1 is an invalid prefix.").arg(prefix))); + const QXmlName::NamespaceCode nsCode = unlockedAllocateNamespace(uri); + + Q_ASSERT(prefix.isEmpty() || QXmlUtils::isNCName(prefix)); + const QXmlName::PrefixCode prefixCode = unlockedAllocatePrefix(prefix); + + return QXmlName(nsCode, StandardLocalNames::empty, prefixCode); +} + +QXmlName::LocalNameCode NamePool::unlockedAllocateLocalName(const QString &ln) +{ + Q_ASSERT_X(QXmlUtils::isNCName(ln), Q_FUNC_INFO, + qPrintable(QString::fromLatin1("Invalid local name: \"%1\"").arg(ln))); + + int indexInLocalNames = m_localNameMapping.value(ln, NoSuchValue); + + if(indexInLocalNames == NoSuchValue) + { + indexInLocalNames = m_localNames.count(); + m_localNames.append(ln); + m_localNameMapping.insert(ln, indexInLocalNames); + } + + return indexInLocalNames; +} + +QXmlName::PrefixCode NamePool::unlockedAllocatePrefix(const QString &prefix) +{ + int indexInPrefixes = m_prefixMapping.value(prefix, NoSuchValue); + + if(indexInPrefixes == NoSuchValue) + { + indexInPrefixes = m_prefixes.count(); + m_prefixes.append(prefix); + m_prefixMapping.insert(prefix, indexInPrefixes); + } + + return indexInPrefixes; +} + +QXmlName::NamespaceCode NamePool::unlockedAllocateNamespace(const QString &uri) +{ + int indexInURIs = m_namespaceMapping.value(uri, NoSuchValue); + + if(indexInURIs == NoSuchValue) + { + indexInURIs = m_namespaces.count(); + m_namespaces.append(uri); + m_namespaceMapping.insert(uri, indexInURIs); + } + + return indexInURIs; +} + +const QString &NamePool::displayPrefix(const QXmlName::NamespaceCode nc) const +{ + switch(nc) + { + case StandardNamespaces::xmlns: return m_prefixes.at(StandardPrefixes::xmlns); + case StandardNamespaces::local: return m_prefixes.at(StandardPrefixes::local); + case StandardNamespaces::xs: return m_prefixes.at(StandardPrefixes::xs); + case StandardNamespaces::xml: return m_prefixes.at(StandardPrefixes::xml); + case StandardNamespaces::fn: return m_prefixes.at(StandardPrefixes::fn); + default: return m_prefixes.at(StandardPrefixes::empty); + } +} + +QString NamePool::displayName(const QXmlName qName) const +{ + QReadLocker l(&lock); + + if(qName.hasNamespace()) + { + if(qName.namespaceURI() == StandardNamespaces::InternalXSLT) + return QLatin1Char('#') + m_localNames.at(qName.localName()); + + const QString &p = displayPrefix(qName.namespaceURI()); + + if(p.isEmpty()) + return QLatin1Char('{') + m_namespaces.at(qName.namespaceURI()) + QLatin1Char('}') + toLexical(qName); + else + return p + QLatin1Char(':') + m_localNames.at(qName.localName()); + } + else + return m_localNames.at(qName.localName()); +} + +QString NamePool::toClarkName(const QXmlName &name) const +{ + if(name.isNull()) + return QLatin1String("QXmlName(null)"); + else + { + if(name.hasNamespace()) + { + const QString ns(stringForNamespace(name.namespaceURI())); + const QString p(stringForPrefix(name.prefix())); + const QString l(stringForLocalName(name.localName())); + + return QChar::fromLatin1('{') + + ns + + QChar::fromLatin1('}') + + (p.isEmpty() ? l : p + QChar::fromLatin1(':') + l); + } + else + return stringForLocalName(name.localName()); + } +} + +QXmlName NamePool::fromClarkName(const QString &clarkName) +{ + if(clarkName.isEmpty()) + return QXmlName(); + + if(clarkName.at(0) == QLatin1Char('{')) + { + const int indexOfRight = clarkName.indexOf(QLatin1Char('}')); + const QString qName(clarkName.right((clarkName.length() - indexOfRight) - 1)); + + if(!XPathHelper::isQName(qName)) + return QXmlName(); + + QString localName; + QString prefix; + + XPathHelper::splitQName(qName, prefix, localName); + + return allocateQName(clarkName.mid(1, indexOfRight - 1), + localName, prefix); + } + else + { + if(QXmlName::isNCName(clarkName)) + return allocateQName(QString(), clarkName); + else + return QXmlName(); + } +} +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qnamepool_p.h b/src/xmlpatterns/utils/qnamepool_p.h new file mode 100644 index 0000000..f348d1b --- /dev/null +++ b/src/xmlpatterns/utils/qnamepool_p.h @@ -0,0 +1,556 @@ +/**************************************************************************** +** +** 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_NamePool_H +#define Patternist_NamePool_H + +#include <QHash> +#include <QReadLocker> +#include <QReadWriteLock> +#include <QSharedData> +#include <QString> +#include <QVector> +#include <QXmlName> + +#include <QtXmlPatterns/private/qprimitives_p.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Store names such as namespace bindings and QNames and allows them to + * be referenced in efficient ways. + * + * Once a string have been inserted it stays there and cannot be removed. The + * only way to deallocate any string in the NamePool is to deallocate the + * NamePool itself, as a whole. + * + * This class is not only reentrant, it is thread-safe in all sense of the + * word. All functions of this class can be called concurrently. This is + * achieved by internal locking. + * + * @author Frans Englich <fenglich@trolltech.com> + * @todo Use QSubStrings, we can save very many heap allocations by that. + * @todo Check limits + */ + class Q_AUTOTEST_EXPORT NamePool : public QSharedData + { + public: + typedef QExplicitlySharedDataPointer<NamePool> Ptr; + + private: + friend class StandardNamespaces; + + enum + { + NoSuchValue = -1, + /** + * This must be identical to the amount of members in + * StandardNamespaces. + */ + StandardNamespaceCount = 11, + StandardPrefixCount = 9, + StandardLocalNameCount = 141 + }; + + QVector<QString> m_prefixes; + QVector<QString> m_namespaces; + QVector<QString> m_localNames; + + /** + * This hash contains no essential data, but speeds up + * finding a prefix in m_prefixes by mapping a prefix(the key) to + * the index into m_prefixes(which the value is). + * + * In other words, one can skip this variable at the cost of having + * to linearly loop over prefixes, in order to find the entry. + */ + QHash<QString, QXmlName::PrefixCode> m_prefixMapping; + + /** + * Same as m_prefixMapping but applies for URIs, and hence m_namespaces instead + * of m_prefixes. + */ + QHash<QString, QXmlName::NamespaceCode> m_namespaceMapping; + + QHash<QString, QXmlName::LocalNameCode> m_localNameMapping; + + enum DefaultCapacities + { + DefaultPrefixCapacity = 10, + DefaultURICapacity = DefaultPrefixCapacity, + /** + * It looks like it's quite common with 40-60 different local names per XML + * vocabulary. For background, see: + * + * - http://englich.wordpress.com/2007/01/11/representing-xml/ + * - http://englich.wordpress.com/2007/01/09/xmlstat/ + */ + DefaultLocalNameCapacity = 60 + }; + + public: + NamePool(); + + /** + * @short Allocates a namespace binding for @p prefix and @p uri. + * + * In the returned QXmlName, the local name is + * StandardLocalNames::empty, and QXmlName::prefix() and + * QXmlName::namespaceUri() returns @p prefix and @p uri, respectively. + * + * In older versions of this code, the class NamespaceBinding existed, + * but as part of having the public class QXmlName, it was dropped and + * a special interpretation/convention involving use of QXmlName was + * adopted. + */ + QXmlName allocateBinding(const QString &prefix, const QString &uri); + + QXmlName allocateQName(const QString &uri, const QString &localName, const QString &prefix = QString()); + + inline QXmlName allocateQName(const QXmlName::NamespaceCode uri, const QString &ln) + { + /* We don't lock here, but we do in allocateLocalName(). */ + return QXmlName(uri, allocateLocalName(ln)); + } + + inline const QString &stringForLocalName(const QXmlName::LocalNameCode code) const + { + const QReadLocker l(&lock); + return m_localNames.at(code); + } + + inline const QString &stringForPrefix(const QXmlName::PrefixCode code) const + { + const QReadLocker l(&lock); + return m_prefixes.at(code); + } + + inline const QString &stringForNamespace(const QXmlName::NamespaceCode code) const + { + const QReadLocker l(&lock); + return m_namespaces.at(code); + } + + QString displayName(const QXmlName qName) const; + + inline QString toLexical(const QXmlName qName) const + { + const QReadLocker l(&lock); + Q_ASSERT_X(!qName.isNull(), "", "It makes no sense to call toLexical() on a null name."); + + if(qName.hasPrefix()) + { + const QString &p = m_prefixes.at(qName.prefix()); + return p + QLatin1Char(':') + m_localNames.at(qName.localName()); + } + else + return m_localNames.at(qName.localName()); + } + + inline QXmlName::NamespaceCode allocateNamespace(const QString &uri) + { + const QWriteLocker l(&lock); + return unlockedAllocateNamespace(uri); + } + + inline QXmlName::LocalNameCode allocateLocalName(const QString &ln) + { + const QWriteLocker l(&lock); + return unlockedAllocateLocalName(ln); + } + + inline QXmlName::PrefixCode allocatePrefix(const QString &prefix) + { + const QWriteLocker l(&lock); + return unlockedAllocatePrefix(prefix); + } + + QString toClarkName(const QXmlName &name) const; + QXmlName fromClarkName(const QString &clarkName); + + private: + /** + * @note This function can not be called concurrently. + */ + QXmlName::NamespaceCode unlockedAllocateNamespace(const QString &uri); + + /** + * @note This function can not be called concurrently. + */ + QXmlName::LocalNameCode unlockedAllocateLocalName(const QString &ln); + + /** + * It's assumed that @p prefix is a valid @c NCName. + * + * @note This function can not be called concurrently. + */ + QXmlName::PrefixCode unlockedAllocatePrefix(const QString &prefix); + + Q_DISABLE_COPY(NamePool) + + /** + * @note This function can not be called concurrently. + */ + const QString &displayPrefix(const QXmlName::NamespaceCode nc) const; + + mutable QReadWriteLock lock; + }; + + /** + * @short Formats QName. + * + * @relates QXmlName + */ + static inline QString formatKeyword(const NamePool::Ptr &np, const QXmlName name) + { + return QLatin1String("<span class='XQuery-keyword'>") + + escape(np->displayName(name)) + + QLatin1String("</span>"); + } + + /** + * @see NamespaceResolver::Constants + */ + class StandardNamespaces + { + public: + enum ID + { + /** + * This does not mean empty in the sense of "empty", but + * in the sense of an empty string, "". + * + * Its value, zero, is significant. + */ + empty = 0, + fn, + local, + xml, + xmlns, + xs, + xsi, + xslt, + /** + * @short A special value that when passed as the namespace part + * to NamespaceResolver::addBinding(), undeclares the prefix. + * + * This is used by the namespace prolog declaration. + * + * A dummy value is added to the name pool. + */ + UndeclarePrefix, + + /** + * Signals that a node shouldn't inherit namespaces from its parent. Must be used + * with StandardPrefixes::StopNamespaceInheritance. + */ + StopNamespaceInheritance, + + /** + * A namespace used to identify for instance @c \#all template + * mode in XSL-T. + */ + InternalXSLT + }; + }; + + // const QString * a = &*qset.insert("foo"); + class StandardLocalNames + { + public: + enum + { + abs, + adjust_dateTime_to_timezone, + adjust_date_to_timezone, + adjust_time_to_timezone, + all, + arity, + avg, + base, + base_uri, + boolean, + ceiling, + codepoint_equal, + codepoints_to_string, + collection, + compare, + concat, + contains, + count, + current, + current_date, + current_dateTime, + current_time, + data, + dateTime, + day_from_date, + day_from_dateTime, + days_from_duration, + deep_equal, + Default, + default_collation, + distinct_values, + doc, + doc_available, + document, + document_uri, + element_available, + empty, + encode_for_uri, + ends_with, + error, + escape_html_uri, + exactly_one, + exists, + False, + floor, + function_available, + function_name, + generate_id, + generic_string_join, + hours_from_dateTime, + hours_from_duration, + hours_from_time, + id, + idref, + implicit_timezone, + index_of, + in_scope_prefixes, + insert_before, + iri_to_uri, + is_schema_aware, + key, + lang, + last, + local_name, + local_name_from_QName, + lower_case, + matches, + max, + min, + minutes_from_dateTime, + minutes_from_duration, + minutes_from_time, + month_from_date, + month_from_dateTime, + months_from_duration, + name, + namespace_uri, + namespace_uri_for_prefix, + namespace_uri_from_QName, + nilled, + node_name, + normalize_space, + normalize_unicode, + Not, + number, + one_or_more, + position, + prefix_from_QName, + product_name, + product_version, + property_name, + QName, + remove, + replace, + resolve_QName, + resolve_uri, + reverse, + root, + round, + round_half_to_even, + seconds_from_dateTime, + seconds_from_duration, + seconds_from_time, + sourceValue, + starts_with, + static_base_uri, + string, + string_join, + string_length, + string_to_codepoints, + subsequence, + substring, + substring_after, + substring_before, + sum, + supports_backwards_compatibility, + supports_serialization, + system_property, + timezone_from_date, + timezone_from_dateTime, + timezone_from_time, + tokenize, + trace, + translate, + True, + type_available, + unordered, + unparsed_entity_public_id, + unparsed_entity_uri, + unparsed_text, + unparsed_text_available, + upper_case, + vendor, + vendor_url, + version, + xml, + xmlns, + year_from_date, + year_from_dateTime, + years_from_duration, + zero_or_one + }; + }; + + class StandardPrefixes + { + public: + enum + { + /** + * This does not mean empty in the sense of "empty", but + * in the sense of an empty string, "". + * + * Its value, zero, is significant. + */ + empty = 0, + fn, + local, + xml, + xmlns, + xs, + xsi, + ns0, + StopNamespaceInheritance + }; + }; +} + +inline QXmlName::LocalNameCode QXmlName::localName() const +{ + return (m_qNameCode & LocalNameMask) >> LocalNameOffset; +} + +inline QXmlName::PrefixCode QXmlName::prefix() const +{ + return (m_qNameCode & PrefixMask) >> PrefixOffset; +} + +inline bool QXmlName::hasPrefix() const +{ + return prefix() != 0; +} + +inline bool QXmlName::hasNamespace() const +{ + return namespaceURI() != 0; +} + +inline QXmlName::NamespaceCode QXmlName::namespaceURI() const +{ + return (m_qNameCode & NamespaceMask) >> NamespaceOffset; +} + +inline bool QXmlName::isLexicallyEqual(const QXmlName &other) const +{ + return (m_qNameCode & LexicalQNameMask) == (other.m_qNameCode & LexicalQNameMask); +} + +inline void QXmlName::setPrefix(const PrefixCode c) +{ + m_qNameCode |= (c << PrefixOffset); +} + +inline void QXmlName::setNamespaceURI(const NamespaceCode c) +{ + m_qNameCode |= (c << NamespaceOffset); +} + +inline void QXmlName::setLocalName(const LocalNameCode c) +{ + m_qNameCode |= (c << LocalNameOffset); +} + +inline QXmlName::Code QXmlName::code() const +{ + return m_qNameCode; +} + +inline QXmlName::QXmlName(const NamespaceCode uri, + const LocalNameCode ln, + const PrefixCode p) : m_qNameCode((uri << NamespaceOffset) + + (ln << LocalNameOffset) + + (p << PrefixOffset)) +{ + /* We can't use members like prefix() here because if one of the + * values are to large, they would overflow into the others. */ + Q_ASSERT_X(p <= MaximumPrefixes, "", + qPrintable(QString::fromLatin1("NamePool prefix limits: max is %1, therefore %2 exceeds.").arg(MaximumPrefixes).arg(p))); + Q_ASSERT_X(ln <= MaximumLocalNames, "", + qPrintable(QString::fromLatin1("NamePool local name limits: max is %1, therefore %2 exceeds.").arg(MaximumLocalNames).arg(ln))); + Q_ASSERT_X(uri <= MaximumNamespaces, "", + qPrintable(QString::fromLatin1("NamePool namespace limits: max is %1, therefore %2 exceeds.").arg(MaximumNamespaces).arg(uri))); +} + + +Q_DECLARE_TYPEINFO(QPatternist::NamePool::Ptr, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qnamespacebinding_p.h b/src/xmlpatterns/utils/qnamespacebinding_p.h new file mode 100644 index 0000000..7fa3a6d --- /dev/null +++ b/src/xmlpatterns/utils/qnamespacebinding_p.h @@ -0,0 +1,143 @@ +/**************************************************************************** +** +** 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_NamespaceBinding_H +#define Patternist_NamespaceBinding_H + +template<typename T> class QVector; + +#include "qxmlname.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Represents a namespace binding: a prefix, and a namespace URI. + * + * @author Frans Englich <fenglich@trolltech.com> + */ + class NamespaceBinding + { + public: + enum + { + InvalidCode = -1 + }; + + typedef QVector<NamespaceBinding> Vector; + + inline NamespaceBinding() : m_prefix(InvalidCode), + m_namespace(InvalidCode) + { + } + + inline NamespaceBinding(const QXmlName::PrefixCode p, + const QXmlName::NamespaceCode n) : m_prefix(p), + m_namespace(n) + { + } + + inline bool operator==(const NamespaceBinding &other) const + { + return m_prefix == other.m_prefix && + m_namespace == other.m_namespace; + } + + inline QXmlName::PrefixCode prefix() const + { + return m_prefix; + } + + inline QXmlName::NamespaceCode namespaceURI() const + { + return m_namespace; + } + + inline bool isNull() const + { + return m_prefix == InvalidCode; + } + + /** + * @short Constructs a NamespaceBinding whose prefix and namespace is + * taken from @p qName. + * + * The local name in @p qName is ignored. @p qName may not be null. + */ + static inline NamespaceBinding fromQXmlName(const QXmlName qName) + { + Q_ASSERT(!qName.isNull()); + return NamespaceBinding(qName.prefix(), qName.namespaceURI()); + } + + private: + QXmlName::PrefixCode m_prefix; + QXmlName::NamespaceCode m_namespace; + }; + + /** + * @relates NamespaceBinding + */ + static inline uint qHash(const NamespaceBinding nb) + { + return (nb.prefix() << 16) + nb.namespaceURI(); + } + +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qnamespaceresolver.cpp b/src/xmlpatterns/utils/qnamespaceresolver.cpp new file mode 100644 index 0000000..0c837d0 --- /dev/null +++ b/src/xmlpatterns/utils/qnamespaceresolver.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qnamespaceresolver_p.h" +#include "qnamepool_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +NamespaceResolver::NamespaceResolver() +{ +} + +NamespaceResolver::~NamespaceResolver() +{ +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qnamespaceresolver_p.h b/src/xmlpatterns/utils/qnamespaceresolver_p.h new file mode 100644 index 0000000..d76f709 --- /dev/null +++ b/src/xmlpatterns/utils/qnamespaceresolver_p.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** 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_NamespaceResolver_H +#define Patternist_NamespaceResolver_H + +#include <QSharedData> +#include <QXmlName> + + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +template<typename A, typename B> class QHash; + +namespace QPatternist +{ + + /** + * @short Base class for namespace resolvers. + * + * @ingroup Patternist + * @author Frans Englich <fenglich@trolltech.com> + */ + class NamespaceResolver : public QSharedData + { + public: + enum Constants + { + NoBinding = -1 + }; + + typedef QExplicitlySharedDataPointer<NamespaceResolver> Ptr; + + /** + * A list of namespace bindings. The key is the prefix, and the value is + * the namespace URI. + */ + typedef QHash<QXmlName::PrefixCode, QXmlName::NamespaceCode> Bindings; + + NamespaceResolver(); + virtual ~NamespaceResolver(); + + /** + * Adds the mapping from @p prefix to @p namespaceURI to + * this NamespaceResolver. If this NamespaceResolver already contains + * a binding involving @p prefix, the old binding is replaced. + */ + virtual void addBinding(const QXmlName nb) = 0; + + /** + * Resolves the @p prefix to the corresponding namespace URI. If no binding + * exists for @p prefix, NoBinding is returned. + * + * @returns the namespace corresponding to @p prefix. + */ + virtual QXmlName::NamespaceCode lookupNamespaceURI(const QXmlName::PrefixCode prefix) const = 0; + + /** + * @returns all bindings this NamespaceResolver handles. + */ + virtual Bindings bindings() const = 0; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qnodenamespaceresolver.cpp b/src/xmlpatterns/utils/qnodenamespaceresolver.cpp new file mode 100644 index 0000000..c694ad4 --- /dev/null +++ b/src/xmlpatterns/utils/qnodenamespaceresolver.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qitem_p.h" +#include "qnamepool_p.h" + +#include "qnodenamespaceresolver_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +NodeNamespaceResolver::NodeNamespaceResolver(const Item &item) : m_node(item.asNode()) +{ + Q_ASSERT(!m_node.isNull()); +} + +void NodeNamespaceResolver::addBinding(const QXmlName nb) +{ + Q_UNUSED(nb); + Q_ASSERT_X(false, Q_FUNC_INFO, "Calling this function for this sub-class makes little sense."); +} + +QXmlName::NamespaceCode NodeNamespaceResolver::lookupNamespaceURI(const QXmlName::PrefixCode prefix) const +{ + const QXmlName::NamespaceCode ns = m_node.namespaceForPrefix(prefix); + + if(ns == NoBinding) + { + if(prefix == StandardPrefixes::empty) + return StandardNamespaces::empty; + else + return NoBinding; + } + else + return ns; +} + +NamespaceResolver::Bindings NodeNamespaceResolver::bindings() const +{ + Q_ASSERT_X(false, Q_FUNC_INFO, "Not implemented."); + return NamespaceResolver::Bindings(); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qnodenamespaceresolver_p.h b/src/xmlpatterns/utils/qnodenamespaceresolver_p.h new file mode 100644 index 0000000..123cfbe --- /dev/null +++ b/src/xmlpatterns/utils/qnodenamespaceresolver_p.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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_NodeNamespaceResolver_H +#define Patternist_NodeNamespaceResolver_H + +#include <QHash> + +#include "qnamespaceresolver_p.h" +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short A NamespaceResolver that use a QXmlNodeModelIndex's in-scope namespace + * bindings for resolving namespaces. + * + * @ingroup Patternist + * @author Frans Englich <fenglich@trolltech.com> + */ + class NodeNamespaceResolver : public NamespaceResolver + { + public: + NodeNamespaceResolver(const Item &item); + + virtual void addBinding(const QXmlName nb); + virtual QXmlName::NamespaceCode lookupNamespaceURI(const QXmlName::PrefixCode prefix) const; + virtual Bindings bindings() const; + + private: + const QXmlNodeModelIndex m_node; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qoutputvalidator.cpp b/src/xmlpatterns/utils/qoutputvalidator.cpp new file mode 100644 index 0000000..51e8063 --- /dev/null +++ b/src/xmlpatterns/utils/qoutputvalidator.cpp @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qpatternistlocale_p.h" + +#include "qoutputvalidator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +OutputValidator::OutputValidator(QAbstractXmlReceiver *const receiver, + const DynamicContext::Ptr &context, + const SourceLocationReflection *const r, + const bool isXSLT) : DelegatingSourceLocationReflection(r) + , m_hasReceivedChildren(false) + , m_receiver(receiver) + , m_context(context) + , m_isXSLT(isXSLT) +{ + Q_ASSERT(receiver); + Q_ASSERT(context); +} + +void OutputValidator::namespaceBinding(const QXmlName &nb) +{ + m_receiver->namespaceBinding(nb); +} + +void OutputValidator::startElement(const QXmlName &name) +{ + m_hasReceivedChildren = false; + m_receiver->startElement(name); + m_attributes.clear(); +} + +void OutputValidator::endElement() +{ + m_hasReceivedChildren = true; + m_receiver->endElement(); +} + +void OutputValidator::attribute(const QXmlName &name, + const QStringRef &value) +{ + if(m_hasReceivedChildren) + { + m_context->error(QtXmlPatterns::tr("It's not possible to add attributes after any other kind of node."), + m_isXSLT ? ReportContext::XTDE0410 : ReportContext::XQTY0024, this); + } + else + { + if(!m_isXSLT && m_attributes.contains(name)) + { + m_context->error(QtXmlPatterns::tr("An attribute by name %1 has already been created.").arg(formatKeyword(m_context->namePool(), name)), + ReportContext::XQDY0025, this); + } + else + { + m_attributes.insert(name); + m_receiver->attribute(name, value); + } + } +} + +void OutputValidator::comment(const QString &value) +{ + m_hasReceivedChildren = true; + m_receiver->comment(value); +} + +void OutputValidator::characters(const QStringRef &value) +{ + m_hasReceivedChildren = true; + m_receiver->characters(value); +} + +void OutputValidator::processingInstruction(const QXmlName &name, + const QString &value) +{ + m_hasReceivedChildren = true; + m_receiver->processingInstruction(name, value); +} + +void OutputValidator::item(const Item &outputItem) +{ + /* We can't send outputItem directly to m_receiver since its item() function + * won't dispatch to this OutputValidator, but to itself. We're not sub-classing here, + * we're delegating. */ + + if(outputItem.isNode()) + sendAsNode(outputItem); + else + { + m_hasReceivedChildren = true; + m_receiver->item(outputItem); + } +} + +void OutputValidator::startDocument() +{ + m_receiver->startDocument(); +} + +void OutputValidator::endDocument() +{ + m_receiver->endDocument(); +} + +void OutputValidator::atomicValue(const QVariant &value) +{ + Q_UNUSED(value); + // TODO +} + +void OutputValidator::endOfSequence() +{ +} + +void OutputValidator::startOfSequence() +{ +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qoutputvalidator_p.h b/src/xmlpatterns/utils/qoutputvalidator_p.h new file mode 100644 index 0000000..15734ff --- /dev/null +++ b/src/xmlpatterns/utils/qoutputvalidator_p.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** 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_OutputValidator_H +#define Patternist_OutputValidator_H + +#include <QSet> + +#include "qdynamiccontext_p.h" +#include "qabstractxmlreceiver.h" +#include "qsourcelocationreflection_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Receives QAbstractXmlReceiver events and validates that they are correct, + * before sending them on to a second QAbstractXmlReceiver. + * + * Currently, this is only checking that attributes appear before other + * nodes. + * + * @ingroup Patternist_xdm + * @author Frans Englich <fenglich@trolltech.com> + * @todo Escape data + */ + class OutputValidator : public QAbstractXmlReceiver + , public DelegatingSourceLocationReflection + { + public: + OutputValidator(QAbstractXmlReceiver *const receiver, + const DynamicContext::Ptr &context, + const SourceLocationReflection *const r, + const bool isXSLT); + + virtual void namespaceBinding(const QXmlName &nb); + + virtual void characters(const QStringRef &value); + virtual void comment(const QString &value); + + virtual void startElement(const QXmlName &name); + + virtual void endElement(); + + virtual void attribute(const QXmlName &name, + const QStringRef &value); + + virtual void processingInstruction(const QXmlName &name, + const QString &value); + + virtual void item(const Item &item); + + virtual void startDocument(); + virtual void endDocument(); + virtual void atomicValue(const QVariant &value); + virtual void endOfSequence(); + virtual void startOfSequence(); + + private: + bool m_hasReceivedChildren; + QAbstractXmlReceiver *const m_receiver; + const DynamicContext::Ptr m_context; + + /** + * Keeps the current received attributes, in order to check uniqueness. + */ + QSet<QXmlName> m_attributes; + const bool m_isXSLT; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/qpatternistlocale.cpp b/src/xmlpatterns/utils/qpatternistlocale.cpp new file mode 100644 index 0000000..990867d --- /dev/null +++ b/src/xmlpatterns/utils/qpatternistlocale.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qpatternistlocale_p.h" + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + QString escape(const QString &input) + { + QString rich; + rich.reserve(int(input.length() * 1.1)); + + for(int i = 0; i < input.length(); ++i) + { + switch(input.at(i).unicode()) + { + case '<': + { + rich += QLatin1String("<"); + break; + } + case '>': + { + rich += QLatin1String(">"); + break; + } + case '&': + { + rich += QLatin1String("&"); + break; + } + case '"': + { + rich += QLatin1String("""); + break; + } + case '\'': + { + rich += QLatin1String("'"); + break; + } + default: + rich += input.at(i); + } + } + + return rich; + } +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qpatternistlocale_p.h b/src/xmlpatterns/utils/qpatternistlocale_p.h new file mode 100644 index 0000000..e3f645f --- /dev/null +++ b/src/xmlpatterns/utils/qpatternistlocale_p.h @@ -0,0 +1,273 @@ +/**************************************************************************** +** +** 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_Locale_H +#define Patternist_Locale_H + +#include <QCoreApplication> +#include <QString> +#include <QUrl> + +#include "qcardinality_p.h" +#include "qnamepool_p.h" +#include "qprimitives_p.h" + +QT_BEGIN_NAMESPACE + +/** + * @file + * @short Contains functions used for formatting arguments, such as keywords and paths, + * in translated strings. + * + * This file was originally called qpatternistlocale_p.h. However, it broke build on MS + * Windows, because it override the locale.h system header. + */ + +namespace QPatternist +{ + /** + * @short Provides a translation context & functions for the QtXmlPatterns + * module. + * + * This class is not supposed to be instantiated. + */ + class QtXmlPatterns + { + public: + Q_DECLARE_TR_FUNCTIONS(QtXmlPatterns) + + private: + /** + * No implementation is provided, this class is not supposed to be + * instantiated. + */ + inline QtXmlPatterns(); + Q_DISABLE_COPY(QtXmlPatterns) + }; + + static inline QString formatKeyword(const QString &keyword) + { + return QLatin1String("<span class='XQuery-keyword'>") + + escape(keyword) + + QLatin1String("</span>"); + } + + /** + * @overload + */ + static inline QString formatKeyword(const QStringRef &keyword) + { + return formatKeyword(keyword.toString()); + } + + static inline QString formatKeyword(const char *const keyword) + { + return formatKeyword(QLatin1String(keyword)); + } + + static inline QString formatKeyword(const QChar keyword) + { + return formatKeyword(QString(keyword)); + } + + /** + * @short Formats element name. + */ + static inline QString formatElement(const QString &element) + { + // for the moment we forward to formatKeyword, that will change later + return formatKeyword(element); + } + + /** + * @overload + */ + static inline QString formatElement(const char *const element) + { + return formatElement(QLatin1String(element)); + } + + /** + * @short Formats attribute name. + */ + static inline QString formatAttribute(const QString &attribute) + { + // for the moment we forward to formatKeyword, that will change later + return formatKeyword(attribute); + } + + /** + * @overload + */ + static inline QString formatAttribute(const char *const attribute) + { + return formatAttribute(QLatin1String(attribute)); + } + + /** + * @short Formats ItemType and SequenceType. + * + * This function is not declared static, because the compiler on target + * aix-xlc-64 won't accept it. + */ + template<typename T> + inline QString formatType(const NamePool::Ptr &np, const T &type) + { + Q_ASSERT(type); + return QLatin1String("<span class='XQuery-type'>") + + escape(type->displayName(np)) + + QLatin1String("</span>"); + } + + /** + * @short Formats Cardinality. + */ + static inline QString formatType(const Cardinality &type) + { + return QLatin1String("<span class='XQuery-type'>") + + escape(type.displayName(Cardinality::IncludeExplanation)) + + QLatin1String("</span>"); + } + + /** + * @short Formats @p uri as a path to a resource, typically it's a filename + * or a URI. + */ + static inline QString formatResourcePath(const QUrl &uri) + { + const QString normalizedURI(escape(uri.toString(QUrl::RemovePassword))); + + return QLatin1String("<span class='XQuery-filepath'><a href='") + + normalizedURI + + QLatin1String("'>") + + normalizedURI + + QLatin1String("</a></span>"); + } + + /** + * @short Formats @p uri for display. + * + * @note It's not guaranteed that URIs being formatted are valid. That can + * be an arbitrary string. + */ + static inline QString formatURI(const QUrl &uri) + { + return QLatin1String("<span class='XQuery-uri'>") + + escape(uri.toString(QUrl::RemovePassword)) + + QLatin1String("</span>"); + } + + /** + * @short Formats @p uri, that's considered to be a URI, for display. + * + * @p uri does not have to be a valid QUrl or valid instance of @c + * xs:anyURI. + */ + static inline QString formatURI(const QString &uri) + { + const QUrl realURI(uri); + return formatURI(realURI); + } + + static inline QString formatData(const QString &data) + { + return QLatin1String("<span class='XQuery-data'>") + + escape(data) + + QLatin1String("</span>"); + } + + /** + * This is an overload, provided for convenience. + */ + static inline QString formatData(const xsInteger data) + { + return formatData(QString::number(data)); + } + + /** + * This is an overload, provided for convenience. + */ + static inline QString formatData(const char *const data) + { + return formatData(QLatin1String(data)); + } + + /** + * This is an overload, provided for convenience. + */ + static inline QString formatData(const QLatin1Char &data) + { + return formatData(QString(data)); + } + + /** + * Formats an arbitrary expression, such as a regular expression + * or XQuery query. + */ + static inline QString formatExpression(const QString &expr) + { + return QLatin1String("<span class='XQuery-expression'>") + + escape(expr) + + QLatin1String("</span>"); + } + +} + +#ifdef Q_NO_TYPESAFE_FLAGS +#error "Patternist does not compile with Q_NO_TYPESAFE_FLAGS set, because the code uses negative enum values. qglobal.h has typedef uint Flags." +#endif + +#ifdef QT_NO_EXCEPTIONS +#error "Patternist uses exceptions and cannot be built without." +#endif + +QT_END_NAMESPACE +#endif diff --git a/src/xmlpatterns/utils/qxpathhelper.cpp b/src/xmlpatterns/utils/qxpathhelper.cpp new file mode 100644 index 0000000..127e21f --- /dev/null +++ b/src/xmlpatterns/utils/qxpathhelper.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include <QStringList> + +#include "private/qxmlutils_p.h" +#include "qbuiltintypes_p.h" +#include "qcommonvalues_p.h" +#include "qnamepool_p.h" + +#include "qxpathhelper_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +bool XPathHelper::isReservedNamespace(const QXmlName::NamespaceCode ns) +{ + /* The order is because of that XFN and WXS are the most common. */ + return ns == StandardNamespaces::fn || + ns == StandardNamespaces::xs || + ns == StandardNamespaces::xml || + ns == StandardNamespaces::xsi; +} + +bool XPathHelper::isQName(const QString &qName) +{ + const QStringList result(qName.split(QLatin1Char(':'))); + const int c = result.count(); + + if(c == 2) + { + return QXmlUtils::isNCName(result.first()) && + QXmlUtils::isNCName(result.last()); + } + else if(c == 1) + return QXmlUtils::isNCName(result.first()); + else + return false; +} + +void XPathHelper::splitQName(const QString &qName, QString &prefix, QString &ncName) +{ + Q_ASSERT_X(isQName(qName), Q_FUNC_INFO, + "qName must be a valid QName."); + + const QStringList result(qName.split(QLatin1Char(':'))); + + if(result.count() == 1) + { + Q_ASSERT(QXmlUtils::isNCName(result.first())); + ncName = result.first(); + } + else + { + Q_ASSERT(result.count() == 2); + Q_ASSERT(QXmlUtils::isNCName(result.first())); + Q_ASSERT(QXmlUtils::isNCName(result.last())); + + prefix = result.first(); + ncName = result.last(); + } +} + +ItemType::Ptr XPathHelper::typeFromKind(const QXmlNodeModelIndex::NodeKind nodeKind) +{ + switch(nodeKind) + { + case QXmlNodeModelIndex::Element: + return BuiltinTypes::element; + case QXmlNodeModelIndex::Attribute: + return BuiltinTypes::attribute; + case QXmlNodeModelIndex::Text: + return BuiltinTypes::text; + case QXmlNodeModelIndex::ProcessingInstruction: + return BuiltinTypes::pi; + case QXmlNodeModelIndex::Comment: + return BuiltinTypes::comment; + case QXmlNodeModelIndex::Document: + return BuiltinTypes::document; + default: + { + Q_ASSERT_X(false, Q_FUNC_INFO, + "A node type that doesn't exist in the XPath Data Model was encountered."); + return ItemType::Ptr(); /* Dummy, silence compiler warning. */ + } + } +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/utils/qxpathhelper_p.h b/src/xmlpatterns/utils/qxpathhelper_p.h new file mode 100644 index 0000000..7bf33e6 --- /dev/null +++ b/src/xmlpatterns/utils/qxpathhelper_p.h @@ -0,0 +1,174 @@ +/**************************************************************************** +** +** 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_XPathHelper_H +#define Patternist_XPathHelper_H + +#include "qcommonnamespaces_p.h" +#include "qitem_p.h" +#include "qpatternistlocale_p.h" +#include "qreportcontext_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Contains helper and utility functions. + * + * The common denominator of its functions is that they do not fit in + * well elsewhere, such as in a particular class. It is preferred if XPathHelper + * goes away, and that functions are in more specific classes. + * + * @ingroup Patternist + * @author Frans Englich <fenglich@trolltech.com> + */ + class XPathHelper + { + public: + /** + * Determines whether @p qName is a valid QName. For example, "body" and "xhtml:body" + * is, but "xhtml::body" or "x:body "(note the whitespace) is not. + * + * @see QNameConstructor::expandQName() + * @see QNameValue + */ + static bool isQName(const QString &qName); + + /** + * @short Splits @p qName into @p localName and @p prefix. + * + * @note @p qName must be a valid QName, and that is not checked. + */ + static void splitQName(const QString &qName, QString &prefix, QString &localName); + + /** + * Determines whether @p ns is a reserved namespace. + * + * @see <a href="http://www.w3.org/TR/xslt20/#reserved-namespaces">XSL Transformations + * (XSLT) Version 2.0, 3.2 Reserved Namespaces</a> + * @see <a href="http://www.w3.org/TR/xquery/#FunctionDeclns">XQuery 1.0: An XML + * Query Language, 4.15 Function Declaration</a> + * @returns @c true if @p ns is a reserved namespace, otherwise @c false. + */ + static bool isReservedNamespace(const QXmlName::NamespaceCode ns); + + /** + * Determines whether @p collation is a supported string collation. If it is + * not, error code @p code is raised via @p context. + */ + template<const ReportContext::ErrorCode code, typename TReportContext> + static inline void checkCollationSupport(const QString &collation, + const TReportContext &context, + const SourceLocationReflection *const r) + { + Q_ASSERT(context); + Q_ASSERT(r); + + if(collation != QLatin1String(CommonNamespaces::UNICODE_COLLATION)) + { + context->error(QtXmlPatterns::tr("Only the Unicode Codepoint " + "Collation is supported(%1). %2 is unsupported.") + .arg(formatURI(QLatin1String(CommonNamespaces::UNICODE_COLLATION))) + .arg(formatURI(collation)), + code, r); + } + } + + static QPatternist::ItemTypePtr typeFromKind(const QXmlNodeModelIndex::NodeKind nodeKind); + + /** + * @short Determines whether @p consists only of whitespace. Characters + * considered whitespace are the ones for which QChar::isSpace() returns @c true for. + * + * For the empty string, @c true is returned. + * + * @returns @c true if @p string consists only of whitespace, otherwise @c false. + */ + static inline bool isWhitespaceOnly(const QStringRef &string) + { + const int len = string.length(); + + for(int i = 0; i < len; ++i) + { + if(!string.at(i).isSpace()) // TODO and this is wrong, see sdk/TODO + return false; + } + + return true; + } + + /** + * @overload + */ + static inline bool isWhitespaceOnly(const QString &string) + { + return isWhitespaceOnly(QStringRef(&string)); + } + + private: + /** + * @short This default constructor has no definition, in order to avoid + * instantiation, since it makes no sense to instantiate this class. + */ + inline XPathHelper(); + + Q_DISABLE_COPY(XPathHelper) + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/utils/utils.pri b/src/xmlpatterns/utils/utils.pri new file mode 100644 index 0000000..2db91c2 --- /dev/null +++ b/src/xmlpatterns/utils/utils.pri @@ -0,0 +1,21 @@ +HEADERS += $$PWD/qautoptr_p.h \ + $$PWD/qcommonnamespaces_p.h \ + $$PWD/qcppcastinghelper_p.h \ + $$PWD/qdebug_p.h \ + $$PWD/qgenericnamespaceresolver_p.h \ + $$PWD/qpatternistlocale_p.h \ + $$PWD/qnamepool_p.h \ + $$PWD/qnamespaceresolver_p.h \ + $$PWD/qoutputvalidator_p.h \ + $$PWD/qxpathhelper_p.h \ + $$PWD/qdelegatingnamespaceresolver_p.h \ + $$PWD/qnodenamespaceresolver_p.h + +SOURCES += $$PWD/qgenericnamespaceresolver.cpp \ + $$PWD/qpatternistlocale.cpp \ + $$PWD/qnamepool.cpp \ + $$PWD/qnamespaceresolver.cpp \ + $$PWD/qoutputvalidator.cpp \ + $$PWD/qxpathhelper.cpp \ + $$PWD/qdelegatingnamespaceresolver.cpp \ + $$PWD/qnodenamespaceresolver.cpp |