| 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_AnyURI_H |
| 51 | #define Patternist_AnyURI_H |
| 52 | |
| 53 | #include <QUrl> |
| 54 | #include <QtDebug> |
| 55 | |
| 56 | #include <private/qatomicstring_p.h> |
| 57 | #include <private/qbuiltintypes_p.h> |
| 58 | #include <private/qpatternistlocale_p.h> |
| 59 | #include <private/qreportcontext_p.h> |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | namespace QPatternist |
| 64 | { |
| 65 | /** |
| 66 | * @short A value of type <tt>xs:anyURI</tt>. |
| 67 | * |
| 68 | * Due to bugs in QUrl and slight differences in behavior and |
| 69 | * interpretation, QUrl can never be used directly for dealing with URIs, |
| 70 | * values of type @c xs:anyURI. Therefore, it's important to use the |
| 71 | * functionality this class provides, such as the functions toQUrl(), |
| 72 | * fromLexical(), isValid(), and resolveURI(). |
| 73 | * |
| 74 | * @see QUrl |
| 75 | * @author Frans Englich <frans.englich@nokia.com> |
| 76 | * @ingroup Patternist_xdm |
| 77 | */ |
| 78 | class AnyURI : public AtomicString |
| 79 | { |
| 80 | public: |
| 81 | typedef QExplicitlySharedDataPointer<AnyURI> Ptr; |
| 82 | |
| 83 | /** |
| 84 | * Creates an instance representing @p value. |
| 85 | * |
| 86 | * @note @p value must be a valid @c xs:anyURI. If it is of interest |
| 87 | * to construct from a lexical representation, use fromLexical(). |
| 88 | */ |
| 89 | static AnyURI::Ptr fromValue(const QString &value); |
| 90 | |
| 91 | static AnyURI::Ptr fromValue(const QUrl &uri); |
| 92 | |
| 93 | /** |
| 94 | * @short Treates @p value as a lexical representation of @c xs:anyURI |
| 95 | * but returns the value instance as a QUrl. |
| 96 | * |
| 97 | * If @p value is not a valid lexical representation of @c xs:anyURI, |
| 98 | * an error is issued via @p context. |
| 99 | * |
| 100 | * If @p isValid is passed, no error is raised and it is instead set |
| 101 | * appropriately. |
| 102 | */ |
| 103 | template<const ReportContext::ErrorCode code, typename TReportContext> |
| 104 | static inline QUrl toQUrl(const QString &value, |
| 105 | const TReportContext &context, |
| 106 | const SourceLocationReflection *const r, |
| 107 | bool *const isValid = 0, |
| 108 | const bool issueError = true) |
| 109 | { |
| 110 | /* QUrl doesn't flag ":/..." so we workaround it. */ |
| 111 | const QString simplified(value.simplified()); |
| 112 | const QUrl uri(simplified, QUrl::StrictMode); |
| 113 | |
| 114 | if(uri.isEmpty() || (uri.isValid() && (!simplified.startsWith(c: QLatin1Char(':')) || !uri.isRelative()))) |
| 115 | { |
| 116 | if(isValid) |
| 117 | *isValid = true; |
| 118 | |
| 119 | return uri; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if(isValid) |
| 124 | *isValid = false; |
| 125 | |
| 126 | if(issueError) |
| 127 | { |
| 128 | context->error(QtXmlPatterns::tr(sourceText: "%1 is not a valid value of type %2." ).arg(formatURI(uri: value), formatType(context->namePool(), BuiltinTypes::xsAnyURI)), |
| 129 | code, r); |
| 130 | } |
| 131 | |
| 132 | return QUrl(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @short Return @c true if @p candidate is a valid @c xs:anyURI, |
| 138 | * otherwise @c false. |
| 139 | */ |
| 140 | static bool isValid(const QString &candidate); |
| 141 | |
| 142 | /** |
| 143 | * @short Constructs a @c xs:anyURI value from the lexical representation @p value. |
| 144 | * |
| 145 | * If @p value is not a valid lexical representation of @c xs:anyURI, |
| 146 | * an error is issued via @p context. |
| 147 | */ |
| 148 | template<const ReportContext::ErrorCode code, typename TReportContext> |
| 149 | static inline AnyURI::Ptr fromLexical(const QString &value, |
| 150 | const TReportContext &context, |
| 151 | const SourceLocationReflection *const r) |
| 152 | { |
| 153 | return AnyURI::Ptr(new AnyURI(toQUrl<code>(value, context, r).toString())); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * If @p value is not a valid lexical representation for @c xs:anyURI, |
| 158 | * a ValidationError is returned. |
| 159 | */ |
| 160 | static AnyURI::Ptr fromLexical(const QString &value); |
| 161 | |
| 162 | /** |
| 163 | * Creates an AnyURI instance representing an absolute URI which |
| 164 | * is created from resolving @p relative against @p base. |
| 165 | * |
| 166 | * This function must be compatible with the resolution semantics |
| 167 | * specified for fn:resolve-uri. In fact, the implementation of fn:resolve-uri, |
| 168 | * ResourceURIFN, relies on this function. |
| 169 | * |
| 170 | * @see <a href="http://www.faqs.org/rfcs/rfc3986.html">RFC 3986 - Uniform |
| 171 | * Resource Identifier (URI): Generic Syntax</a> |
| 172 | * @see <a href ="http://www.w3.org/TR/xpath-functions/#func-resolve-uri">XQuery 1.0 |
| 173 | * and XPath 2.0 Functions and Operators, 8.1 fn:resolve-uri</a> |
| 174 | */ |
| 175 | static AnyURI::Ptr resolveURI(const QString &relative, |
| 176 | const QString &base); |
| 177 | |
| 178 | virtual ItemType::Ptr type() const; |
| 179 | |
| 180 | /** |
| 181 | * @short Returns this @c xs:anyURI value in a QUrl. |
| 182 | */ |
| 183 | inline QUrl toQUrl() const |
| 184 | { |
| 185 | Q_ASSERT_X(QUrl(m_value).isValid(), Q_FUNC_INFO, |
| 186 | qPrintable(QString::fromLatin1("%1 is apparently not ok for QUrl." ).arg(m_value))); |
| 187 | return QUrl(m_value); |
| 188 | } |
| 189 | protected: |
| 190 | friend class CommonValues; |
| 191 | |
| 192 | AnyURI(const QString &value); |
| 193 | }; |
| 194 | |
| 195 | /** |
| 196 | * @short Formats @p uri, that's considered to be a URI, for display. |
| 197 | */ |
| 198 | static inline QString formatURI(const NamePool::Ptr &np, const QXmlName::NamespaceCode &uri) |
| 199 | { |
| 200 | return formatURI(uri: np->stringForNamespace(code: uri)); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | QT_END_NAMESPACE |
| 205 | |
| 206 | #endif |
| 207 | |