| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qabstractfloat_p.h"
|
|---|
| 43 | #include "qanyuri_p.h"
|
|---|
| 44 | #include "qboolean_p.h"
|
|---|
| 45 | #include "qbuiltintypes_p.h"
|
|---|
| 46 | #include "qcommonnamespaces_p.h"
|
|---|
| 47 | #include "qcommonvalues_p.h"
|
|---|
| 48 | #include "qliteral_p.h"
|
|---|
| 49 | #include "qatomicstring_p.h"
|
|---|
| 50 |
|
|---|
| 51 | #include "qnodefns_p.h"
|
|---|
| 52 |
|
|---|
| 53 | QT_BEGIN_NAMESPACE
|
|---|
| 54 |
|
|---|
| 55 | using namespace QPatternist;
|
|---|
| 56 |
|
|---|
| 57 | Item NameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 58 | {
|
|---|
| 59 | const Item node(m_operands.first()->evaluateSingleton(context));
|
|---|
| 60 |
|
|---|
| 61 | if(node)
|
|---|
| 62 | {
|
|---|
| 63 | const QXmlName name(node.asNode().name());
|
|---|
| 64 |
|
|---|
| 65 | if(name.isNull())
|
|---|
| 66 | return CommonValues::EmptyString;
|
|---|
| 67 | else
|
|---|
| 68 | return AtomicString::fromValue(context->namePool()->toLexical(name));
|
|---|
| 69 | }
|
|---|
| 70 | else
|
|---|
| 71 | return CommonValues::EmptyString;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Item LocalNameFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 75 | {
|
|---|
| 76 | const Item node(m_operands.first()->evaluateSingleton(context));
|
|---|
| 77 |
|
|---|
| 78 | if(node)
|
|---|
| 79 | {
|
|---|
| 80 | const QXmlName name(node.asNode().name());
|
|---|
| 81 |
|
|---|
| 82 | if(name.isNull())
|
|---|
| 83 | return CommonValues::EmptyString;
|
|---|
| 84 | else
|
|---|
| 85 | return AtomicString::fromValue(context->namePool()->stringForLocalName(name.localName()));
|
|---|
| 86 | }
|
|---|
| 87 | else
|
|---|
| 88 | return CommonValues::EmptyString;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | Item NamespaceURIFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 92 | {
|
|---|
| 93 | const Item node(m_operands.first()->evaluateSingleton(context));
|
|---|
| 94 |
|
|---|
| 95 | if(node)
|
|---|
| 96 | {
|
|---|
| 97 | const QXmlName name(node.asNode().name());
|
|---|
| 98 |
|
|---|
| 99 | if(name.isNull())
|
|---|
| 100 | return CommonValues::EmptyAnyURI;
|
|---|
| 101 | else
|
|---|
| 102 | return toItem(AnyURI::fromValue(context->namePool()->stringForNamespace(name.namespaceURI())));
|
|---|
| 103 | }
|
|---|
| 104 | else
|
|---|
| 105 | return CommonValues::EmptyAnyURI;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | Item NumberFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 109 | {
|
|---|
| 110 | const Item item(m_operands.first()->evaluateSingleton(context));
|
|---|
| 111 |
|
|---|
| 112 | if(!item)
|
|---|
| 113 | return CommonValues::DoubleNaN;
|
|---|
| 114 |
|
|---|
| 115 | const Item val(cast(item, context));
|
|---|
| 116 | Q_ASSERT(val);
|
|---|
| 117 |
|
|---|
| 118 | if(val.as<AtomicValue>()->hasError())
|
|---|
| 119 | return CommonValues::DoubleNaN;
|
|---|
| 120 | else
|
|---|
| 121 | return val;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | Expression::Ptr NumberFN::typeCheck(const StaticContext::Ptr &context,
|
|---|
| 125 | const SequenceType::Ptr &reqType)
|
|---|
| 126 | {
|
|---|
| 127 | const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
|
|---|
| 128 | const ItemType::Ptr sourceType(m_operands.first()->staticType()->itemType());
|
|---|
| 129 |
|
|---|
| 130 | if(BuiltinTypes::xsDouble->xdtTypeMatches(sourceType))
|
|---|
| 131 | {
|
|---|
| 132 | /* The operand is already xs:double, no need for fn:number(). */
|
|---|
| 133 | return m_operands.first()->typeCheck(context, reqType);
|
|---|
| 134 | }
|
|---|
| 135 | else if(prepareCasting(context, sourceType))
|
|---|
| 136 | return me;
|
|---|
| 137 | else
|
|---|
| 138 | {
|
|---|
| 139 | /* Casting to xs:double will never succeed and we would always return NaN.*/
|
|---|
| 140 | return wrapLiteral(CommonValues::DoubleNaN, context, this)->typeCheck(context, reqType);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | bool LangFN::isLangMatch(const QString &candidate, const QString &toMatch)
|
|---|
| 145 | {
|
|---|
| 146 | if(QString::compare(candidate, toMatch, Qt::CaseInsensitive) == 0)
|
|---|
| 147 | return true;
|
|---|
| 148 |
|
|---|
| 149 | return candidate.startsWith(toMatch, Qt::CaseInsensitive)
|
|---|
| 150 | && candidate.length() > toMatch.length()
|
|---|
| 151 | && candidate.at(toMatch.length()) == QLatin1Char('-');
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | Item LangFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 155 | {
|
|---|
| 156 | const Item langArg(m_operands.first()->evaluateSingleton(context));
|
|---|
| 157 | const QString lang(langArg ? langArg.stringValue() : QString());
|
|---|
| 158 |
|
|---|
| 159 | const QXmlName xmlLang(StandardNamespaces::xml, StandardLocalNames::lang, StandardPrefixes::xml);
|
|---|
| 160 | const QXmlNodeModelIndex langNode(m_operands.at(1)->evaluateSingleton(context).asNode());
|
|---|
| 161 |
|
|---|
| 162 | const QXmlNodeModelIndex::Iterator::Ptr ancestors(langNode.iterate(QXmlNodeModelIndex::AxisAncestorOrSelf));
|
|---|
| 163 | QXmlNodeModelIndex ancestor(ancestors->next());
|
|---|
| 164 |
|
|---|
| 165 | while(!ancestor.isNull())
|
|---|
| 166 | {
|
|---|
| 167 | const QXmlNodeModelIndex::Iterator::Ptr attributes(ancestor.iterate(QXmlNodeModelIndex::AxisAttribute));
|
|---|
| 168 | QXmlNodeModelIndex attribute(attributes->next());
|
|---|
| 169 |
|
|---|
| 170 | while(!attribute.isNull())
|
|---|
| 171 | {
|
|---|
| 172 | Q_ASSERT(attribute.kind() == QXmlNodeModelIndex::Attribute);
|
|---|
| 173 |
|
|---|
| 174 | if(attribute.name() == xmlLang)
|
|---|
| 175 | {
|
|---|
| 176 | if(isLangMatch(attribute.stringValue(), lang))
|
|---|
| 177 | return CommonValues::BooleanTrue;
|
|---|
| 178 | else
|
|---|
| 179 | return CommonValues::BooleanFalse;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | attribute = attributes->next();
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | ancestor = ancestors->next();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | return CommonValues::BooleanFalse;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | Item RootFN::evaluateSingleton(const DynamicContext::Ptr &context) const
|
|---|
| 192 | {
|
|---|
| 193 | const Item arg(m_operands.first()->evaluateSingleton(context));
|
|---|
| 194 |
|
|---|
| 195 | if(arg)
|
|---|
| 196 | return arg.asNode().root();
|
|---|
| 197 | else
|
|---|
| 198 | return Item();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | SequenceType::Ptr RootFN::staticType() const
|
|---|
| 202 | {
|
|---|
| 203 | if(m_operands.isEmpty())
|
|---|
| 204 | return makeGenericSequenceType(BuiltinTypes::node, Cardinality::exactlyOne());
|
|---|
| 205 | else
|
|---|
| 206 | return makeGenericSequenceType(BuiltinTypes::node, m_operands.first()->staticType()->cardinality().toWithoutMany());
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | QT_END_NAMESPACE
|
|---|