//! [2] FileTree::FileTree(const QXmlNamePool& pool) : QSimpleXmlNodeModel(pool), m_filterAllowAll(QDir::AllEntries | QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden), m_sortFlags(QDir::Name) { QXmlNamePool np = namePool(); m_names.resize(7); m_names[File] = QXmlName(np, QLatin1String("file")); m_names[Directory] = QXmlName(np, QLatin1String("directory")); m_names[AttributeFileName] = QXmlName(np, QLatin1String("fileName")); m_names[AttributeFilePath] = QXmlName(np, QLatin1String("filePath")); m_names[AttributeSize] = QXmlName(np, QLatin1String("size")); m_names[AttributeMIMEType] = QXmlName(np, QLatin1String("mimeType")); m_names[AttributeSuffix] = QXmlName(np, QLatin1String("suffix")); }
QXmlName QObjectXmlModel::name(const QXmlNodeModelIndex &n) const { switch (toNodeType(n)) { case IsQObject: return QXmlName(namePool(), QLatin1String("QObject")); case MetaObject: return QXmlName(namePool(), QLatin1String("metaObject")); case QObjectClassName: case MetaObjectClassName: return QXmlName(namePool(), QLatin1String("className")); case QObjectProperty: return QXmlName(namePool(), toMetaProperty(n).name()); case MetaObjects: return QXmlName(namePool(), QLatin1String("metaObjects")); case MetaObjectSuperClass: return QXmlName(namePool(), QLatin1String("superClass")); } Q_ASSERT(false); return QXmlName(); }
void tst_QXmlName::fromClarkName_data() const { QTest::addColumn<QString>("input"); QTest::addColumn<QXmlName>("expected"); QTest::addColumn<QXmlNamePool>("namePool"); QXmlNamePool np; QTest::newRow("A null string") << QString() << QXmlName() << np; QTest::newRow("An empty string") << QString(QLatin1String("")) << QXmlName() << np; QTest::newRow("A single local name") << QString(QLatin1String("foo")) << QXmlName(np, QLatin1String("foo")) << np; QTest::newRow("Has prefix, but no namespace, that's invalid") << QString(QLatin1String("prefix:foo")) << QXmlName() << np; QTest::newRow("Namespace, local name, no prefix") << QString(QLatin1String("{def}abc")) << QXmlName(np, QLatin1String("abc"), QLatin1String("def")) << np; QTest::newRow("Namespace, local name, prefix") << QString(QLatin1String("{def}p:abc")) << QXmlName(np, QLatin1String("abc"), QLatin1String("def"), QLatin1String("p")) << np; QTest::newRow("Namespace, local name, prefix syntax error") << QString(QLatin1String("{def}:abc")) << QXmlName() << np; QTest::newRow("Namespace, local name syntax error, prefix") << QString(QLatin1String("{def}p:")) << QXmlName() << np; QTest::newRow("Only local name which is invalid") << QString(QLatin1String(":::")) << QXmlName() << np; QTest::newRow("Namespace, invalid local name") << QString(QLatin1String("{def}a|bc")) << QXmlName() << np; QTest::newRow("Namespace, local name, invalid prefix") << QString(QLatin1String("{def}a|b:c")) << QXmlName() << np; QTest::newRow("A single left curly, invalid") << QString(QLatin1String("{")) << QXmlName() << np; QTest::newRow("A single left curly, invalid") << QString(QLatin1String("{aaswd")) << QXmlName() << np; }
/*! Check that QXmlName can be used inside QVariant. */ void tst_QXmlName::withinQVariant() const { /* The extra paranthesis silences a warning on win32-msvc2005. */ QVariant value(qVariantFromValue(QXmlName())); }
/*! \overload If \a localName is a valid \l {QXmlName::isNCName()} {NCName}, this function is equivalent to the following snippet. \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 2 A QXmlName is constructed from \a localName, and is passed to the appropriate overload along with \a device. \sa QXmlName::isNCName() */ void QXmlQuery::bindVariable(const QString &localName, QIODevice *device) { bindVariable(QXmlName(d->namePool, localName), device); }
/*! \overload This function constructs a QXmlName from \a localName using the query's \l {QXmlNamePool} {namespace}. The function then behaves as the overloaded function. It is equivalent to the following snippet. \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 0 */ void QXmlQuery::bindVariable(const QString &localName, const QXmlItem &value) { bindVariable(QXmlName(d->namePool, localName), value); }
/*! \overload Has the same behavior and effects as the function being overloaded, but takes the variable name \a localName as a QString. \a query is used as in the overloaded function. \since 4.5 */ void QXmlQuery::bindVariable(const QString &localName, const QXmlQuery &query) { return bindVariable(QXmlName(d->namePool, localName), query); }
void AccelTreeBuilder<FromDocument>::attribute(const QXmlName &name, const QStringRef &value) { /* Attributes adds a namespace binding, so lets synthesize one. * * We optimize by checking whether we have a namespace for which a binding would * be generated. Happens relatively rarely. */ if(name.hasPrefix()) namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix())); m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Attribute, 0, name)); ++m_preNumber; ++m_size.top(); m_isPreviousAtomic = false; if(name.namespaceURI() == StandardNamespaces::xml && name.localName() == StandardLocalNames::id) { const QString normalized(value.toString().simplified()); if(QXmlUtils::isNCName(normalized)) { const QXmlName::LocalNameCode id = m_namePool->allocateLocalName(normalized); const int oldSize = m_document->m_IDs.count(); m_document->m_IDs.insert(id, currentParent()); /* We don't run the value through m_attributeCompress here, because * the likelyhood of it deing identical to another attribute is * very small. */ m_document->data.insert(m_preNumber, normalized); /** * In the case that we're called for doc-available(), m_context is * null, and we need to flag somehow that we failed to load this * document. */ if(oldSize == m_document->m_IDs.count() && m_context) // TODO { Q_ASSERT(m_context); m_context->error(QtXmlPatterns::tr("An %1-attribute with value %2 has already been declared.") .arg(formatKeyword("xml:id"), formatData(normalized)), FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091, this); } } else if(m_context) // TODO { Q_ASSERT(m_context); /* If we're building from an XML Document(e.g, we're fed from QXmlStreamReader, we raise FODC0002, * otherwise XQDY0091. */ m_context->error(QtXmlPatterns::tr("An %1-attribute must have a " "valid %2 as value, which %3 isn't.").arg(formatKeyword("xml:id"), formatType(m_namePool, BuiltinTypes::xsNCName), formatData(value.toString())), FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091, this); } } else m_document->data.insert(m_preNumber, *m_attributeCompress.insert(value.toString())); }
void Loader::load() { QFile in(QLatin1String("tree.xml")); /* LoadingModel::m_result will be null, signalling failure. */ if(!in.open(QIODevice::ReadOnly)) return; QXmlStreamReader reader(&in); while(!reader.atEnd()) { reader.readNext(); switch(reader.tokenType()) { case QXmlStreamReader::StartDocument: /* Fallthrough. */ case QXmlStreamReader::StartElement: { QXmlName name; if(reader.tokenType() == QXmlStreamReader::StartElement) { name = QXmlName(m_namePool, reader.name().toString(), reader.namespaceUri().toString(), reader.prefix().toString()); } /* Else, the name is null. */ LoadingModel::Node *const tmp = new LoadingModel::Node(reader.tokenType() == QXmlStreamReader::StartElement ? QXmlNodeModelIndex::Element : QXmlNodeModelIndex::Document, m_parentStack.top(), QString(), name); m_result.append(tmp); if(m_currentNode) { if(m_currentNode->parent == m_parentStack.top()) m_currentNode->followingSibling = tmp; } const QXmlStreamAttributes attributes(reader.attributes()); const int len = attributes.count(); for(int i = 0; i < len; ++i) { const QXmlStreamAttribute &attr = attributes.at(i); const LoadingModel::Node *const a = new LoadingModel::Node(QXmlNodeModelIndex::Attribute, m_parentStack.top(), attr.value().toString(), QXmlName(m_namePool, attr.name().toString(), attr.namespaceUri().toString(), attr.prefix().toString())); /* We add it also to m_result such that compareOrder() is correct * for attributes. m_result owns a. */ tmp->attributes.append(a); m_result.append(a); } adjustSiblings(tmp); m_parentStack.push(m_currentNode); break; } case QXmlStreamReader::EndDocument: /* Fallthrough. */ case QXmlStreamReader::EndElement: { m_currentNode->followingSibling = 0; m_currentNode = m_parentStack.pop(); if(reader.tokenType() == QXmlStreamReader::EndDocument) const_cast<LoadingModel::Node *>(m_result.first())->followingSibling = 0; break; } case QXmlStreamReader::Characters: { LoadingModel::Node *const tmp = new LoadingModel::Node(QXmlNodeModelIndex::Text, m_parentStack.top(), reader.text().toString()); m_result.append(tmp); adjustSiblings(tmp); break; } case QXmlStreamReader::ProcessingInstruction: { LoadingModel::Node *const tmp = new LoadingModel::Node(QXmlNodeModelIndex::ProcessingInstruction, m_parentStack.top(), reader.processingInstructionData().toString(), QXmlName(m_namePool, reader.processingInstructionTarget().toString())); m_result.append(tmp); adjustSiblings(tmp); break; } case QXmlStreamReader::Comment: { LoadingModel::Node *const tmp = new LoadingModel::Node(QXmlNodeModelIndex::Comment, m_parentStack.top(), reader.text().toString()); m_result.append(tmp); adjustSiblings(tmp); break; } case QXmlStreamReader::DTD: /* Fallthrough. */ case QXmlStreamReader::EntityReference: { Q_ASSERT_X(false, Q_FUNC_INFO, "We don't support this."); /* Fallthrough. */ } case QXmlStreamReader::NoToken: /* Fallthrough. */ case QXmlStreamReader::Invalid: { qWarning(qPrintable(reader.errorString())); m_result.clear(); return; } } } if(reader.hasError()) { qWarning(qPrintable(reader.errorString())); m_result.clear(); } }