f9d759981c697d2c7b7580ba571a809dda36b625
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / data / qanyuri_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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 <private/qatomicstring_p.h>
59 #include <private/qbuiltintypes_p.h>
60 #include <private/qpatternistlocale_p.h>
61 #include <private/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