1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QStringList>
44 #include "private/qxmlutils_p.h"
45 #include "qbuiltintypes_p.h"
46 #include "qcommonvalues_p.h"
47 #include "qnamepool_p.h"
49 #include "qxpathhelper_p.h"
53 using namespace QPatternist;
55 bool XPathHelper::isReservedNamespace(const QXmlName::NamespaceCode ns)
57 /* The order is because of that XFN and WXS are the most common. */
58 return ns == StandardNamespaces::fn ||
59 ns == StandardNamespaces::xs ||
60 ns == StandardNamespaces::xml ||
61 ns == StandardNamespaces::xsi;
64 bool XPathHelper::isQName(const QString &qName)
66 const QStringList result(qName.split(QLatin1Char(':')));
67 const int c = result.count();
71 return QXmlUtils::isNCName(result.first()) &&
72 QXmlUtils::isNCName(result.last());
75 return QXmlUtils::isNCName(result.first());
80 void XPathHelper::splitQName(const QString &qName, QString &prefix, QString &ncName)
82 Q_ASSERT_X(isQName(qName), Q_FUNC_INFO,
83 "qName must be a valid QName.");
85 const QStringList result(qName.split(QLatin1Char(':')));
87 if(result.count() == 1)
89 Q_ASSERT(QXmlUtils::isNCName(result.first()));
90 ncName = result.first();
94 Q_ASSERT(result.count() == 2);
95 Q_ASSERT(QXmlUtils::isNCName(result.first()));
96 Q_ASSERT(QXmlUtils::isNCName(result.last()));
98 prefix = result.first();
99 ncName = result.last();
103 ItemType::Ptr XPathHelper::typeFromKind(const QXmlNodeModelIndex::NodeKind nodeKind)
107 case QXmlNodeModelIndex::Element:
108 return BuiltinTypes::element;
109 case QXmlNodeModelIndex::Attribute:
110 return BuiltinTypes::attribute;
111 case QXmlNodeModelIndex::Text:
112 return BuiltinTypes::text;
113 case QXmlNodeModelIndex::ProcessingInstruction:
114 return BuiltinTypes::pi;
115 case QXmlNodeModelIndex::Comment:
116 return BuiltinTypes::comment;
117 case QXmlNodeModelIndex::Document:
118 return BuiltinTypes::document;
121 Q_ASSERT_X(false, Q_FUNC_INFO,
122 "A node type that doesn't exist in the XPath Data Model was encountered.");
123 return ItemType::Ptr(); /* Dummy, silence compiler warning. */
128 QUrl XPathHelper::normalizeQueryURI(const QUrl &uri)
130 Q_ASSERT_X(uri.isEmpty() || uri.isValid(), Q_FUNC_INFO,
131 "The URI passed to QXmlQuery::setQuery() must be valid or empty.");
133 return QUrl::fromLocalFile(QCoreApplication::applicationFilePath());
134 else if(uri.isRelative())
135 return QUrl::fromLocalFile(QCoreApplication::applicationFilePath()).resolved(uri);