| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_XPathHelper_H |
| 51 | #define Patternist_XPathHelper_H |
| 52 | |
| 53 | #include <private/qcommonnamespaces_p.h> |
| 54 | #include <private/qitem_p.h> |
| 55 | #include <private/qpatternistlocale_p.h> |
| 56 | #include <private/qreportcontext_p.h> |
| 57 | |
| 58 | QT_BEGIN_NAMESPACE |
| 59 | |
| 60 | namespace QPatternist |
| 61 | { |
| 62 | /** |
| 63 | * @short Contains helper and utility functions. |
| 64 | * |
| 65 | * The common denominator of its functions is that they do not fit in |
| 66 | * well elsewhere, such as in a particular class. It is preferred if XPathHelper |
| 67 | * goes away, and that functions are in more specific classes. |
| 68 | * |
| 69 | * @ingroup Patternist |
| 70 | * @author Frans Englich <frans.englich@nokia.com> |
| 71 | */ |
| 72 | class XPathHelper |
| 73 | { |
| 74 | public: |
| 75 | /** |
| 76 | * Determines whether @p qName is a valid QName. For example, "body" and "xhtml:body" |
| 77 | * is, but "xhtml::body" or "x:body "(note the whitespace) is not. |
| 78 | * |
| 79 | * @see QNameConstructor::expandQName() |
| 80 | * @see QNameValue |
| 81 | */ |
| 82 | static bool isQName(const QString &qName); |
| 83 | |
| 84 | /** |
| 85 | * @short Splits @p qName into @p localName and @p prefix. |
| 86 | * |
| 87 | * @note @p qName must be a valid QName, and that is not checked. |
| 88 | */ |
| 89 | static void splitQName(const QString &qName, QString &prefix, QString &localName); |
| 90 | |
| 91 | /** |
| 92 | * Determines whether @p ns is a reserved namespace. |
| 93 | * |
| 94 | * @see <a href="http://www.w3.org/TR/xslt20/#reserved-namespaces">XSL Transformations |
| 95 | * (XSLT) Version 2.0, 3.2 Reserved Namespaces</a> |
| 96 | * @see <a href="http://www.w3.org/TR/xquery/#FunctionDeclns">XQuery 1.0: An XML |
| 97 | * Query Language, 4.15 Function Declaration</a> |
| 98 | * @returns @c true if @p ns is a reserved namespace, otherwise @c false. |
| 99 | */ |
| 100 | static bool isReservedNamespace(const QXmlName::NamespaceCode ns); |
| 101 | |
| 102 | /** |
| 103 | * Determines whether @p collation is a supported string collation. If it is |
| 104 | * not, error code @p code is raised via @p context. |
| 105 | */ |
| 106 | template<const ReportContext::ErrorCode code, typename TReportContext> |
| 107 | static inline void checkCollationSupport(const QString &collation, |
| 108 | const TReportContext &context, |
| 109 | const SourceLocationReflection *const r) |
| 110 | { |
| 111 | Q_ASSERT(context); |
| 112 | Q_ASSERT(r); |
| 113 | |
| 114 | if(collation != QLatin1String(CommonNamespaces::UNICODE_COLLATION)) |
| 115 | { |
| 116 | context->error(QtXmlPatterns::tr(sourceText: "Only the Unicode Codepoint " |
| 117 | "Collation is supported(%1). %2 is unsupported." ) |
| 118 | .arg(a: formatURI(uri: QLatin1String(CommonNamespaces::UNICODE_COLLATION))) |
| 119 | .arg(a: formatURI(uri: collation)), |
| 120 | code, r); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static QPatternist::ItemTypePtr typeFromKind(const QXmlNodeModelIndex::NodeKind nodeKind); |
| 125 | |
| 126 | /** |
| 127 | * Normalizes an @p uri by resolving it to the application directory if empty. |
| 128 | */ |
| 129 | static QUrl normalizeQueryURI(const QUrl &uri); |
| 130 | |
| 131 | /** |
| 132 | * @short Determines whether @p consists only of whitespace. Characters |
| 133 | * considered whitespace are the ones for which QChar::isSpace() returns @c true for. |
| 134 | * |
| 135 | * For the empty string, @c true is returned. |
| 136 | * |
| 137 | * @returns @c true if @p string consists only of whitespace, otherwise @c false. |
| 138 | */ |
| 139 | static inline bool isWhitespaceOnly(const QStringRef &string) |
| 140 | { |
| 141 | const int len = string.length(); |
| 142 | |
| 143 | for(int i = 0; i < len; ++i) |
| 144 | { |
| 145 | if(!string.at(i).isSpace()) // TODO and this is wrong, see sdk/TODO |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @overload |
| 154 | */ |
| 155 | static inline bool isWhitespaceOnly(const QString &string) |
| 156 | { |
| 157 | return isWhitespaceOnly(string: QStringRef(&string)); |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | /** |
| 162 | * @short This default constructor has no definition, in order to avoid |
| 163 | * instantiation, since it makes no sense to instantiate this class. |
| 164 | */ |
| 165 | inline XPathHelper(); |
| 166 | |
| 167 | Q_DISABLE_COPY(XPathHelper) |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | QT_END_NAMESPACE |
| 172 | |
| 173 | #endif |
| 174 | |