Bump version to 6.4.0.12
[LibreOffice.git] / xmloff / source / core / nmspmap.cxx
blobc1ee7412f4389dc51237b6e5fa39309d532eaa00
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <sal/log.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmlnmspe.hxx>
32 using namespace ::xmloff::token;
34 /* The basic idea of this class is that we have two ways to search our
35 * data, by prefix and by key. We use an unordered_map for fast prefix
36 * searching and an STL map for fast key searching.
38 * The references to an 'Index' refer to an earlier implementation of the
39 * name space map and remain to support code which uses these interfaces.
41 * In this implementation, key and index should always be the same number.
43 * All references to Indices are now deprecated and the corresponding
44 * 'Key' methods should be used instead
46 * Martin 13/06/01
49 static const OUString sEmpty;
51 SvXMLNamespaceMap::SvXMLNamespaceMap()
52 : sXMLNS( GetXMLToken ( XML_XMLNS ) )
56 SvXMLNamespaceMap::SvXMLNamespaceMap( const SvXMLNamespaceMap& rMap )
57 : sXMLNS( GetXMLToken ( XML_XMLNS ) )
59 aNameHash = rMap.aNameHash;
60 aNameMap = rMap.aNameMap;
63 SvXMLNamespaceMap& SvXMLNamespaceMap::operator=( const SvXMLNamespaceMap& rMap )
65 aNameHash = rMap.aNameHash;
66 aNameMap = rMap.aNameMap;
67 return *this;
70 SvXMLNamespaceMap::~SvXMLNamespaceMap()
74 bool SvXMLNamespaceMap::operator ==( const SvXMLNamespaceMap& rCmp ) const
76 return aNameHash == rCmp.aNameHash;
79 sal_uInt16 SvXMLNamespaceMap::Add_( const OUString& rPrefix, const OUString &rName, sal_uInt16 nKey )
81 if( XML_NAMESPACE_UNKNOWN == nKey )
83 // create a new unique key with UNKNOWN flag set
84 nKey = XML_NAMESPACE_UNKNOWN_FLAG;
87 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
88 if( aIter == aNameMap.end() )
89 break;
90 nKey++;
92 while ( true );
94 ::rtl::Reference<NameSpaceEntry> pEntry(new NameSpaceEntry);
95 pEntry->sName = rName;
96 pEntry->nKey = nKey;
97 pEntry->sPrefix = rPrefix;
98 aNameHash[ rPrefix ] = pEntry;
99 aNameMap [ nKey ] = pEntry;
100 return nKey;
103 sal_uInt16 SvXMLNamespaceMap::Add( const OUString& rPrefix, const OUString& rName,
104 sal_uInt16 nKey )
106 if( XML_NAMESPACE_UNKNOWN == nKey )
107 nKey = GetKeyByName( rName );
109 #ifdef NDEBUG
110 if( XML_NAMESPACE_NONE == nKey )
111 return USHRT_MAX;
112 #else
113 assert(XML_NAMESPACE_NONE != nKey);
114 #endif
116 if ( aNameHash.find ( rPrefix ) == aNameHash.end() )
117 nKey = Add_( rPrefix, rName, nKey );
119 return nKey;
122 sal_uInt16 SvXMLNamespaceMap::AddIfKnown( const OUString& rPrefix, const OUString& rName )
124 sal_uInt16 nKey = GetKeyByName( rName );
126 #ifdef NDEBUG
127 if( XML_NAMESPACE_NONE == nKey )
128 return XML_NAMESPACE_UNKNOWN;
129 #else
130 assert(nKey != XML_NAMESPACE_NONE);
131 #endif
133 if( XML_NAMESPACE_UNKNOWN != nKey )
135 NameSpaceHash::const_iterator aIter = aNameHash.find( rPrefix );
136 if( aIter == aNameHash.end() || (*aIter).second->sName != rName )
137 nKey = Add_( rPrefix, rName, nKey );
140 return nKey;
144 sal_uInt16 SvXMLNamespaceMap::GetKeyByPrefix( const OUString& rPrefix ) const
146 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
147 return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
150 sal_uInt16 SvXMLNamespaceMap::GetKeyByName( const OUString& rName ) const
152 sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
153 auto aIter = std::find_if(aNameHash.cbegin(), aNameHash.cend(),
154 [&rName](const NameSpaceHash::value_type& rEntry) { return rEntry.second->sName == rName; });
156 if (aIter != aNameHash.cend())
157 nKey = (*aIter).second->nKey;
159 return nKey;
162 const OUString& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey ) const
164 NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
165 return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
168 const OUString& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey ) const
170 NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
171 return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
174 OUString SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey ) const
176 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
177 if (aIter == aNameMap.end())
178 return OUString();
180 const OUString & prefix( (*aIter).second->sPrefix );
181 if (prefix.isEmpty()) // default namespace
182 return sXMLNS;
184 return sXMLNS + ":" + prefix;
187 OUString SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey,
188 const OUString& rLocalName,
189 bool bCache) const
191 // We always want to return at least the rLocalName...
193 switch ( nKey )
195 case XML_NAMESPACE_UNKNOWN:
196 // ...if it's a completely unknown namespace, assert and return the local name
197 SAL_WARN("xmloff.core", "unknown namespace, probable missing xmlns: declaration");
198 [[fallthrough]];
199 case XML_NAMESPACE_NONE:
200 // ...if there isn't one, return the local name
201 return rLocalName;
202 case XML_NAMESPACE_XMLNS:
204 // ...if it's in the xmlns namespace, make the prefix
205 // don't bother caching this, it rarely happens
206 OUStringBuffer sQName;
207 sQName.append ( sXMLNS );
208 if (!rLocalName.isEmpty()) // not default namespace
210 sQName.append ( ':' );
211 sQName.append ( rLocalName );
213 return sQName.makeStringAndClear();
215 case XML_NAMESPACE_XML:
217 // this namespace is reserved, and needs not to be declared
218 return GetXMLToken(XML_XML) + ":" + rLocalName;
220 default:
222 QNameCache::const_iterator aQCacheIter;
223 if (bCache)
224 aQCacheIter = aQNameCache.find ( QNamePair ( nKey, rLocalName ) );
225 else
226 aQCacheIter = aQNameCache.end();
227 if ( aQCacheIter != aQNameCache.end() )
228 return (*aQCacheIter).second;
229 else
231 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
232 if ( aIter != aNameMap.end() )
234 // ...if it's in our map, make the prefix
235 const OUString & prefix( (*aIter).second->sPrefix );
236 OUStringBuffer sQName(prefix.getLength() + 1 + rLocalName.getLength());
237 if (!prefix.isEmpty()) // not default namespace
239 sQName.append( prefix );
240 sQName.append( ':' );
242 sQName.append ( rLocalName );
243 if (bCache)
245 OUString sString(sQName.makeStringAndClear());
246 aQNameCache.emplace(QNamePair(nKey, rLocalName), sString);
247 return sString;
249 else
250 return sQName.makeStringAndClear();
252 else
254 // ... if it isn't, this is a Bad Thing, assert and return the local name
255 assert(false);
256 return rLocalName;
263 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName_(
264 const OUString& rAttrName,
265 OUString *pLocalName) const
267 return GetKeyByAttrName_( rAttrName, nullptr, pLocalName, nullptr, false/*bCache*/ );
270 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName_( const OUString& rAttrName,
271 OUString *pPrefix,
272 OUString *pLocalName,
273 OUString *pNamespace,
274 bool bCache) const
276 sal_uInt16 nKey;
278 NameSpaceHash::const_iterator it;
279 if (bCache)
280 it = aNameCache.find ( rAttrName );
281 else
282 it = aNameCache.end();
283 if ( it != aNameCache.end() )
285 const NameSpaceEntry &rEntry = *((*it).second);
286 if ( pPrefix )
287 *pPrefix = rEntry.sPrefix;
288 if ( pLocalName )
289 *pLocalName = rEntry.sName;
290 nKey = rEntry.nKey;
291 if ( pNamespace )
293 NameSpaceMap::const_iterator aMapIter = aNameMap.find (nKey);
294 *pNamespace = aMapIter != aNameMap.end() ? (*aMapIter).second->sName : OUString();
297 else
299 OUString sEntryPrefix, sEntryName;
301 sal_Int32 nColonPos = rAttrName.indexOf( ':' );
302 if( -1 == nColonPos )
304 // case: no ':' found -> default namespace
305 sEntryName = rAttrName;
307 else
309 // normal case: ':' found -> get prefix/suffix
310 sEntryPrefix = rAttrName.copy( 0L, nColonPos );
311 sEntryName = rAttrName.copy( nColonPos + 1 );
314 if( pPrefix )
315 *pPrefix = sEntryPrefix;
316 if( pLocalName )
317 *pLocalName = sEntryName;
319 NameSpaceHash::const_iterator aIter = aNameHash.find( sEntryPrefix );
320 if ( aIter != aNameHash.end() )
322 // found: retrieve namespace key
323 nKey = (*aIter).second->nKey;
324 if ( pNamespace )
325 *pNamespace = (*aIter).second->sName;
327 else if ( sEntryPrefix == sXMLNS )
328 // not found, but xmlns prefix: return xmlns 'namespace'
329 nKey = XML_NAMESPACE_XMLNS;
330 else if( nColonPos == -1 )
331 // not found, and no namespace: 'namespace' none
332 nKey = XML_NAMESPACE_NONE;
333 else
334 nKey = XML_NAMESPACE_UNKNOWN;
336 if (bCache)
338 rtl::Reference<NameSpaceEntry> xEntry(new NameSpaceEntry);
339 xEntry->sPrefix = std::move(sEntryPrefix);
340 xEntry->sName = std::move(sEntryName);
341 xEntry->nKey = std::move(nKey);
342 aNameCache.emplace(rAttrName, std::move(xEntry));
346 return nKey;
349 sal_uInt16 SvXMLNamespaceMap::GetFirstKey() const
351 return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
354 sal_uInt16 SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey ) const
356 NameSpaceMap::const_iterator aIter = aNameMap.find ( nLastKey );
357 return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
361 // All methods after this are deprecated...
363 sal_uInt16 SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey )
365 return nKey;
367 sal_uInt16 SvXMLNamespaceMap::GetFirstIndex() const
369 return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
372 sal_uInt16 SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx ) const
374 NameSpaceMap::const_iterator aIter = aNameMap.find ( nOldIdx );
375 return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
378 void SvXMLNamespaceMap::AddAtIndex( const OUString& rPrefix,
379 const OUString& rName, sal_uInt16 nKey )
381 if( XML_NAMESPACE_UNKNOWN == nKey )
382 nKey = GetKeyByName( rName );
384 assert(XML_NAMESPACE_NONE != nKey);
385 if( XML_NAMESPACE_NONE != nKey && ! ( aNameHash.count ( rPrefix ) ) )
387 Add_( rPrefix, rName, nKey );
391 OUString SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx ) const
393 return GetAttrNameByKey( nIdx );
396 OUString SvXMLNamespaceMap::GetQNameByIndex( sal_uInt16 nIdx,
397 const OUString& rLocalName ) const
399 return GetQNameByKey( nIdx, rLocalName );
402 const OUString& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx ) const
404 NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
405 return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
408 const OUString& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx ) const
410 NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
411 return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
414 sal_uInt16 SvXMLNamespaceMap::GetIndexByPrefix( const OUString& rPrefix ) const
416 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
417 return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
419 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName(
420 const OUString& rAttrName,
421 OUString *pLocalName) const
423 return GetKeyByAttrName_( rAttrName, nullptr, pLocalName );
426 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName( const OUString& rAttrName,
427 OUString *pPrefix,
428 OUString *pLocalName,
429 OUString *pNamespace ) const
431 return GetKeyByAttrName_ ( rAttrName, pPrefix, pLocalName, pNamespace );
434 bool SvXMLNamespaceMap::NormalizeURI( OUString& rName )
436 // try OASIS + W3 URI normalization
437 bool bSuccess = NormalizeOasisURN( rName );
438 if( ! bSuccess )
439 bSuccess = NormalizeW3URI( rName );
440 return bSuccess;
443 bool SvXMLNamespaceMap::NormalizeW3URI( OUString& rName )
445 // check if URI matches:
446 // http://www.w3.org/[0-9]*/[:letter:]*
447 // (year)/(WG name)
448 // For the following WG/standards names:
449 // - xforms
451 bool bSuccess = false;
452 const OUString& sURIPrefix = GetXMLToken( XML_URI_W3_PREFIX );
453 if( rName.startsWith( sURIPrefix ) )
455 const OUString& sURISuffix = GetXMLToken( XML_URI_XFORMS_SUFFIX );
456 sal_Int32 nCompareFrom = rName.getLength() - sURISuffix.getLength();
457 if( rName.copy( nCompareFrom ) == sURISuffix )
459 // found W3 prefix, and xforms suffix
460 rName = GetXMLToken( XML_N_XFORMS_1_0 );
461 bSuccess = true;
464 return bSuccess;
467 bool SvXMLNamespaceMap::NormalizeOasisURN( OUString& rName )
469 // #i38644#
470 // we exported the wrong namespace for smil, so we correct this here on load
471 // for older documents
472 if( IsXMLToken( rName, ::xmloff::token::XML_N_SVG ) )
474 rName = GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT );
475 return true;
477 else if( IsXMLToken( rName, ::xmloff::token::XML_N_FO ) )
479 rName = GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT );
480 return true;
482 else if( IsXMLToken( rName, ::xmloff::token::XML_N_SMIL ) ||
483 IsXMLToken( rName, ::xmloff::token::XML_N_SMIL_OLD ) )
485 rName = GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT );
486 return true;
490 // Check if URN matches
491 // :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
492 // |---| |---| |-----|
493 // TC-Id Sub-Id Version
495 sal_Int32 nNameLen = rName.getLength();
496 // :urn:oasis:names:tc.*
497 const OUString& rOasisURN = GetXMLToken( XML_URN_OASIS_NAMES_TC );
498 if( !rName.startsWith( rOasisURN ) )
499 return false;
501 // :urn:oasis:names:tc:.*
502 sal_Int32 nPos = rOasisURN.getLength();
503 if( nPos >= nNameLen || rName[nPos] != ':' )
504 return false;
506 // :urn:oasis:names:tc:[^:]:.*
507 sal_Int32 nTCIdStart = nPos+1;
508 sal_Int32 nTCIdEnd = rName.indexOf( ':', nTCIdStart );
509 if( -1 == nTCIdEnd )
510 return false;
512 // :urn:oasis:names:tc:[^:]:xmlns.*
513 nPos = nTCIdEnd + 1;
514 OUString sTmp( rName.copy( nPos ) );
515 const OUString& rXMLNS = GetXMLToken( XML_XMLNS );
516 if( !sTmp.startsWith( rXMLNS ) )
517 return false;
519 // :urn:oasis:names:tc:[^:]:xmlns:.*
520 nPos += rXMLNS.getLength();
521 if( nPos >= nNameLen || rName[nPos] != ':' )
522 return false;
524 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
525 nPos = rName.indexOf( ':', nPos+1 );
526 if( -1 == nPos )
527 return false;
529 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
530 sal_Int32 nVersionStart = nPos+1;
531 if( nVersionStart+2 >= nNameLen ||
532 -1 != rName.indexOf( ':', nVersionStart ) )
533 return false;
535 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
536 if( rName[nVersionStart] != '1' || rName[nVersionStart+1] != '.' )
537 return false;
539 // replace [tcid] with current TCID and version with current version.
541 rName = rName.copy( 0, nTCIdStart ) +
542 GetXMLToken( XML_OPENDOCUMENT ) +
543 rName.copy( nTCIdEnd, nVersionStart-nTCIdEnd ) +
544 GetXMLToken( XML_1_0 );
546 return true;
549 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */