Qt 4.8
qapplytemplate.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 #include <QtDebug>
43 
44 #include "qaxisstep_p.h"
45 #include "qcommonsequencetypes_p.h"
46 #include "qcommonvalues_p.h"
47 #include "qitemmappingiterator_p.h"
49 #include "qpatternistlocale_p.h"
50 
51 #include "qapplytemplate_p.h"
52 
54 
55 using namespace QPatternist;
56 
58  const WithParam::Hash &withParams,
59  const TemplateMode::Ptr &defaultMode) : TemplateInvoker(withParams)
60  , m_mode(mode)
61  , m_defaultMode(defaultMode)
62 {
64  "Either a mode, or the default mode must be supplied.");
65 }
66 
68  const DynamicContext::Ptr &) const
69 {
70  return Item(node);
71 }
72 
74  const DynamicContext::Ptr &context) const
75 {
76  Q_UNUSED(item);
77  return evaluateSequence(context);
78 }
79 
81 {
82  if(m_mode)
83  return m_mode;
84  else
85  {
86  const TemplateMode::Ptr currentMode(context->currentTemplateMode());
87 
88  if(currentMode)
89  return currentMode;
90  else
91  return m_defaultMode;
92  }
93 }
94 
96  const TemplateMode::Ptr &templateMode) const
97 {
98  const int count = templateMode->templatePatterns.count();
99  Template::Ptr result;
100  /* It's redundant to initialize these values, but it suppresses false
101  * positives with GCC. */
102  PatternPriority priority = 0;
103  TemplatePattern::ID id = -1;
104 
105  /* Possible optimization: detecting ambiguous rule matches could be forked off to a
106  * low prioirity thread. */
107  for(int i = 0; i < count; ++i)
108  {
109  const TemplatePattern::Ptr &candidate = templateMode->templatePatterns.at(i);
110  if(candidate->matchPattern()->evaluateEBV(context))
111  {
112  if(result)
113  {
114  if( candidate->id() != id
115  && candidate->priority() == priority
116  && candidate->templateTarget()->importPrecedence ==
117  result->importPrecedence)
118  {
119  context->error(QtXmlPatterns::tr("Ambiguous rule match."),
121  }
122  else
123  break;
124  }
125  else
126  {
127  result = candidate->templateTarget();
128  priority = candidate->priority();
129  id = candidate->id();
130  }
131  }
132  }
133 
134  return result;
135 }
136 
138 {
139  const TemplateMode::Ptr templateMode(effectiveMode(context));
140  const Template::Ptr &templateMatch = findTemplate(context, templateMode);
141 
142  if(templateMatch)
143  return templateMatch->body->evaluateSequence(templateMatch->createContext(this, context, false));
144  else
145  {
146  /* None of our templates matched. Proceed with a built-in. */
147  const Item current(context->contextItem());
148  // TODO it can be an atomic value?
149  const QXmlNodeModelIndex::NodeKind kind(current.asNode().kind());
150 
152  {
153  pDebug() << "No template match, using builtin template for element() | document-node()";
154 
155  const Item::Iterator::Ptr focusIterator(makeItemMappingIterator<Item>(ConstPtr(this),
156  current.asNode().iterate(QXmlNodeModelIndex::AxisChild),
157  context));
158 
159  const DynamicContext::Ptr focus(context->createFocus());
160  focus->setFocusIterator(focusIterator);
161  return makeSequenceMappingIterator<Item>(ConstPtr(this), focusIterator, focus);
162  }
164  }
165 }
166 
168 {
169  /* If we have a mode, we will never need the default mode. */
170  if(m_mode)
172 
173  return TemplateInvoker::compress(context);
174 }
175 
177 {
179 }
180 
182 {
183  return visitor->visit(this);
184 }
185 
187 {
189 }
190 
192 {
193  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
194  Q_UNUSED(sign);
195  return false;
196 }
197 
199 {
200  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
201  return Expression::Ptr();
202 }
203 
205 {
206  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
208 }
209 
virtual Expression::Ptr body() const
Returns the body of the function/template/component that is being called.
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
The QXmlNodeModelIndex class identifies a node in an XML node model subclassed from QAbstractXmlNodeM...
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const ImportPrecedence importPrecedence
Definition: qtemplate_p.h:105
virtual bool configureRecursion(const CallTargetDescription::Ptr &sign)
QExplicitlySharedDataPointer< const ApplyTemplate > ConstPtr
static const SequenceType::Ptr ZeroOrMoreItems
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
Base class for classes that invokes templates, such as CallTemplate and ApplyTemplate.
virtual CallTargetDescription::Ptr callTargetDescription() const
Item::Iterator::Ptr mapToSequence(const Item &item, const DynamicContext::Ptr &context) const
static const EmptyIterator< Item >::Ptr emptyIterator
TemplatePattern::Vector templatePatterns
const TemplateMode::Ptr m_mode
static int sign(int x)
Expression::Ptr body
Definition: qtemplate_p.h:98
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
DynamicContext::Ptr createFocus()
virtual Item contextItem() const =0
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
virtual Properties properties() const
The namespace for the internal API of QtXmlPatterns.
virtual void setFocusIterator(const Item::Iterator::Ptr &it)=0
Template::Ptr findTemplate(const DynamicContext::Ptr &context, const TemplateMode::Ptr &templateMode) const
TemplateMode::Ptr m_defaultMode
virtual SequenceType::Ptr staticType() const
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
NodeKind
Identifies a kind of node.
TemplateMode::Ptr effectiveMode(const DynamicContext::Ptr &context) const
DynamicContext::Ptr createContext(const TemplateInvoker *const invoker, const DynamicContext::Ptr &context, const bool isCallTemplate) const
Definition: qtemplate.cpp:81
void reset()
Resets this to be null.
Definition: qshareddata.h:150
Item mapToItem(const QXmlNodeModelIndex &node, const DynamicContext::Ptr &context) const
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
ApplyTemplate(const TemplateMode::Ptr &mode, const WithParam::Hash &withParams, const TemplateMode::Ptr &defaultMode)
mode may be null. If it is, ApplyTemplate interprets that as that it should use the #current mode...
virtual QExplicitlySharedDataPointer< TemplateMode > currentTemplateMode() const =0
Returns the current template mode that is in effect.
xsDouble PatternPriority
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QExplicitlySharedDataPointer< CallTargetDescription > Ptr
Expression::Ptr compress(const StaticContext::Ptr &context)
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
QNoDebug pDebug()
Definition: qdebug_p.h:99
#define Q_FUNC_INFO
Definition: qglobal.h:1871