1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further 15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free 20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 22 ** following information to ensure the GNU Lesser General Public License 23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 25 ** 26 ** As a special exception, The Qt Company gives you certain additional 27 ** rights. These rights are described in The Qt Company LGPL Exception 28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 29 ** 30 ** GNU General Public License Usage 31 ** Alternatively, this file may be used under the terms of the GNU 32 ** General Public License version 3.0 as published by the Free Software 33 ** Foundation and appearing in the file LICENSE.GPL included in the 34 ** packaging of this file. Please review the following information to 35 ** ensure the GNU General Public License version 3.0 requirements will be 36 ** met: http://www.gnu.org/copyleft/gpl.html. 37 ** 38 ** $QT_END_LICENSE$ 39 ** 40 ****************************************************************************/ 41 42 // 43 // W A R N I N G 44 // ------------- 45 // 46 // This file is not part of the Qt API. It exists purely as an 47 // implementation detail. This header file may change from version to 48 // version without notice, or even be removed. 49 // 50 // We mean it. 51 52 #ifndef Patternist_AnyURI_H 53 #define Patternist_AnyURI_H 54 55 #include <QUrl> 56 #include <QtDebug> 57 58 #include "qatomicstring_p.h" 59 #include "qbuiltintypes_p.h" 60 #include "qpatternistlocale_p.h" 61 #include "qreportcontext_p.h" 62 63 QT_BEGIN_HEADER 64 65 QT_BEGIN_NAMESPACE 66 67 namespace QPatternist 68 { 69 /** 70 * @short A value of type <tt>xs:anyURI</tt>. 71 * 72 * Due to bugs in QUrl and slight differences in behavior and 73 * interpretation, QUrl can never be used directly for dealing with URIs, 74 * values of type @c xs:anyURI. Therefore, it's important to use the 75 * functionality this class provides, such as the functions toQUrl(), 76 * fromLexical(), isValid(), and resolveURI(). 77 * 78 * @see QUrl 79 * @author Frans Englich <frans.englich@nokia.com> 80 * @ingroup Patternist_xdm 81 */ 82 class AnyURI : public AtomicString 83 { 84 public: 85 typedef QExplicitlySharedDataPointer<AnyURI> Ptr; 86 87 /** 88 * Creates an instance representing @p value. 89 * 90 * @note @p value must be a valid @c xs:anyURI. If it is of interest 91 * to construct from a lexical representation, use fromLexical(). 92 */ 93 static AnyURI::Ptr fromValue(const QString &value); 94 95 static AnyURI::Ptr fromValue(const QUrl &uri); 96 97 /** 98 * @short Treates @p value as a lexical representation of @c xs:anyURI 99 * but returns the value instance as a QUrl. 100 * 101 * If @p value is not a valid lexical representation of @c xs:anyURI, 102 * an error is issued via @p context. 103 * 104 * If @p isValid is passed, no error is raised and it is instead set 105 * appropriately. 106 */ 107 template<const ReportContext::ErrorCode code, typename TReportContext> 108 static inline QUrl toQUrl(const QString &value, 109 const TReportContext &context, 110 const SourceLocationReflection *const r, 111 bool *const isValid = 0, 112 const bool issueError = true) 113 { 114 /* QUrl doesn't flag ":/..." so we workaround it. */ 115 const QString simplified(value.simplified()); 116 const QUrl uri(simplified, QUrl::StrictMode); 117 118 if(uri.isEmpty() || (uri.isValid() && (!simplified.startsWith(QLatin1Char(':')) || !uri.isRelative()))) 119 { 120 if(isValid) 121 *isValid = true; 122 123 return uri; 124 } 125 else 126 { 127 if(isValid) 128 *isValid = false; 129 130 if(issueError) 131 { 132 context->error(QtXmlPatterns::tr("%1 is not a valid value of type %2.").arg(formatURI(value), formatType(context->namePool(), BuiltinTypes::xsAnyURI)), 133 code, r); 134 } 135 136 return QUrl(); 137 } 138 } 139 140 /** 141 * @short Return @c true if @p candidate is a valid @c xs:anyURI, 142 * otherwise @c false. 143 */ 144 static bool isValid(const QString &candidate); 145 146 /** 147 * @short Constructs a @c xs:anyURI value from the lexical representation @p value. 148 * 149 * If @p value is not a valid lexical representation of @c xs:anyURI, 150 * an error is issued via @p context. 151 */ 152 template<const ReportContext::ErrorCode code, typename TReportContext> fromLexical(const QString & value,const TReportContext & context,const SourceLocationReflection * const r)153 static inline AnyURI::Ptr fromLexical(const QString &value, 154 const TReportContext &context, 155 const SourceLocationReflection *const r) 156 { 157 return AnyURI::Ptr(new AnyURI(toQUrl<code>(value, context, r).toString())); 158 } 159 160 /** 161 * If @p value is not a valid lexical representation for @c xs:anyURI, 162 * a ValidationError is returned. 163 */ 164 static AnyURI::Ptr fromLexical(const QString &value); 165 166 /** 167 * Creates an AnyURI instance representing an absolute URI which 168 * is created from resolving @p relative against @p base. 169 * 170 * This function must be compatible with the resolution semantics 171 * specified for fn:resolve-uri. In fact, the implementation of fn:resolve-uri, 172 * ResourceURIFN, relies on this function. 173 * 174 * @see <a href="http://www.faqs.org/rfcs/rfc3986.html">RFC 3986 - Uniform 175 * Resource Identifier (URI): Generic Syntax</a> 176 * @see <a href ="http://www.w3.org/TR/xpath-functions/#func-resolve-uri">XQuery 1.0 177 * and XPath 2.0 Functions and Operators, 8.1 fn:resolve-uri</a> 178 */ 179 static AnyURI::Ptr resolveURI(const QString &relative, 180 const QString &base); 181 182 virtual ItemType::Ptr type() const; 183 184 /** 185 * @short Returns this @c xs:anyURI value in a QUrl. 186 */ toQUrl()187 inline QUrl toQUrl() const 188 { 189 Q_ASSERT_X(QUrl(m_value).isValid(), Q_FUNC_INFO, 190 qPrintable(QString::fromLatin1("%1 is apparently not ok for QUrl.").arg(m_value))); 191 return QUrl(m_value); 192 } 193 protected: 194 friend class CommonValues; 195 196 AnyURI(const QString &value); 197 }; 198 199 /** 200 * @short Formats @p uri, that's considered to be a URI, for display. 201 */ formatURI(const NamePool::Ptr & np,const QXmlName::NamespaceCode & uri)202 static inline QString formatURI(const NamePool::Ptr &np, const QXmlName::NamespaceCode &uri) 203 { 204 return formatURI(np->stringForNamespace(uri)); 205 } 206 } 207 208 QT_END_NAMESPACE 209 210 QT_END_HEADER 211 212 #endif 213