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 <tools/debug.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.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 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 SvXMLNamespaceMap::SvXMLNamespaceMap()
50 : sXMLNS( GetXMLToken ( XML_XMLNS
) )
54 SvXMLNamespaceMap::SvXMLNamespaceMap( const SvXMLNamespaceMap
& rMap
)
55 : sXMLNS( GetXMLToken ( XML_XMLNS
) )
57 aNameHash
= rMap
.aNameHash
;
58 aNameMap
= rMap
.aNameMap
;
61 void SvXMLNamespaceMap::operator=( const SvXMLNamespaceMap
& rMap
)
63 aNameHash
= rMap
.aNameHash
;
64 aNameMap
= rMap
.aNameMap
;
67 SvXMLNamespaceMap::~SvXMLNamespaceMap()
71 bool SvXMLNamespaceMap::operator ==( const SvXMLNamespaceMap
& rCmp
) const
73 return aNameHash
== rCmp
.aNameHash
;
76 sal_uInt16
SvXMLNamespaceMap::_Add( const OUString
& rPrefix
, const OUString
&rName
, sal_uInt16 nKey
)
78 if( XML_NAMESPACE_UNKNOWN
== nKey
)
80 // create a new unique key with UNKNOWN flag set
81 nKey
= XML_NAMESPACE_UNKNOWN_FLAG
;
84 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nKey
);
85 if( aIter
== aNameMap
.end() )
91 ::rtl::Reference
<NameSpaceEntry
> pEntry(new NameSpaceEntry
);
92 pEntry
->sName
= rName
;
94 pEntry
->sPrefix
= rPrefix
;
95 aNameHash
[ rPrefix
] = pEntry
;
96 aNameMap
[ nKey
] = pEntry
;
100 sal_uInt16
SvXMLNamespaceMap::Add( const OUString
& rPrefix
, const OUString
& rName
,
103 if( XML_NAMESPACE_UNKNOWN
== nKey
)
104 nKey
= GetKeyByName( rName
);
106 DBG_ASSERT( XML_NAMESPACE_NONE
!= nKey
,
107 "SvXMLNamespaceMap::Add: invalid namespace key" );
109 if( XML_NAMESPACE_NONE
== nKey
)
112 if ( aNameHash
.find ( rPrefix
) == aNameHash
.end() )
113 nKey
= _Add( rPrefix
, rName
, nKey
);
118 sal_uInt16
SvXMLNamespaceMap::AddIfKnown( const OUString
& rPrefix
, const OUString
& rName
)
120 sal_uInt16 nKey
= GetKeyByName( rName
);
122 DBG_ASSERT( XML_NAMESPACE_NONE
!= nKey
,
123 "SvXMLNamespaceMap::AddIfKnown: invalid namespace key" );
125 if( XML_NAMESPACE_NONE
== nKey
)
126 return XML_NAMESPACE_UNKNOWN
;
128 if( XML_NAMESPACE_UNKNOWN
!= nKey
)
130 NameSpaceHash::const_iterator aIter
= aNameHash
.find( rPrefix
);
131 if( aIter
== aNameHash
.end() || (*aIter
).second
->sName
!= rName
)
132 nKey
= _Add( rPrefix
, rName
, nKey
);
139 sal_uInt16
SvXMLNamespaceMap::GetKeyByPrefix( const OUString
& rPrefix
) const
141 NameSpaceHash::const_iterator aIter
= aNameHash
.find(rPrefix
);
142 return (aIter
!= aNameHash
.end()) ? (*aIter
).second
->nKey
: USHRT_MAX
;
145 sal_uInt16
SvXMLNamespaceMap::GetKeyByName( const OUString
& rName
) const
147 sal_uInt16 nKey
= XML_NAMESPACE_UNKNOWN
;
148 NameSpaceHash::const_iterator aIter
= aNameHash
.begin(), aEnd
= aNameHash
.end();
149 while (aIter
!= aEnd
)
151 if ((*aIter
).second
->sName
== rName
)
153 nKey
= (*aIter
).second
->nKey
;
161 const OUString
& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey
) const
163 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nKey
);
164 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sPrefix
: sEmpty
;
167 const OUString
& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey
) const
169 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nKey
);
170 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sName
: sEmpty
;
173 OUString
SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey
) const
175 OUStringBuffer sAttrName
;
176 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nKey
);
177 if (aIter
!= aNameMap
.end())
179 sAttrName
.append( sXMLNS
);
180 const OUString
& prefix( (*aIter
).second
->sPrefix
);
181 if (!prefix
.isEmpty()) // not default namespace
183 sAttrName
.append( ':' );
184 sAttrName
.append( prefix
);
187 return sAttrName
.makeStringAndClear();
190 OUString
SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey
,
191 const OUString
& rLocalName
,
194 // We always want to return at least the rLocalName...
198 case XML_NAMESPACE_UNKNOWN
:
199 // ...if it's a completely unknown namespace, assert and return the local name
200 DBG_ASSERT( false, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
201 case XML_NAMESPACE_NONE
:
202 // ...if there isn't one, return the local name
204 case XML_NAMESPACE_XMLNS
:
206 // ...if it's in the xmlns namespace, make the prefix
207 // don't bother caching this, it rarely happens
208 OUStringBuffer sQName
;
209 sQName
.append ( sXMLNS
);
210 if (!rLocalName
.isEmpty()) // not default namespace
212 sQName
.append ( ':' );
213 sQName
.append ( rLocalName
);
215 return sQName
.makeStringAndClear();
217 case XML_NAMESPACE_XML
:
219 // this namespace is reserved, and needs not to be declared
220 OUStringBuffer sQName
;
221 sQName
.append ( GetXMLToken(XML_XML
) );
222 sQName
.append ( ':' );
223 sQName
.append ( rLocalName
);
224 return sQName
.makeStringAndClear();
228 QNameCache::const_iterator aQCacheIter
;
230 aQCacheIter
= aQNameCache
.find ( QNamePair ( nKey
, rLocalName
) );
232 aQCacheIter
= aQNameCache
.end();
233 if ( aQCacheIter
!= aQNameCache
.end() )
234 return (*aQCacheIter
).second
;
237 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nKey
);
238 if ( aIter
!= aNameMap
.end() )
240 OUStringBuffer sQName
;
241 // ...if it's in our map, make the prefix
242 const OUString
& prefix( (*aIter
).second
->sPrefix
);
243 if (!prefix
.isEmpty()) // not default namespace
245 sQName
.append( prefix
);
246 sQName
.append( ':' );
248 sQName
.append ( rLocalName
);
251 OUString
sString(sQName
.makeStringAndClear());
253 QNameCache::value_type(
254 QNamePair(nKey
, rLocalName
), sString
));
258 return sQName
.makeStringAndClear();
262 // ... if it isn't, this is a Bad Thing, assert and return the local name
263 DBG_ASSERT( false, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
271 sal_uInt16
SvXMLNamespaceMap::_GetKeyByAttrName(
272 const OUString
& rAttrName
,
273 OUString
*pLocalName
,
276 return _GetKeyByAttrName( rAttrName
, 0, pLocalName
, 0, bCache
);
279 sal_uInt16
SvXMLNamespaceMap::_GetKeyByAttrName( const OUString
& rAttrName
,
281 OUString
*pLocalName
,
282 OUString
*pNamespace
,
285 sal_uInt16 nKey
= XML_NAMESPACE_UNKNOWN
;
287 NameSpaceHash::const_iterator it
;
289 it
= aNameCache
.find ( rAttrName
);
291 it
= aNameCache
.end();
292 if ( it
!= aNameCache
.end() )
294 const NameSpaceEntry
&rEntry
= *((*it
).second
);
296 *pPrefix
= rEntry
.sPrefix
;
298 *pLocalName
= rEntry
.sName
;
302 NameSpaceMap::const_iterator aMapIter
= aNameMap
.find (nKey
);
303 *pNamespace
= aMapIter
!= aNameMap
.end() ? (*aMapIter
).second
->sName
: sEmpty
;
308 rtl::Reference
<NameSpaceEntry
> xEntry(new NameSpaceEntry());
310 sal_Int32 nColonPos
= rAttrName
.indexOf( ':' );
311 if( -1L == nColonPos
)
313 // case: no ':' found -> default namespace
314 (xEntry
->sPrefix
).clear();
315 xEntry
->sName
= rAttrName
;
319 // normal case: ':' found -> get prefix/suffix
320 xEntry
->sPrefix
= rAttrName
.copy( 0L, nColonPos
);
321 xEntry
->sName
= rAttrName
.copy( nColonPos
+ 1L );
325 *pPrefix
= xEntry
->sPrefix
;
327 *pLocalName
= xEntry
->sName
;
329 NameSpaceHash::const_iterator aIter
= aNameHash
.find( xEntry
->sPrefix
);
330 if ( aIter
!= aNameHash
.end() )
332 // found: retrieve namespace key
333 nKey
= xEntry
->nKey
= (*aIter
).second
->nKey
;
335 *pNamespace
= (*aIter
).second
->sName
;
337 else if ( xEntry
->sPrefix
== sXMLNS
)
338 // not found, but xmlns prefix: return xmlns 'namespace'
339 nKey
= xEntry
->nKey
= XML_NAMESPACE_XMLNS
;
340 else if( nColonPos
== -1L )
341 // not found, and no namespace: 'namespace' none
342 nKey
= xEntry
->nKey
= XML_NAMESPACE_NONE
;
346 aNameCache
.insert(NameSpaceHash::value_type(rAttrName
, xEntry
));
353 sal_uInt16
SvXMLNamespaceMap::GetFirstKey() const
355 return aNameMap
.empty() ? USHRT_MAX
: (*aNameMap
.begin()).second
->nKey
;
358 sal_uInt16
SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey
) const
360 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nLastKey
);
361 return (++aIter
== aNameMap
.end()) ? USHRT_MAX
: (*aIter
).second
->nKey
;
365 // All methods after this are deprecated...
367 sal_uInt16
SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey
)
371 sal_uInt16
SvXMLNamespaceMap::GetFirstIndex() const
373 return aNameMap
.empty() ? USHRT_MAX
: (*aNameMap
.begin()).second
->nKey
;
376 sal_uInt16
SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx
) const
378 NameSpaceMap::const_iterator aIter
= aNameMap
.find ( nOldIdx
);
379 return (++aIter
== aNameMap
.end()) ? USHRT_MAX
: (*aIter
).second
->nKey
;
382 bool SvXMLNamespaceMap::AddAtIndex( sal_uInt16
/*nIdx*/, const OUString
& rPrefix
,
383 const OUString
& rName
, sal_uInt16 nKey
)
387 if( XML_NAMESPACE_UNKNOWN
== nKey
)
388 nKey
= GetKeyByName( rName
);
390 DBG_ASSERT( XML_NAMESPACE_NONE
!= nKey
,
391 "SvXMLNamespaceMap::AddAtIndex: invalid namespace key" );
392 if( XML_NAMESPACE_NONE
!= nKey
&& ! ( aNameHash
.count ( rPrefix
) ) )
394 _Add( rPrefix
, rName
, nKey
);
400 OUString
SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx
) const
402 return GetAttrNameByKey( nIdx
);
405 OUString
SvXMLNamespaceMap::GetQNameByIndex( sal_uInt16 nIdx
,
406 const OUString
& rLocalName
) const
408 return GetQNameByKey( nIdx
, rLocalName
);
411 const OUString
& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx
) const
413 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nIdx
);
414 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sPrefix
: sEmpty
;
417 const OUString
& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx
) const
419 NameSpaceMap::const_iterator aIter
= aNameMap
.find (nIdx
);
420 return (aIter
!= aNameMap
.end()) ? (*aIter
).second
->sName
: sEmpty
;
423 sal_uInt16
SvXMLNamespaceMap::GetIndexByPrefix( const OUString
& rPrefix
) const
425 NameSpaceHash::const_iterator aIter
= aNameHash
.find(rPrefix
);
426 return (aIter
!= aNameHash
.end()) ? (*aIter
).second
->nKey
: USHRT_MAX
;
428 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName(
429 const OUString
& rAttrName
,
430 OUString
*pLocalName
,
431 sal_uInt16
/*nIdxGuess*/) const
433 return _GetKeyByAttrName( rAttrName
, 0, pLocalName
, 0 );
436 sal_uInt16
SvXMLNamespaceMap::GetKeyByAttrName( const OUString
& rAttrName
,
438 OUString
*pLocalName
,
439 OUString
*pNamespace
,
440 sal_uInt16
/*nIdxGuess*/ ) const
442 return _GetKeyByAttrName ( rAttrName
, pPrefix
, pLocalName
, pNamespace
);
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
.copy( nCompareFrom
).equals( 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 OUString
sTmp( rName
.copy( nPos
) );
526 const OUString
& rXMLNS
= GetXMLToken( XML_XMLNS
);
527 if( !sTmp
.startsWith( 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
.copy( 0, nTCIdStart
) +
553 GetXMLToken( XML_OPENDOCUMENT
) +
554 rName
.copy( nTCIdEnd
, nVersionStart
-nTCIdEnd
) +
555 GetXMLToken( XML_1_0
);
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */