source: trunk/src/xmlpatterns/data/qanyuri_p.h@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 7.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
63QT_BEGIN_HEADER
64
65QT_BEGIN_NAMESPACE
66
67namespace 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 <[email protected]>
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>