Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / acceltree / qacceltreeresourceloader_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
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_AccelTreeResourceLoader_H
53 #define Patternist_AccelTreeResourceLoader_H
54
55 #include <QtCore/QEventLoop>
56 #include <QtNetwork/QNetworkReply>
57
58 #include <QAbstractXmlReceiver>
59 #include <private/qacceltree_p.h>
60 #include <private/qacceltreebuilder_p.h>
61 #include <private/qdeviceresourceloader_p.h>
62 #include <private/qnamepool_p.h>
63 #include <private/qnetworkaccessdelegator_p.h>
64 #include <private/qreportcontext_p.h>
65
66 QT_BEGIN_HEADER
67
68 QT_BEGIN_NAMESPACE
69
70 class QIODevice;
71
72 namespace QPatternist
73 {
74     /**
75      * @short An helper class which enables QNetworkAccessManager
76      * to be used in a blocking manner.
77      *
78      * @see AccelTreeResourceLoader::load()
79      * @author Frans Englich <frans.englich@nokia.com>
80      */
81     class NetworkLoop : public QEventLoop
82     {
83         Q_OBJECT
84     public:
85         NetworkLoop() : m_hasReceivedError(false)
86         {
87         }
88
89     public Q_SLOTS:
90         void error(QNetworkReply::NetworkError code)
91         {
92             Q_UNUSED(code);
93             m_hasReceivedError = true;
94             exit(1);
95         }
96
97         void finished()
98         {
99             if(m_hasReceivedError)
100                 exit(1);
101             else
102                 exit(0);
103         }
104     private:
105         bool m_hasReceivedError;
106     };
107
108     /**
109      * @short Handles requests for documents, and instantiates
110      * them as AccelTree instances.
111      *
112      * @author Frans Englich <frans.englich@nokia.com>
113      */
114     class Q_AUTOTEST_EXPORT AccelTreeResourceLoader : public DeviceResourceLoader
115     {
116     public:
117         /**
118          * Describes the behaviour of the resource loader in case of an
119          * error.
120          */
121         enum ErrorHandling
122         {
123             FailOnError,        ///< The resource loader will report the error via the report context.
124             ContinueOnError     ///< The resource loader will report no error and return an empty QNetworkReply.
125         };
126
127         /**
128          * AccelTreeResourceLoader does not own @p context.
129          */
130         AccelTreeResourceLoader(const NamePool::Ptr &np,
131                                 const NetworkAccessDelegator::Ptr &networkDelegator, AccelTreeBuilder<true>::Features = AccelTreeBuilder<true>::NoneFeature);
132
133         virtual Item openDocument(const QUrl &uri,
134                                   const ReportContext::Ptr &context);
135         virtual Item openDocument(QIODevice *source, const QUrl &documentUri,
136                                   const ReportContext::Ptr &context);
137         virtual SequenceType::Ptr announceDocument(const QUrl &uri, const Usage usageHint);
138         virtual bool isDocumentAvailable(const QUrl &uri);
139
140         virtual bool isUnparsedTextAvailable(const QUrl &uri,
141                                              const QString &encoding);
142
143         virtual Item openUnparsedText(const QUrl &uri,
144                                       const QString &encoding,
145                                       const ReportContext::Ptr &context,
146                                       const SourceLocationReflection *const where);
147
148         /**
149          * @short Helper function that do NetworkAccessDelegator::get(), but
150          * does it blocked.
151          *
152          * The returned QNetworkReply has emitted QNetworkReply::finished().
153          *
154          * The caller owns the return QIODevice instance.
155          *
156          * @p context may be @c null or valid. If @c null, no error reporting
157          * is done and @c null is returned.
158          *
159          * @see NetworkAccessDelegator
160          */
161         static QNetworkReply *load(const QUrl &uri,
162                                    QNetworkAccessManager *const networkManager,
163                                    const ReportContext::Ptr &context, ErrorHandling handling = FailOnError);
164
165         /**
166          * @overload
167          */
168         static QNetworkReply *load(const QUrl &uri,
169                                    const NetworkAccessDelegator::Ptr &networkDelegator,
170                                    const ReportContext::Ptr &context, ErrorHandling handling = FailOnError);
171
172         /**
173          * @short Returns the URIs this AccelTreeResourceLoader has loaded
174          * which are for devices through variable bindings.
175          */
176         virtual QSet<QUrl> deviceURIs() const;
177
178         virtual void clear(const QUrl &uri);
179
180     private:
181         static bool streamToReceiver(QIODevice *const dev,
182                                      AccelTreeBuilder<true> *const receiver,
183                                      const NamePool::Ptr &np,
184                                      const ReportContext::Ptr &context,
185                                      const QUrl &uri);
186         bool retrieveDocument(const QUrl &uri,
187                               const ReportContext::Ptr &context);
188         bool retrieveDocument(QIODevice *source, const QUrl &documentUri,
189                               const ReportContext::Ptr &context);
190         /**
191          * If @p context is @c null, no error reporting should be done.
192          */
193         bool retrieveUnparsedText(const QUrl &uri,
194                                   const QString &encoding,
195                                   const ReportContext::Ptr &context,
196                                   const SourceLocationReflection *const where);
197
198         QHash<QUrl, AccelTree::Ptr>             m_loadedDocuments;
199         const NamePool::Ptr                     m_namePool;
200         const NetworkAccessDelegator::Ptr       m_networkAccessDelegator;
201         QHash<QPair<QUrl, QString>, QString>    m_unparsedTexts;
202         AccelTreeBuilder<true>::Features        m_features;
203     };
204 }
205
206 QT_END_NAMESPACE
207
208 QT_END_HEADER
209
210 #endif