1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
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.
52 #ifndef Patternist_AnyURI_H
53 #define Patternist_AnyURI_H
58 #include <private/qatomicstring_p.h>
59 #include <private/qbuiltintypes_p.h>
60 #include <private/qpatternistlocale_p.h>
61 #include <private/qreportcontext_p.h>
70 * @short A value of type <tt>xs:anyURI</tt>.
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().
79 * @author Frans Englich <frans.englich@nokia.com>
80 * @ingroup Patternist_xdm
82 class AnyURI : public AtomicString
85 typedef QExplicitlySharedDataPointer<AnyURI> Ptr;
88 * Creates an instance representing @p value.
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().
93 static AnyURI::Ptr fromValue(const QString &value);
95 static AnyURI::Ptr fromValue(const QUrl &uri);
98 * @short Treates @p value as a lexical representation of @c xs:anyURI
99 * but returns the value instance as a QUrl.
101 * If @p value is not a valid lexical representation of @c xs:anyURI,
102 * an error is issued via @p context.
104 * If @p isValid is passed, no error is raised and it is instead set
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)
114 /* QUrl doesn't flag ":/..." so we workaround it. */
115 const QString simplified(value.simplified());
116 const QUrl uri(simplified, QUrl::StrictMode);
118 if(uri.isEmpty() || (uri.isValid() && (!simplified.startsWith(QLatin1Char(':')) || !uri.isRelative())))
132 context->error(QtXmlPatterns::tr("%1 is not a valid value of type %2.").arg(formatURI(value), formatType(context->namePool(), BuiltinTypes::xsAnyURI)),
141 * @short Return @c true if @p candidate is a valid @c xs:anyURI,
142 * otherwise @c false.
144 static bool isValid(const QString &candidate);
147 * @short Constructs a @c xs:anyURI value from the lexical representation @p value.
149 * If @p value is not a valid lexical representation of @c xs:anyURI,
150 * an error is issued via @p context.
152 template<const ReportContext::ErrorCode code, typename TReportContext>
153 static inline AnyURI::Ptr fromLexical(const QString &value,
154 const TReportContext &context,
155 const SourceLocationReflection *const r)
157 return AnyURI::Ptr(new AnyURI(toQUrl<code>(value, context, r).toString()));
161 * If @p value is not a valid lexical representation for @c xs:anyURI,
162 * a ValidationError is returned.
164 static AnyURI::Ptr fromLexical(const QString &value);
167 * Creates an AnyURI instance representing an absolute URI which
168 * is created from resolving @p relative against @p base.
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.
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>
179 static AnyURI::Ptr resolveURI(const QString &relative,
180 const QString &base);
182 virtual ItemType::Ptr type() const;
185 * @short Returns this @c xs:anyURI value in a QUrl.
187 inline QUrl toQUrl() const
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);
194 friend class CommonValues;
196 AnyURI(const QString &value);
200 * @short Formats @p uri, that's considered to be a URI, for display.
202 static inline QString formatURI(const NamePool::Ptr &np, const QXmlName::NamespaceCode &uri)
204 return formatURI(np->stringForNamespace(uri));