Bump for 3.6-28
[LibreOffice.git] / xmloff / source / core / nmspmap.cxx
blob06e6caa547e91bdbc7919556564dc0333b5d5786
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include <tools/debug.hxx>
32 #include <rtl/ustring.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/nmspmap.hxx>
38 #include "xmloff/xmlnmspe.hxx"
41 using ::rtl::OUString;
42 using ::rtl::OUStringBuffer;
43 using namespace ::xmloff::token;
45 /* The basic idea of this class is that we have two two ways to search our
46 * data...by prefix and by key. We use an STL boost::unordered_map for fast prefix
47 * searching and an STL map for fast key searching.
49 * The references to an 'Index' refer to an earlier implementation of the
50 * name space map and remain to support code which uses these interfaces.
52 * In this implementation, key and index should always be the same number.
54 * All references to Indices are now deprecated and the corresponding
55 * 'Key' methods should be used instead
57 * Martin 13/06/01
60 SvXMLNamespaceMap::SvXMLNamespaceMap()
61 : sXMLNS( GetXMLToken ( XML_XMLNS ) )
65 SvXMLNamespaceMap::SvXMLNamespaceMap( const SvXMLNamespaceMap& rMap )
66 : sXMLNS( GetXMLToken ( XML_XMLNS ) )
68 aNameHash = rMap.aNameHash;
69 aNameMap = rMap.aNameMap;
72 void SvXMLNamespaceMap::operator=( const SvXMLNamespaceMap& rMap )
74 aNameHash = rMap.aNameHash;
75 aNameMap = rMap.aNameMap;
78 SvXMLNamespaceMap::~SvXMLNamespaceMap()
82 int SvXMLNamespaceMap::operator ==( const SvXMLNamespaceMap& rCmp ) const
84 return static_cast < int > (aNameHash == rCmp.aNameHash);
87 sal_uInt16 SvXMLNamespaceMap::_Add( const OUString& rPrefix, const OUString &rName, sal_uInt16 nKey )
89 if( XML_NAMESPACE_UNKNOWN == nKey )
91 // create a new unique key with UNKNOWN flag set
92 nKey = XML_NAMESPACE_UNKNOWN_FLAG;
95 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
96 if( aIter == aNameMap.end() )
97 break;
98 nKey++;
100 while ( sal_True );
102 ::rtl::Reference<NameSpaceEntry> pEntry(new NameSpaceEntry);
103 pEntry->sName = rName;
104 pEntry->nKey = nKey;
105 pEntry->sPrefix = rPrefix;
106 aNameHash[ rPrefix ] = pEntry;
107 aNameMap [ nKey ] = pEntry;
108 return nKey;
111 sal_uInt16 SvXMLNamespaceMap::Add( const OUString& rPrefix, const OUString& rName,
112 sal_uInt16 nKey )
114 if( XML_NAMESPACE_UNKNOWN == nKey )
115 nKey = GetKeyByName( rName );
117 DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
118 "SvXMLNamespaceMap::Add: invalid namespace key" );
120 if( XML_NAMESPACE_NONE == nKey )
121 return USHRT_MAX;
123 if ( aNameHash.find ( rPrefix ) == aNameHash.end() )
124 nKey = _Add( rPrefix, rName, nKey );
126 return nKey;
129 sal_uInt16 SvXMLNamespaceMap::AddIfKnown( const OUString& rPrefix, const OUString& rName )
131 sal_uInt16 nKey = GetKeyByName( rName );
133 DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
134 "SvXMLNamespaceMap::AddIfKnown: invalid namespace key" );
136 if( XML_NAMESPACE_NONE == nKey )
137 return XML_NAMESPACE_UNKNOWN;
139 if( XML_NAMESPACE_UNKNOWN != nKey )
141 NameSpaceHash::const_iterator aIter = aNameHash.find( rPrefix );
142 if( aIter == aNameHash.end() || (*aIter).second->sName != rName )
143 nKey = _Add( rPrefix, rName, nKey );
146 return nKey;
150 sal_uInt16 SvXMLNamespaceMap::GetKeyByPrefix( const OUString& rPrefix ) const
152 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
153 return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
156 sal_uInt16 SvXMLNamespaceMap::GetKeyByName( const OUString& rName ) const
158 sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
159 NameSpaceHash::const_iterator aIter = aNameHash.begin(), aEnd = aNameHash.end();
160 while (aIter != aEnd )
162 if ((*aIter).second->sName == rName)
164 nKey = (*aIter).second->nKey;
165 break;
167 ++aIter;
169 return nKey;
172 const OUString& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey ) const
174 NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
175 return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
178 const OUString& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey ) const
180 NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
181 return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
184 OUString SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey ) const
186 OUStringBuffer sAttrName;
187 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
188 if (aIter != aNameMap.end())
190 sAttrName.append( sXMLNS );
191 const ::rtl::OUString & prefix( (*aIter).second->sPrefix );
192 if (!prefix.isEmpty()) // not default namespace
194 sAttrName.append( sal_Unicode(':') );
195 sAttrName.append( prefix );
198 return sAttrName.makeStringAndClear();
201 OUString SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey,
202 const OUString& rLocalName,
203 sal_Bool bCache) const
205 // We always want to return at least the rLocalName...
207 switch ( nKey )
209 case XML_NAMESPACE_UNKNOWN:
210 // ...if it's a completely unknown namespace, assert and return the local name
211 DBG_ASSERT( sal_False, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
212 case XML_NAMESPACE_NONE:
213 // ...if there isn't one, return the local name
214 return rLocalName;
215 case XML_NAMESPACE_XMLNS:
217 // ...if it's in the xmlns namespace, make the prefix
218 // don't bother caching this, it rarely happens
219 OUStringBuffer sQName;
220 sQName.append ( sXMLNS );
221 if (!rLocalName.isEmpty()) // not default namespace
223 sQName.append ( sal_Unicode(':') );
224 sQName.append ( rLocalName );
226 return sQName.makeStringAndClear();
228 case XML_NAMESPACE_XML:
230 // this namespace is reserved, and needs not to be declared
231 OUStringBuffer sQName;
232 sQName.append ( GetXMLToken(XML_XML) );
233 sQName.append ( sal_Unicode(':') );
234 sQName.append ( rLocalName );
235 return sQName.makeStringAndClear();
237 default:
239 QNameCache::const_iterator aQCacheIter;
240 if (bCache)
241 aQCacheIter = aQNameCache.find ( QNamePair ( nKey, rLocalName ) );
242 else
243 aQCacheIter = aQNameCache.end();
244 if ( aQCacheIter != aQNameCache.end() )
245 return (*aQCacheIter).second;
246 else
248 NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
249 if ( aIter != aNameMap.end() )
251 OUStringBuffer sQName;
252 // ...if it's in our map, make the prefix
253 const OUString & prefix( (*aIter).second->sPrefix );
254 if (!prefix.isEmpty()) // not default namespace
256 sQName.append( prefix );
257 sQName.append( sal_Unicode(':') );
259 sQName.append ( rLocalName );
260 if (bCache)
262 OUString sString(sQName.makeStringAndClear());
263 aQNameCache.insert(
264 QNameCache::value_type(
265 QNamePair(nKey, rLocalName), sString));
266 return sString;
268 else
269 return sQName.makeStringAndClear();
271 else
273 // ... if it isn't, this is a Bad Thing, assert and return the local name
274 DBG_ASSERT( sal_False, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
275 return rLocalName;
282 sal_uInt16 SvXMLNamespaceMap::_GetKeyByAttrName(
283 const OUString& rAttrName,
284 OUString *pLocalName,
285 sal_Bool bCache) const
287 return _GetKeyByAttrName( rAttrName, 0, pLocalName, 0, bCache );
290 sal_uInt16 SvXMLNamespaceMap::_GetKeyByAttrName( const OUString& rAttrName,
291 OUString *pPrefix,
292 OUString *pLocalName,
293 OUString *pNamespace,
294 sal_Bool bCache) const
296 sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
298 NameSpaceHash::const_iterator it;
299 if (bCache)
300 it = aNameCache.find ( rAttrName );
301 else
302 it = aNameCache.end();
303 if ( it != aNameCache.end() )
305 const NameSpaceEntry &rEntry = *((*it).second);
306 if ( pPrefix )
307 *pPrefix = rEntry.sPrefix;
308 if ( pLocalName )
309 *pLocalName = rEntry.sName;
310 nKey = rEntry.nKey;
311 if ( pNamespace )
313 NameSpaceMap::const_iterator aMapIter = aNameMap.find (nKey);
314 *pNamespace = aMapIter != aNameMap.end() ? (*aMapIter).second->sName : sEmpty;
317 else
319 rtl::Reference<NameSpaceEntry> xEntry(new NameSpaceEntry());
321 sal_Int32 nColonPos = rAttrName.indexOf( sal_Unicode(':') );
322 if( -1L == nColonPos )
324 // case: no ':' found -> default namespace
325 xEntry->sPrefix = OUString();
326 xEntry->sName = rAttrName;
328 else
330 // normal case: ':' found -> get prefix/suffix
331 xEntry->sPrefix = rAttrName.copy( 0L, nColonPos );
332 xEntry->sName = rAttrName.copy( nColonPos + 1L );
335 if( pPrefix )
336 *pPrefix = xEntry->sPrefix;
337 if( pLocalName )
338 *pLocalName = xEntry->sName;
340 NameSpaceHash::const_iterator aIter = aNameHash.find( xEntry->sPrefix );
341 if ( aIter != aNameHash.end() )
343 // found: retrieve namespace key
344 nKey = xEntry->nKey = (*aIter).second->nKey;
345 if ( pNamespace )
346 *pNamespace = (*aIter).second->sName;
348 else if ( xEntry->sPrefix == sXMLNS )
349 // not found, but xmlns prefix: return xmlns 'namespace'
350 nKey = xEntry->nKey = XML_NAMESPACE_XMLNS;
351 else if( nColonPos == -1L )
352 // not found, and no namespace: 'namespace' none
353 nKey = xEntry->nKey = XML_NAMESPACE_NONE;
355 if (bCache)
357 aNameCache.insert(NameSpaceHash::value_type(rAttrName, xEntry));
361 return nKey;
364 sal_uInt16 SvXMLNamespaceMap::GetFirstKey() const
366 return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
369 sal_uInt16 SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey ) const
371 NameSpaceMap::const_iterator aIter = aNameMap.find ( nLastKey );
372 return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
376 // All methods after this are deprecated...
378 sal_uInt16 SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey ) const
380 return nKey;
382 sal_uInt16 SvXMLNamespaceMap::GetFirstIndex() const
384 return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
387 sal_uInt16 SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx ) const
389 NameSpaceMap::const_iterator aIter = aNameMap.find ( nOldIdx );
390 return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
393 sal_Bool SvXMLNamespaceMap::AddAtIndex( sal_uInt16 /*nIdx*/, const OUString& rPrefix,
394 const OUString& rName, sal_uInt16 nKey )
396 sal_Bool bRet = sal_False;
398 if( XML_NAMESPACE_UNKNOWN == nKey )
399 nKey = GetKeyByName( rName );
401 DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
402 "SvXMLNamespaceMap::AddAtIndex: invalid namespace key" );
403 if( XML_NAMESPACE_NONE != nKey && ! ( aNameHash.count ( rPrefix ) ) )
405 _Add( rPrefix, rName, nKey );
406 bRet = sal_True;
408 return bRet;
411 OUString SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx ) const
413 return GetAttrNameByKey( nIdx );
416 OUString SvXMLNamespaceMap::GetQNameByIndex( sal_uInt16 nIdx,
417 const OUString& rLocalName ) const
419 return GetQNameByKey( nIdx, rLocalName );
422 const OUString& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx ) const
424 NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
425 return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
428 const OUString& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx ) const
430 NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
431 return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
434 sal_uInt16 SvXMLNamespaceMap::GetIndexByPrefix( const OUString& rPrefix ) const
436 NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
437 return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
439 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName(
440 const OUString& rAttrName,
441 OUString *pLocalName,
442 sal_uInt16 /*nIdxGuess*/) const
444 return _GetKeyByAttrName( rAttrName, 0, pLocalName, 0 );
447 sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName( const OUString& rAttrName,
448 OUString *pPrefix,
449 OUString *pLocalName,
450 OUString *pNamespace,
451 sal_uInt16 /*nIdxGuess*/ ) const
453 return _GetKeyByAttrName ( rAttrName, pPrefix, pLocalName, pNamespace );
456 sal_Bool SvXMLNamespaceMap::NormalizeURI( ::rtl::OUString& rName )
458 // try OASIS + W3 URI normalization
459 sal_Bool bSuccess = NormalizeOasisURN( rName );
460 if( ! bSuccess )
461 bSuccess = NormalizeW3URI( rName );
462 return bSuccess;
465 sal_Bool SvXMLNamespaceMap::NormalizeW3URI( ::rtl::OUString& rName )
467 // check if URI matches:
468 // http://www.w3.org/[0-9]*/[:letter:]*
469 // (year)/(WG name)
470 // For the following WG/standards names:
471 // - xforms
473 sal_Bool bSuccess = sal_False;
474 const OUString sURIPrefix = GetXMLToken( XML_URI_W3_PREFIX );
475 if( rName.compareTo( sURIPrefix, sURIPrefix.getLength() ) == 0 )
477 const OUString sURISuffix = GetXMLToken( XML_URI_XFORMS_SUFFIX );
478 sal_Int32 nCompareFrom = rName.getLength() - sURISuffix.getLength();
479 if( rName.copy( nCompareFrom ).equals( sURISuffix ) )
481 // found W3 prefix, and xforms suffix
482 rName = GetXMLToken( XML_N_XFORMS_1_0 );
483 bSuccess = sal_True;
486 return bSuccess;
489 sal_Bool SvXMLNamespaceMap::NormalizeOasisURN( ::rtl::OUString& rName )
491 // #i38644#
492 // we exported the wrong namespace for smil, so we correct this here on load
493 // for older documents
494 if( IsXMLToken( rName, ::xmloff::token::XML_N_SVG ) )
496 rName = GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT );
497 return sal_True;
499 else if( IsXMLToken( rName, ::xmloff::token::XML_N_FO ) )
501 rName = GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT );
502 return sal_True;
504 else if( IsXMLToken( rName, ::xmloff::token::XML_N_SMIL ) ||
505 IsXMLToken( rName, ::xmloff::token::XML_N_SMIL_OLD ) )
507 rName = GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT );
508 return sal_True;
512 // Check if URN matches
513 // :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
514 // |---| |---| |-----|
515 // TC-Id Sub-Id Version
517 sal_Int32 nNameLen = rName.getLength();
518 // :urn:oasis:names:tc.*
519 const OUString& rOasisURN = GetXMLToken( XML_URN_OASIS_NAMES_TC );
520 if( 0 != rName.compareTo( rOasisURN, rOasisURN.getLength() ) )
521 return sal_False;
523 // :urn:oasis:names:tc:.*
524 sal_Int32 nPos = rOasisURN.getLength();
525 if( nPos >= nNameLen || rName[nPos] != ':' )
526 return sal_False;
528 // :urn:oasis:names:tc:[^:]:.*
529 sal_Int32 nTCIdStart = nPos+1;
530 sal_Int32 nTCIdEnd = rName.indexOf( ':', nTCIdStart );
531 if( -1 == nTCIdEnd )
532 return sal_False;
534 // :urn:oasis:names:tc:[^:]:xmlns.*
535 nPos = nTCIdEnd + 1;
536 OUString sTmp( rName.copy( nPos ) );
537 const OUString& rXMLNS = GetXMLToken( XML_XMLNS );
538 if( 0!= sTmp.compareTo( rXMLNS, rXMLNS.getLength() ) )
539 return sal_False;
541 // :urn:oasis:names:tc:[^:]:xmlns:.*
542 nPos += rXMLNS.getLength();
543 if( nPos >= nNameLen || rName[nPos] != ':' )
544 return sal_False;
546 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
547 nPos = rName.indexOf( ':', nPos+1 );
548 if( -1 == nPos )
549 return sal_False;
551 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
552 sal_Int32 nVersionStart = nPos+1;
553 if( nVersionStart+2 >= nNameLen ||
554 -1 != rName.indexOf( ':', nVersionStart ) )
555 return sal_False;
557 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
558 if( rName[nVersionStart] != '1' || rName[nVersionStart+1] != '.' )
559 return sal_False;
561 // replace [tcid] with current TCID and version with current version.
562 OUStringBuffer aNewName( nNameLen +20 );
563 aNewName.append( rName.copy( 0, nTCIdStart ) );
564 aNewName.append( GetXMLToken( XML_OPENDOCUMENT ) );
565 aNewName.append( rName.copy( nTCIdEnd, nVersionStart-nTCIdEnd ) );
566 aNewName.append( GetXMLToken( XML_1_0 ) );
568 rName = aNewName.makeStringAndClear();
570 return sal_True;
573 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */