|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
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> |
|
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 */ |
|
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 */ |
|
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 |