1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include "private/qxmlutils_p.h"
45 #include "qxpathhelper_p.h"
47 #include "qnamepool_p.h"
51 using namespace QPatternist;
55 m_localNameMapping .reserve(DefaultLocalNameCapacity + StandardLocalNameCount);
56 m_localNames .reserve(DefaultLocalNameCapacity + StandardLocalNameCount);
57 m_namespaceMapping .reserve(DefaultURICapacity + StandardNamespaceCount);
58 m_namespaces .reserve(DefaultURICapacity + StandardNamespaceCount);
59 m_prefixes .reserve(DefaultPrefixCapacity + StandardPrefixCount);
60 m_prefixMapping .reserve(DefaultPrefixCapacity + StandardPrefixCount);
64 unlockedAllocateNamespace(QString());
65 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2005/xpath-functions"));
66 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2005/xquery-local-functions"));
67 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/XML/1998/namespace"));
68 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2000/xmlns/"));
69 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2001/XMLSchema"));
70 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/2001/XMLSchema-instance"));
71 unlockedAllocateNamespace(QLatin1String("http://www.w3.org/1999/XSL/Transform"));
73 /* For UndeclarePrefix, StopNamespaceInheritance and InternalXSLT. We use two
74 * arbitrary strings that aren't used. For instance, if we just used an
75 * empty QString, unlockedAllocateNamespace() would assign it
76 * StandardNamespaces::empty. However, it's important that the string
77 * is an invalid namespace, since otherwise user strings would get
78 * assigned the same IDs. */
79 unlockedAllocateNamespace(QLatin1String(" | 1 "));
80 unlockedAllocateNamespace(QLatin1String(" | 2 "));
81 unlockedAllocateNamespace(QLatin1String(" | InternalXSLT"));
83 Q_ASSERT_X(m_namespaces.count() == StandardNamespaceCount, Q_FUNC_INFO,
84 qPrintable(QString::fromLatin1("Expected is %1, actual is %2.").arg(StandardNamespaceCount).arg(m_namespaces.count())));
89 unlockedAllocatePrefix(QString());
90 unlockedAllocatePrefix(QLatin1String("fn"));
91 unlockedAllocatePrefix(QLatin1String("local"));
92 unlockedAllocatePrefix(QLatin1String("xml"));
93 unlockedAllocatePrefix(QLatin1String("xmlns"));
94 unlockedAllocatePrefix(QLatin1String("xs"));
95 unlockedAllocatePrefix(QLatin1String("xsi"));
96 unlockedAllocatePrefix(QLatin1String("ns0"));
97 unlockedAllocatePrefix(QLatin1String("|||")); /* Invalid NCName, for StopNamespaceInheritance. */
99 Q_ASSERT_X(m_prefixes.count() == StandardPrefixCount, Q_FUNC_INFO,
100 qPrintable(QString::fromLatin1("Expected is %1, actual is %2.").arg(StandardPrefixCount).arg(m_prefixes.count())));
105 /* Same order as the enum in StandardLocalNames. That is, alphabetically. */
106 unlockedAllocateLocalName(QLatin1String("abs"));
107 unlockedAllocateLocalName(QLatin1String("adjust-dateTime-to-timezone"));
108 unlockedAllocateLocalName(QLatin1String("adjust-date-to-timezone"));
109 unlockedAllocateLocalName(QLatin1String("adjust-time-to-timezone"));
110 unlockedAllocateLocalName(QLatin1String("all"));
111 unlockedAllocateLocalName(QLatin1String("arity"));
112 unlockedAllocateLocalName(QLatin1String("avg"));
113 unlockedAllocateLocalName(QLatin1String("base"));
114 unlockedAllocateLocalName(QLatin1String("base-uri"));
115 unlockedAllocateLocalName(QLatin1String("boolean"));
116 unlockedAllocateLocalName(QLatin1String("ceiling"));
117 unlockedAllocateLocalName(QLatin1String("codepoint-equal"));
118 unlockedAllocateLocalName(QLatin1String("codepoints-to-string"));
119 unlockedAllocateLocalName(QLatin1String("collection"));
120 unlockedAllocateLocalName(QLatin1String("compare"));
121 unlockedAllocateLocalName(QLatin1String("concat"));
122 unlockedAllocateLocalName(QLatin1String("contains"));
123 unlockedAllocateLocalName(QLatin1String("count"));
124 unlockedAllocateLocalName(QLatin1String("current"));
125 unlockedAllocateLocalName(QLatin1String("current-date"));
126 unlockedAllocateLocalName(QLatin1String("current-dateTime"));
127 unlockedAllocateLocalName(QLatin1String("current-time"));
128 unlockedAllocateLocalName(QLatin1String("data"));
129 unlockedAllocateLocalName(QLatin1String("dateTime"));
130 unlockedAllocateLocalName(QLatin1String("day-from-date"));
131 unlockedAllocateLocalName(QLatin1String("day-from-dateTime"));
132 unlockedAllocateLocalName(QLatin1String("days-from-duration"));
133 unlockedAllocateLocalName(QLatin1String("deep-equal"));
134 unlockedAllocateLocalName(QLatin1String("default"));
135 unlockedAllocateLocalName(QLatin1String("default-collation"));
136 unlockedAllocateLocalName(QLatin1String("distinct-values"));
137 unlockedAllocateLocalName(QLatin1String("doc"));
138 unlockedAllocateLocalName(QLatin1String("doc-available"));
139 unlockedAllocateLocalName(QLatin1String("document"));
140 unlockedAllocateLocalName(QLatin1String("document-uri"));
141 unlockedAllocateLocalName(QLatin1String("element-available"));
142 unlockedAllocateLocalName(QLatin1String("empty"));
143 unlockedAllocateLocalName(QLatin1String("encode-for-uri"));
144 unlockedAllocateLocalName(QLatin1String("ends-with"));
145 unlockedAllocateLocalName(QLatin1String("error"));
146 unlockedAllocateLocalName(QLatin1String("escape-html-uri"));
147 unlockedAllocateLocalName(QLatin1String("exactly-one"));
148 unlockedAllocateLocalName(QLatin1String("exists"));
149 unlockedAllocateLocalName(QLatin1String("false"));
150 unlockedAllocateLocalName(QLatin1String("floor"));
151 unlockedAllocateLocalName(QLatin1String("function-available"));
152 unlockedAllocateLocalName(QLatin1String("function-name"));
153 unlockedAllocateLocalName(QLatin1String("generate-id"));
154 unlockedAllocateLocalName(QLatin1String("generic-string-join"));
155 unlockedAllocateLocalName(QLatin1String("hours-from-dateTime"));
156 unlockedAllocateLocalName(QLatin1String("hours-from-duration"));
157 unlockedAllocateLocalName(QLatin1String("hours-from-time"));
158 unlockedAllocateLocalName(QLatin1String("id"));
159 unlockedAllocateLocalName(QLatin1String("idref"));
160 unlockedAllocateLocalName(QLatin1String("implicit-timezone"));
161 unlockedAllocateLocalName(QLatin1String("index-of"));
162 unlockedAllocateLocalName(QLatin1String("in-scope-prefixes"));
163 unlockedAllocateLocalName(QLatin1String("insert-before"));
164 unlockedAllocateLocalName(QLatin1String("iri-to-uri"));
165 unlockedAllocateLocalName(QLatin1String("is-schema-aware"));
166 unlockedAllocateLocalName(QLatin1String("key"));
167 unlockedAllocateLocalName(QLatin1String("lang"));
168 unlockedAllocateLocalName(QLatin1String("last"));
169 unlockedAllocateLocalName(QLatin1String("local-name"));
170 unlockedAllocateLocalName(QLatin1String("local-name-from-QName"));
171 unlockedAllocateLocalName(QLatin1String("lower-case"));
172 unlockedAllocateLocalName(QLatin1String("matches"));
173 unlockedAllocateLocalName(QLatin1String("max"));
174 unlockedAllocateLocalName(QLatin1String("min"));
175 unlockedAllocateLocalName(QLatin1String("minutes-from-dateTime"));
176 unlockedAllocateLocalName(QLatin1String("minutes-from-duration"));
177 unlockedAllocateLocalName(QLatin1String("minutes-from-time"));
178 unlockedAllocateLocalName(QLatin1String("month-from-date"));
179 unlockedAllocateLocalName(QLatin1String("month-from-dateTime"));
180 unlockedAllocateLocalName(QLatin1String("months-from-duration"));
181 unlockedAllocateLocalName(QLatin1String("name"));
182 unlockedAllocateLocalName(QLatin1String("namespace-uri"));
183 unlockedAllocateLocalName(QLatin1String("namespace-uri-for-prefix"));
184 unlockedAllocateLocalName(QLatin1String("namespace-uri-from-QName"));
185 unlockedAllocateLocalName(QLatin1String("nilled"));
186 unlockedAllocateLocalName(QLatin1String("node-name"));
187 unlockedAllocateLocalName(QLatin1String("normalize-space"));
188 unlockedAllocateLocalName(QLatin1String("normalize-unicode"));
189 unlockedAllocateLocalName(QLatin1String("not"));
190 unlockedAllocateLocalName(QLatin1String("number"));
191 unlockedAllocateLocalName(QLatin1String("one-or-more"));
192 unlockedAllocateLocalName(QLatin1String("position"));
193 unlockedAllocateLocalName(QLatin1String("prefix-from-QName"));
194 unlockedAllocateLocalName(QLatin1String("product-name"));
195 unlockedAllocateLocalName(QLatin1String("product-version"));
196 unlockedAllocateLocalName(QLatin1String("property-name"));
197 unlockedAllocateLocalName(QLatin1String("QName"));
198 unlockedAllocateLocalName(QLatin1String("remove"));
199 unlockedAllocateLocalName(QLatin1String("replace"));
200 unlockedAllocateLocalName(QLatin1String("resolve-QName"));
201 unlockedAllocateLocalName(QLatin1String("resolve-uri"));
202 unlockedAllocateLocalName(QLatin1String("reverse"));
203 unlockedAllocateLocalName(QLatin1String("root"));
204 unlockedAllocateLocalName(QLatin1String("round"));
205 unlockedAllocateLocalName(QLatin1String("round-half-to-even"));
206 unlockedAllocateLocalName(QLatin1String("seconds-from-dateTime"));
207 unlockedAllocateLocalName(QLatin1String("seconds-from-duration"));
208 unlockedAllocateLocalName(QLatin1String("seconds-from-time"));
209 unlockedAllocateLocalName(QLatin1String("sourceValue"));
210 unlockedAllocateLocalName(QLatin1String("starts-with"));
211 unlockedAllocateLocalName(QLatin1String("static-base-uri"));
212 unlockedAllocateLocalName(QLatin1String("string"));
213 unlockedAllocateLocalName(QLatin1String("string-join"));
214 unlockedAllocateLocalName(QLatin1String("string-length"));
215 unlockedAllocateLocalName(QLatin1String("string-to-codepoints"));
216 unlockedAllocateLocalName(QLatin1String("subsequence"));
217 unlockedAllocateLocalName(QLatin1String("substring"));
218 unlockedAllocateLocalName(QLatin1String("substring-after"));
219 unlockedAllocateLocalName(QLatin1String("substring-before"));
220 unlockedAllocateLocalName(QLatin1String("sum"));
221 unlockedAllocateLocalName(QLatin1String("supports-backwards-compatibility"));
222 unlockedAllocateLocalName(QLatin1String("supports-serialization"));
223 unlockedAllocateLocalName(QLatin1String("system-property"));
224 unlockedAllocateLocalName(QLatin1String("timezone-from-date"));
225 unlockedAllocateLocalName(QLatin1String("timezone-from-dateTime"));
226 unlockedAllocateLocalName(QLatin1String("timezone-from-time"));
227 unlockedAllocateLocalName(QLatin1String("tokenize"));
228 unlockedAllocateLocalName(QLatin1String("trace"));
229 unlockedAllocateLocalName(QLatin1String("translate"));
230 unlockedAllocateLocalName(QLatin1String("true"));
231 unlockedAllocateLocalName(QLatin1String("type-available"));
232 unlockedAllocateLocalName(QLatin1String("unordered"));
233 unlockedAllocateLocalName(QLatin1String("unparsed-entity-public-id"));
234 unlockedAllocateLocalName(QLatin1String("unparsed-entity-uri"));
235 unlockedAllocateLocalName(QLatin1String("unparsed-text"));
236 unlockedAllocateLocalName(QLatin1String("unparsed-text-available"));
237 unlockedAllocateLocalName(QLatin1String("upper-case"));
238 unlockedAllocateLocalName(QLatin1String("vendor"));
239 unlockedAllocateLocalName(QLatin1String("vendor-url"));
240 unlockedAllocateLocalName(QLatin1String("version"));
241 unlockedAllocateLocalName(QLatin1String("xml"));
242 unlockedAllocateLocalName(QLatin1String("xmlns"));
243 unlockedAllocateLocalName(QLatin1String("year-from-date"));
244 unlockedAllocateLocalName(QLatin1String("year-from-dateTime"));
245 unlockedAllocateLocalName(QLatin1String("years-from-duration"));
246 unlockedAllocateLocalName(QLatin1String("zero-or-one"));
247 Q_ASSERT(m_localNames.count() == StandardLocalNameCount);
251 QXmlName NamePool::allocateQName(const QString &uri, const QString &localName, const QString &prefix)
253 QWriteLocker l(&lock);
257 for(int i = 0; i < localName.length(); ++i)
258 codepoints.append(QString::number(localName.at(i).unicode()) + QLatin1Char(' '));
260 pDebug() << Q_FUNC_INFO << localName << "codepoints:" << codepoints;
263 Q_ASSERT_X(QXmlUtils::isNCName(localName), Q_FUNC_INFO,
264 qPrintable(QString::fromLatin1("'%1' is an invalid NCName.").arg(localName)));
266 const QXmlName::NamespaceCode nsCode = unlockedAllocateNamespace(uri);
267 const QXmlName::LocalNameCode localCode = unlockedAllocateLocalName(localName);
269 Q_ASSERT(prefix.isEmpty() || QXmlUtils::isNCName(prefix));
270 const QXmlName::PrefixCode prefixCode = unlockedAllocatePrefix(prefix);
272 return QXmlName(nsCode, localCode, prefixCode);
275 QXmlName NamePool::allocateBinding(const QString &prefix, const QString &uri)
277 QWriteLocker l(&lock);
279 Q_ASSERT_X(prefix.isEmpty() || QXmlUtils::isNCName(prefix), Q_FUNC_INFO,
280 qPrintable(QString::fromLatin1("%1 is an invalid prefix.").arg(prefix)));
281 const QXmlName::NamespaceCode nsCode = unlockedAllocateNamespace(uri);
283 Q_ASSERT(prefix.isEmpty() || QXmlUtils::isNCName(prefix));
284 const QXmlName::PrefixCode prefixCode = unlockedAllocatePrefix(prefix);
286 return QXmlName(nsCode, StandardLocalNames::empty, prefixCode);
289 QXmlName::LocalNameCode NamePool::unlockedAllocateLocalName(const QString &ln)
291 Q_ASSERT_X(QXmlUtils::isNCName(ln), Q_FUNC_INFO,
292 qPrintable(QString::fromLatin1("Invalid local name: \"%1\"").arg(ln)));
294 int indexInLocalNames = m_localNameMapping.value(ln, NoSuchValue);
296 if(indexInLocalNames == NoSuchValue)
298 indexInLocalNames = m_localNames.count();
299 m_localNames.append(ln);
300 m_localNameMapping.insert(ln, indexInLocalNames);
303 return indexInLocalNames;
306 QXmlName::PrefixCode NamePool::unlockedAllocatePrefix(const QString &prefix)
308 int indexInPrefixes = m_prefixMapping.value(prefix, NoSuchValue);
310 if(indexInPrefixes == NoSuchValue)
312 indexInPrefixes = m_prefixes.count();
313 m_prefixes.append(prefix);
314 m_prefixMapping.insert(prefix, indexInPrefixes);
317 return indexInPrefixes;
320 QXmlName::NamespaceCode NamePool::unlockedAllocateNamespace(const QString &uri)
322 int indexInURIs = m_namespaceMapping.value(uri, NoSuchValue);
324 if(indexInURIs == NoSuchValue)
326 indexInURIs = m_namespaces.count();
327 m_namespaces.append(uri);
328 m_namespaceMapping.insert(uri, indexInURIs);
334 const QString &NamePool::displayPrefix(const QXmlName::NamespaceCode nc) const
338 case StandardNamespaces::xmlns: return m_prefixes.at(StandardPrefixes::xmlns);
339 case StandardNamespaces::local: return m_prefixes.at(StandardPrefixes::local);
340 case StandardNamespaces::xs: return m_prefixes.at(StandardPrefixes::xs);
341 case StandardNamespaces::xml: return m_prefixes.at(StandardPrefixes::xml);
342 case StandardNamespaces::fn: return m_prefixes.at(StandardPrefixes::fn);
343 default: return m_prefixes.at(StandardPrefixes::empty);
347 QString NamePool::displayName(const QXmlName qName) const
349 QReadLocker l(&lock);
351 if(qName.hasNamespace())
353 if(qName.namespaceURI() == StandardNamespaces::InternalXSLT)
354 return QLatin1Char('#') + m_localNames.at(qName.localName());
356 const QString &p = displayPrefix(qName.namespaceURI());
359 return QLatin1Char('{') + m_namespaces.at(qName.namespaceURI()) + QLatin1Char('}') + toLexical(qName);
361 return p + QLatin1Char(':') + m_localNames.at(qName.localName());
364 return m_localNames.at(qName.localName());
367 QString NamePool::toClarkName(const QXmlName &name) const
370 return QLatin1String("QXmlName(null)");
373 if(name.hasNamespace())
375 const QString ns(stringForNamespace(name.namespaceURI()));
376 const QString p(stringForPrefix(name.prefix()));
377 const QString l(stringForLocalName(name.localName()));
379 return QChar::fromLatin1('{')
381 + QChar::fromLatin1('}')
382 + (p.isEmpty() ? l : p + QChar::fromLatin1(':') + l);
385 return stringForLocalName(name.localName());
389 QXmlName NamePool::fromClarkName(const QString &clarkName)
391 if(clarkName.isEmpty())
394 if(clarkName.at(0) == QLatin1Char('{'))
396 const int indexOfRight = clarkName.indexOf(QLatin1Char('}'));
397 const QString qName(clarkName.right((clarkName.length() - indexOfRight) - 1));
399 if(!XPathHelper::isQName(qName))
405 XPathHelper::splitQName(qName, prefix, localName);
407 return allocateQName(clarkName.mid(1, indexOfRight - 1),
412 if(QXmlName::isNCName(clarkName))
413 return allocateQName(QString(), clarkName);