| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@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 | 
|---|