xref: /aoo41x/main/xmloff/source/core/nmspmap.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir #include <tools/debug.hxx>
31*cdf0e10cSrcweir #include <rtl/ustring.hxx>
32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #ifndef _XMLTOKEN_HXX
35*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
44*cdf0e10cSrcweir using namespace ::xmloff::token;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir /* The basic idea of this class is that we have two two ways to search our
47*cdf0e10cSrcweir  * data...by prefix and by key. We use an STL hash_map for fast prefix
48*cdf0e10cSrcweir  * searching and an STL map for fast key searching.
49*cdf0e10cSrcweir  *
50*cdf0e10cSrcweir  * The references to an 'Index' refer to an earlier implementation of the
51*cdf0e10cSrcweir  * name space map and remain to support code which uses these interfaces.
52*cdf0e10cSrcweir  *
53*cdf0e10cSrcweir  * In this implementation, key and index should always be the same number.
54*cdf0e10cSrcweir  *
55*cdf0e10cSrcweir  * All references to Indices are now deprecated and the corresponding
56*cdf0e10cSrcweir  * 'Key' methods should be used instead
57*cdf0e10cSrcweir  *
58*cdf0e10cSrcweir  * Martin 13/06/01
59*cdf0e10cSrcweir  */
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir SvXMLNamespaceMap::SvXMLNamespaceMap()
62*cdf0e10cSrcweir : sXMLNS( GetXMLToken ( XML_XMLNS ) )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir SvXMLNamespaceMap::SvXMLNamespaceMap( const SvXMLNamespaceMap& rMap )
67*cdf0e10cSrcweir : sXMLNS( GetXMLToken ( XML_XMLNS ) )
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	aNameHash = rMap.aNameHash;
70*cdf0e10cSrcweir 	aNameMap  = rMap.aNameMap;
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir void SvXMLNamespaceMap::operator=( const SvXMLNamespaceMap& rMap )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     aNameHash = rMap.aNameHash;
76*cdf0e10cSrcweir     aNameMap = rMap.aNameMap;
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir SvXMLNamespaceMap::~SvXMLNamespaceMap()
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	QNameCache::iterator aIter = aQNameCache.begin(), aEnd = aQNameCache.end();
82*cdf0e10cSrcweir 	while ( aIter != aEnd )
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		const OUString *pString = (*aIter).first.second;
85*cdf0e10cSrcweir 		aIter++;
86*cdf0e10cSrcweir 		delete pString;
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir int SvXMLNamespaceMap::operator ==( const SvXMLNamespaceMap& rCmp ) const
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	return static_cast < int > (aNameHash == rCmp.aNameHash);
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::_Add( const OUString& rPrefix, const OUString &rName, sal_uInt16 nKey )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	if( XML_NAMESPACE_UNKNOWN == nKey )
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		// create a new unique key with UNKNOWN flag set
100*cdf0e10cSrcweir 		nKey = XML_NAMESPACE_UNKNOWN_FLAG;
101*cdf0e10cSrcweir 		do
102*cdf0e10cSrcweir 		{
103*cdf0e10cSrcweir 			NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
104*cdf0e10cSrcweir 			if( aIter == aNameMap.end() )
105*cdf0e10cSrcweir 				break;
106*cdf0e10cSrcweir 			nKey++;
107*cdf0e10cSrcweir 		}
108*cdf0e10cSrcweir 		while ( sal_True );
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir     ::vos::ORef<NameSpaceEntry> pEntry(new NameSpaceEntry);
111*cdf0e10cSrcweir 	pEntry->sName   = rName;
112*cdf0e10cSrcweir 	pEntry->nKey    = nKey;
113*cdf0e10cSrcweir 	pEntry->sPrefix = rPrefix;
114*cdf0e10cSrcweir 	aNameHash[ rPrefix ] = pEntry;
115*cdf0e10cSrcweir 	aNameMap [ nKey ]	 = pEntry;
116*cdf0e10cSrcweir 	return nKey;
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::Add( const OUString& rPrefix, const OUString& rName,
120*cdf0e10cSrcweir 							   sal_uInt16 nKey )
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	if( XML_NAMESPACE_UNKNOWN == nKey )
123*cdf0e10cSrcweir 		nKey = GetKeyByName( rName );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
126*cdf0e10cSrcweir 				"SvXMLNamespaceMap::Add: invalid namespace key" );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	if( XML_NAMESPACE_NONE == nKey )
129*cdf0e10cSrcweir 		return USHRT_MAX;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	if ( aNameHash.find ( rPrefix ) == aNameHash.end() )
132*cdf0e10cSrcweir 		nKey = _Add( rPrefix, rName, nKey );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	return nKey;
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::AddIfKnown( const OUString& rPrefix, const OUString& rName )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	sal_uInt16 nKey = GetKeyByName( rName );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
142*cdf0e10cSrcweir 				"SvXMLNamespaceMap::AddIfKnown: invalid namespace key" );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	if( XML_NAMESPACE_NONE == nKey )
145*cdf0e10cSrcweir 		return XML_NAMESPACE_UNKNOWN;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	if( XML_NAMESPACE_UNKNOWN != nKey )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         NameSpaceHash::const_iterator aIter = aNameHash.find( rPrefix );
150*cdf0e10cSrcweir         if( aIter == aNameHash.end() || (*aIter).second->sName != rName )
151*cdf0e10cSrcweir             nKey = _Add( rPrefix, rName, nKey );
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	return nKey;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetKeyByPrefix( const OUString& rPrefix ) const
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir 	NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
161*cdf0e10cSrcweir 	return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetKeyByName( const OUString& rName ) const
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 	sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
167*cdf0e10cSrcweir 	NameSpaceHash::const_iterator aIter = aNameHash.begin(), aEnd = aNameHash.end();
168*cdf0e10cSrcweir 	while (aIter != aEnd )
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		if ((*aIter).second->sName == rName)
171*cdf0e10cSrcweir 		{
172*cdf0e10cSrcweir 			nKey = (*aIter).second->nKey;
173*cdf0e10cSrcweir 			break;
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 		aIter++;
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 	return nKey;
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir const OUString& SvXMLNamespaceMap::GetPrefixByKey( sal_uInt16 nKey ) const
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
183*cdf0e10cSrcweir 	return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir const OUString& SvXMLNamespaceMap::GetNameByKey( sal_uInt16 nKey ) const
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find (nKey);
189*cdf0e10cSrcweir 	return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir OUString SvXMLNamespaceMap::GetAttrNameByKey( sal_uInt16 nKey ) const
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir 	OUStringBuffer sAttrName;
195*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
196*cdf0e10cSrcweir 	if (aIter != aNameMap.end())
197*cdf0e10cSrcweir 	{
198*cdf0e10cSrcweir 		sAttrName.append( sXMLNS  );
199*cdf0e10cSrcweir         const ::rtl::OUString & prefix( (*aIter).second->sPrefix );
200*cdf0e10cSrcweir         if (prefix.getLength()) // not default namespace
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             sAttrName.append( sal_Unicode(':') );
203*cdf0e10cSrcweir             sAttrName.append( prefix );
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir 	}
206*cdf0e10cSrcweir 	return sAttrName.makeStringAndClear();
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir OUString SvXMLNamespaceMap::GetQNameByKey( sal_uInt16 nKey,
210*cdf0e10cSrcweir 							const OUString& rLocalName,
211*cdf0e10cSrcweir                             sal_Bool bCache) const
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir 	// We always want to return at least the rLocalName...
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	switch ( nKey )
216*cdf0e10cSrcweir 	{
217*cdf0e10cSrcweir 		case XML_NAMESPACE_UNKNOWN:
218*cdf0e10cSrcweir 			// ...if it's a completely unknown namespace, assert and return the local name
219*cdf0e10cSrcweir 			DBG_ASSERT( sal_False, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
220*cdf0e10cSrcweir 		case XML_NAMESPACE_NONE:
221*cdf0e10cSrcweir 			// ...if there isn't one, return the local name
222*cdf0e10cSrcweir 			return rLocalName;
223*cdf0e10cSrcweir 		case XML_NAMESPACE_XMLNS:
224*cdf0e10cSrcweir 		{
225*cdf0e10cSrcweir 			// ...if it's in the xmlns namespace, make the prefix
226*cdf0e10cSrcweir 			// don't bother caching this, it rarely happens
227*cdf0e10cSrcweir 			OUStringBuffer sQName;
228*cdf0e10cSrcweir 			sQName.append ( sXMLNS );
229*cdf0e10cSrcweir             if (rLocalName.getLength()) // not default namespace
230*cdf0e10cSrcweir             {
231*cdf0e10cSrcweir                 sQName.append ( sal_Unicode(':') );
232*cdf0e10cSrcweir                 sQName.append ( rLocalName );
233*cdf0e10cSrcweir             }
234*cdf0e10cSrcweir 			return sQName.makeStringAndClear();;
235*cdf0e10cSrcweir 		}
236*cdf0e10cSrcweir 		case XML_NAMESPACE_XML:
237*cdf0e10cSrcweir 		{
238*cdf0e10cSrcweir             // this namespace is reserved, and needs not to be declared
239*cdf0e10cSrcweir 			OUStringBuffer sQName;
240*cdf0e10cSrcweir 			sQName.append ( GetXMLToken(XML_XML) );
241*cdf0e10cSrcweir 			sQName.append ( sal_Unicode(':') );
242*cdf0e10cSrcweir 			sQName.append ( rLocalName );
243*cdf0e10cSrcweir 			return sQName.makeStringAndClear();;
244*cdf0e10cSrcweir 		}
245*cdf0e10cSrcweir 		default:
246*cdf0e10cSrcweir 		{
247*cdf0e10cSrcweir             QNameCache::const_iterator aQCacheIter;
248*cdf0e10cSrcweir             if (bCache)
249*cdf0e10cSrcweir 			    aQCacheIter = aQNameCache.find ( QNamePair ( nKey, &rLocalName ) );
250*cdf0e10cSrcweir             else
251*cdf0e10cSrcweir                 aQCacheIter = aQNameCache.end();
252*cdf0e10cSrcweir 			if ( aQCacheIter != aQNameCache.end() )
253*cdf0e10cSrcweir 				return (*aQCacheIter).second;
254*cdf0e10cSrcweir 			else
255*cdf0e10cSrcweir 			{
256*cdf0e10cSrcweir 				NameSpaceMap::const_iterator aIter = aNameMap.find ( nKey );
257*cdf0e10cSrcweir 				if ( aIter != aNameMap.end() )
258*cdf0e10cSrcweir 				{
259*cdf0e10cSrcweir 					OUStringBuffer sQName;
260*cdf0e10cSrcweir 					// ...if it's in our map, make the prefix
261*cdf0e10cSrcweir                     const OUString & prefix( (*aIter).second->sPrefix );
262*cdf0e10cSrcweir                     if (prefix.getLength()) // not default namespace
263*cdf0e10cSrcweir                     {
264*cdf0e10cSrcweir                         sQName.append( prefix );
265*cdf0e10cSrcweir                         sQName.append( sal_Unicode(':') );
266*cdf0e10cSrcweir                     }
267*cdf0e10cSrcweir                     sQName.append ( rLocalName );
268*cdf0e10cSrcweir                     if (bCache)
269*cdf0e10cSrcweir                     {
270*cdf0e10cSrcweir                         OUString sString(sQName.makeStringAndClear());
271*cdf0e10cSrcweir                         OUString *pString = new OUString ( rLocalName );
272*cdf0e10cSrcweir                         const_cast < QNameCache * > (&aQNameCache)->operator[] ( QNamePair ( nKey, pString ) ) = sString;
273*cdf0e10cSrcweir                         return sString;
274*cdf0e10cSrcweir                     }
275*cdf0e10cSrcweir                     else
276*cdf0e10cSrcweir                         return sQName.makeStringAndClear();
277*cdf0e10cSrcweir 				}
278*cdf0e10cSrcweir 				else
279*cdf0e10cSrcweir 				{
280*cdf0e10cSrcweir 					// ... if it isn't, this is a Bad Thing, assert and return the local name
281*cdf0e10cSrcweir 					DBG_ASSERT( sal_False, "SvXMLNamespaceMap::GetQNameByKey: invalid namespace key" );
282*cdf0e10cSrcweir 					return rLocalName;
283*cdf0e10cSrcweir 				}
284*cdf0e10cSrcweir 			}
285*cdf0e10cSrcweir 		}
286*cdf0e10cSrcweir 	}
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::_GetKeyByAttrName(
290*cdf0e10cSrcweir 							const OUString& rAttrName,
291*cdf0e10cSrcweir 							OUString *pLocalName,
292*cdf0e10cSrcweir                             sal_Bool bCache) const
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir 	return _GetKeyByAttrName( rAttrName, 0, pLocalName, 0, bCache );
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::_GetKeyByAttrName( const OUString& rAttrName,
298*cdf0e10cSrcweir 											OUString *pPrefix,
299*cdf0e10cSrcweir 											OUString *pLocalName,
300*cdf0e10cSrcweir 											OUString *pNamespace,
301*cdf0e10cSrcweir                                             sal_Bool bCache) const
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir 	sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN;
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir     NameSpaceHash::const_iterator it;
306*cdf0e10cSrcweir     if (bCache)
307*cdf0e10cSrcweir         it = aNameCache.find ( rAttrName );
308*cdf0e10cSrcweir     else
309*cdf0e10cSrcweir         it = aNameCache.end();
310*cdf0e10cSrcweir     if ( it != aNameCache.end() )
311*cdf0e10cSrcweir 	{
312*cdf0e10cSrcweir         const NameSpaceEntry &rEntry = (*it).second.getBody();
313*cdf0e10cSrcweir         if ( pPrefix )
314*cdf0e10cSrcweir             *pPrefix = rEntry.sPrefix;
315*cdf0e10cSrcweir         if ( pLocalName )
316*cdf0e10cSrcweir             *pLocalName = rEntry.sName;
317*cdf0e10cSrcweir         nKey = rEntry.nKey;
318*cdf0e10cSrcweir         if ( pNamespace )
319*cdf0e10cSrcweir 		{
320*cdf0e10cSrcweir             NameSpaceMap::const_iterator aMapIter = aNameMap.find (nKey);
321*cdf0e10cSrcweir             *pNamespace = aMapIter != aNameMap.end() ? (*aMapIter).second->sName : sEmpty;
322*cdf0e10cSrcweir         }
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir     else
325*cdf0e10cSrcweir     {
326*cdf0e10cSrcweir 	vos::ORef<NameSpaceEntry> xEntry(new NameSpaceEntry());
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir         sal_Int32 nColonPos = rAttrName.indexOf( sal_Unicode(':') );
329*cdf0e10cSrcweir         if( -1L == nColonPos )
330*cdf0e10cSrcweir         {
331*cdf0e10cSrcweir             // case: no ':' found -> default namespace
332*cdf0e10cSrcweir             xEntry->sPrefix = OUString();
333*cdf0e10cSrcweir             xEntry->sName = rAttrName;
334*cdf0e10cSrcweir         }
335*cdf0e10cSrcweir         else
336*cdf0e10cSrcweir         {
337*cdf0e10cSrcweir             // normal case: ':' found -> get prefix/suffix
338*cdf0e10cSrcweir             xEntry->sPrefix = rAttrName.copy( 0L, nColonPos );
339*cdf0e10cSrcweir             xEntry->sName = rAttrName.copy( nColonPos + 1L );
340*cdf0e10cSrcweir         }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir         if( pPrefix )
343*cdf0e10cSrcweir             *pPrefix = xEntry->sPrefix;
344*cdf0e10cSrcweir         if( pLocalName )
345*cdf0e10cSrcweir             *pLocalName = xEntry->sName;
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir         NameSpaceHash::const_iterator aIter = aNameHash.find( xEntry->sPrefix );
348*cdf0e10cSrcweir         if ( aIter != aNameHash.end() )
349*cdf0e10cSrcweir 		{
350*cdf0e10cSrcweir             // found: retrieve namespace key
351*cdf0e10cSrcweir             nKey = xEntry->nKey = (*aIter).second->nKey;
352*cdf0e10cSrcweir             if ( pNamespace )
353*cdf0e10cSrcweir                 *pNamespace = (*aIter).second->sName;
354*cdf0e10cSrcweir         }
355*cdf0e10cSrcweir         else if ( xEntry->sPrefix == sXMLNS )
356*cdf0e10cSrcweir             // not found, but xmlns prefix: return xmlns 'namespace'
357*cdf0e10cSrcweir             nKey = xEntry->nKey = XML_NAMESPACE_XMLNS;
358*cdf0e10cSrcweir         else if( nColonPos == -1L )
359*cdf0e10cSrcweir             // not found, and no namespace: 'namespace' none
360*cdf0e10cSrcweir             nKey = xEntry->nKey = XML_NAMESPACE_NONE;
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir         if (bCache)
363*cdf0e10cSrcweir 	{
364*cdf0e10cSrcweir 	    typedef std::pair< const rtl::OUString, vos::ORef<NameSpaceEntry> > value_type;
365*cdf0e10cSrcweir 	    (void) const_cast<NameSpaceHash*>(&aNameCache)->insert (value_type (rAttrName, xEntry));
366*cdf0e10cSrcweir 	}
367*cdf0e10cSrcweir     }
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 	return nKey;
370*cdf0e10cSrcweir }
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetFirstKey() const
373*cdf0e10cSrcweir {
374*cdf0e10cSrcweir 	return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
375*cdf0e10cSrcweir }
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetNextKey( sal_uInt16 nLastKey ) const
378*cdf0e10cSrcweir {
379*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find ( nLastKey );
380*cdf0e10cSrcweir 	return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
381*cdf0e10cSrcweir }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir // All methods after this are deprecated...
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetKeyByIndex( sal_uInt16 nIdx ) const
387*cdf0e10cSrcweir {
388*cdf0e10cSrcweir 	return nIdx;
389*cdf0e10cSrcweir }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetIndexByKey( sal_uInt16 nKey ) const
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir 	return nKey;
394*cdf0e10cSrcweir }
395*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetFirstIndex() const
396*cdf0e10cSrcweir {
397*cdf0e10cSrcweir 	return aNameMap.empty() ? USHRT_MAX : (*aNameMap.begin()).second->nKey;
398*cdf0e10cSrcweir }
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetNextIndex( sal_uInt16 nOldIdx ) const
401*cdf0e10cSrcweir {
402*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find ( nOldIdx );
403*cdf0e10cSrcweir 	return (++aIter == aNameMap.end()) ? USHRT_MAX : (*aIter).second->nKey;
404*cdf0e10cSrcweir }
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir sal_Bool SvXMLNamespaceMap::AddAtIndex( sal_uInt16 /*nIdx*/, const OUString& rPrefix,
407*cdf0e10cSrcweir 									const OUString& rName, sal_uInt16 nKey )
408*cdf0e10cSrcweir {
409*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 	if( XML_NAMESPACE_UNKNOWN == nKey )
412*cdf0e10cSrcweir 		nKey = GetKeyByName( rName );
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 	DBG_ASSERT( XML_NAMESPACE_NONE != nKey,
415*cdf0e10cSrcweir 				"SvXMLNamespaceMap::AddAtIndex: invalid namespace key" );
416*cdf0e10cSrcweir 	if( XML_NAMESPACE_NONE != nKey && ! ( aNameHash.count ( rPrefix ) ) )
417*cdf0e10cSrcweir 	{
418*cdf0e10cSrcweir 		_Add( rPrefix, rName, nKey );
419*cdf0e10cSrcweir 		bRet = sal_True;
420*cdf0e10cSrcweir 	}
421*cdf0e10cSrcweir 	return bRet;
422*cdf0e10cSrcweir }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir sal_Bool SvXMLNamespaceMap::AddAtIndex( sal_uInt16 nIdx, const sal_Char *pPrefix,
425*cdf0e10cSrcweir 					 				const sal_Char *pName, sal_uInt16 nKey )
426*cdf0e10cSrcweir {
427*cdf0e10cSrcweir 	OUString sPrefix( OUString::createFromAscii(pPrefix) );
428*cdf0e10cSrcweir 	OUString sName( OUString::createFromAscii(pName) );
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 	return AddAtIndex( nIdx, sPrefix, sName, nKey );
431*cdf0e10cSrcweir }
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir OUString SvXMLNamespaceMap::GetAttrNameByIndex( sal_uInt16 nIdx ) const
434*cdf0e10cSrcweir {
435*cdf0e10cSrcweir 	return GetAttrNameByKey( nIdx );
436*cdf0e10cSrcweir }
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir OUString SvXMLNamespaceMap::GetQNameByIndex( sal_uInt16 nIdx,
439*cdf0e10cSrcweir 										   const OUString& rLocalName ) const
440*cdf0e10cSrcweir {
441*cdf0e10cSrcweir 	return GetQNameByKey( nIdx, rLocalName );
442*cdf0e10cSrcweir }
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir const OUString& SvXMLNamespaceMap::GetPrefixByIndex( sal_uInt16 nIdx ) const
445*cdf0e10cSrcweir {
446*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
447*cdf0e10cSrcweir 	return (aIter != aNameMap.end()) ? (*aIter).second->sPrefix : sEmpty;
448*cdf0e10cSrcweir }
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir const OUString& SvXMLNamespaceMap::GetNameByIndex( sal_uInt16 nIdx ) const
451*cdf0e10cSrcweir {
452*cdf0e10cSrcweir 	NameSpaceMap::const_iterator aIter = aNameMap.find (nIdx);
453*cdf0e10cSrcweir 	return (aIter != aNameMap.end()) ? (*aIter).second->sName : sEmpty;
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetIndexByPrefix( const OUString& rPrefix ) const
457*cdf0e10cSrcweir {
458*cdf0e10cSrcweir 	NameSpaceHash::const_iterator aIter = aNameHash.find(rPrefix);
459*cdf0e10cSrcweir 	return (aIter != aNameHash.end()) ? (*aIter).second->nKey : USHRT_MAX;
460*cdf0e10cSrcweir }
461*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName(
462*cdf0e10cSrcweir 							const OUString& rAttrName,
463*cdf0e10cSrcweir 							OUString *pLocalName,
464*cdf0e10cSrcweir 							sal_uInt16 /*nIdxGuess*/) const
465*cdf0e10cSrcweir {
466*cdf0e10cSrcweir 	return _GetKeyByAttrName( rAttrName, 0, pLocalName, 0 );
467*cdf0e10cSrcweir }
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir sal_uInt16 SvXMLNamespaceMap::GetKeyByAttrName( const OUString& rAttrName,
470*cdf0e10cSrcweir 											OUString *pPrefix,
471*cdf0e10cSrcweir 											OUString *pLocalName,
472*cdf0e10cSrcweir 											OUString *pNamespace,
473*cdf0e10cSrcweir 											sal_uInt16 /*nIdxGuess*/ ) const
474*cdf0e10cSrcweir {
475*cdf0e10cSrcweir 	return _GetKeyByAttrName ( rAttrName, pPrefix, pLocalName, pNamespace );
476*cdf0e10cSrcweir }
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir sal_Bool SvXMLNamespaceMap::NormalizeURI( ::rtl::OUString& rName )
479*cdf0e10cSrcweir {
480*cdf0e10cSrcweir     // try OASIS + W3 URI normalization
481*cdf0e10cSrcweir     sal_Bool bSuccess = NormalizeOasisURN( rName );
482*cdf0e10cSrcweir     if( ! bSuccess )
483*cdf0e10cSrcweir         bSuccess = NormalizeW3URI( rName );
484*cdf0e10cSrcweir     return bSuccess;
485*cdf0e10cSrcweir }
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir sal_Bool SvXMLNamespaceMap::NormalizeW3URI( ::rtl::OUString& rName )
488*cdf0e10cSrcweir {
489*cdf0e10cSrcweir     // check if URI matches:
490*cdf0e10cSrcweir     // http://www.w3.org/[0-9]*/[:letter:]*
491*cdf0e10cSrcweir     //                   (year)/(WG name)
492*cdf0e10cSrcweir     // For the following WG/standards names:
493*cdf0e10cSrcweir     // - xforms
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
496*cdf0e10cSrcweir 	const OUString sURIPrefix = GetXMLToken( XML_URI_W3_PREFIX );
497*cdf0e10cSrcweir     if( rName.compareTo( sURIPrefix, sURIPrefix.getLength() ) == 0 )
498*cdf0e10cSrcweir     {
499*cdf0e10cSrcweir         const OUString sURISuffix = GetXMLToken( XML_URI_XFORMS_SUFFIX );
500*cdf0e10cSrcweir         sal_Int32 nCompareFrom = rName.getLength() - sURISuffix.getLength();
501*cdf0e10cSrcweir         if( rName.copy( nCompareFrom ).equals( sURISuffix ) )
502*cdf0e10cSrcweir         {
503*cdf0e10cSrcweir             // found W3 prefix, and xforms suffix
504*cdf0e10cSrcweir             rName = GetXMLToken( XML_N_XFORMS_1_0 );
505*cdf0e10cSrcweir             bSuccess = sal_True;
506*cdf0e10cSrcweir         }
507*cdf0e10cSrcweir     }
508*cdf0e10cSrcweir     return bSuccess;
509*cdf0e10cSrcweir }
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir sal_Bool SvXMLNamespaceMap::NormalizeOasisURN( ::rtl::OUString& rName )
512*cdf0e10cSrcweir {
513*cdf0e10cSrcweir 	// #i38644#
514*cdf0e10cSrcweir 	// we exported the wrong namespace for smil, so we correct this here on load
515*cdf0e10cSrcweir 	// for older documents
516*cdf0e10cSrcweir 	if( IsXMLToken( rName, ::xmloff::token::XML_N_SVG ) )
517*cdf0e10cSrcweir 	{
518*cdf0e10cSrcweir 		rName = GetXMLToken( ::xmloff::token::XML_N_SVG_COMPAT );
519*cdf0e10cSrcweir 		return sal_True;
520*cdf0e10cSrcweir 	}
521*cdf0e10cSrcweir 	else if( IsXMLToken( rName, ::xmloff::token::XML_N_FO ) )
522*cdf0e10cSrcweir 	{
523*cdf0e10cSrcweir 		rName = GetXMLToken( ::xmloff::token::XML_N_FO_COMPAT );
524*cdf0e10cSrcweir 		return sal_True;
525*cdf0e10cSrcweir 	}
526*cdf0e10cSrcweir 	else if( IsXMLToken( rName, ::xmloff::token::XML_N_SMIL ) ||
527*cdf0e10cSrcweir 		  	 IsXMLToken( rName, ::xmloff::token::XML_N_SMIL_OLD )  )
528*cdf0e10cSrcweir 	{
529*cdf0e10cSrcweir 		rName = GetXMLToken( ::xmloff::token::XML_N_SMIL_COMPAT );
530*cdf0e10cSrcweir 		return sal_True;
531*cdf0e10cSrcweir 	}
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir 	//
534*cdf0e10cSrcweir 	// Check if URN matches
535*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]*:xmlns:[^:]*:1.[^:]*
536*cdf0e10cSrcweir 	//                     |---|       |---| |-----|
537*cdf0e10cSrcweir 	//                     TC-Id      Sub-Id Version
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir 	sal_Int32 nNameLen = rName.getLength();
540*cdf0e10cSrcweir 	// :urn:oasis:names:tc.*
541*cdf0e10cSrcweir 	const OUString& rOasisURN = GetXMLToken( XML_URN_OASIS_NAMES_TC );
542*cdf0e10cSrcweir 	if( 0 != rName.compareTo( rOasisURN, rOasisURN.getLength() ) )
543*cdf0e10cSrcweir 		return sal_False;
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir 	// :urn:oasis:names:tc:.*
546*cdf0e10cSrcweir 	sal_Int32 nPos = rOasisURN.getLength();
547*cdf0e10cSrcweir 	if( nPos >= nNameLen || rName[nPos] != ':' )
548*cdf0e10cSrcweir 		return sal_False;
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:.*
551*cdf0e10cSrcweir 	sal_Int32 nTCIdStart = nPos+1;
552*cdf0e10cSrcweir 	sal_Int32 nTCIdEnd = rName.indexOf( ':', nTCIdStart );
553*cdf0e10cSrcweir 	if( -1 == nTCIdEnd )
554*cdf0e10cSrcweir 		return sal_False;
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:xmlns.*
557*cdf0e10cSrcweir 	nPos = nTCIdEnd + 1;
558*cdf0e10cSrcweir 	OUString sTmp( rName.copy( nPos ) );
559*cdf0e10cSrcweir 	const OUString& rXMLNS = GetXMLToken( XML_XMLNS );
560*cdf0e10cSrcweir 	if( 0!= sTmp.compareTo( rXMLNS, rXMLNS.getLength() ) )
561*cdf0e10cSrcweir 		return sal_False;
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:xmlns:.*
564*cdf0e10cSrcweir 	nPos += rXMLNS.getLength();
565*cdf0e10cSrcweir 	if( nPos >= nNameLen || rName[nPos] != ':' )
566*cdf0e10cSrcweir 		return sal_False;
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:xmlns:[^:]*:.*
569*cdf0e10cSrcweir 	nPos = rName.indexOf( ':', nPos+1 );
570*cdf0e10cSrcweir 	if( -1 == nPos )
571*cdf0e10cSrcweir 		return sal_False;
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:xmlns:[^:]*:[^:][^:][^:][^:]*
574*cdf0e10cSrcweir 	sal_Int32 nVersionStart = nPos+1;
575*cdf0e10cSrcweir 	if( nVersionStart+2 >= nNameLen ||
576*cdf0e10cSrcweir 		-1 != rName.indexOf( ':', nVersionStart ) )
577*cdf0e10cSrcweir 		return sal_False;
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir 	// :urn:oasis:names:tc:[^:]:xmlns:[^:]*:1\.[^:][^:]*
580*cdf0e10cSrcweir 	if( rName[nVersionStart] != '1' || rName[nVersionStart+1] != '.' )
581*cdf0e10cSrcweir 		return sal_False;
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir 	// replace [tcid] with current TCID and version with current version.
584*cdf0e10cSrcweir 	OUStringBuffer aNewName( nNameLen +20 );
585*cdf0e10cSrcweir 	aNewName.append( rName.copy( 0, nTCIdStart ) );
586*cdf0e10cSrcweir 	aNewName.append( GetXMLToken( XML_OPENDOCUMENT ) );
587*cdf0e10cSrcweir 	aNewName.append( rName.copy( nTCIdEnd, nVersionStart-nTCIdEnd ) );
588*cdf0e10cSrcweir 	aNewName.append( GetXMLToken( XML_1_0 ) );
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir 	rName = aNewName.makeStringAndClear();
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir 	return sal_True;
593*cdf0e10cSrcweir }
594