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>
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
49 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
;
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() )
94 ::rtl::Reference
<NameSpaceEntry
> pEntry(new NameSpaceEntry
);
95 pEntry
->sName
= rName
;
97 pEntry
->sPrefix
= rPrefix
;
98 aNameHash
[ rPrefix
] = pEntry
;
99 aNameMap
[ nKey
] = pEntry
;
103 sal_uInt16
SvXMLNamespaceMap::Add( const OUString
& rPrefix
, const OUString
& rName
,
106 if( XML_NAMESPACE_UNKNOWN
== nKey
)
107 nKey
= GetKeyByName( rName
);
110 if( XML_NAMESPACE_NONE
== nKey
)
113 assert(XML_NAMESPACE_NONE
!= nKey
);
116 if ( aNameHash
.find ( rPrefix
) == aNameHash
.end() )
117 nKey
= Add_( rPrefix
, rName
, nKey
);
122 sal_uInt16
SvXMLNamespaceMap::AddIfKnown( const OUString
& rPrefix
, const OUString
& rName
)
124 sal_uInt16 nKey
= GetKeyByName( rName
);
127 if( XML_NAMESPACE_NONE
== nKey
)
128 return XML_NAMESPACE_UNKNOWN
;
130 assert(nKey
!= XML_NAMESPACE_NONE
);
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
);
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
;
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())
180 const OUString
& prefix( (*aIter
).second
->sPrefix
);
181 if (prefix
.isEmpty()) // default namespace
184 return sXMLNS
+ ":" + prefix
;
187 OUString
SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey
,
188 const OUString
& rLocalName
,
191 // We always want to return at least the rLocalName...
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");
199 case XML_NAMESPACE_NONE
:
200 // ...if there isn't one, return the local name
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
;
222 QNameCache::const_iterator aQCacheIter
;
224 aQCacheIter
= aQNameCache
.find ( QNamePair ( nKey
, rLocalName
) );
226 aQCacheIter
= aQNameCache
.end();
227 if ( aQCacheIter
!= aQNameCache
.end() )
228 return (*aQCacheIter
).second
;
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
);
245 OUString
sString(sQName
.makeStringAndClear());
246 aQNameCache
.emplace(QNamePair(nKey
, rLocalName
), sString
);
250 return sQName
.makeStringAndClear();
254 // ... if it isn't, this is a Bad Thing, assert and return the local name
263 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrValueQName(
264 const OUString
& rAttrValue
,
265 OUString
*pLocalName
) const
267 return GetKeyByQName(rAttrValue
, nullptr, pLocalName
, nullptr, QNameMode::AttrValue
);
271 @param rQName either attribute name or qualified/namespaced attribute value
272 @param bCacheAttrName true: rQName is element or attribute name, cache it
273 false: rQName is attribute value, may contain extra ':', don't cache it
275 sal_uInt16
SvXMLNamespaceMap::GetKeyByQName(const OUString
& rQName
,
277 OUString
*pLocalName
,
278 OUString
*pNamespace
,
279 QNameMode
const eMode
) const
283 NameSpaceHash::const_iterator it
;
284 if (eMode
== QNameMode::AttrNameCached
)
285 it
= aNameCache
.find ( rQName
);
287 it
= aNameCache
.end();
288 if ( it
!= aNameCache
.end() )
290 const NameSpaceEntry
&rEntry
= *((*it
).second
);
292 *pPrefix
= rEntry
.sPrefix
;
294 *pLocalName
= rEntry
.sName
;
298 NameSpaceMap::const_iterator aMapIter
= aNameMap
.find (nKey
);
299 *pNamespace
= aMapIter
!= aNameMap
.end() ? (*aMapIter
).second
->sName
: OUString();
304 OUString sEntryPrefix
, sEntryName
;
306 sal_Int32 nColonPos
= rQName
.indexOf( ':' );
307 if( -1 == nColonPos
)
309 // case: no ':' found -> default namespace
314 // normal case: ':' found -> get prefix/suffix
315 sEntryPrefix
= rQName
.copy( 0L, nColonPos
);
316 sEntryName
= rQName
.copy( nColonPos
+ 1 );
319 if (eMode
== QNameMode::AttrNameCached
&& sEntryName
.indexOf(':') != -1)
321 SAL_INFO("xmloff", "invalid attribute name with multiple ':'");
323 return XML_NAMESPACE_UNKNOWN
;
327 *pPrefix
= sEntryPrefix
;
329 *pLocalName
= sEntryName
;
331 NameSpaceHash::const_iterator aIter
= aNameHash
.find( sEntryPrefix
);
332 if ( aIter
!= aNameHash
.end() )
334 // found: retrieve namespace key
335 nKey
= (*aIter
).second
->nKey
;
337 *pNamespace
= (*aIter
).second
->sName
;
339 else if ( sEntryPrefix
== sXMLNS
)
340 // not found, but xmlns prefix: return xmlns 'namespace'
341 nKey
= XML_NAMESPACE_XMLNS
;
342 else if( nColonPos
== -1 )
343 // not found, and no namespace: 'namespace' none
344 nKey
= XML_NAMESPACE_NONE
;
346 nKey
= XML_NAMESPACE_UNKNOWN
;
348 if (eMode
== QNameMode::AttrNameCached
)
350 rtl::Reference
<NameSpaceEntry
> xEntry(new NameSpaceEntry
);
351 xEntry
->sPrefix
= std::move(sEntryPrefix
);
352 xEntry
->sName
= std::move(sEntryName
);
353 xEntry
->nKey
= std::move(nKey
);
354 aNameCache
.emplace(rQName
, std::move(xEntry
));
361 sal_uInt16
SvXMLNamespaceMap::GetFirstKey() const
363 return aNameMap
.empty() ? USHRT_MAX
: (*aNameMap
.begin()).second
->nKey
;
366 sal_uInt16
SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey
) const
368 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nLastKey
);
369 return (++aIter
== aNameMap
.end()) ? USHRT_MAX
: (*aIter
).second
->nKey
;
373 // All methods after this are deprecated...
375 sal_uInt16
SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey
)
379 sal_uInt16
SvXMLNamespaceMap::GetFirstIndex() const
381 return aNameMap
.empty() ? USHRT_MAX
: (*aNameMap
.begin()).second
->nKey
;
384 sal_uInt16
SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx
) const
386 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nOldIdx
);
387 return (++aIter
== aNameMap
.end()) ? USHRT_MAX
: (*aIter
).second
->nKey
;
390 void SvXMLNamespaceMap::AddAtIndex( const OUString
& rPrefix
,
391 const OUString
& rName
, sal_uInt16 nKey
)
393 if( XML_NAMESPACE_UNKNOWN
== nKey
)
394 nKey
= GetKeyByName( rName
);
396 assert(XML_NAMESPACE_NONE
!= nKey
);
397 if( XML_NAMESPACE_NONE
!= nKey
&& ! ( aNameHash
.count ( rPrefix
) ) )
399 Add_( rPrefix
, rName
, nKey
);
403 OUString
SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx
) const
405 return GetAttrNameByKey( nIdx
);
408 const OUString
& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx
) const
410 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nIdx
);
411 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sPrefix
: sEmpty
;
414 const OUString
& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx
) const
416 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nIdx
);
417 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sName
: sEmpty
;
420 sal_uInt16
SvXMLNamespaceMap::GetIndexByPrefix( const OUString
& rPrefix
) const
422 NameSpaceHash::const_iterator aIter
= aNameHash
.find(rPrefix
);
423 return (aIter
!= aNameHash
.end()) ? (*aIter
).second
->nKey
: USHRT_MAX
;
425 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName(
426 const OUString
& rAttrName
,
427 OUString
*pLocalName
) const
429 return GetKeyByQName(rAttrName
, nullptr, pLocalName
, nullptr, QNameMode::AttrNameCached
);
432 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName( const OUString
& rAttrName
,
434 OUString
*pLocalName
,
435 OUString
*pNamespace
) const
437 return GetKeyByQName(rAttrName
, pPrefix
, pLocalName
, pNamespace
, QNameMode::AttrNameCached
);
440 bool SvXMLNamespaceMap::NormalizeURI( OUString
& rName
)
442 // try OASIS + W3 URI normalization
443 bool bSuccess
= NormalizeOasisURN( rName
);
445 bSuccess
= NormalizeW3URI( rName
);
449 bool SvXMLNamespaceMap::NormalizeW3URI( OUString
& rName
)
451 // check if URI matches:
452 // http://www.w3.org/[0-9]*/[:letter:]*
454 // For the following WG/standards names:
457 bool bSuccess
= false;
458 const OUString
& sURIPrefix
= GetXMLToken( XML_URI_W3_PREFIX
);
459 if( rName
.startsWith( sURIPrefix
) )
461 const OUString
& sURISuffix
= GetXMLToken( XML_URI_XFORMS_SUFFIX
);
462 sal_Int32 nCompareFrom
= rName
.getLength() - sURISuffix
.getLength();
463 if( rName
.subView( nCompareFrom
) == sURISuffix
)
465 // found W3 prefix, and xforms suffix
466 rName
= GetXMLToken( XML_N_XFORMS_1_0
);
473 bool SvXMLNamespaceMap::NormalizeOasisURN( OUString
& rName
)
476 // we exported the wrong namespace for smil, so we correct this here on load
477 // for older documents
478 if( IsXMLToken( rName
, ::xmloff::token::XML_N_SVG
) )
480 rName
= GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT
);
483 else if( IsXMLToken( rName
, ::xmloff::token::XML_N_FO
) )
485 rName
= GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT
);
488 else if( IsXMLToken( rName
, ::xmloff::token::XML_N_SMIL
) ||
489 IsXMLToken( rName
, ::xmloff::token::XML_N_SMIL_OLD
) )
491 rName
= GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT
);
496 // Check if URN matches
497 // :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
498 // |---| |---| |-----|
499 // TC-Id Sub-Id Version
501 sal_Int32 nNameLen
= rName
.getLength();
502 // :urn:oasis:names:tc.*
503 const OUString
& rOasisURN
= GetXMLToken( XML_URN_OASIS_NAMES_TC
);
504 if( !rName
.startsWith( rOasisURN
) )
507 // :urn:oasis:names:tc:.*
508 sal_Int32 nPos
= rOasisURN
.getLength();
509 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
512 // :urn:oasis:names:tc:[^:]:.*
513 sal_Int32 nTCIdStart
= nPos
+1;
514 sal_Int32 nTCIdEnd
= rName
.indexOf( ':', nTCIdStart
);
518 // :urn:oasis:names:tc:[^:]:xmlns.*
520 OUString
sTmp( rName
.copy( nPos
) );
521 const OUString
& rXMLNS
= GetXMLToken( XML_XMLNS
);
522 if( !sTmp
.startsWith( rXMLNS
) )
525 // :urn:oasis:names:tc:[^:]:xmlns:.*
526 nPos
+= rXMLNS
.getLength();
527 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
530 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
531 nPos
= rName
.indexOf( ':', nPos
+1 );
535 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
536 sal_Int32 nVersionStart
= nPos
+1;
537 if( nVersionStart
+2 >= nNameLen
||
538 -1 != rName
.indexOf( ':', nVersionStart
) )
541 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
542 if( rName
[nVersionStart
] != '1' || rName
[nVersionStart
+1] != '.' )
545 // replace [tcid] with current TCID and version with current version.
547 rName
= rName
.subView( 0, nTCIdStart
) +
548 GetXMLToken( XML_OPENDOCUMENT
) +
549 rName
.subView( nTCIdEnd
, nVersionStart
-nTCIdEnd
) +
550 GetXMLToken( XML_1_0
);
555 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */