Qt 4.8
Public Types | Public Functions | Private Functions | Properties | List of all members
QPatternist::VariableLoader Class Reference

#include <qvariableloader_p.h>

Inheritance diagram for QPatternist::VariableLoader:
QPatternist::ExternalVariableLoader QSharedData

Public Types

typedef QHash< QXmlName, QVariantBindingHash
 
typedef QExplicitlySharedDataPointer< VariableLoaderPtr
 
- Public Types inherited from QPatternist::ExternalVariableLoader
typedef QExplicitlySharedDataPointer< ExternalVariableLoaderPtr
 

Public Functions

void addBinding (const QXmlName &name, const QVariant &value)
 
virtual QPatternist::SequenceType::Ptr announceExternalVariable (const QXmlName name, const QPatternist::SequenceType::Ptr &declaredType)
 
virtual QPatternist::Item::Iterator::Ptr evaluateSequence (const QXmlName name, const QPatternist::DynamicContext::Ptr &)
 
virtual QPatternist::Item evaluateSingleton (const QXmlName name, const QPatternist::DynamicContext::Ptr &)
 
bool hasBinding (const QXmlName &name) const
 
bool invalidationRequired (const QXmlName &name, const QVariant &variant) const
 
bool isSameType (const QVariant &v1, const QVariant &v2) const
 
void removeBinding (const QXmlName &name)
 
QVariant valueFor (const QXmlName &name) const
 
 VariableLoader (const NamePool::Ptr &np, const VariableLoader::Ptr &previousLoader=VariableLoader::Ptr())
 
- Public Functions inherited from QPatternist::ExternalVariableLoader
virtual bool evaluateEBV (const QXmlName name, const QExplicitlySharedDataPointer< DynamicContext > &context)
 
virtual Item::Iterator::Ptr evaluateSequence (const QXmlName name, const QExplicitlySharedDataPointer< DynamicContext > &context)
 
virtual Item evaluateSingleton (const QXmlName name, const QExplicitlySharedDataPointer< DynamicContext > &context)
 
 ExternalVariableLoader ()
 
virtual ~ExternalVariableLoader ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 

Private Functions

QPatternist::Item itemForName (const QXmlName &name) const
 

Properties

BindingHash m_bindingHash
 
const NamePool::Ptr m_namePool
 
VariableLoader::Ptr m_previousLoader
 

Additional Inherited Members

- Public Variables inherited from QSharedData
QAtomicInt ref
 

Detailed Description

Definition at line 68 of file qvariableloader_p.h.

Typedefs

◆ BindingHash

Definition at line 71 of file qvariableloader_p.h.

◆ Ptr

Definition at line 72 of file qvariableloader_p.h.

Constructors and Destructors

◆ VariableLoader()

QPatternist::VariableLoader::VariableLoader ( const NamePool::Ptr np,
const VariableLoader::Ptr previousLoader = VariableLoader::Ptr() 
)
inline

Definition at line 74 of file qvariableloader_p.h.

75  : m_namePool(np)
76  , m_previousLoader(previousLoader)
77 
78  {
79  }
VariableLoader::Ptr m_previousLoader
const NamePool::Ptr m_namePool

Functions

◆ addBinding()

void VariableLoader::addBinding ( const QXmlName name,
const QVariant value 
)

Definition at line 251 of file qvariableloader.cpp.

Referenced by VariableLoader().

253 {
254  m_bindingHash.insert(name, value);
255 }
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753

◆ announceExternalVariable()

SequenceType::Ptr VariableLoader::announceExternalVariable ( const QXmlName  name,
const QPatternist::SequenceType::Ptr declaredType 
)
virtual

Called when Patternist encounters an external variable in the query. It is guaranteed to be called once for each external variable appearing in a query module.

Parameters
namethe name of the variable. Quaranteed to never be null.
declaredTypethe type that the user declared the variable to be of. Whether this type matches the actual value of the variable or not is irrelevant. Patternist will do the necessary error handling based on the sequence type that is returned from this function. If the user didn't declare a type, the type is item()*(zero or more items). Quaranteed to never be null.
Returns
the sequence type of the value this ExternalVariableLoader actually supplies. However, if the ExternalVariableLoader knows it cannot supply a variable by this name, null should be returned.

Reimplemented from QPatternist::ExternalVariableLoader.

Definition at line 121 of file qvariableloader.cpp.

Referenced by VariableLoader().

123 {
124  Q_UNUSED(declaredType);
125  const QVariant &variant = m_bindingHash.value(name);
126 
127 
128  if(variant.isNull())
129  return SequenceType::Ptr();
130  else if(variant.userType() == qMetaTypeId<QIODevice *>())
132  else if(variant.userType() == qMetaTypeId<QXmlQuery>())
133  {
134  const QXmlQuery variableQuery(qvariant_cast<QXmlQuery>(variant));
135  return variableQuery.d->expression()->staticType();
136  }
137  else
138  {
139  return makeGenericSequenceType(AtomicValue::qtToXDMType(qvariant_cast<QXmlItem>(variant)),
141  }
142 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
static const SequenceType::Ptr ExactlyOneAnyURI
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
static Cardinality exactlyOne()
const char * variant
static ItemType::Ptr qtToXDMType(const QXmlItem &item)
#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
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML...
Definition: qxmlquery.h:79

◆ evaluateSequence()

Item::Iterator::Ptr VariableLoader::evaluateSequence ( const QXmlName  name,
const QPatternist::DynamicContext::Ptr context 
)
virtual

Definition at line 144 of file qvariableloader.cpp.

Referenced by VariableLoader().

146 {
147 
148  const QVariant &variant = m_bindingHash.value(name);
149  Q_ASSERT_X(!variant.isNull(), Q_FUNC_INFO,
150  "We assume that we have a binding.");
151 
152  /* Same code as in the default clause below. */
153  if(variant.userType() == qMetaTypeId<QIODevice *>())
154  return makeSingletonIterator(itemForName(name));
155  else if(variant.userType() == qMetaTypeId<QXmlQuery>())
156  {
157  const QXmlQuery variableQuery(qvariant_cast<QXmlQuery>(variant));
158 
159  return variableQuery.d->expression()->evaluateSequence(DynamicContext::Ptr(new TemporaryTreesRedirectingContext(variableQuery.d->dynamicContext(), context)));
160  }
161 
162  const QVariant v(qvariant_cast<QXmlItem>(variant).toAtomicValue());
163 
164  switch(v.type())
165  {
167  return Item::Iterator::Ptr(new StringListIterator(v.toStringList()));
168  case QVariant::List:
169  return Item::Iterator::Ptr(new VariantListIterator(v.toList()));
170  default:
171  return makeSingletonIterator(itemForName(name));
172  }
173 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
QPatternist::Item itemForName(const QXmlName &name) const
const char * variant
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML...
Definition: qxmlquery.h:79
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ evaluateSingleton()

Item VariableLoader::evaluateSingleton ( const QXmlName  name,
const QPatternist::DynamicContext::Ptr  
)
virtual

Definition at line 199 of file qvariableloader.cpp.

Referenced by VariableLoader().

201 {
202  return itemForName(name);
203 }
QPatternist::Item itemForName(const QXmlName &name) const

◆ hasBinding()

bool VariableLoader::hasBinding ( const QXmlName name) const

Definition at line 235 of file qvariableloader.cpp.

Referenced by VariableLoader().

236 {
237  return m_bindingHash.contains(name)
239 }
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
VariableLoader::Ptr m_previousLoader
bool hasBinding(const QXmlName &name) const

◆ invalidationRequired()

bool VariableLoader::invalidationRequired ( const QXmlName name,
const QVariant variant 
) const

Definition at line 257 of file qvariableloader.cpp.

Referenced by VariableLoader().

259 {
260  return hasBinding(name) && !isSameType(valueFor(name), variant);
261 }
QVariant valueFor(const QXmlName &name) const
bool hasBinding(const QXmlName &name) const
bool isSameType(const QVariant &v1, const QVariant &v2) const

◆ isSameType()

bool VariableLoader::isSameType ( const QVariant v1,
const QVariant v2 
) const

Definition at line 205 of file qvariableloader.cpp.

Referenced by VariableLoader().

207 {
208  /* Are both of type QIODevice *? */
209  if(v1.userType() == qMetaTypeId<QIODevice *>() && v1.userType() == v2.userType())
210  return true;
211 
212  /* Ok, we have two QXmlItems. */
213  const QXmlItem i1(qvariant_cast<QXmlItem>(v1));
214  const QXmlItem i2(qvariant_cast<QXmlItem>(v2));
215 
216  if(i1.isNode())
217  {
218  Q_ASSERT(false);
219  return false;
220  }
221  else if(i2.isAtomicValue())
222  return i1.toAtomicValue().type() == i2.toAtomicValue().type();
223  else
224  {
225  /* One is an atomic, the other is a node or they are null. */
226  return false;
227  }
228 }
The QXmlItem class contains either an XML node or an atomic value.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913

◆ itemForName()

Item VariableLoader::itemForName ( const QXmlName name) const
inlineprivate

Definition at line 175 of file qvariableloader.cpp.

Referenced by VariableLoader().

176 {
177  const QVariant &variant = m_bindingHash.value(name);
178 
179  if(variant.userType() == qMetaTypeId<QIODevice *>())
180  return Item(AnyURI::fromValue(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:") + m_namePool->stringForLocalName(name.localName())));
181 
182  const QXmlItem item(qvariant_cast<QXmlItem>(variant));
183 
184  if(item.isNode())
185  return Item::fromPublic(item);
186  else
187  {
188  const QVariant atomicValue(item.toAtomicValue());
189  /* If the atomicValue is null it means it doesn't exist in m_bindingHash, and therefore it must
190  * be a QIODevice, since Patternist guarantees to only ask for variables that announceExternalVariable()
191  * has accepted. */
192  if(atomicValue.isNull())
193  return Item(AnyURI::fromValue(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:") + m_namePool->stringForLocalName(name.localName())));
194  else
195  return AtomicValue::toXDM(atomicValue);
196  }
197 }
const QString & stringForLocalName(const QXmlName::LocalNameCode code) const
Definition: qnamepool_p.h:168
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QXmlItem class contains either an XML node or an atomic value.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const NamePool::Ptr m_namePool
QString localName(const QXmlNamePool &query) const
Returns the local name.
Definition: qxmlname.cpp:387
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static Item fromPublic(const QXmlItem &i)
Definition: qitem_p.h:408
const char * variant
static Item toXDM(const QVariant &value)
static AnyURI::Ptr fromValue(const QString &value)
Definition: qanyuri.cpp:56

◆ removeBinding()

void VariableLoader::removeBinding ( const QXmlName name)

Definition at line 230 of file qvariableloader.cpp.

Referenced by VariableLoader().

231 {
232  m_bindingHash.remove(name);
233 }
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784

◆ valueFor()

QVariant VariableLoader::valueFor ( const QXmlName name) const

Definition at line 241 of file qvariableloader.cpp.

Referenced by QPatternist::URILoader::createRequest(), and VariableLoader().

242 {
243  if(m_bindingHash.contains(name))
244  return m_bindingHash.value(name);
245  else if(m_previousLoader)
246  return m_previousLoader->valueFor(name);
247  else
248  return QVariant();
249 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
VariableLoader::Ptr m_previousLoader
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
QVariant valueFor(const QXmlName &name) const

Properties

◆ m_bindingHash

BindingHash QPatternist::VariableLoader::m_bindingHash
private

Definition at line 107 of file qvariableloader_p.h.

◆ m_namePool

const NamePool::Ptr QPatternist::VariableLoader::m_namePool
private

Definition at line 105 of file qvariableloader_p.h.

◆ m_previousLoader

VariableLoader::Ptr QPatternist::VariableLoader::m_previousLoader
private

Definition at line 106 of file qvariableloader_p.h.


The documentation for this class was generated from the following files: