src/xmlpatterns/api/qxmlquery_p.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 QXMLQUERY_P_H
       
    53 #define QXMLQUERY_P_H
       
    54 
       
    55 #include <QAbstractMessageHandler>
       
    56 #include <QAbstractUriResolver>
       
    57 #include <QPointer>
       
    58 #include <QSourceLocation>
       
    59 #include <QUrl>
       
    60 #include <QVariant>
       
    61 #include <QXmlName>
       
    62 #include <QXmlNamePool>
       
    63 #include <QXmlQuery>
       
    64 
       
    65 #include "qacceltreebuilder_p.h"
       
    66 #include "qacceltreeresourceloader_p.h"
       
    67 #include "qcoloringmessagehandler_p.h"
       
    68 #include "qcommonsequencetypes_p.h"
       
    69 #include "qexpressionfactory_p.h"
       
    70 #include "qfocus_p.h"
       
    71 #include "qfunctionfactorycollection_p.h"
       
    72 #include "qgenericdynamiccontext_p.h"
       
    73 #include "qgenericstaticcontext_p.h"
       
    74 #include "qnamepool_p.h"
       
    75 #include "qnetworkaccessdelegator_p.h"
       
    76 #include "qreferencecountedvalue_p.h"
       
    77 #include "qresourcedelegator_p.h"
       
    78 #include "qstaticfocuscontext_p.h"
       
    79 #include "quriloader_p.h"
       
    80 #include "qvariableloader_p.h"
       
    81 
       
    82 QT_BEGIN_NAMESPACE
       
    83 
       
    84 class QXmlQueryPrivate
       
    85 {
       
    86 public:
       
    87 
       
    88     inline QXmlQueryPrivate(const QXmlNamePool &np = QXmlNamePool()) : namePool(np)
       
    89                                                                      , messageHandler(0)
       
    90                                                                      , uriResolver(0)
       
    91                                                                      , queryLanguage(QXmlQuery::XQuery10)
       
    92                                                                      , m_networkAccessDelegator(new QPatternist::NetworkAccessDelegator(0, 0))
       
    93     {
       
    94         m_networkAccessDelegator->m_variableURIManager = new QPatternist::URILoader(ownerObject(), namePool.d, variableLoader());
       
    95     }
       
    96 
       
    97     void detach()
       
    98     {
       
    99         if(m_variableLoader)
       
   100             m_variableLoader = QPatternist::VariableLoader::Ptr(new QPatternist::VariableLoader(namePool.d, m_variableLoader));
       
   101 
       
   102         delete m_networkAccessDelegator->m_variableURIManager;
       
   103         m_networkAccessDelegator->m_variableURIManager = new QPatternist::URILoader(ownerObject(), namePool.d, m_variableLoader);
       
   104 
       
   105         if(m_resourceLoader)
       
   106         {
       
   107             const QPatternist::AccelTreeResourceLoader::Ptr nev(new QPatternist::AccelTreeResourceLoader(namePool.d,
       
   108                                                                                                          m_networkAccessDelegator));
       
   109             m_resourceLoader = QPatternist::ResourceLoader::Ptr(new QPatternist::ResourceDelegator(m_resourceLoader->deviceURIs(),
       
   110                                                                                                    m_resourceLoader,
       
   111                                                                                                    nev));
       
   112         }
       
   113     }
       
   114 
       
   115     bool isValid()
       
   116     {
       
   117         return expression();
       
   118     }
       
   119 
       
   120     inline void recompileRequired()
       
   121     {
       
   122         m_expr.reset();
       
   123     }
       
   124 
       
   125     inline QPatternist::VariableLoader::Ptr variableLoader()
       
   126     {
       
   127         if(!m_variableLoader)
       
   128             m_variableLoader = QPatternist::VariableLoader::Ptr(new QPatternist::VariableLoader(namePool.d));
       
   129 
       
   130         return m_variableLoader;
       
   131     }
       
   132 
       
   133     inline QPatternist::GenericStaticContext::Ptr staticContext()
       
   134     {
       
   135         if(m_staticContext && m_expr)
       
   136             return m_staticContext;
       
   137         /* Else, re-create the staticContext. */
       
   138 
       
   139         if(!messageHandler)
       
   140             messageHandler = new QPatternist::ColoringMessageHandler(ownerObject());
       
   141 
       
   142         if(!m_functionFactory)
       
   143         {
       
   144             if(queryLanguage == QXmlQuery::XSLT20)
       
   145                 m_functionFactory = QPatternist::FunctionFactoryCollection::xslt20Factory(namePool.d);
       
   146             else
       
   147                 m_functionFactory = QPatternist::FunctionFactoryCollection::xpath20Factory(namePool.d);
       
   148         }
       
   149 
       
   150         const QPatternist::GenericStaticContext::Ptr genericStaticContext(new QPatternist::GenericStaticContext(namePool.d,
       
   151                                                                                                                 messageHandler,
       
   152                                                                                                                 queryURI,
       
   153                                                                                                                 m_functionFactory,
       
   154                                                                                                                 queryLanguage));
       
   155         genericStaticContext->setResourceLoader(resourceLoader());
       
   156 
       
   157         genericStaticContext->setExternalVariableLoader(variableLoader());
       
   158 
       
   159         m_staticContext = genericStaticContext;
       
   160 
       
   161         if(!contextItem.isNull())
       
   162             m_staticContext = QPatternist::StaticContext::Ptr(new QPatternist::StaticFocusContext(QPatternist::AtomicValue::qtToXDMType(contextItem), m_staticContext));
       
   163         else if(   queryLanguage == QXmlQuery::XmlSchema11IdentityConstraintField
       
   164                 || queryLanguage == QXmlQuery::XmlSchema11IdentityConstraintSelector
       
   165                 || queryLanguage == QXmlQuery::XPath20)
       
   166             m_staticContext = QPatternist::StaticContext::Ptr(new QPatternist::StaticFocusContext(QPatternist::BuiltinTypes::node, m_staticContext));
       
   167 
       
   168         for (int i = 0; i < m_additionalNamespaceBindings.count(); ++i) {
       
   169             m_staticContext->namespaceBindings()->addBinding(m_additionalNamespaceBindings.at(i));
       
   170         }
       
   171 
       
   172         return m_staticContext;
       
   173     }
       
   174 
       
   175     inline QPatternist::DynamicContext::Ptr dynamicContext(QAbstractXmlReceiver *const callback = 0)
       
   176     {
       
   177         const QPatternist::StaticContext::Ptr statContext(staticContext());
       
   178         Q_ASSERT(statContext);
       
   179 
       
   180         QPatternist::GenericDynamicContext::Ptr dynContext(new QPatternist::GenericDynamicContext(namePool.d, statContext->messageHandler(),
       
   181                                                                                                   statContext->sourceLocations()));
       
   182 
       
   183         QPatternist::AutoPtr<QPatternist::NodeBuilder> nodeBuilder(new QPatternist::AccelTreeBuilder<false>(QUrl(), QUrl(), namePool.d,
       
   184                                                                                                             dynContext.data()));
       
   185         dynContext->setNodeBuilder(nodeBuilder);
       
   186 
       
   187         dynContext->setResourceLoader(statContext->resourceLoader());
       
   188         dynContext->setExternalVariableLoader(statContext->externalVariableLoader());
       
   189         dynContext->setUriResolver(uriResolver);
       
   190 
       
   191         if(callback)
       
   192             dynContext->setOutputReceiver(callback);
       
   193 
       
   194         if(contextItem.isNull())
       
   195             return dynContext;
       
   196         else
       
   197         {
       
   198             QPatternist::DynamicContext::Ptr focus(new QPatternist::Focus(dynContext));
       
   199             QPatternist::Item::Iterator::Ptr it(QPatternist::makeSingletonIterator(QPatternist::Item::fromPublic(contextItem)));
       
   200             it->next();
       
   201             focus->setFocusIterator(it);
       
   202             return focus;
       
   203         }
       
   204     }
       
   205 
       
   206     inline QPatternist::AccelTreeResourceLoader::Ptr resourceLoader()
       
   207     {
       
   208         if(!m_resourceLoader)
       
   209             m_resourceLoader = (new QPatternist::AccelTreeResourceLoader(namePool.d, m_networkAccessDelegator));
       
   210 
       
   211         return m_resourceLoader;
       
   212     }
       
   213 
       
   214     void setRequiredType(const QPatternist::SequenceType::Ptr &seqType)
       
   215     {
       
   216         Q_ASSERT(seqType);
       
   217         if(!m_requiredType || m_requiredType->is(seqType))
       
   218             return;
       
   219 
       
   220         m_requiredType = seqType;
       
   221         m_staticContext.reset();
       
   222     }
       
   223 
       
   224     QPatternist::SequenceType::Ptr requiredType()
       
   225     {
       
   226         if(m_requiredType)
       
   227             return m_requiredType;
       
   228         else
       
   229         {
       
   230             m_requiredType = QPatternist::CommonSequenceTypes::ZeroOrMoreItems;
       
   231             return m_requiredType;
       
   232         }
       
   233     }
       
   234 
       
   235     QPatternist::Expression::Ptr expression(QIODevice *const queryDevice = 0)
       
   236     {
       
   237         if(m_expr && !queryDevice)
       
   238             return m_expr;
       
   239 
       
   240         /* If we need to update, but we don't have any source code, we can
       
   241          * never create an Expression. */
       
   242         if(!queryDevice)
       
   243             return QPatternist::Expression::Ptr();
       
   244 
       
   245         try
       
   246         {
       
   247             /* The static context has source locations, and they need to be
       
   248              * updated to the new query. */
       
   249             m_staticContext.reset();
       
   250 
       
   251             if(!m_expressionFactory)
       
   252                 m_expressionFactory = QPatternist::ExpressionFactory::Ptr(new QPatternist::ExpressionFactory());
       
   253 
       
   254             m_expr = m_expressionFactory->createExpression(queryDevice, staticContext(),
       
   255                                                            queryLanguage,
       
   256                                                            requiredType(),
       
   257                                                            queryURI,
       
   258                                                            initialTemplateName);
       
   259         }
       
   260         catch(const QPatternist::Exception)
       
   261         {
       
   262             m_expr.reset();
       
   263 
       
   264             /* We don't call m_staticContext.reset() because it shouldn't be
       
   265              * necessary, since m_staticContext is changed when the expression
       
   266              * is changed. */
       
   267         }
       
   268 
       
   269         return m_expr;
       
   270     }
       
   271 
       
   272     inline void addAdditionalNamespaceBinding(const QXmlName &binding)
       
   273     {
       
   274         m_additionalNamespaceBindings.append(binding);
       
   275     }
       
   276 
       
   277     QXmlNamePool                                namePool;
       
   278     QPointer<QAbstractMessageHandler>           messageHandler;
       
   279     /**
       
   280      * Must be absolute and valid.
       
   281      */
       
   282     QUrl                                        queryURI;
       
   283     const QAbstractUriResolver *                uriResolver;
       
   284     QXmlItem                                    contextItem;
       
   285     QXmlName                                    initialTemplateName;
       
   286 
       
   287     inline void setExpressionFactory(const QPatternist::ExpressionFactory::Ptr &expr)
       
   288     {
       
   289         m_expressionFactory = expr;
       
   290     }
       
   291 
       
   292     QXmlQuery::QueryLanguage                    queryLanguage;
       
   293     QPointer<QNetworkAccessManager>             userNetworkManager;
       
   294 
       
   295     inline QObject *ownerObject()
       
   296     {
       
   297         if(!m_owner)
       
   298             m_owner = new QPatternist::ReferenceCountedValue<QObject>(new QObject());
       
   299 
       
   300         return m_owner->value;
       
   301     }
       
   302 
       
   303     QPatternist::ExpressionFactory::Ptr         m_expressionFactory;
       
   304     QPatternist::StaticContext::Ptr             m_staticContext;
       
   305     QPatternist::VariableLoader::Ptr            m_variableLoader;
       
   306     QPatternist::DeviceResourceLoader::Ptr      m_resourceLoader;
       
   307     /**
       
   308      * This is the AST for the query.
       
   309      */
       
   310     QPatternist::Expression::Ptr                m_expr;
       
   311     QPatternist::ReferenceCountedValue<QObject>::Ptr m_owner;
       
   312 
       
   313     /**
       
   314      * This is our effective network manager, that we end up using. The one the
       
   315      * user sets is userNetworkManager.
       
   316      */
       
   317     QPatternist::SequenceType::Ptr              m_requiredType;
       
   318     QPatternist::FunctionFactory::Ptr           m_functionFactory;
       
   319     QPatternist::NetworkAccessDelegator::Ptr    m_networkAccessDelegator;
       
   320 
       
   321     QList<QXmlName>                             m_additionalNamespaceBindings;
       
   322 };
       
   323 
       
   324 QT_END_NAMESPACE
       
   325 
       
   326 QT_END_HEADER
       
   327 
       
   328 #endif