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
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() )
102 ::rtl::Reference
<NameSpaceEntry
> pEntry(new NameSpaceEntry
);
103 pEntry
->sName
= rName
;
105 pEntry
->sPrefix
= rPrefix
;
106 aNameHash
[ rPrefix
] = pEntry
;
107 aNameMap
[ nKey
] = pEntry
;
111 sal_uInt16
SvXMLNamespaceMap::Add( const OUString
& rPrefix
, const OUString
& rName
,
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
)
123 if ( aNameHash
.find ( rPrefix
) == aNameHash
.end() )
124 nKey
= _Add( rPrefix
, rName
, 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
);
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
;
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...
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
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();
239 QNameCache::const_iterator aQCacheIter
;
241 aQCacheIter
= aQNameCache
.find ( QNamePair ( nKey
, rLocalName
) );
243 aQCacheIter
= aQNameCache
.end();
244 if ( aQCacheIter
!= aQNameCache
.end() )
245 return (*aQCacheIter
).second
;
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
);
262 OUString
sString(sQName
.makeStringAndClear());
264 QNameCache::value_type(
265 QNamePair(nKey
, rLocalName
), sString
));
269 return sQName
.makeStringAndClear();
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" );
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
,
292 OUString
*pLocalName
,
293 OUString
*pNamespace
,
294 sal_Bool bCache
) const
296 sal_uInt16 nKey
= XML_NAMESPACE_UNKNOWN
;
298 NameSpaceHash::const_iterator it
;
300 it
= aNameCache
.find ( rAttrName
);
302 it
= aNameCache
.end();
303 if ( it
!= aNameCache
.end() )
305 const NameSpaceEntry
&rEntry
= *((*it
).second
);
307 *pPrefix
= rEntry
.sPrefix
;
309 *pLocalName
= rEntry
.sName
;
313 NameSpaceMap::const_iterator aMapIter
= aNameMap
.find (nKey
);
314 *pNamespace
= aMapIter
!= aNameMap
.end() ? (*aMapIter
).second
->sName
: sEmpty
;
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
;
330 // normal case: ':' found -> get prefix/suffix
331 xEntry
->sPrefix
= rAttrName
.copy( 0L, nColonPos
);
332 xEntry
->sName
= rAttrName
.copy( nColonPos
+ 1L );
336 *pPrefix
= xEntry
->sPrefix
;
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
;
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
;
357 aNameCache
.insert(NameSpaceHash::value_type(rAttrName
, xEntry
));
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
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
);
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
,
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
);
461 bSuccess
= NormalizeW3URI( rName
);
465 sal_Bool
SvXMLNamespaceMap::NormalizeW3URI( ::rtl::OUString
& rName
)
467 // check if URI matches:
468 // http://www.w3.org/[0-9]*/[:letter:]*
470 // For the following WG/standards names:
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
);
489 sal_Bool
SvXMLNamespaceMap::NormalizeOasisURN( ::rtl::OUString
& rName
)
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
);
499 else if( IsXMLToken( rName
, ::xmloff::token::XML_N_FO
) )
501 rName
= GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT
);
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
);
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() ) )
523 // :urn:oasis:names:tc:.*
524 sal_Int32 nPos
= rOasisURN
.getLength();
525 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
528 // :urn:oasis:names:tc:[^:]:.*
529 sal_Int32 nTCIdStart
= nPos
+1;
530 sal_Int32 nTCIdEnd
= rName
.indexOf( ':', nTCIdStart
);
534 // :urn:oasis:names:tc:[^:]:xmlns.*
536 OUString
sTmp( rName
.copy( nPos
) );
537 const OUString
& rXMLNS
= GetXMLToken( XML_XMLNS
);
538 if( 0!= sTmp
.compareTo( rXMLNS
, rXMLNS
.getLength() ) )
541 // :urn:oasis:names:tc:[^:]:xmlns:.*
542 nPos
+= rXMLNS
.getLength();
543 if( nPos
>= nNameLen
|| rName
[nPos
] != ':' )
546 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
547 nPos
= rName
.indexOf( ':', nPos
+1 );
551 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
552 sal_Int32 nVersionStart
= nPos
+1;
553 if( nVersionStart
+2 >= nNameLen
||
554 -1 != rName
.indexOf( ':', nVersionStart
) )
557 // :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
558 if( rName
[nVersionStart
] != '1' || rName
[nVersionStart
+1] != '.' )
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();
573 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */