1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/namespacemap.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <o3tl/string_view.hxx>
33 using namespace ::xmloff::token
;
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.
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.
42 * In this implementation, key and index should always be the same number.
44 * All references to Indices are now deprecated and the corresponding
45 * 'Key' methods should be used instead
50 const OUString sEmpty
;
52 SvXMLNamespaceMap::SvXMLNamespaceMap()
53 : sXMLNS( GetXMLToken ( XML_XMLNS
) )
55 // approx worst-case size
56 aNameHash
.reserve(20);
57 maKeyToNamespaceMap
.reserve(20);
60 SvXMLNamespaceMap::SvXMLNamespaceMap( const SvXMLNamespaceMap
& rMap
)
61 : sXMLNS( GetXMLToken ( XML_XMLNS
) )
63 aNameHash
= rMap
.aNameHash
;
64 maKeyToNamespaceMap
= rMap
.maKeyToNamespaceMap
;
67 SvXMLNamespaceMap
& SvXMLNamespaceMap::operator=( const SvXMLNamespaceMap
& rMap
)
69 aNameHash
= rMap
.aNameHash
;
70 maKeyToNamespaceMap
= rMap
.maKeyToNamespaceMap
;
74 SvXMLNamespaceMap::~SvXMLNamespaceMap()
78 void SvXMLNamespaceMap::Clear()
82 maKeyToNamespaceMap
.clear();
87 bool SvXMLNamespaceMap::operator ==( const SvXMLNamespaceMap
& rCmp
) const
89 return aNameHash
== rCmp
.aNameHash
;
92 sal_uInt16
SvXMLNamespaceMap::Add_( const OUString
& rPrefix
, const OUString
&rName
, sal_uInt16 nKey
)
94 if( XML_NAMESPACE_UNKNOWN
== nKey
)
96 // create a new unique key with UNKNOWN flag set
97 nKey
= XML_NAMESPACE_UNKNOWN_FLAG
;
100 auto aIter
= maKeyToNamespaceMap
.find ( nKey
);
101 if( aIter
== maKeyToNamespaceMap
.end() )
107 aNameHash
.insert_or_assign( rPrefix
, NameSpaceEntry
{ rName
, rPrefix
, nKey
} );
108 maKeyToNamespaceMap
.insert_or_assign( nKey
, KeyToNameSpaceMapEntry
{ rName
, rPrefix
} );
112 sal_uInt16
SvXMLNamespaceMap::Add( const OUString
& rPrefix
, const OUString
& rName
,
115 if( XML_NAMESPACE_UNKNOWN
== nKey
)
116 nKey
= GetKeyByName( rName
);
119 if( XML_NAMESPACE_NONE
== nKey
)
122 assert(XML_NAMESPACE_NONE
!= nKey
);
125 if ( aNameHash
.find ( rPrefix
) == aNameHash
.end() )
126 nKey
= Add_( rPrefix
, rName
, nKey
);
131 sal_uInt16
SvXMLNamespaceMap::AddIfKnown( const OUString
& rPrefix
, const OUString
& rName
)
133 sal_uInt16 nKey
= GetKeyByName( rName
);
136 if( XML_NAMESPACE_NONE
== nKey
)
137 return XML_NAMESPACE_UNKNOWN
;
139 assert(nKey
!= XML_NAMESPACE_NONE
);
142 if( XML_NAMESPACE_UNKNOWN
!= nKey
)
144 NameSpaceHash::const_iterator aIter
= aNameHash
.find( rPrefix
);
145 if( aIter
== aNameHash
.end() || (*aIter
).second
.sName
!= rName
)
146 nKey
= Add_( rPrefix
, rName
, nKey
);
153 sal_uInt16
SvXMLNamespaceMap::GetKeyByPrefix( const OUString
& rPrefix
) const
155 NameSpaceHash::const_iterator aIter
= aNameHash
.find(rPrefix
);
156 return (aIter
!= aNameHash
.end()) ? (*aIter
).second
.nKey
: USHRT_MAX
;
159 sal_uInt16
SvXMLNamespaceMap::GetKeyByName( const OUString
& rName
) const
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
; });
165 if (aIter
!= aNameHash
.cend())
166 nKey
= (*aIter
).second
.nKey
;
171 const OUString
& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey
) const
173 auto aIter
= maKeyToNamespaceMap
.find (nKey
);
174 return (aIter
!= maKeyToNamespaceMap
.end()) ? (*aIter
).second
.sPrefix
: sEmpty
;
177 const OUString
& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey
) const
179 auto aIter
= maKeyToNamespaceMap
.find (nKey
);
180 return (aIter
!= maKeyToNamespaceMap
.end()) ? (*aIter
).second
.sName
: sEmpty
;
183 OUString
SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey
) const
185 auto aIter
= maKeyToNamespaceMap
.find ( nKey
);
186 if (aIter
== maKeyToNamespaceMap
.end())
189 const OUString
& prefix( (*aIter
).second
.sPrefix
);
190 if (prefix
.isEmpty()) // default namespace
193 return sXMLNS
+ ":" + prefix
;
196 OUString
SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey
,
197 const OUString
& rLocalName
,
200 // We always want to return at least the rLocalName...
204 case XML_NAMESPACE_UNKNOWN
:
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");
208 case XML_NAMESPACE_NONE
:
209 // ...if there isn't one, return the local name
211 case XML_NAMESPACE_XMLNS
:
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
219 sQName
.append ( ':' );
220 sQName
.append ( rLocalName
);
222 return sQName
.makeStringAndClear();
224 case XML_NAMESPACE_XML
:
226 // this namespace is reserved, and needs not to be declared
227 return GetXMLToken(XML_XML
) + ":" + rLocalName
;
231 QNameCache::const_iterator aQCacheIter
;
233 aQCacheIter
= aQNameCache
.find ( QNamePair ( nKey
, rLocalName
) );
235 aQCacheIter
= aQNameCache
.end();
236 if ( aQCacheIter
!= aQNameCache
.end() )
237 return (*aQCacheIter
).second
;
240 auto aIter
= maKeyToNamespaceMap
.find ( nKey
);
241 if ( aIter
!= maKeyToNamespaceMap
.end() )
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
248 sQName
.append( prefix
);
249 sQName
.append( ':' );
251 sQName
.append ( rLocalName
);
254 OUString
sString(sQName
.makeStringAndClear());
255 aQNameCache
.emplace(QNamePair(nKey
, rLocalName
), sString
);
259 return sQName
.makeStringAndClear();
263 // ... if it isn't, this is a Bad Thing, assert and return the local name
272 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrValueQName(
273 const OUString
& rAttrValue
,
274 OUString
*pLocalName
) const
276 return GetKeyByQName(rAttrValue
, nullptr, pLocalName
, nullptr, QNameMode::AttrValue
);
280 @param rQName either attribute name or qualified/namespaced attribute value
281 @param bCacheAttrName true: rQName is element or attribute name, cache it
282 false: rQName is attribute value, may contain extra ':', don't cache it
284 sal_uInt16
SvXMLNamespaceMap::GetKeyByQName(const OUString
& rQName
,
286 OUString
*pLocalName
,
287 OUString
*pNamespace
,
288 QNameMode
const eMode
) const
292 NameSpaceHash::const_iterator it
;
293 if (eMode
== QNameMode::AttrNameCached
)
294 it
= aNameCache
.find ( rQName
);
296 it
= aNameCache
.end();
297 if ( it
!= aNameCache
.end() )
299 const NameSpaceEntry
&rEntry
= (*it
).second
;
301 *pPrefix
= rEntry
.sPrefix
;
303 *pLocalName
= rEntry
.sName
;
307 auto aMapIter
= maKeyToNamespaceMap
.find (nKey
);
308 *pNamespace
= aMapIter
!= maKeyToNamespaceMap
.end() ? (*aMapIter
).second
.sName
: OUString();
313 OUString sEntryPrefix
, sEntryName
;
315 sal_Int32 nColonPos
= rQName
.indexOf( ':' );
316 if( -1 == nColonPos
)
318 // case: no ':' found -> default namespace
323 // normal case: ':' found -> get prefix/suffix
324 sEntryPrefix
= rQName
.copy( 0, nColonPos
);
325 sEntryName
= rQName
.copy( nColonPos
+ 1 );
328 if (eMode
== QNameMode::AttrNameCached
&& sEntryName
.indexOf(':') != -1)
330 SAL_INFO("xmloff", "invalid attribute name with multiple ':'");
332 return XML_NAMESPACE_UNKNOWN
;
336 *pPrefix
= sEntryPrefix
;
338 *pLocalName
= sEntryName
;
340 NameSpaceHash::const_iterator aIter
= aNameHash
.find( sEntryPrefix
);
341 if ( aIter
!= aNameHash
.end() )
343 // found: retrieve namespace key
344 nKey
= (*aIter
).second
.nKey
;
346 *pNamespace
= (*aIter
).second
.sName
;
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
;
355 nKey
= XML_NAMESPACE_UNKNOWN
;
357 if (eMode
== QNameMode::AttrNameCached
)
359 aNameCache
.insert_or_assign(rQName
, NameSpaceEntry
{std::move(sEntryName
), std::move(sEntryPrefix
), nKey
});
366 sal_uInt16
SvXMLNamespaceMap::GetFirstKey() const
368 return maKeyToNamespaceMap
.empty() ? USHRT_MAX
: (*maKeyToNamespaceMap
.begin()).first
;
371 sal_uInt16
SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey
) const
373 auto aIter
= maKeyToNamespaceMap
.find ( nLastKey
);
374 return (++aIter
== maKeyToNamespaceMap
.end()) ? USHRT_MAX
: (*aIter
).first
;
378 // All methods after this are deprecated...
380 sal_uInt16
SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey
)
384 sal_uInt16
SvXMLNamespaceMap::GetFirstIndex() const
386 return maKeyToNamespaceMap
.empty() ? USHRT_MAX
: (*maKeyToNamespaceMap
.begin()).first
;
389 sal_uInt16
SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx
) const
391 auto aIter
= maKeyToNamespaceMap
.find ( nOldIdx
);
392 return (++aIter
== maKeyToNamespaceMap
.end()) ? USHRT_MAX
: (*aIter
).first
;
395 void SvXMLNamespaceMap::AddAtIndex( const OUString
& rPrefix
,
396 const OUString
& rName
, sal_uInt16 nKey
)
398 if( XML_NAMESPACE_UNKNOWN
== nKey
)
399 nKey
= GetKeyByName( rName
);
401 assert(XML_NAMESPACE_NONE
!= nKey
);
402 if( XML_NAMESPACE_NONE
!= nKey
&& ! ( aNameHash
.count ( rPrefix
) ) )
404 Add_( rPrefix
, rName
, nKey
);
408 OUString
SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx
) const
410 return GetAttrNameByKey( nIdx
);
413 const OUString
& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx
) const
415 auto aIter
= maKeyToNamespaceMap
.find (nIdx
);
416 return (aIter
!= maKeyToNamespaceMap
.end()) ? (*aIter
).second
.sPrefix
: sEmpty
;
419 const OUString
& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx
) const
421 auto aIter
= maKeyToNamespaceMap
.find (nIdx
);
422 return (aIter
!= maKeyToNamespaceMap
.end()) ? (*aIter
).second
.sName
: sEmpty
;
425 sal_uInt16
SvXMLNamespaceMap::GetIndexByPrefix( const OUString
& rPrefix
) const
427 NameSpaceHash::const_iterator aIter
= aNameHash
.find(rPrefix
);
428 return (aIter
!= aNameHash
.end()) ? (*aIter
).second
.nKey
: USHRT_MAX
;
430 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName(
431 const OUString
& rAttrName
,
432 OUString
*pLocalName
) const
434 return GetKeyByQName(rAttrName
, nullptr, pLocalName
, nullptr, QNameMode::AttrNameCached
);
437 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName( const OUString
& rAttrName
,
439 OUString
*pLocalName
,
440 OUString
*pNamespace
) const
442 return GetKeyByQName(rAttrName
, pPrefix
, pLocalName
, pNamespace
, QNameMode::AttrNameCached
);
445 bool SvXMLNamespaceMap::NormalizeURI( OUString
& rName
)
447 // try OASIS + W3 URI normalization
448 bool bSuccess
= NormalizeOasisURN( rName
);
450 bSuccess
= NormalizeW3URI( rName
);
454 bool SvXMLNamespaceMap::NormalizeW3URI( OUString
& rName
)
456 // check if URI matches:
457 // http://www.w3.org/[0-9]*/[:letter:]*
459 // For the following WG/standards names:
462 bool bSuccess
= false;
463 const OUString
& sURIPrefix
= GetXMLToken( XML_URI_W3_PREFIX
);
464 if( rName
.startsWith( sURIPrefix
) )
466 const OUString
& sURISuffix
= GetXMLToken( XML_URI_XFORMS_SUFFIX
);
467 sal_Int32 nCompareFrom
= rName
.getLength() - sURISuffix
.getLength();
468 if( rName
.subView( nCompareFrom
) == sURISuffix
)
470 // found W3 prefix, and xforms suffix
471 rName
= GetXMLToken( XML_N_XFORMS_1_0
);
478 bool SvXMLNamespaceMap::NormalizeOasisURN( OUString
& rName
)
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
) )
485 rName
= GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT
);
488 else if( IsXMLToken( rName
, ::xmloff::token::XML_N_FO
) )
490 rName
= GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT
);
493 else if( IsXMLToken( rName
, ::xmloff::token::XML_N_SMIL
) ||
494 IsXMLToken( rName
, ::xmloff::token::XML_N_SMIL_OLD
) )
496 rName
= GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT
);
501 // Check if URN matches
502 // :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
503 // |---| |---| |-----|
504 // TC-Id Sub-Id Version
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
) )
512 // :urn:oasis:names:tc:.*
513 sal_Int32 nPos
= rOasisURN
.getLength();
514 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
517 // :urn:oasis:names:tc:[^:]:.*
518 sal_Int32 nTCIdStart
= nPos
+1;
519 sal_Int32 nTCIdEnd
= rName
.indexOf( ':', nTCIdStart
);
523 // :urn:oasis:names:tc:[^:]:xmlns.*
525 std::u16string_view
sTmp( rName
.subView( nPos
) );
526 const OUString
& rXMLNS
= GetXMLToken( XML_XMLNS
);
527 if( !o3tl::starts_with(sTmp
, rXMLNS
) )
530 // :urn:oasis:names:tc:[^:]:xmlns:.*
531 nPos
+= rXMLNS
.getLength();
532 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
535 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
536 nPos
= rName
.indexOf( ':', nPos
+1 );
540 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
541 sal_Int32 nVersionStart
= nPos
+1;
542 if( nVersionStart
+2 >= nNameLen
||
543 -1 != rName
.indexOf( ':', nVersionStart
) )
546 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
547 if( rName
[nVersionStart
] != '1' || rName
[nVersionStart
+1] != '.' )
550 // replace [tcid] with current TCID and version with current version.
552 rName
= rName
.subView( 0, nTCIdStart
) +
553 GetXMLToken( XML_OPENDOCUMENT
) +
554 rName
.subView( nTCIdEnd
, nVersionStart
-nTCIdEnd
) +
555 GetXMLToken( XML_1_0
);
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */