LibreOffice Module xmloff (master) 1
namespacemap.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <rtl/ustring.hxx>
23#include <rtl/ustrbuf.hxx>
24#include <sal/log.hxx>
25
26#include <xmloff/xmltoken.hxx>
28
30#include <o3tl/string_view.hxx>
31
32
33using namespace ::xmloff::token;
34
35/* The basic idea of this class is that we have two ways to search our
36 * data, by prefix and by key. We use an unordered_map for fast prefix
37 * searching and an STL map for fast key searching.
38 *
39 * The references to an 'Index' refer to an earlier implementation of the
40 * name space map and remain to support code which uses these interfaces.
41 *
42 * In this implementation, key and index should always be the same number.
43 *
44 * All references to Indices are now deprecated and the corresponding
45 * 'Key' methods should be used instead
46 *
47 * Martin 13/06/01
48 */
49
50const OUString sEmpty;
51
53: sXMLNS( GetXMLToken ( XML_XMLNS ) )
54{
55 // approx worst-case size
56 aNameHash.reserve(20);
57 maKeyToNamespaceMap.reserve(20);
58}
59
61: sXMLNS( GetXMLToken ( XML_XMLNS ) )
62{
63 aNameHash = rMap.aNameHash;
65}
66
68{
69 aNameHash = rMap.aNameHash;
71 return *this;
72}
73
75{
76}
77
79{
80 aNameHash.clear();
81 aNameCache.clear();
82 maKeyToNamespaceMap.clear();
83 aQNameCache.clear();
84}
85
86
88{
89 return aNameHash == rCmp.aNameHash;
90}
91
92sal_uInt16 SvXMLNamespaceMap::Add_( const OUString& rPrefix, const OUString &rName, sal_uInt16 nKey )
93{
94 if( XML_NAMESPACE_UNKNOWN == nKey )
95 {
96 // create a new unique key with UNKNOWN flag set
98 do
99 {
100 auto aIter = maKeyToNamespaceMap.find ( nKey );
101 if( aIter == maKeyToNamespaceMap.end() )
102 break;
103 nKey++;
104 }
105 while ( true );
106 }
107 aNameHash.insert_or_assign( rPrefix, NameSpaceEntry{ rName, rPrefix, nKey} );
108 maKeyToNamespaceMap.insert_or_assign( nKey, KeyToNameSpaceMapEntry{ rName, rPrefix} );
109 return nKey;
110}
111
112sal_uInt16 SvXMLNamespaceMap::Add( const OUString& rPrefix, const OUString& rName,
113 sal_uInt16 nKey )
114{
115 if( XML_NAMESPACE_UNKNOWN == nKey )
116 nKey = GetKeyByName( rName );
117
118#ifdef NDEBUG
119 if( XML_NAMESPACE_NONE == nKey )
120 return USHRT_MAX;
121#else
122 assert(XML_NAMESPACE_NONE != nKey);
123#endif
124
125 if ( aNameHash.find ( rPrefix ) == aNameHash.end() )
126 nKey = Add_( rPrefix, rName, nKey );
127
128 return nKey;
129}
130
131sal_uInt16 SvXMLNamespaceMap::AddIfKnown( const OUString& rPrefix, const OUString& rName )
132{
133 sal_uInt16 nKey = GetKeyByName( rName );
134
135#ifdef NDEBUG
136 if( XML_NAMESPACE_NONE == nKey )
138#else
139 assert(nKey != XML_NAMESPACE_NONE);
140#endif
141
142 if( XML_NAMESPACE_UNKNOWN != nKey )
143 {
144 NameSpaceHash::const_iterator aIter = aNameHash.find( rPrefix );
145 if( aIter == aNameHash.end() || (*aIter).second.sName != rName )
146 nKey = Add_( rPrefix, rName, nKey );
147 }
148
149 return nKey;
150}
151
152
153sal_uInt16 SvXMLNamespaceMap::GetKeyByPrefix( const OUString& rPrefix ) const
154{
155 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
156 return (aIter != aNameHash.end()) ? (*aIter).second.nKey : USHRT_MAX;
157}
158
159sal_uInt16 SvXMLNamespaceMap::GetKeyByName( const OUString& rName ) const
160{
161 sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
162 auto aIter = std::find_if(aNameHash.cbegin(), aNameHash.cend(),
163 [&rName](const NameSpaceHash::value_type& rEntry) { return rEntry.second.sName == rName; });
164
165 if (aIter != aNameHash.cend())
166 nKey = (*aIter).second.nKey;
167
168 return nKey;
169}
170
171const OUString& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey ) const
172{
173 auto aIter = maKeyToNamespaceMap.find (nKey);
174 return (aIter != maKeyToNamespaceMap.end()) ? (*aIter).second.sPrefix : sEmpty;
175}
176
177const OUString& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey ) const
178{
179 auto aIter = maKeyToNamespaceMap.find (nKey);
180 return (aIter != maKeyToNamespaceMap.end()) ? (*aIter).second.sName : sEmpty;
181}
182
183OUString SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey ) const
184{
185 auto aIter = maKeyToNamespaceMap.find ( nKey );
186 if (aIter == maKeyToNamespaceMap.end())
187 return OUString();
188
189 const OUString & prefix( (*aIter).second.sPrefix );
190 if (prefix.isEmpty()) // default namespace
191 return sXMLNS;
192
193 return sXMLNS + ":" + prefix;
194}
195
196OUString SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey,
197 const OUString& rLocalName,
198 bool bCache) const
199{
200 // We always want to return at least the rLocalName...
201
202 switch ( nKey )
203 {
205 // ...if it's a completely unknown namespace, assert and return the local name
206 SAL_WARN("xmloff.core", "unknown namespace, probable missing xmlns: declaration");
207 [[fallthrough]];
209 // ...if there isn't one, return the local name
210 return rLocalName;
212 {
213 // ...if it's in the xmlns namespace, make the prefix
214 // don't bother caching this, it rarely happens
215 OUStringBuffer sQName;
216 sQName.append ( sXMLNS );
217 if (!rLocalName.isEmpty()) // not default namespace
218 {
219 sQName.append ( ':' );
220 sQName.append ( rLocalName );
221 }
222 return sQName.makeStringAndClear();
223 }
225 {
226 // this namespace is reserved, and needs not to be declared
227 return GetXMLToken(XML_XML) + ":" + rLocalName;
228 }
229 default:
230 {
231 QNameCache::const_iterator aQCacheIter;
232 if (bCache)
233 aQCacheIter = aQNameCache.find ( QNamePair ( nKey, rLocalName ) );
234 else
235 aQCacheIter = aQNameCache.end();
236 if ( aQCacheIter != aQNameCache.end() )
237 return (*aQCacheIter).second;
238 else
239 {
240 auto aIter = maKeyToNamespaceMap.find ( nKey );
241 if ( aIter != maKeyToNamespaceMap.end() )
242 {
243 // ...if it's in our map, make the prefix
244 const OUString & prefix( (*aIter).second.sPrefix );
245 OUStringBuffer sQName(prefix.getLength() + 1 + rLocalName.getLength());
246 if (!prefix.isEmpty()) // not default namespace
247 {
248 sQName.append( prefix );
249 sQName.append( ':' );
250 }
251 sQName.append ( rLocalName );
252 if (bCache)
253 {
254 OUString sString(sQName.makeStringAndClear());
255 aQNameCache.emplace(QNamePair(nKey, rLocalName), sString);
256 return sString;
257 }
258 else
259 return sQName.makeStringAndClear();
260 }
261 else
262 {
263 // ... if it isn't, this is a Bad Thing, assert and return the local name
264 assert(false);
265 return rLocalName;
266 }
267 }
268 }
269 }
270}
271
273 const OUString& rAttrValue,
274 OUString *pLocalName) const
275{
276 return GetKeyByQName(rAttrValue, nullptr, pLocalName, nullptr, QNameMode::AttrValue);
277}
278
284sal_uInt16 SvXMLNamespaceMap::GetKeyByQName(const OUString& rQName,
285 OUString *pPrefix,
286 OUString *pLocalName,
287 OUString *pNamespace,
288 QNameMode const eMode) const
289{
290 sal_uInt16 nKey;
291
292 NameSpaceHash::const_iterator it;
294 it = aNameCache.find ( rQName );
295 else
296 it = aNameCache.end();
297 if ( it != aNameCache.end() )
298 {
299 const NameSpaceEntry &rEntry = (*it).second;
300 if ( pPrefix )
301 *pPrefix = rEntry.sPrefix;
302 if ( pLocalName )
303 *pLocalName = rEntry.sName;
304 nKey = rEntry.nKey;
305 if ( pNamespace )
306 {
307 auto aMapIter = maKeyToNamespaceMap.find (nKey);
308 *pNamespace = aMapIter != maKeyToNamespaceMap.end() ? (*aMapIter).second.sName : OUString();
309 }
310 }
311 else
312 {
313 OUString sEntryPrefix, sEntryName;
314
315 sal_Int32 nColonPos = rQName.indexOf( ':' );
316 if( -1 == nColonPos )
317 {
318 // case: no ':' found -> default namespace
319 sEntryName = rQName;
320 }
321 else
322 {
323 // normal case: ':' found -> get prefix/suffix
324 sEntryPrefix = rQName.copy( 0, nColonPos );
325 sEntryName = rQName.copy( nColonPos + 1 );
326 }
327
328 if (eMode == QNameMode::AttrNameCached && sEntryName.indexOf(':') != -1)
329 {
330 SAL_INFO("xmloff", "invalid attribute name with multiple ':'");
331 assert(false);
333 }
334
335 if( pPrefix )
336 *pPrefix = sEntryPrefix;
337 if( pLocalName )
338 *pLocalName = sEntryName;
339
340 NameSpaceHash::const_iterator aIter = aNameHash.find( sEntryPrefix );
341 if ( aIter != aNameHash.end() )
342 {
343 // found: retrieve namespace key
344 nKey = (*aIter).second.nKey;
345 if ( pNamespace )
346 *pNamespace = (*aIter).second.sName;
347 }
348 else if ( sEntryPrefix == sXMLNS )
349 // not found, but xmlns prefix: return xmlns 'namespace'
350 nKey = XML_NAMESPACE_XMLNS;
351 else if( nColonPos == -1 )
352 // not found, and no namespace: 'namespace' none
353 nKey = XML_NAMESPACE_NONE;
354 else
356
358 {
359 aNameCache.insert_or_assign(rQName, NameSpaceEntry{std::move(sEntryName), std::move(sEntryPrefix), nKey});
360 }
361 }
362
363 return nKey;
364}
365
367{
368 return maKeyToNamespaceMap.empty() ? USHRT_MAX : (*maKeyToNamespaceMap.begin()).first;
369}
370
371sal_uInt16 SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey ) const
372{
373 auto aIter = maKeyToNamespaceMap.find ( nLastKey );
374 return (++aIter == maKeyToNamespaceMap.end()) ? USHRT_MAX : (*aIter).first;
375}
376
377
378// All methods after this are deprecated...
379
380sal_uInt16 SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey )
381{
382 return nKey;
383}
385{
386 return maKeyToNamespaceMap.empty() ? USHRT_MAX : (*maKeyToNamespaceMap.begin()).first;
387}
388
389sal_uInt16 SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx ) const
390{
391 auto aIter = maKeyToNamespaceMap.find ( nOldIdx );
392 return (++aIter == maKeyToNamespaceMap.end()) ? USHRT_MAX : (*aIter).first;
393}
394
395void SvXMLNamespaceMap::AddAtIndex( const OUString& rPrefix,
396 const OUString& rName, sal_uInt16 nKey )
397{
398 if( XML_NAMESPACE_UNKNOWN == nKey )
399 nKey = GetKeyByName( rName );
400
401 assert(XML_NAMESPACE_NONE != nKey);
402 if( XML_NAMESPACE_NONE != nKey && ! ( aNameHash.count ( rPrefix ) ) )
403 {
404 Add_( rPrefix, rName, nKey );
405 }
406}
407
408OUString SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx ) const
409{
410 return GetAttrNameByKey( nIdx );
411}
412
413const OUString& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx ) const
414{
415 auto aIter = maKeyToNamespaceMap.find (nIdx);
416 return (aIter != maKeyToNamespaceMap.end()) ? (*aIter).second.sPrefix : sEmpty;
417}
418
419const OUString& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx ) const
420{
421 auto aIter = maKeyToNamespaceMap.find (nIdx);
422 return (aIter != maKeyToNamespaceMap.end()) ? (*aIter).second.sName : sEmpty;
423}
424
425sal_uInt16 SvXMLNamespaceMap::GetIndexByPrefix( const OUString& rPrefix ) const
426{
427 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
428 return (aIter != aNameHash.end()) ? (*aIter).second.nKey : USHRT_MAX;
429}
431 const OUString& rAttrName,
432 OUString *pLocalName) const
433{
434 return GetKeyByQName(rAttrName, nullptr, pLocalName, nullptr, QNameMode::AttrNameCached);
435}
436
437sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName( const OUString& rAttrName,
438 OUString *pPrefix,
439 OUString *pLocalName,
440 OUString *pNamespace ) const
441{
442 return GetKeyByQName(rAttrName, pPrefix, pLocalName, pNamespace, QNameMode::AttrNameCached);
443}
444
445bool SvXMLNamespaceMap::NormalizeURI( OUString& rName )
446{
447 // try OASIS + W3 URI normalization
448 bool bSuccess = NormalizeOasisURN( rName );
449 if( ! bSuccess )
450 bSuccess = NormalizeW3URI( rName );
451 return bSuccess;
452}
453
455{
456 // check if URI matches:
457 // http://www.w3.org/[0-9]*/[:letter:]*
458 // (year)/(WG name)
459 // For the following WG/standards names:
460 // - xforms
461
462 bool bSuccess = false;
463 const OUString& sURIPrefix = GetXMLToken( XML_URI_W3_PREFIX );
464 if( rName.startsWith( sURIPrefix ) )
465 {
466 const OUString& sURISuffix = GetXMLToken( XML_URI_XFORMS_SUFFIX );
467 sal_Int32 nCompareFrom = rName.getLength() - sURISuffix.getLength();
468 if( rName.subView( nCompareFrom ) == sURISuffix )
469 {
470 // found W3 prefix, and xforms suffix
471 rName = GetXMLToken( XML_N_XFORMS_1_0 );
472 bSuccess = true;
473 }
474 }
475 return bSuccess;
476}
477
479{
480 // #i38644#
481 // we exported the wrong namespace for smil, so we correct this here on load
482 // for older documents
483 if( IsXMLToken( rName, ::xmloff::token::XML_N_SVG ) )
484 {
485 rName = GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT );
486 return true;
487 }
488 else if( IsXMLToken( rName, ::xmloff::token::XML_N_FO ) )
489 {
490 rName = GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT );
491 return true;
492 }
493 else if( IsXMLToken( rName, ::xmloff::token::XML_N_SMIL ) ||
494 IsXMLToken( rName, ::xmloff::token::XML_N_SMIL_OLD ) )
495 {
496 rName = GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT );
497 return true;
498 }
499
500
501 // Check if URN matches
502 // :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
503 // |---| |---| |-----|
504 // TC-Id Sub-Id Version
505
506 sal_Int32 nNameLen = rName.getLength();
507 // :urn:oasis:names:tc.*
508 const OUString& rOasisURN = GetXMLToken( XML_URN_OASIS_NAMES_TC );
509 if( !rName.startsWith( rOasisURN ) )
510 return false;
511
512 // :urn:oasis:names:tc:.*
513 sal_Int32 nPos = rOasisURN.getLength();
514 if( nPos >= nNameLen || rName[nPos] != ':' )
515 return false;
516
517 // :urn:oasis:names:tc:[^:]:.*
518 sal_Int32 nTCIdStart = nPos+1;
519 sal_Int32 nTCIdEnd = rName.indexOf( ':', nTCIdStart );
520 if( -1 == nTCIdEnd )
521 return false;
522
523 // :urn:oasis:names:tc:[^:]:xmlns.*
524 nPos = nTCIdEnd + 1;
525 std::u16string_view sTmp( rName.subView( nPos ) );
526 const OUString& rXMLNS = GetXMLToken( XML_XMLNS );
527 if( !o3tl::starts_with(sTmp, rXMLNS ) )
528 return false;
529
530 // :urn:oasis:names:tc:[^:]:xmlns:.*
531 nPos += rXMLNS.getLength();
532 if( nPos >= nNameLen || rName[nPos] != ':' )
533 return false;
534
535 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
536 nPos = rName.indexOf( ':', nPos+1 );
537 if( -1 == nPos )
538 return false;
539
540 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
541 sal_Int32 nVersionStart = nPos+1;
542 if( nVersionStart+2 >= nNameLen ||
543 -1 != rName.indexOf( ':', nVersionStart ) )
544 return false;
545
546 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
547 if( rName[nVersionStart] != '1' || rName[nVersionStart+1] != '.' )
548 return false;
549
550 // replace [tcid] with current TCID and version with current version.
551
552 rName = rName.subView( 0, nTCIdStart ) +
553 GetXMLToken( XML_OPENDOCUMENT ) +
554 rName.subView( nTCIdEnd, nVersionStart-nTCIdEnd ) +
555 GetXMLToken( XML_1_0 );
556
557 return true;
558}
559
560/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString sPrefix
sal_uInt16 nKey
OUString GetQNameByKey(sal_uInt16 nKey, const OUString &rLocalName, bool bCache=true) const
QNameCache aQNameCache
NameSpaceHash aNameHash
const OUString & GetNameByKey(sal_uInt16 nKey) const
sal_uInt16 GetFirstKey() const
void AddAtIndex(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey)
const OUString & GetPrefixByIndex(sal_uInt16 nIdx) const
NameSpaceHash aNameCache
SvXMLNamespaceMap & operator=(const SvXMLNamespaceMap &rCmp)
sal_uInt16 GetKeyByPrefix(const OUString &rPrefix) const
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
static bool NormalizeOasisURN(OUString &rName)
static bool NormalizeW3URI(OUString &rName)
OUString GetAttrNameByKey(sal_uInt16 nKey) const
KeyToNameSpaceMap maKeyToNamespaceMap
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
sal_uInt16 AddIfKnown(const OUString &rPrefix, const OUString &rName)
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
sal_uInt16 GetIndexByPrefix(const OUString &rPrefix) const
sal_uInt16 GetNextIndex(sal_uInt16 nOldIdx) const
bool operator==(const SvXMLNamespaceMap &rCmp) const
SAL_DLLPRIVATE sal_uInt16 Add_(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey)
sal_uInt16 GetFirstIndex() const
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
OUString GetAttrNameByIndex(sal_uInt16 nIdx) const
sal_uInt16 GetKeyByQName(const OUString &rQName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace, QNameMode eMode) const
const OUString & GetNameByIndex(sal_uInt16 nIdx) const
static bool NormalizeURI(OUString &rName)
static sal_uInt16 GetIndexByKey(sal_uInt16 nKey)
sal_uInt16 GetKeyByName(const OUString &rName) const
const OUString & GetPrefixByKey(sal_uInt16 nKey) const
Mode eMode
sal_uInt16 nPos
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
constexpr OUStringLiteral first
const OUString sEmpty
const sal_uInt16 XML_NAMESPACE_UNKNOWN_FLAG
::std::pair< sal_uInt16, OUString > QNamePair
const sal_uInt16 XML_NAMESPACE_NONE
const sal_uInt16 XML_NAMESPACE_UNKNOWN
const sal_uInt16 XML_NAMESPACE_XMLNS
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3581
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3525
constexpr sal_uInt16 XML_NAMESPACE_XML