75a0293c5b8ec85c0a676d61bd2183fe450fa83c
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternssdk / DebugExpressionFactory.cpp
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 test suite 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 #include <QStringList>
43 #include <QVariant>
44 #include <QtDebug>
45 #include <QXmlNamePool>
46
47 #include <private/qfunctionfactorycollection_p.h>
48
49 #include "ASTItem.h"
50 #include "ExpressionInfo.h"
51 #include "ExpressionNamer.h"
52 #include "Global.h"
53
54 #include "DebugExpressionFactory.h"
55
56 using namespace QPatternistSDK;
57 using namespace QPatternist;
58
59 static const QPatternist::ExpressionVisitor::Ptr namer(new ExpressionNamer());
60
61 QStringList DebugExpressionFactory::availableFunctionSignatures()
62 {
63     const QPatternist::FunctionFactory::Ptr factory(QPatternist::FunctionFactoryCollection::xpath20Factory(Global::namePool()));
64     const QPatternist::FunctionSignature::Hash signs(factory->functionSignatures());
65     const QPatternist::FunctionSignature::Hash::const_iterator end(signs.constEnd());
66     QPatternist::FunctionSignature::Hash::const_iterator it(signs.constBegin());
67     QStringList retval;
68
69     while(it != end)
70     {
71         retval << it.value()->displayName(Global::namePool());
72         ++it;
73     }
74
75     return retval;
76 }
77
78 ASTItem *DebugExpressionFactory::buildASTTree(const QPatternist::Expression::Ptr &expr,
79                                               ASTItem *parent,
80                                               const QPatternist::SequenceType::Ptr &reqType)
81 {
82     Q_ASSERT(expr);
83     const QPatternist::ExpressionVisitorResult::Ptr exprInfo(expr->accept(namer));
84     Q_ASSERT(exprInfo);
85     const ExpressionInfo *const constExprInfo = static_cast<const ExpressionInfo *>(exprInfo.data());
86     const QString name(constExprInfo->first);
87     const QString details(constExprInfo->second);
88     const QString rType(reqType ? reqType->displayName(Global::namePool()) : QLatin1String("Not specified"));
89
90     /* ---------- Handle its staticType() -------- */
91     const QPatternist::SequenceType::Ptr type(expr->staticType());
92     QString seqType;
93
94     if(type)
95         seqType = type->displayName(Global::namePool());
96     else
97         seqType = QLatin1String("no type, null pointer returned");
98     /* ------------------------------------------- */
99
100     ASTItem *const node = new ASTItem(parent, name, details, seqType, rType);
101
102     /* ------------ Handle child nodes ----------- */
103     const QPatternist::Expression::List children(expr->operands());
104     QPatternist::Expression::List::const_iterator it(children.constBegin());
105     const QPatternist::Expression::List::const_iterator end(children.constEnd());
106
107     const QPatternist::SequenceType::List reqTypes(expr->expectedOperandTypes());
108     const QPatternist::SequenceType::List::const_iterator typeEnd(reqTypes.constEnd());
109     QPatternist::SequenceType::List::const_iterator typeIt(reqTypes.constBegin());
110     QPatternist::SequenceType::Ptr t;
111
112     for(; it != end; ++it)
113     {
114         if(typeIt != typeEnd)
115         {
116             t = *typeIt;
117             ++typeIt;
118         }
119
120         node->appendChild(buildASTTree(*it, node, t));
121     }
122     /* ------------------------------------------- */
123
124     return node;
125 }
126
127 QPatternist::Expression::Ptr
128 DebugExpressionFactory::createExpression(QIODevice *const expr,
129                                          const QPatternist::StaticContext::Ptr &context,
130                                          const QXmlQuery::QueryLanguage lang,
131                                          const QPatternist::SequenceType::Ptr &requiredType,
132                                          const QUrl &baseURI,
133                                          const QXmlName &initialTemplateName)
134 {
135     /* Create the root node. */
136     m_ast = new ASTItem(0, QString());
137
138     return ExpressionFactory::createExpression(expr, context, lang, requiredType, baseURI, initialTemplateName);
139 }
140
141 void DebugExpressionFactory::processTreePass(const QPatternist::Expression::Ptr &expr,
142                                              const CompilationStage stage)
143 {
144     ASTItem *newChild = 0;
145
146     switch(stage)
147     {
148         case QueryBodyInitial:
149         {
150             newChild = new ASTItem(m_ast, QLatin1String("Initial Build"));
151             break;
152         }
153         case QueryBodyTypeCheck:
154         {
155             newChild = new ASTItem(m_ast, QLatin1String("Type Check"));
156             break;
157         }
158         case QueryBodyCompression:
159         {
160             newChild = new ASTItem(m_ast, QLatin1String("Compression"));
161             break;
162         }
163         case UserFunctionTypeCheck:
164         {
165             newChild = new ASTItem(m_ast, QLatin1String("User Function Type Check"));
166             break;
167         }
168         case UserFunctionCompression:
169         {
170             newChild = new ASTItem(m_ast, QLatin1String("User Function Compression"));
171             break;
172         }
173         case GlobalVariableTypeCheck:
174         {
175             newChild = new ASTItem(m_ast, QLatin1String("Global Variable Type Check"));
176             break;
177         }
178     }
179
180     Q_ASSERT(newChild);
181     m_ast->appendChild(newChild);
182     newChild->appendChild(buildASTTree(expr, newChild, QPatternist::SequenceType::Ptr()));
183 }
184
185 void DebugExpressionFactory::processTemplateRule(const Expression::Ptr &body,
186                                                  const TemplatePattern::Ptr &pattern,
187                                                  const QXmlName &mode,
188                                                  const TemplateCompilationStage stage)
189 {
190     const char * title;
191
192     switch(stage)
193     {
194         case TemplateInitial:
195         {
196             title = "Initial Build";
197             break;
198         }
199         case TemplateTypeCheck:
200         {
201             title = "Type Check";
202             break;
203         }
204         case TemplateCompress:
205         {
206             title = "Compression";
207             break;
208         }
209     }
210
211     const QString modeName(Global::namePool()->displayName(mode));
212     Q_ASSERT(title);
213     ASTItem *const newChild = new ASTItem(m_ast, QLatin1String("T-Rule ")
214                                                  + QLatin1String(title)
215                                                  + QLatin1String(" mode: ")
216                                                  + modeName
217                                                  + QLatin1String(" priority: ")
218                                                  + QString::number(pattern->priority()));
219     m_ast->appendChild(newChild);
220
221     newChild->appendChild(buildASTTree(pattern->matchPattern(), newChild, QPatternist::SequenceType::Ptr()));
222     newChild->appendChild(buildASTTree(body, newChild, QPatternist::SequenceType::Ptr()));
223 }
224
225 void DebugExpressionFactory::processNamedTemplate(const QXmlName &name,
226                                                   const Expression::Ptr &body,
227                                                   const TemplateCompilationStage stage)
228 {
229     const char * title;
230
231     switch(stage)
232     {
233         case TemplateInitial:
234         {
235             title = "Named Template Initial Build";
236             break;
237         }
238         case TemplateTypeCheck:
239         {
240             title = "Named Template Type Check";
241             break;
242         }
243         case TemplateCompress:
244         {
245             title = "Named Template Compression";
246             break;
247         }
248     }
249
250     Q_ASSERT(title);
251     ASTItem *const newChild = new ASTItem(m_ast, QLatin1String(title)
252                                                  + QLatin1String(": ")
253                                                  + Global::namePool()->displayName(name));
254
255     m_ast->appendChild(newChild);
256     newChild->appendChild(buildASTTree(body, newChild, QPatternist::SequenceType::Ptr()));
257 }
258
259 ASTItem *DebugExpressionFactory::astTree() const
260 {
261     return m_ast;
262 }
263
264 // vim: et:ts=4:sw=4:sts=4